Updated Project calculation for hours and progress.
This commit is contained in:
@@ -29,9 +29,11 @@ var ProjectSchema = new mongoose.Schema({
|
||||
},
|
||||
hoursplanned: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
hoursworked: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
id_parent: {
|
||||
type: String,
|
||||
@@ -64,6 +66,7 @@ var ProjectSchema = new mongoose.Schema({
|
||||
},
|
||||
progressCalc: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
modified: {
|
||||
type: Boolean,
|
||||
@@ -93,15 +96,37 @@ ProjectSchema.methods.toJSON = function () {
|
||||
};
|
||||
|
||||
|
||||
ProjectSchema.statics.findByUserIdAndCat = function (userId, category) {
|
||||
ProjectSchema.statics.findByUserIdAndIdParent = function (userId, id_parent) {
|
||||
var Project = this;
|
||||
|
||||
return Project.find({
|
||||
'userId': userId,
|
||||
'category': category,
|
||||
});
|
||||
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;
|
||||
|
||||
@@ -113,13 +138,28 @@ ProjectSchema.statics.findAllByUserId = function (userId) {
|
||||
|
||||
};
|
||||
|
||||
ProjectSchema.statics.getArrCategoryInTable = function (userId) {
|
||||
ProjectSchema.statics.getArrIdParentInTable = function (userId) {
|
||||
var Project = this;
|
||||
|
||||
return Project.find({ 'userId': userId }).distinct("category")
|
||||
.then(arrcategory => {
|
||||
return arrcategory
|
||||
})
|
||||
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 '';
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
@@ -134,6 +174,40 @@ ProjectSchema.statics.getAllProjects = async function (userId) {
|
||||
|
||||
};
|
||||
|
||||
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('myproj', myproj);
|
||||
|
||||
objdatacalc.setValuesToRecord(myproj);
|
||||
|
||||
console.log('updateCalc', myproj._id, objdatacalc);
|
||||
|
||||
myproj.save()
|
||||
.then(() => {
|
||||
// console.log('salvato proj!');
|
||||
})
|
||||
.catch(err => {
|
||||
console.log("Error updateCalc", err.message);
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}).catch(e => {
|
||||
console.log('Error: ', e);
|
||||
return false;
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
ProjectSchema.pre('save', function (next) {
|
||||
// var Project = this;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user