Files
freeplanet_serverside/server/models/project.js
2019-04-09 21:07:30 +02:00

256 lines
4.4 KiB
JavaScript

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: {
type: String,
},
id_parent: {
type: String,
},
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: {
type: String,
},
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.findAllByUserId = function (userId) {
var Project = this;
return Project.find({ 'typeproj': server_constants.TypeProj.TYPE_PROJECT,
$or: [
{ 'userId': userId },
{'privacyread' : server_constants.Privacy.all}
]
}).then(ris => {
return ris
})
};
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;
let obj = [];
obj.arrproj = await Project.findAllByUserId(userId);
return obj;
};
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 };