var mongoose = require('mongoose'); const _ = require('lodash'); const tools = require('../tools/general'); 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 ProjectSchema = new mongoose.Schema({ userId: { type: String, }, pos: { type: Number, }, descr: { type: String, }, longdescr: { type: String, }, hoursplanned: { type: Number, }, hoursworked: { type: Number, }, id_parent: { type: String, }, priority: { type: Number, }, completed: { type: Boolean, default: false }, created_at: { type: Date }, modify_at: { type: Date }, completed_at: { type: Date }, expiring_at: { type: Date, }, enableExpiring: { type: Boolean, default: false }, id_prev: { type: String, }, progressCalc: { type: Number, }, modified: { type: Boolean, }, }); ProjectSchema.methods.toJSON = function () { var Project = this; var projObject = Project.toObject(); // console.log(projObject); return _.pick(projObject, tools.allfieldProjectWithId()); }; ProjectSchema.statics.findByUserIdAndCat = function (userId, category) { var Project = this; return Project.find({ 'userId': userId, 'category': category, }); }; ProjectSchema.statics.findAllByUserId = function (userId) { var Project = this; return Project.find({ 'userId': userId, }).then(ris => { return ris }) }; ProjectSchema.statics.getArrCategoryInTable = function (userId) { var Project = this; return Project.find({ 'userId': userId }).distinct("category") .then(arrcategory => { return arrcategory }) }; ProjectSchema.statics.getAllProjects = async function (userId) { var Project = this; let obj = []; obj.arrproj = await Project.findAllByUserId(userId); return obj; }; ProjectSchema.pre('save', function (next) { // var Project = this; // console.log('Project.expiring_at', Project.expiring_at); next(); }); var Project = mongoose.model('Projects', ProjectSchema); module.exports = { Project };