var mongoose = require('mongoose'); const _ = require('lodash'); const tools = require('../tools/general'); var server_constants = require('../tools/server_constants'); 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, }, typeproj: { type: Number, }, id_main_project: mongoose.Schema.Types.ObjectId, id_parent: mongoose.Schema.Types.ObjectId, priority: { type: Number, }, statusproj: { type: Number, default: 0 }, created_at: { type: Date }, modify_at: { type: Date }, completed_at: { type: Date }, expiring_at: { type: Date, }, enableExpiring: { type: Boolean, default: false }, id_prev: mongoose.Schema.Types.ObjectId, modified: { type: Boolean, }, live_url: { type: String, }, test_url: { type: String, }, totalphases: { type: Number, default: 1 }, actualphase: { type: Number, default: 1 }, hoursplanned: { type: Number, default: 0 }, hoursworked: { type: Number, default: 0 }, progressCalc: { type: Number, default: 0 }, begin_development: { type: Date, }, begin_test: { type: Date, }, hoursweeky_plannedtowork: { type: Number, default: 0 }, endwork_estimate: { type: Date }, privacyread: { type: String }, privacywrite: { type: String } }); ProjectSchema.methods.toJSON = function () { var Project = this; var projObject = Project.toObject(); // console.log(projObject); return _.pick(projObject, tools.allfieldProjectWithId()); }; ProjectSchema.statics.findByUserIdAndIdParent = function (userId, id_parent) { var Project = this; if (userId === '') { return Project.find({ 'id_parent': id_parent, }); } else { return Project.find({ 'userId': userId, 'id_parent': id_parent, }); } }; ProjectSchema.statics.findProjectByUserId = function (userId, idproj) { var Project = this; if (userId === '') { return Project.findOne({ '_id': idproj, }); } else { return Project.findOne({ 'userId': userId, '_id': idproj, }); } }; ProjectSchema.statics.findAllProjByUserId = async function (userId) { var Project = this; return Project.aggregate([ { $graphLookup: { from: "projects", startWith: "$id_main_project", connectFromField: "id_main_project", connectToField: "_id", as: "ris", restrictSearchWithMatch: { $or: [{ privacyread: server_constants.Privacy.all }, { userId: userId }] } } }, { $match: { "ris.privacyread": { $exists: true } } }, // { // $project: { // "descr": 1, // "typeproj": 1, // "id_main_project": 1, // } // } ]).then(ris1 => { console.log('findAllProjByUserId', ris1); return ris1; }) // return Project.find({ // $or: [ // { 'userId': userId }, // {'privacyread' : server_constants.Privacy.all} // ], // $or: [ // { 'typeproj': server_constants.TypeProj.TYPE_PROJECT }, // { 'typeproj': server_constants.TypeProj.TYPE_SUBDIR } // ], }; ProjectSchema.statics.getArrIdParentInTable = function (userId) { var Project = this; return Project.find({ 'userId': userId }).distinct("id_parent") }; ProjectSchema.statics.getIdParentByIdProj = function (idProj) { var Project = this; console.log('INIT getIdParentByIdProj', idProj); return Project.findOne({ '_id': idProj }).then(rec => { if (!!rec) { console.log('getIdParentByIdProj', rec.id_parent); return rec.id_parent; } else { return ''; } }).catch(e => { return ''; }) }; ProjectSchema.statics.getAllProjects = async function (userId) { var Project = this; console.log('getAllProjects'); let obj = []; const projbase = await Project.findById(process.env.PROJECT_ID_MAIN) .then(ris => { console.log('ris', ris); if (!!ris._doc) return ris._doc; else return null; }); obj.arrproj = await Project.findAllProjByUserId(userId); obj.arrproj.push(projbase); return obj; }; ProjectSchema.statics.enabletoModify = async function (userId, idProj) { var Project = this; console.log('getAllProjects'); let obj = []; return Project.findOne({ '_id': idProj, $or: [{ 'privacywrite': server_constants.Privacy.all, 'userId': userId }] }).then(ris => { return (!!ris); }); }; ProjectSchema.statics.updateCalc = async function (userId, idproj, objdatacalc, recIn) { return new Promise((resolve, reject) => { if (!!recIn) { return resolve(recIn); } else { return resolve(Project.findProjectByUserId(userId, idproj)) } }).then((myproj) => { if (!!myproj) { console.log('objdatacalc progressCalc', objdatacalc.mydata.progressCalc); objdatacalc.setValuesToRecord(myproj); // console.log('updateCalc', myproj._id, myproj.progressCalc); return myproj.save() .then(() => { // console.log('salvato proj!'); return true; }) .catch(err => { console.log("Error updateCalc", err.message); }); } return false; }).catch(e => { console.log('Error: ', e); return false; }); }; 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 };