Updated Project calculation for hours and progress.
This commit is contained in:
@@ -10,6 +10,7 @@ var { authenticate } = require('../middleware/authenticate');
|
||||
// var mongoose = require('mongoose');
|
||||
|
||||
var { Project } = require('../models/project');
|
||||
const { Todo } = require('./../models/todo');
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
@@ -65,7 +66,6 @@ router.post('/', authenticate, (req, res) => {
|
||||
});
|
||||
|
||||
|
||||
|
||||
router.patch('/:id', authenticate, (req, res) => {
|
||||
var id = req.params.id;
|
||||
var body = _.pick(req.body, tools.allfieldProject());
|
||||
@@ -118,16 +118,21 @@ router.get('/:userId', authenticate, (req, res) => {
|
||||
|
||||
// Extract all the projects of the userId only
|
||||
// Project.findAllByUserIdAndCat(userId, category).then((projects) => {
|
||||
Project.getAllProjects(userId).then((objprojects) => {
|
||||
return Project.getAllProjects(userId).then((objprojects) => {
|
||||
tools.mylog('projects', objprojects.arrproj.length);
|
||||
|
||||
objout = calcProjects(objprojects);
|
||||
return objprojects
|
||||
|
||||
res.send({ projects: objout });
|
||||
}).then((objprojects) => {
|
||||
// tools.mylog('objprojects', objprojects);
|
||||
|
||||
tools.mylog('objout', objout);
|
||||
|
||||
// res.send({ projects: objprojects.arrproj, categories: objprojects.arrcategories });
|
||||
// if (tools.EXECUTE_CALCPROJ) {
|
||||
// return calcProjects('', objprojects).then(objout => {
|
||||
// res.send({ projects: objprojects.arrproj });
|
||||
// });
|
||||
// } else {
|
||||
res.send({ projects: objprojects.arrproj });
|
||||
// }
|
||||
}).catch((e) => {
|
||||
console.log(e);
|
||||
res.status(400).send(e);
|
||||
@@ -135,20 +140,28 @@ router.get('/:userId', authenticate, (req, res) => {
|
||||
|
||||
});
|
||||
|
||||
function calcProjects(obj) {
|
||||
// USATO SOLO LE PRIME VOLTE! O A RICHIESTA!
|
||||
async function calcProjects(userId, obj) {
|
||||
|
||||
let myarr = tools.jsonCopy(obj.arrproj);
|
||||
// let myarr = tools.jsonCopy(obj.arrproj);
|
||||
let myarr = [...obj.arrproj];
|
||||
|
||||
for (const indrec in myarr) {
|
||||
// Calculate the Progression of the Project
|
||||
if (myarr.length > 0) {
|
||||
let promiseChain = Promise.resolve();
|
||||
|
||||
// Find the todos for this project
|
||||
|
||||
// sum the progression
|
||||
|
||||
myarr[indrec].progressCalc = Math.round(Math.random() * 100);
|
||||
for (const rec of myarr) {
|
||||
promiseChain = promiseChain.then(() => {
|
||||
// Find the todos for this project
|
||||
// Calculate the Progression of the Project // sum the progression
|
||||
return Todo.calculateTreeTodo(userId, rec._id, false, rec._id, false)
|
||||
})
|
||||
}
|
||||
return promiseChain
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
return myarr
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ const tools = require('../tools/general');
|
||||
|
||||
var server_constants = require('../tools/server_constants');
|
||||
|
||||
var { Project } = require('../models/project');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
var mongoose = require('mongoose');
|
||||
@@ -66,12 +68,11 @@ router.post('/', authenticate, (req, res) => {
|
||||
});
|
||||
|
||||
|
||||
|
||||
router.patch('/:id', authenticate, (req, res) => {
|
||||
var id = req.params.id;
|
||||
var body = _.pick(req.body, tools.allfieldTodo());
|
||||
|
||||
tools.mylogshow('PATCH TODO: ', id)
|
||||
tools.mylogshow('PATCH TODO: ', id);
|
||||
|
||||
if (!ObjectID.isValid(id)) {
|
||||
tools.mylog('ERROR: id not VALID', id);
|
||||
@@ -79,26 +80,31 @@ router.patch('/:id', authenticate, (req, res) => {
|
||||
}
|
||||
|
||||
|
||||
Todo.findByIdAndUpdate(id, { $set: body }, { new: true })
|
||||
.then((todo) => {
|
||||
if (!todo) {
|
||||
tools.mylogshow(' TODO NOT FOUND !: id:', id, 'body: ', body);
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
Todo.findByIdAndUpdate(id, { $set: body }, { new: true }).then((todo) => {
|
||||
if (!todo) {
|
||||
tools.mylogshow(' TODO NOT FOUND !: id:', id, 'body: ', body);
|
||||
return res.status(404).send();
|
||||
}
|
||||
let level = 0;
|
||||
return Todo.calculateTreeTodo(todo.userId, todo.category, true, todo.category, false)
|
||||
.then(objdatacalc => {
|
||||
tools.mylogshow(' TODO TO MODIFY: ', todo.descr, todo.expiring_at);
|
||||
|
||||
tools.mylogshow(' TODO TO MODIFY: ', todo.descr, todo.expiring_at);
|
||||
if (todo.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 });
|
||||
}
|
||||
|
||||
if (todo.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 });
|
||||
}
|
||||
todo.modified = false;
|
||||
|
||||
todo.modified = false;
|
||||
tools.mylog('PATCH ', todo.descr, todo._id);
|
||||
|
||||
tools.mylog('PATCH ', todo.descr, todo._id);
|
||||
res.send({ todo, objdatacalc });
|
||||
});
|
||||
|
||||
res.send({ todo });
|
||||
}).catch((e) => {
|
||||
}).catch((e) => {
|
||||
tools.mylogserr('Error patch TODO: ', e);
|
||||
res.status(400).send();
|
||||
})
|
||||
@@ -121,7 +127,6 @@ router.get('/:userId', authenticate, (req, res) => {
|
||||
}
|
||||
|
||||
// Extract all the todos of the userId only
|
||||
// Todo.findAllByUserIdAndCat(userId, category).then((todos) => {
|
||||
Todo.getAllTodo(userId).then((objtodos) => {
|
||||
|
||||
tools.mylog('todos', objtodos.arrtodos.length);
|
||||
|
||||
Reference in New Issue
Block a user