++ Projects
This commit is contained in:
176
server/router/projects_router.js
Normal file
176
server/router/projects_router.js
Normal file
@@ -0,0 +1,176 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
var server_constants = require('../tools/server_constants');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
// var mongoose = require('mongoose');
|
||||
|
||||
var { Project } = require('../models/project');
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
|
||||
|
||||
router.post('/', authenticate, (req, res) => {
|
||||
|
||||
var body = _.pick(req.body, tools.allfieldProjectWithId());
|
||||
tools.mylogshow('PROJ INPUT', body);
|
||||
var project = new Project(body);
|
||||
|
||||
// project.expiring_at = new Date(project.expiring_at);
|
||||
|
||||
tools.mylog('ID :', project._id, project.descr, project.userId, req.user._id);
|
||||
|
||||
if (!('descr' in req.body)) {
|
||||
return res.status(400).send({ code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC });
|
||||
}
|
||||
|
||||
if (String(project.userId) !== String(req.user._id)) {
|
||||
// I'm trying to write something not mine!
|
||||
tools.mylog('project.userId = ', project.userId, 'req.user._id', req.user._id);
|
||||
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
|
||||
}
|
||||
|
||||
tools.mylog('PROJECT POST :', project.descr, project._id);
|
||||
|
||||
project.modified = false;
|
||||
if (!project.descr) {
|
||||
console.log('RECORD NON VALIDO !?', req.body)
|
||||
}
|
||||
|
||||
project.save().then((writeresult) => {
|
||||
let idobj = writeresult._id;
|
||||
Project.findById(idobj)
|
||||
.then(record => {
|
||||
tools.mylog('REC SAVED :', record.descr);
|
||||
|
||||
tools.sendNotificationToUser(project.userId, 'Project: ' + record.descr, record.descr, '/project/' + project.category, 'project')
|
||||
.then(ris => {
|
||||
if (ris) {
|
||||
res.send({ record });
|
||||
} else {
|
||||
// already sent the error on calling sendNotificationToUser
|
||||
}
|
||||
})
|
||||
})
|
||||
}).catch((e) => {
|
||||
console.log('ERRORE in PROJECT POST', e.message);
|
||||
res.status(400).send(e);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
router.patch('/:id', authenticate, (req, res) => {
|
||||
var id = req.params.id;
|
||||
var body = _.pick(req.body, tools.allfieldProject());
|
||||
|
||||
tools.mylogshow('PATCH PROJECT: ', id)
|
||||
|
||||
if (!ObjectID.isValid(id)) {
|
||||
tools.mylog('ERROR: id not VALID', id);
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
|
||||
Project.findByIdAndUpdate(id, { $set: body }, { new: true }).then((project) => {
|
||||
tools.mylogshow(' PROJECT TO MODIFY: ', project.descr, project.expiring_at);
|
||||
if (!project) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
if (project.userId !== String(req.user._id)) {
|
||||
// I'm trying to write something not mine!
|
||||
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
|
||||
}
|
||||
|
||||
project.modified = false;
|
||||
|
||||
tools.mylog('PATCH ', project.descr, project._id);
|
||||
|
||||
res.send({ project });
|
||||
}).catch((e) => {
|
||||
tools.mylogserr('Error patch PROJECT: ', e);
|
||||
res.status(400).send();
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
router.get('/:userId', authenticate, (req, res) => {
|
||||
var userId = req.params.userId;
|
||||
// var category = req.params.category;
|
||||
|
||||
tools.mylog('GET PROJECTS : ', req.params);
|
||||
|
||||
if (!ObjectID.isValid(userId)) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
if (userId !== String(req.user._id)) {
|
||||
// I'm trying to write something not mine!
|
||||
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
|
||||
}
|
||||
|
||||
// Extract all the projects of the userId only
|
||||
// Project.findAllByUserIdAndCat(userId, category).then((projects) => {
|
||||
Project.getAllProjects(userId).then((objprojects) => {
|
||||
tools.mylog('projects', objprojects.arrproj.length);
|
||||
|
||||
objout = calcProjects(objprojects);
|
||||
|
||||
res.send({ projects: objout });
|
||||
|
||||
tools.mylog('objout', objout);
|
||||
|
||||
// res.send({ projects: objprojects.arrproj, categories: objprojects.arrcategories });
|
||||
}).catch((e) => {
|
||||
console.log(e);
|
||||
res.status(400).send(e);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
function calcProjects(obj) {
|
||||
|
||||
let myarr = tools.jsonCopy(obj.arrproj);
|
||||
|
||||
for (const indrec in myarr) {
|
||||
// Calculate the Progression of the Project
|
||||
|
||||
// Find the todos for this project
|
||||
|
||||
// sum the progression
|
||||
|
||||
myarr[indrec].progressCalc = 1;
|
||||
}
|
||||
return myarr
|
||||
|
||||
}
|
||||
|
||||
router.delete('/:id', authenticate, (req, res) => {
|
||||
var id = req.params.id;
|
||||
|
||||
if (!ObjectID.isValid(id)) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
Project.findByIdAndRemove(id).then((project) => {
|
||||
if (!project) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
tools.mylog('DELETED ', project.descr, project._id);
|
||||
|
||||
res.send({ project });
|
||||
}).catch((e) => {
|
||||
res.status(400).send();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user