var mongoose = require('mongoose'); const _ = require('lodash'); const tools = require('../tools/general'); var { Project } = require('./project'); mongoose.Promise = global.Promise; mongoose.level = "F"; // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true }); mongoose.set('debug', process.env.DEBUG); var TodoSchema = new mongoose.Schema({ userId: { type: String, }, pos: { type: Number, }, category: { type: String, }, descr: { type: String, }, priority: { type: Number, }, statustodo: { type: Number, default: 0 }, created_at: { type: Date }, modify_at: { type: Date }, start_date: { type: Date, }, completed_at: { type: Date }, expiring_at: { type: Date, }, enableExpiring: { type: Boolean, default: false }, id_prev: mongoose.Schema.Types.ObjectId, progress: { type: Number, }, phase: { type: Number, }, assigned_to_userId: { type: String, }, hoursplanned: { type: Number, }, hoursworked: { type: Number, }, modified: { type: Boolean, }, }); TodoSchema.methods.toJSON = function () { var todo = this; var todoObject = todo.toObject(); // console.log(todoObject); return _.pick(todoObject, tools.allfieldTodoWithId()); }; TodoSchema.statics.findByUserIdAndIdParent = function (userId, category, phase = '') { var Todo = this; let tofind = { 'category': category, }; if (userId !== '') { tofind['userId'] = userId; } if (!!phase) { tofind['phase'] = phase; } return Todo.find(tofind); }; TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') { var Todo = this; if (category === '') { return Todo.find({ 'userId': userId, }).then(ris => { //return tools.mapSort(ris) return ris }) } else { return Todo.find({ 'userId': userId, 'category': category, }); } }; TodoSchema.statics.getArrIdParentInTable = function (userId) { var Todo = this; return Todo.find({ 'userId': userId }).distinct("category") .then(arrcategory => { return arrcategory }) }; TodoSchema.statics.getAllTodo = async function (userId) { var Todo = this; const arralltodo = await Todo.findAllByUserIdAndCat(userId); // console.log('arralltodo', arralltodo); let obj = []; obj.arrcategories = await Todo.getArrIdParentInTable(userId); const arrtodos = []; if (obj.arrcategories.length > 0) { for (const mycat of obj.arrcategories) { const arrfiltrato = arralltodo.filter(item => item.category === mycat); // console.log('arrfiltrato ', mycat, arrfiltrato); arrtodos.push(tools.mapSort(arrfiltrato)) } } obj.arrtodos = arrtodos; // console.log('obj', obj); return obj; }; class CalcTodo { constructor(phase) { this.mydata = { phase: phase, numitem: 0 }; this.clean() } clean() { this.mydata.hoursplanned = 0; this.mydata.hoursworked = 0; this.mydata.progressCalc = 0; } addDataProj(datain) { if (!!datain) { if (datain.actualphase === this.mydata.phase) { CalcTodo.addFields(this.mydata, datain); } } } 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() { if (this.mydata.numitem > 0) { this.mydata.progressCalc = Math.round(this.mydata.progressCalc / this.mydata.numitem); } } getData() { // return tools.jsonCopy(this.mydata); return { ...this.mydata } } } TodoSchema.statics.calculateTreeTodo = async function (actualphase, userId, idproj, calcalsoUpper, masterproj, nocalcDown) { // console.log('calculateTreeTodo', 'actualphase', actualphase, idproj); const myrecproj = await Project.findProjectByUserId(userId, idproj); // const id_parent = await Project.getIdParentByIdProj(idproj); let objdata = new CalcTodo(actualphase); let promiseChain = Promise.resolve(); return await Project.findByUserIdAndIdParent(userId, idproj) .then(arrsubproj => { // console.log(' ', arrsubproj.length, 'SubProjects trovati'); // 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(actualphase, userId, subproj._id, calcalsoUpper, masterproj, true) .then((subobjdata) => { objdata.addDataProj(subobjdata); }); }); } else { promiseChain = promiseChain.then(() => { objdata.addDataProj(subproj); }); } } } return promiseChain; }) .then(() => { // console.log('objdata', objdata); // 2) Calculate the Todos of this project return Todo.calculateTodoHoursAndProgress(userId, idproj, myrecproj.actualphase); }) .then((objdatatodos) => { if (myrecproj.actualphase === actualphase) { // console.log('objdatatodos', objdatatodos); objdata.addDataTodo(objdatatodos); // End Calculate objdata.endDataCalc(); // Update into the DB: return Project.updateCalc(userId, idproj, objdata, null) .then((ris) => { if (ris) return objdata.getData(); else return null; }); } else { return null; } }) .then((ris) => { if (calcalsoUpper) { if (!!myrecproj.id_parent) { // Calculate also the upper Projects ! return new Promise((resolve, reject) => { Todo.calculateTreeTodo(actualphase, userId, myrecproj.id_parent, true, masterproj, false); resolve(ris) }); } else { return new Promise((resolve, reject) => { resolve() }); } } else { return new Promise((resolve, reject) => { resolve() }); } }) }; TodoSchema.statics.calculateTodoHoursAndProgress = async function (userId, idproj, actualphase) { var Todo = this; let objdata = new CalcTodo(actualphase); return await Todo.findByUserIdAndIdParent(userId, idproj, actualphase) .then(arrtodo => { // console.log(' calculateTodo *', arrtodo.length, '* FOUND'); for (let itemtodo of arrtodo) { objdata.addDataTodo(itemtodo); } objdata.endDataCalc(); return objdata.getData(); }) .catch(e => { console.log('Error: ', e); return null; }); }; TodoSchema.pre('save', function (next) { // var todo = this; // console.log('todo.expiring_at', todo.expiring_at); next(); }); var Todo = mongoose.model('Todos', TodoSchema); module.exports = { Todo };