Updated Project calculation for hours and progress.
This commit is contained in:
@@ -29,9 +29,11 @@ var ProjectSchema = new mongoose.Schema({
|
||||
},
|
||||
hoursplanned: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
hoursworked: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
id_parent: {
|
||||
type: String,
|
||||
@@ -64,6 +66,7 @@ var ProjectSchema = new mongoose.Schema({
|
||||
},
|
||||
progressCalc: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
modified: {
|
||||
type: Boolean,
|
||||
@@ -93,15 +96,37 @@ ProjectSchema.methods.toJSON = function () {
|
||||
};
|
||||
|
||||
|
||||
ProjectSchema.statics.findByUserIdAndCat = function (userId, category) {
|
||||
ProjectSchema.statics.findByUserIdAndIdParent = function (userId, id_parent) {
|
||||
var Project = this;
|
||||
|
||||
return Project.find({
|
||||
'userId': userId,
|
||||
'category': category,
|
||||
});
|
||||
if (userId === '') {
|
||||
return Project.find({
|
||||
'id_parent': id_parent,
|
||||
});
|
||||
} else {
|
||||
return Project.find({
|
||||
'userId': userId,
|
||||
'id_parent': id_parent,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
ProjectSchema.statics.findProjectByUserId = function (userId, idproj) {
|
||||
var Project = this;
|
||||
|
||||
if (userId === '') {
|
||||
return Project.findOne({
|
||||
'_id': idproj,
|
||||
});
|
||||
} else {
|
||||
return Project.findOne({
|
||||
'userId': userId,
|
||||
'_id': idproj,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
ProjectSchema.statics.findAllByUserId = function (userId) {
|
||||
var Project = this;
|
||||
|
||||
@@ -113,13 +138,28 @@ ProjectSchema.statics.findAllByUserId = function (userId) {
|
||||
|
||||
};
|
||||
|
||||
ProjectSchema.statics.getArrCategoryInTable = function (userId) {
|
||||
ProjectSchema.statics.getArrIdParentInTable = function (userId) {
|
||||
var Project = this;
|
||||
|
||||
return Project.find({ 'userId': userId }).distinct("category")
|
||||
.then(arrcategory => {
|
||||
return arrcategory
|
||||
})
|
||||
return Project.find({ 'userId': userId }).distinct("id_parent")
|
||||
|
||||
};
|
||||
|
||||
ProjectSchema.statics.getIdParentByIdProj = function (idProj) {
|
||||
var Project = this;
|
||||
|
||||
console.log('INIT getIdParentByIdProj', idProj);
|
||||
|
||||
return Project.findOne({ '_id': idProj }).then(rec => {
|
||||
if (!!rec) {
|
||||
console.log('getIdParentByIdProj', rec.id_parent);
|
||||
return rec.id_parent;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}).catch(e => {
|
||||
return '';
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
@@ -134,6 +174,40 @@ ProjectSchema.statics.getAllProjects = async function (userId) {
|
||||
|
||||
};
|
||||
|
||||
ProjectSchema.statics.updateCalc = async function (userId, idproj, objdatacalc, recIn) {
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!!recIn) {
|
||||
return resolve(recIn);
|
||||
} else {
|
||||
return resolve(Project.findProjectByUserId(userId, idproj))
|
||||
}
|
||||
}).then((myproj) => {
|
||||
if (!!myproj) {
|
||||
// console.log('myproj', myproj);
|
||||
|
||||
objdatacalc.setValuesToRecord(myproj);
|
||||
|
||||
console.log('updateCalc', myproj._id, objdatacalc);
|
||||
|
||||
myproj.save()
|
||||
.then(() => {
|
||||
// console.log('salvato proj!');
|
||||
})
|
||||
.catch(err => {
|
||||
console.log("Error updateCalc", err.message);
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}).catch(e => {
|
||||
console.log('Error: ', e);
|
||||
return false;
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
ProjectSchema.pre('save', function (next) {
|
||||
// var Project = this;
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ const _ = require('lodash');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
var { Project } = require('./project');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
@@ -40,6 +42,9 @@ var TodoSchema = new mongoose.Schema({
|
||||
modify_at: {
|
||||
type: Date
|
||||
},
|
||||
start_date: {
|
||||
type: Date,
|
||||
},
|
||||
completed_at: {
|
||||
type: Date
|
||||
},
|
||||
@@ -56,6 +61,18 @@ var TodoSchema = new mongoose.Schema({
|
||||
progress: {
|
||||
type: Number,
|
||||
},
|
||||
phase: {
|
||||
type: Number,
|
||||
},
|
||||
assigned_to_userId: {
|
||||
type: String,
|
||||
},
|
||||
hoursplanned: {
|
||||
type: Number,
|
||||
},
|
||||
hoursworked: {
|
||||
type: Number,
|
||||
},
|
||||
modified: {
|
||||
type: Boolean,
|
||||
},
|
||||
@@ -71,13 +88,19 @@ TodoSchema.methods.toJSON = function () {
|
||||
};
|
||||
|
||||
|
||||
TodoSchema.statics.findByUserIdAndCat = function (userId, category) {
|
||||
TodoSchema.statics.findByUserIdAndIdParent = function (userId, category) {
|
||||
var Todo = this;
|
||||
|
||||
return Todo.find({
|
||||
'userId': userId,
|
||||
'category': category,
|
||||
});
|
||||
if (userId === '') {
|
||||
return Todo.find({
|
||||
'category': category,
|
||||
});
|
||||
} else {
|
||||
return Todo.find({
|
||||
'userId': userId,
|
||||
'category': category,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') {
|
||||
@@ -87,7 +110,8 @@ TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') {
|
||||
return Todo.find({
|
||||
'userId': userId,
|
||||
}).then(ris => {
|
||||
return tools.mapSort(ris)
|
||||
//return tools.mapSort(ris)
|
||||
return ris
|
||||
})
|
||||
|
||||
} else {
|
||||
@@ -98,7 +122,7 @@ TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') {
|
||||
}
|
||||
};
|
||||
|
||||
TodoSchema.statics.getArrCategoryInTable = function (userId) {
|
||||
TodoSchema.statics.getArrIdParentInTable = function (userId) {
|
||||
var Todo = this;
|
||||
|
||||
return Todo.find({ 'userId': userId }).distinct("category")
|
||||
@@ -112,16 +136,16 @@ TodoSchema.statics.getAllTodo = async function (userId) {
|
||||
var Todo = this;
|
||||
|
||||
const arralltodo = await Todo.findAllByUserIdAndCat(userId);
|
||||
// console.log('arralltodo', );
|
||||
// console.log('arralltodo', arralltodo);
|
||||
|
||||
let obj = [];
|
||||
|
||||
obj.arrcategories = await Todo.getArrCategoryInTable(userId);
|
||||
obj.arrcategories = await Todo.getArrIdParentInTable(userId);
|
||||
|
||||
const arrtodos = [];
|
||||
if (obj.arrcategories.length > 0) {
|
||||
for (let mycat in obj.arrcategories) {
|
||||
arrtodos.push(arralltodo.filter(item => item.category === obj.arrcategories[mycat]))
|
||||
arrtodos.push(tools.mapSort(arralltodo.filter(item => item.category === obj.arrcategories[mycat])))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +157,144 @@ TodoSchema.statics.getAllTodo = async function (userId) {
|
||||
|
||||
};
|
||||
|
||||
class CalcTodo {
|
||||
constructor() {
|
||||
this.mydata = {
|
||||
hoursworked: 0,
|
||||
hoursplanned: 0,
|
||||
progressCalc: 0,
|
||||
numitem: 0
|
||||
}
|
||||
}
|
||||
|
||||
addData(datain) {
|
||||
if (!!datain) {
|
||||
this.mydata.hoursworked += datain.hoursworked;
|
||||
this.mydata.hoursplanned += datain.hoursplanned;
|
||||
if (!!datain.progressCalc)
|
||||
this.mydata.progressCalc += datain.progressCalc;
|
||||
else if (!!datain.progress)
|
||||
this.mydata.progressCalc += datain.progress;
|
||||
|
||||
this.mydata.numitem++;
|
||||
}
|
||||
}
|
||||
|
||||
endDataCalc() {
|
||||
if (this.mydata.numitem > 0) {
|
||||
this.mydata.progressCalc = Math.round(this.mydata.progressCalc / this.mydata.numitem);
|
||||
}
|
||||
}
|
||||
|
||||
setValuesToRecord(objout) {
|
||||
objout.hoursworked = this.mydata.hoursworked;
|
||||
objout.hoursplanned = this.mydata.hoursplanned;
|
||||
objout.progressCalc = this.mydata.progressCalc;
|
||||
}
|
||||
|
||||
getData() {
|
||||
// return tools.jsonCopy(this.mydata);
|
||||
return { ...this.mydata }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TodoSchema.statics.calculateTreeTodo = async function (userId, idproj, calcalsoUpper, masterproj, nocalcDown) {
|
||||
console.log('calculateTreeTodo', idproj);
|
||||
|
||||
let objdata = new CalcTodo();
|
||||
|
||||
let promiseChain = Promise.resolve();
|
||||
|
||||
return await Project.findByUserIdAndIdParent(userId, idproj)
|
||||
.then(arrsubproj => {
|
||||
console.log('arrsubproj', 'userId', userId, 'idproj', idproj, arrsubproj.length);
|
||||
|
||||
if (!nocalcDown) {
|
||||
// 1) Calculate the SubProjects of this project Main
|
||||
for (const subproj of arrsubproj) {
|
||||
if (!calcalsoUpper) { // not include the first Project because it was already calculated before
|
||||
promiseChain = promiseChain.then(() => {
|
||||
return Todo.calculateTreeTodo(userId, subproj._id, calcalsoUpper, masterproj, true)
|
||||
.then((subobjdata) => {
|
||||
objdata.addData(subobjdata);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
promiseChain = promiseChain.then(() => {
|
||||
objdata.addData(subproj);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return promiseChain;
|
||||
})
|
||||
.then(() => {
|
||||
console.log('objdata', objdata);
|
||||
// 2) Calculate the Todos of this project
|
||||
return Todo.calculateTodoHoursAndProgress(userId, idproj);
|
||||
})
|
||||
.then((objdatatodos) => {
|
||||
objdata.addData(objdatatodos);
|
||||
|
||||
// End Calculate
|
||||
objdata.endDataCalc();
|
||||
|
||||
// Update into the DB:
|
||||
return Project.updateCalc(userId, idproj, objdata, null)
|
||||
.then(() => {
|
||||
return objdata.getData();
|
||||
});
|
||||
})
|
||||
.then((ris) => {
|
||||
if (calcalsoUpper) {
|
||||
return Project.getIdParentByIdProj(idproj)
|
||||
.then(idparent => {
|
||||
console.log('idparent', idparent);
|
||||
if (!!idparent) {
|
||||
// Calculate also the upper Projects !
|
||||
return new Promise((resolve, reject) => {
|
||||
Todo.calculateTreeTodo(userId, idparent, true, masterproj, true);
|
||||
resolve(ris)
|
||||
});
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve()
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve()
|
||||
});
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
|
||||
TodoSchema.statics.calculateTodoHoursAndProgress = async function (userId, idproj) {
|
||||
var Todo = this;
|
||||
|
||||
let objdata = new CalcTodo();
|
||||
|
||||
return await Todo.findByUserIdAndIdParent(userId, idproj)
|
||||
.then(arrtodo => {
|
||||
for (let itemtodo of arrtodo) {
|
||||
objdata.addData(itemtodo);
|
||||
}
|
||||
|
||||
objdata.endDataCalc();
|
||||
|
||||
return objdata.getData();
|
||||
})
|
||||
.catch(e => {
|
||||
console.log('Error: ', e);
|
||||
return null;
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
TodoSchema.pre('save', function (next) {
|
||||
// var todo = this;
|
||||
|
||||
@@ -142,7 +304,6 @@ TodoSchema.pre('save', function (next) {
|
||||
});
|
||||
|
||||
|
||||
|
||||
var Todo = mongoose.model('Todos', TodoSchema);
|
||||
|
||||
module.exports = { Todo };
|
||||
|
||||
Reference in New Issue
Block a user