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

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;
}
}
;