133 lines
2.2 KiB
JavaScript
133 lines
2.2 KiB
JavaScript
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,
|
|
},
|
|
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.allfieldTodoWithId());
|
|
};
|
|
|
|
|
|
ProjectSchema.statics.findByUserIdAndCat = function (userId, category) {
|
|
var Project = this;
|
|
|
|
return Project.find({
|
|
'userId': userId,
|
|
'category': category,
|
|
});
|
|
};
|
|
|
|
ProjectSchema.statics.findAllByUserIdAndCat = function (userId, category = '') {
|
|
var Project = this;
|
|
|
|
if (category === '') {
|
|
return Project.find({
|
|
'userId': userId,
|
|
}).then(ris => {
|
|
return tools.mapSort(ris)
|
|
})
|
|
|
|
} else {
|
|
return Project.find({
|
|
'userId': userId,
|
|
'category': category,
|
|
});
|
|
}
|
|
};
|
|
|
|
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.findAllByUserIdAndCat(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 };
|
|
|