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