d2cad7438e
* upgrades jest to latest version * tests Project model with mockingoose * extracts createProject controller to own file * tests createProject controller with sinon-mongoose
32 lines
668 B
JavaScript
32 lines
668 B
JavaScript
import Project from '../../models/project';
|
|
|
|
export default function createProject(req, res) {
|
|
let projectValues = {
|
|
user: req.user._id
|
|
};
|
|
|
|
projectValues = Object.assign(projectValues, req.body);
|
|
|
|
function sendFailure() {
|
|
res.json({ success: false });
|
|
}
|
|
|
|
function populateUserData(newProject) {
|
|
return Project.populate(
|
|
newProject,
|
|
{ path: 'user', select: 'username' },
|
|
(err, newProjectWithUser) => {
|
|
if (err) {
|
|
sendFailure();
|
|
return;
|
|
}
|
|
res.json(newProjectWithUser);
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
return Project.create(projectValues)
|
|
.then(populateUserData)
|
|
.catch(sendFailure);
|
|
}
|