diff --git a/src/controllers/bmdashboard/bmMaterialsController.js b/src/controllers/bmdashboard/bmMaterialsController.js index a31ed460e..fb6003e90 100644 --- a/src/controllers/bmdashboard/bmMaterialsController.js +++ b/src/controllers/bmdashboard/bmMaterialsController.js @@ -1,4 +1,4 @@ -const mongoose = require('mongoose') +const mongoose = require('mongoose'); const bmMaterialsController = function (ItemMaterial) { const bmMaterialsList = async function _matsList(req, res) { @@ -7,37 +7,37 @@ 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); } @@ -45,4 +45,4 @@ const bmMaterialsController = function (ItemMaterial) { return { bmMaterialsList }; }; -module.exports = bmMaterialsController; \ No newline at end of file +module.exports = bmMaterialsController; diff --git a/src/controllers/bmdashboard/bmProjectsController.js b/src/controllers/bmdashboard/bmProjectsController.js index 946869bce..f80eea6c0 100644 --- a/src/controllers/bmdashboard/bmProjectsController.js +++ b/src/controllers/bmdashboard/bmProjectsController.js @@ -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); } diff --git a/src/routes/bmdashboard/bmProjectsRouter.js b/src/routes/bmdashboard/bmProjectsRouter.js index 204efbfad..05adfa59d 100644 --- a/src/routes/bmdashboard/bmProjectsRouter.js +++ b/src/routes/bmdashboard/bmProjectsRouter.js @@ -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; diff --git a/src/startup/routes.js b/src/startup/routes.js index 7208535d8..aa786dbbd 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -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);