++ hoursweeky_plannedtowork, endwork_estimate, totalphases , actualphase

This commit is contained in:
Paolo Arena
2019-04-07 21:24:27 +02:00
parent 1856818d3a
commit 7b444bc561
5 changed files with 163 additions and 88 deletions

View File

@@ -27,14 +27,6 @@ var ProjectSchema = new mongoose.Schema({
longdescr: { longdescr: {
type: String, type: String,
}, },
hoursplanned: {
type: Number,
default: 0
},
hoursworked: {
type: Number,
default: 0
},
id_parent: { id_parent: {
type: String, type: String,
}, },
@@ -64,10 +56,6 @@ var ProjectSchema = new mongoose.Schema({
id_prev: { id_prev: {
type: String, type: String,
}, },
progressCalc: {
type: Number,
default: 0
},
modified: { modified: {
type: Boolean, type: Boolean,
}, },
@@ -77,12 +65,39 @@ var ProjectSchema = new mongoose.Schema({
test_url: { test_url: {
type: String, type: String,
}, },
totalphases: {
type: Number,
default: 1
},
actualphase: {
type: Number,
default: 1
},
hoursplanned: {
type: Number,
default: 0
},
hoursworked: {
type: Number,
default: 0
},
progressCalc: {
type: Number,
default: 0
},
begin_development: { begin_development: {
type: Date, type: Date,
}, },
begin_test: { begin_test: {
type: Date, type: Date,
}, },
hoursweeky_plannedtowork: {
type: Number,
default: 0
},
endwork_estimate: {
type: Date
}
}); });
@@ -176,7 +191,6 @@ ProjectSchema.statics.getAllProjects = async function (userId) {
ProjectSchema.statics.updateCalc = async function (userId, idproj, objdatacalc, recIn) { ProjectSchema.statics.updateCalc = async function (userId, idproj, objdatacalc, recIn) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!!recIn) { if (!!recIn) {
return resolve(recIn); return resolve(recIn);
@@ -185,21 +199,22 @@ ProjectSchema.statics.updateCalc = async function (userId, idproj, objdatacalc,
} }
}).then((myproj) => { }).then((myproj) => {
if (!!myproj) { if (!!myproj) {
// console.log('myproj', myproj); console.log('objdatacalc progressCalc', objdatacalc.mydata.progressCalc);
objdatacalc.setValuesToRecord(myproj); objdatacalc.setValuesToRecord(myproj);
console.log('updateCalc', myproj._id, objdatacalc); // console.log('updateCalc', myproj._id, myproj.progressCalc);
myproj.save() return myproj.save()
.then(() => { .then(() => {
// console.log('salvato proj!'); // console.log('salvato proj!');
return true;
}) })
.catch(err => { .catch(err => {
console.log("Error updateCalc", err.message); console.log("Error updateCalc", err.message);
}); });
} }
return true; return false;
}).catch(e => { }).catch(e => {
console.log('Error: ', e); console.log('Error: ', e);
return false; return false;

View File

@@ -88,19 +88,23 @@ TodoSchema.methods.toJSON = function () {
}; };
TodoSchema.statics.findByUserIdAndIdParent = function (userId, category) { TodoSchema.statics.findByUserIdAndIdParent = function (userId, category, phase = '') {
var Todo = this; var Todo = this;
if (userId === '') { let tofind = {
return Todo.find({
'category': category, 'category': category,
}); };
} else {
return Todo.find({ if (userId !== '') {
'userId': userId, tofind['userId'] = userId;
'category': category,
});
} }
if (!!phase) {
tofind['phase'] = phase;
}
return Todo.find(tofind);
}; };
TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') { TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') {
@@ -158,27 +162,61 @@ TodoSchema.statics.getAllTodo = async function (userId) {
}; };
class CalcTodo { class CalcTodo {
constructor() { constructor(phase) {
this.mydata = { this.mydata = {
hoursworked: 0, phase: phase,
hoursplanned: 0,
progressCalc: 0,
numitem: 0 numitem: 0
} };
this.clean()
} }
addData(datain) { clean() {
this.mydata.hoursplanned = 0;
this.mydata.hoursworked = 0;
this.mydata.progressCalc = 0;
}
addDataProj(datain) {
if (!!datain) { if (!!datain) {
this.mydata.hoursworked += datain.hoursworked; if (datain.actualphase === this.mydata.phase) {
this.mydata.hoursplanned += datain.hoursplanned; CalcTodo.addFields(this.mydata, datain);
if (!!datain.progressCalc)
this.mydata.progressCalc += datain.progressCalc;
else if (!!datain.progress)
this.mydata.progressCalc += datain.progress;
this.mydata.numitem++;
} }
} }
}
addDataTodo(datain) {
if (!!datain) {
if (datain.phase === this.mydata.phase) {
CalcTodo.addFields(this.mydata, datain);
}
}
}
static addFields(recout, recin) {
// console.log('addFields', recin);
recout.hoursworked += recin.hoursworked;
recout.hoursplanned += recin.hoursplanned;
if (!!recin.progressCalc)
recout.progressCalc += recin.progressCalc;
else
recout.progressCalc += recin.progress;
recout.numitem++;
}
static copyFields(recout, recin) {
recout.hoursworked = recin.hoursworked;
recout.hoursplanned = recin.hoursplanned;
if (!!recin.progressCalc)
recout.progressCalc = recin.progressCalc;
else
recout.progressCalc = 0;
}
setValuesToRecord(objout) {
CalcTodo.copyFields(objout, this.mydata)
}
endDataCalc() { endDataCalc() {
if (this.mydata.numitem > 0) { if (this.mydata.numitem > 0) {
@@ -186,43 +224,40 @@ class CalcTodo {
} }
} }
setValuesToRecord(objout) {
objout.hoursworked = this.mydata.hoursworked;
objout.hoursplanned = this.mydata.hoursplanned;
objout.progressCalc = this.mydata.progressCalc;
}
getData() { getData() {
// return tools.jsonCopy(this.mydata); // return tools.jsonCopy(this.mydata);
return { ...this.mydata } return { ...this.mydata }
} }
} }
TodoSchema.statics.calculateTreeTodo = async function (actualphase, userId, idproj, calcalsoUpper, masterproj, nocalcDown) {
// console.log('calculateTreeTodo', 'actualphase', actualphase, idproj);
TodoSchema.statics.calculateTreeTodo = async function (userId, idproj, calcalsoUpper, masterproj, nocalcDown) { const myrecproj = await Project.findProjectByUserId(userId, idproj);
console.log('calculateTreeTodo', idproj); // const id_parent = await Project.getIdParentByIdProj(idproj);
let objdata = new CalcTodo(); let objdata = new CalcTodo(actualphase);
let promiseChain = Promise.resolve(); let promiseChain = Promise.resolve();
return await Project.findByUserIdAndIdParent(userId, idproj) return await Project.findByUserIdAndIdParent(userId, idproj)
.then(arrsubproj => { .then(arrsubproj => {
console.log('arrsubproj', 'userId', userId, 'idproj', idproj, arrsubproj.length); // console.log(' ', arrsubproj.length, 'SubProjects trovati');
// console.log('arrsubproj', 'userId', userId, 'idproj', idproj, arrsubproj.length);
if (!nocalcDown) { if (!nocalcDown) {
// 1) Calculate the SubProjects of this project Main // 1) Calculate the SubProjects of this project Main
for (const subproj of arrsubproj) { for (const subproj of arrsubproj) {
if (!calcalsoUpper) { // not include the first Project because it was already calculated before if (!calcalsoUpper) { // not include the first Project because it was already calculated before
promiseChain = promiseChain.then(() => { promiseChain = promiseChain.then(() => {
return Todo.calculateTreeTodo(userId, subproj._id, calcalsoUpper, masterproj, true) return Todo.calculateTreeTodo(actualphase, userId, subproj._id, calcalsoUpper, masterproj, true)
.then((subobjdata) => { .then((subobjdata) => {
objdata.addData(subobjdata); objdata.addDataProj(subobjdata);
}); });
}); });
} else { } else {
promiseChain = promiseChain.then(() => { promiseChain = promiseChain.then(() => {
objdata.addData(subproj); objdata.addDataProj(subproj);
}); });
} }
} }
@@ -231,31 +266,37 @@ TodoSchema.statics.calculateTreeTodo = async function (userId, idproj, calcalsoU
return promiseChain; return promiseChain;
}) })
.then(() => { .then(() => {
console.log('objdata', objdata); // console.log('objdata', objdata);
// 2) Calculate the Todos of this project // 2) Calculate the Todos of this project
return Todo.calculateTodoHoursAndProgress(userId, idproj); return Todo.calculateTodoHoursAndProgress(userId, idproj, myrecproj.actualphase);
}) })
.then((objdatatodos) => { .then((objdatatodos) => {
objdata.addData(objdatatodos);
if (myrecproj.actualphase === actualphase) {
// console.log('objdatatodos', objdatatodos);
objdata.addDataTodo(objdatatodos);
// End Calculate // End Calculate
objdata.endDataCalc(); objdata.endDataCalc();
// Update into the DB: // Update into the DB:
return Project.updateCalc(userId, idproj, objdata, null) return Project.updateCalc(userId, idproj, objdata, null)
.then(() => { .then((ris) => {
if (ris)
return objdata.getData(); return objdata.getData();
else
return null;
}); });
} else {
return null;
}
}) })
.then((ris) => { .then((ris) => {
if (calcalsoUpper) { if (calcalsoUpper) {
return Project.getIdParentByIdProj(idproj) if (!!myrecproj.id_parent) {
.then(idparent => {
console.log('idparent', idparent);
if (!!idparent) {
// Calculate also the upper Projects ! // Calculate also the upper Projects !
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Todo.calculateTreeTodo(userId, idparent, true, masterproj, false); Todo.calculateTreeTodo(actualphase, userId, myrecproj.id_parent, true, masterproj, false);
resolve(ris) resolve(ris)
}); });
} else { } else {
@@ -263,7 +304,6 @@ TodoSchema.statics.calculateTreeTodo = async function (userId, idproj, calcalsoU
resolve() resolve()
}); });
} }
});
} else { } else {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve() resolve()
@@ -272,16 +312,16 @@ TodoSchema.statics.calculateTreeTodo = async function (userId, idproj, calcalsoU
}) })
}; };
TodoSchema.statics.calculateTodoHoursAndProgress = async function (userId, idproj, actualphase) {
TodoSchema.statics.calculateTodoHoursAndProgress = async function (userId, idproj) {
var Todo = this; var Todo = this;
let objdata = new CalcTodo(); let objdata = new CalcTodo(actualphase);
return await Todo.findByUserIdAndIdParent(userId, idproj) return await Todo.findByUserIdAndIdParent(userId, idproj, actualphase)
.then(arrtodo => { .then(arrtodo => {
// console.log(' calculateTodo *', arrtodo.length, '* FOUND');
for (let itemtodo of arrtodo) { for (let itemtodo of arrtodo) {
objdata.addData(itemtodo); objdata.addDataTodo(itemtodo);
} }
objdata.endDataCalc(); objdata.endDataCalc();

View File

@@ -79,11 +79,12 @@ router.patch('/:id', authenticate, (req, res) => {
Project.findByIdAndUpdate(id, { $set: body }, { new: true }).then((project) => { Project.findByIdAndUpdate(id, { $set: body }, { new: true }).then((project) => {
tools.mylogshow(' PROJECT TO MODIFY: ', project.descr, body); tools.mylogshow(' PROJECT TO MODIFY: ', project.descr);
if (!project) { if (!project) {
return res.status(404).send(); return res.status(404).send();
} }
if (project.userId !== String(req.user._id)) { if (project.userId !== String(req.user._id)) {
// I'm trying to write something not mine! // I'm trying to write something not mine!
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER }); return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
@@ -91,9 +92,17 @@ router.patch('/:id', authenticate, (req, res) => {
project.modified = false; project.modified = false;
tools.mylog('PATCH ', project.descr, project._id); // Recalculate
return calcSingleProject('', project)
.then(objout => {
tools.mylog('PATCH ', project.descr, project._id, project.progressCalc);
return Project.findById(id).then((projectris) => {
// console.log('projectris progressCalc', projectris.progressCalc);
res.send({ projectris });
});
});
res.send({ project });
}).catch((e) => { }).catch((e) => {
tools.mylogserr('Error patch PROJECT: ', e); tools.mylogserr('Error patch PROJECT: ', e);
res.status(400).send(); res.status(400).send();
@@ -153,7 +162,7 @@ async function calcProjects(userId, obj) {
promiseChain = promiseChain.then(() => { promiseChain = promiseChain.then(() => {
// Find the todos for this project // Find the todos for this project
// Calculate the Progression of the Project // sum the progression // Calculate the Progression of the Project // sum the progression
return Todo.calculateTreeTodo(userId, rec._id, false, rec._id, false) return Todo.calculateTreeTodo(userId, rec._id)
}) })
} }
return promiseChain return promiseChain
@@ -165,6 +174,13 @@ async function calcProjects(userId, obj) {
} }
// USATO SOLO LE PRIME VOLTE! O A RICHIESTA!
async function calcSingleProject(userId, myproj) {
return await Todo.calculateTreeTodo(myproj.actualphase, userId, myproj._id, false, myproj._id, false)
}
router.delete('/:id', authenticate, (req, res) => { router.delete('/:id', authenticate, (req, res) => {
var id = req.params.id; var id = req.params.id;

View File

@@ -88,7 +88,7 @@ router.patch('/:id', authenticate, (req, res) => {
} }
let level = 0; let level = 0;
return Todo.calculateTreeTodo(todo.userId, todo.category, true, todo.category, false) return Todo.calculateTreeTodo(todo.phase, todo.userId, todo.category, true, todo.category, false)
.then(objdatacalc => { .then(objdatacalc => {
tools.mylogshow(' TODO TO MODIFY: ', todo.descr, todo.expiring_at); tools.mylogshow(' TODO TO MODIFY: ', todo.descr, todo.expiring_at);

View File

@@ -24,6 +24,10 @@ webpush.setVapidDetails(subject, publicVapidKey, privateVapidKey);
module.exports = { module.exports = {
TYPE_PROJECT: 1,
TYPE_TODO: 2,
MAX_PHASES: 5,
FIRST_PROJ: '__PROJECTS', FIRST_PROJ: '__PROJECTS',
EXECUTE_CALCPROJ: true, EXECUTE_CALCPROJ: true,
getHostname: function () { getHostname: function () {
@@ -62,7 +66,7 @@ module.exports = {
// #TODO Projects++ Add fields ... // #TODO Projects++ Add fields ...
allfieldProject: function () { allfieldProject: function () {
return ['userId', 'pos', 'id_parent', 'descr', 'longdescr', 'hoursplanned', 'hoursworked', 'priority', 'statusproj', 'created_at', 'modify_at', return ['userId', 'pos', 'id_parent', 'descr', 'longdescr', 'hoursplanned', 'hoursworked', 'priority', 'statusproj', 'created_at', 'modify_at',
'completed_at', 'expiring_at', 'enableExpiring', 'id_prev', 'progressCalc', 'modified', 'live_url', 'test_url', 'begin_development', 'begin_test'] 'completed_at', 'expiring_at', 'enableExpiring', 'id_prev', 'progressCalc', 'modified', 'live_url', 'test_url', 'begin_development', 'begin_test', 'totalphases', 'actualphase', 'hoursweeky_plannedtowork', 'endwork_estimate']
}, },
allfieldProjectWithId: function () { allfieldProjectWithId: function () {