Skip to content

Commit

Permalink
update projects controller to return basic proj info
Browse files Browse the repository at this point in the history
  • Loading branch information
tdkent committed Oct 26, 2023
1 parent 63de1d1 commit 815b0f5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 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;
23 changes: 18 additions & 5 deletions src/controllers/bmdashboard/bmProjectsController.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
const mongoose = require('mongoose');

const bmMProjectsController = function () {
// fetches projects with reference to BM userProfile id
const bmMProjectsController = function (UserProfile) {
const bmProjectsSummary = async function _projSumm(req, res) {
const { userId } = req.params;
try {
res.json({ message: 'Hello world' });
const projectData = await UserProfile
// fetch user profile, return only projects array
.findOne({ _id: userId }, { projects: 1 })
// populate data with projects documents using the ObjectId in the projects array
.populate({
path: 'projects',
// limit to projects with category value 'Housing'
match: { category: 'Housing' },
// returns only these fields
select: '_id projectName isActive createdDatetime',
})
.exec()
.then(result => result.projects)
.catch(error => res.status(500).send(error));

// .then(results => res.status(200).send(results))
// .catch(error => res.status(500).send(error))
// for each project, find all materials in the project
res.status(200).send(projectData);
} catch (err) {
res.json(err);
}
Expand Down
7 changes: 3 additions & 4 deletions src/routes/bmdashboard/bmProjectsRouter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const express = require('express');

const routes = function () {
const routes = function (userProfile) {
const projectsRouter = express.Router();
const controller = require('../../controllers/bmdashboard/bmProjectsController')();
// const controller = require('../../controllers/bmdashboard/bmMaterialsController')(itemMaterial);
const controller = require('../../controllers/bmdashboard/bmProjectsController')(userProfile);

projectsRouter.route('/projects')
projectsRouter.route('/projects/:userId')
.get(controller.bmProjectsSummary);

return projectsRouter;
Expand Down
2 changes: 1 addition & 1 deletion src/startup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const mouseoverTextRouter = require('../routes/mouseoverTextRouter')(mouseoverTe
// bm dashboard
const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')();
const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(inventoryItemMaterial);
const bmProjectsRouter = require('../routes/bmdashboard/bmProjectsRouter')();
const bmProjectsRouter = require('../routes/bmdashboard/bmProjectsRouter')(userProfile);

module.exports = function (app) {
app.use('/api', forgotPwdRouter);
Expand Down

0 comments on commit 815b0f5

Please sign in to comment.