Calcolo Hours

This commit is contained in:
Paolo Arena
2021-02-11 02:20:35 +01:00
parent 686d6bf97a
commit 88ae9af12c
12 changed files with 335 additions and 33 deletions

View File

@@ -1,4 +1,4 @@
mongoose = require('mongoose');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const tools = require('../tools/general');

98
src/server/models/hours.js Executable file
View File

@@ -0,0 +1,98 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const tools = require('../tools/general');
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const HoursSchema = new Schema({
idapp: {
type: String,
},
userId: { type: Schema.Types.ObjectId, ref: 'User' },
descr: {
type: String,
},
todoId: {
type: String,
},
date: {
type: Date,
default: Date.now
},
time_start: {
type: Number,
},
time_end: {
type: Number,
},
hours: {
type: Number,
},
});
var Hours = module.exports = mongoose.model('Hours', HoursSchema);
module.exports.getFieldsForSearch = function () {
return [{ field: 'descr', type: tools.FieldType.string }]
};
module.exports.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
module.exports.findAllIdApp = async function (idapp) {
const myfind = { idapp };
return await Hours.find(myfind);
};
module.exports.calculateHoursTodo = async function (idtodo) {
const Hours = this;
if (idtodo) {
const myfind = [
{
$match: { todoId: idtodo }
},
{
$group:
{
_id: "$todoId",
totalAmount: {
$sum: "$hours"
},
count: {
$sum: 1
}
}
}
];
try {
const ris = await Hours.aggregate(myfind);
if (ris.length > 0) {
return ris[0].totalAmount;
} else {
return 0;
}
} catch (e) {
console.log('e', e);
return 0;
}
} else {
return 0;
}
}
;

View File

@@ -2,9 +2,11 @@ var mongoose = require('mongoose');
const _ = require('lodash');
const { ObjectID } = require('mongodb');
const tools = require('../tools/general');
var { Project } = require('./project');
const Hours = require('./hours');
var server_constants = require('../tools/server_constants');
@@ -98,12 +100,14 @@ TodoSchema.methods.toJSON = function () {
return _.pick(todoObject, tools.allfieldTodoWithId());
};
TodoSchema.statics.findByUserIdAndIdParent = function (userId, category, phase = '') {
var Todo = this;
let tofind = {
'category': category,
category: ObjectID(category),
$or:
[{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
};
if (userId !== '') {
@@ -293,6 +297,7 @@ class CalcTodo {
constructor(phase) {
this.mydata = {
_id: '',
phase: phase,
numitem: 0
};
@@ -306,17 +311,21 @@ class CalcTodo {
this.mydata.progressCalc = 0;
}
addDataProj(datain) {
async addDataProj(datain) {
if (!!datain) {
if (datain.actualphase === this.mydata.phase) {
if (datain._id)
datain.hoursworked = await Hours.calculateHoursTodo(datain._id.toString());
CalcTodo.addFields(this.mydata, datain, true);
}
}
}
addDataTodo(datain) {
async addDataTodo(datain) {
if (!!datain) {
if (datain.phase === this.mydata.phase) {
if (datain._id)
datain.hoursworked = await Hours.calculateHoursTodo(datain._id.toString());
CalcTodo.addFields(this.mydata, datain, false);
}
}
@@ -324,6 +333,7 @@ class CalcTodo {
static addFields(recout, recin, isproj) {
// console.log('addFields', recin);
recout.hoursworked += recin.hoursworked;
recout.hoursplanned += recin.hoursplanned;
let hoursleft = (recin.hoursplanned - recin.hoursworked);
@@ -351,7 +361,6 @@ class CalcTodo {
else
recout.progressCalc = 0;
}
}
setValuesToRecord(objout) {
@@ -393,12 +402,12 @@ TodoSchema.statics.calculateTreeTodo = async function (actualphase, userId, idpr
promiseChain = promiseChain.then(() => {
return Todo.calculateTreeTodo(actualphase, userId, subproj._id, calcalsoUpper, masterproj, true)
.then((subobjdata) => {
objdata.addDataProj(subobjdata);
return objdata.addDataProj(subobjdata);
});
});
} else {
promiseChain = promiseChain.then(() => {
objdata.addDataProj(subproj);
return objdata.addDataProj(subproj);
});
}
}
@@ -420,19 +429,19 @@ TodoSchema.statics.calculateTreeTodo = async function (actualphase, userId, idpr
if (!!myrecproj) {
if (myrecproj.actualphase === actualphase) {
// console.log('objdatatodos', objdatatodos);
objdata.addDataTodo(objdatatodos);
return objdata.addDataTodo(objdatatodos)
.then(() => {
// End Calculate
objdata.endDataCalc();
// 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;
// Update into the DB:
return Project.updateCalc(userId, idproj, objdata, null)
.then((ris) => (ris) ? objdata.getData() : null);
});
} else {
return Project.updateCalc(userId, idproj, objdata, null)
.then((ris) => (ris) ? objdata.getData() : null);
}
}
}
@@ -449,15 +458,17 @@ TodoSchema.statics.calculateTreeTodo = async function (actualphase, userId, idpr
});
} else {
return new Promise((resolve, reject) => {
resolve()
resolve(ris)
});
}
}
}
return new Promise((resolve, reject) => {
resolve()
resolve(ris)
});
}).catch((e) => {
console.log('error', e.message)
})
};
@@ -469,9 +480,16 @@ TodoSchema.statics.calculateTodoHoursAndProgress = async function (userId, idpro
return await Todo.findByUserIdAndIdParent(userId, idproj, actualphase)
.then(arrtodo => {
// console.log(' calculateTodo *', arrtodo.length, '* FOUND');
for (let itemtodo of arrtodo) {
objdata.addDataTodo(itemtodo);
let promiseChain = Promise.resolve();
for (const itemtodo of arrtodo) {
promiseChain = promiseChain.then(() => {
return objdata.addDataTodo(itemtodo);
})
}
return promiseChain;
}).then(() => {
objdata.endDataCalc();
@@ -485,6 +503,7 @@ TodoSchema.statics.calculateTodoHoursAndProgress = async function (userId, idpro
};
TodoSchema.pre('save', function (next) {
// var todo = this;

View File

@@ -221,6 +221,12 @@ const UserSchema = new mongoose.Schema({
manage_telegram: {
type: Boolean
},
resplist: {
type: Boolean
},
workerslist: {
type: Boolean
},
dateofbirth: {
type: Date,
},
@@ -1263,6 +1269,28 @@ UserSchema.statics.getusersManagers = async function (idapp) {
});
};
UserSchema.statics.getusersRespList = async function (idapp) {
const User = this;
return await User.find({ idapp, 'profile.resplist': true }, { _id:1, username: 1, name: 1, surname: 1 })
.then((arrrec) => {
return (!!arrrec) ? arrrec : null;
}).catch((e) => {
console.error('getusersRespList', e);
});
};
UserSchema.statics.getusersWorkersList = async function (idapp) {
const User = this;
return await User.find({ idapp, 'profile.workerslist': true }, { _id:1, username: 1, name: 1, surname: 1 })
.then((arrrec) => {
return (!!arrrec) ? arrrec : null;
}).catch((e) => {
console.error('getusersWorkersList', e);
});
};
UserSchema.statics.getusersManagersAndZoomeri = async function (idapp) {
const User = this;