-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #590 from OneCommunityGlobal/Tim_add_bm_projects_s…
…ummary_routing Tim add bm projects summary routing
- Loading branch information
Showing
7 changed files
with
131 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters