Skip to content

Commit

Permalink
Merge pull request #590 from OneCommunityGlobal/Tim_add_bm_projects_s…
Browse files Browse the repository at this point in the history
…ummary_routing

Tim add bm projects summary routing
  • Loading branch information
one-community authored Nov 9, 2023
2 parents b8f6ffd + ec5d405 commit d8a2a11
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 17 deletions.
24 changes: 12 additions & 12 deletions src/controllers/bmdashboard/bmMaterialsController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mongoose = require('mongoose')
const mongoose = require('mongoose');

const bmMaterialsController = function (ItemMaterial) {
const bmMaterialsList = async function _matsList(req, res) {
Expand All @@ -7,42 +7,42 @@ const bmMaterialsController = function (ItemMaterial) {
.populate([
{
path: 'project',
select: '_id projectName'
select: '_id projectName',
},
{
path: 'inventoryItemType',
select: '_id name uom totalStock totalAvailable'
select: '_id name uom totalStock totalAvailable',
},
{
path: 'usageRecord',
populate: {
path: 'createdBy',
select: '_id firstName lastName'
}
select: '_id firstName lastName',
},
},
{
path: 'updateRecord',
populate: {
path: 'createdBy',
select: '_id firstName lastName'
}
select: '_id firstName lastName',
},
},
{
path: 'purchaseRecord',
populate: {
path: 'createdBy',
select: '_id firstName lastName'
}
}
select: '_id firstName lastName',
},
},
])
.exec()
.then(results => res.status(200).send(results))
.catch(error => res.status(500).send(error))
.catch(error => res.status(500).send(error));
} catch (err) {
res.json(err);
}
};
return { bmMaterialsList };
};

module.exports = bmMaterialsController;
module.exports = bmMaterialsController;
79 changes: 79 additions & 0 deletions src/controllers/bmdashboard/bmProjectController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// TODO: uncomment when executing auth checks
// const jwt = require('jsonwebtoken');
// const config = require('../../config');

const bmMProjectController = function (BuildingProject) {
// TODO: uncomment when executing auth checks
// const { JWT_SECRET } = config;

const fetchAllProjects = async (req, res) => {
//! Note: for easier testing this route currently returns all projects from the db
// TODO: uncomment the lines below to return only projects where field buildingManager === userid
// const token = req.headers.authorization;
// const { userid } = jwt.verify(token, JWT_SECRET);
try {
const projectData = await BuildingProject
// TODO: uncomment this line to filter by buildingManager field
// .find({ buildingManager: userid })
.find()
.populate([
{
path: 'buildingManager',
select: '_id firstName lastName email',
},
{
path: 'team',
select: '_id firstName lastName email',
},
])
.exec()
.then(result => result)
.catch(error => res.status(500).send(error));
res.status(200).send(projectData);
} catch (err) {
res.json(err);
}
};

// fetches single project by project id
const fetchSingleProject = async (req, res) => {
//! Note: for easier testing this route currently returns the project without an auth check
// TODO: uncomment the lines below to check the user's ability to view the current project
// const token = req.headers.authorization;
// const { userid } = jwt.verify(token, JWT_SECRET);
const { projectId } = req.params;
try {
BuildingProject
.findById(projectId)
.populate([
{
path: 'buildingManager',
select: '_id firstName lastName email',
},
{
path: 'team',
select: '_id firstName lastName email',
},
])
.exec()
.then(project => res.status(200).send(project))
// TODO: uncomment this block to execute the auth check
// authenticate request by comparing userId param with buildingManager id field
// Note: _id has type object and must be converted to string
// .then((project) => {
// if (userid !== project.buildingManager._id.toString()) {
// return res.status(403).send({
// message: 'You are not authorized to view this record.',
// });
// }
// return res.status(200).send(project);
// })
.catch(error => res.status(500).send(error));
} catch (err) {
res.json(err);
}
};
return { fetchAllProjects, fetchSingleProject };
};

module.exports = bmMProjectController;
15 changes: 15 additions & 0 deletions src/models/bmdashboard/buildingProject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose = require('mongoose');

const { Schema } = mongoose;

const buildingProject = new Schema({
isActive: Boolean,
name: String,
template: String, // construction template (ie Earthbag Village)
location: String, // use lat/lng instead?
dateCreated: { type: Date, default: Date.now },
buildingManager: { type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile' }, // BM's id
team: [{ type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile' }],
});

module.exports = mongoose.model('buildingProject', buildingProject, 'buildingProjects');
6 changes: 3 additions & 3 deletions src/models/inventoryItemMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ const InventoryItemMaterial = new Schema({
poId: { type: String, required: true },
sellerId: { type: String, required: true },
quantity: { type: Number, required: true }, // adds to stockBought
unitPrice: {type: Number, required: true},
unitPrice: { type: Number, required: true },
currency: { type: String, required: true },
subtotal: { type: Number, required: true },
tax: { type: Number, required: true },
shipping: { type: Number, required: true },
}]
})
}],
});

module.exports = mongoose.model('inventoryItemMaterial', InventoryItemMaterial, 'inventoryMaterial');
4 changes: 2 additions & 2 deletions src/routes/bmdashboard/bmMaterialsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ materialsRouter.route('/materials')
.get(controller.bmMaterialsList);

return materialsRouter;
}
};

module.exports = routes;
module.exports = routes;
16 changes: 16 additions & 0 deletions src/routes/bmdashboard/bmProjectRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');

const routes = function (buildingProject) {
const projectRouter = express.Router();
const controller = require('../../controllers/bmdashboard/bmProjectController')(buildingProject);

projectRouter.route('/projects')
.get(controller.fetchAllProjects);

projectRouter.route('/project/:projectId')
.get(controller.fetchSingleProject);

return projectRouter;
};

module.exports = routes;
4 changes: 4 additions & 0 deletions src/startup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const profileInitialSetuptoken = require('../models/profileInitialSetupToken');
const reason = require('../models/reason');
const mouseoverText = require('../models/mouseoverText');
const inventoryItemMaterial = require('../models/inventoryItemMaterial');
const buildingProject = require('../models/bmdashboard/buildingProject');


const userProfileRouter = require('../routes/userProfileRouter')(userProfile);
const badgeRouter = require('../routes/badgeRouter')(badge);
Expand Down Expand Up @@ -59,6 +61,7 @@ const mouseoverTextRouter = require('../routes/mouseoverTextRouter')(mouseoverTe
// bm dashboard
const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')();
const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial);
const bmProjectRouter = require('../routes/bmdashboard/bmProjectRouter')(buildingProject);

module.exports = function (app) {
app.use('/api', forgotPwdRouter);
Expand Down Expand Up @@ -93,4 +96,5 @@ module.exports = function (app) {
// bm dashboard
app.use('/api/bm', bmLoginRouter);
app.use('/api/bm', bmMaterialsRouter);
app.use('/api/bm', bmProjectRouter);
};

0 comments on commit d8a2a11

Please sign in to comment.