From 76f292758b2d0289d0585fee1c8f0357b7946f4c Mon Sep 17 00:00:00 2001 From: Nikhil Trehan Date: Mon, 21 Oct 2024 15:46:55 -0400 Subject: [PATCH] Added API that fetches all projectManagers --- backend/controllers/project.controller.js | 22 ++++++++++++++++++++++ backend/routers/projects.router.js | 3 +++ 2 files changed, 25 insertions(+) diff --git a/backend/controllers/project.controller.js b/backend/controllers/project.controller.js index 37c15025..1ac59f0d 100644 --- a/backend/controllers/project.controller.js +++ b/backend/controllers/project.controller.js @@ -67,4 +67,26 @@ ProjectController.destroy = async function (req, res) { }; +ProjectController.getProjectManagers = async function (req, res) { + try { + const userProjectMap = {}; + const projects = await Project.find({ + managedByUsers: { $exists: true, $ne: [] } + }); + + for (const project of projects) { + for (const user of project.managedByUsers) { + if (!userProjectMap[user]) { + userProjectMap[user] = []; + } + userProjectMap[user].push(project.name); + } + } + return res.status(200).send(userProjectMap); + } catch (err) { + return res.sendStatus(400); + } +}; + + module.exports = ProjectController; diff --git a/backend/routers/projects.router.js b/backend/routers/projects.router.js index 533e300b..939059a4 100644 --- a/backend/routers/projects.router.js +++ b/backend/routers/projects.router.js @@ -5,6 +5,9 @@ const { ProjectController } = require('../controllers'); const { AuthUtil } = require("../middleware"); // The base is /api/projects + +router.get('/projectManagers', ProjectController.getProjectManagers); + router.get('/', ProjectController.project_list); // Its a put because we have to send the PM projects to be filtered here