150 lines
2.5 KiB
JavaScript
150 lines
2.5 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 TodoSchema = new mongoose.Schema({
|
|
userId: {
|
|
type: String,
|
|
},
|
|
pos: {
|
|
type: Number,
|
|
},
|
|
category: {
|
|
type: String,
|
|
},
|
|
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,
|
|
},
|
|
progress: {
|
|
type: Number,
|
|
},
|
|
modified: {
|
|
type: Boolean,
|
|
},
|
|
});
|
|
|
|
TodoSchema.methods.toJSON = function () {
|
|
var todo = this;
|
|
var todoObject = todo.toObject();
|
|
|
|
// console.log(todoObject);
|
|
|
|
return _.pick(todoObject, tools.allfieldTodoWithId());
|
|
};
|
|
|
|
|
|
TodoSchema.statics.findByUserIdAndCat = function (userId, category) {
|
|
var Todo = this;
|
|
|
|
return Todo.find({
|
|
'userId': userId,
|
|
'category': category,
|
|
});
|
|
};
|
|
|
|
TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') {
|
|
var Todo = this;
|
|
|
|
if (category === '') {
|
|
return Todo.find({
|
|
'userId': userId,
|
|
}).then(ris => {
|
|
return tools.mapSort(ris)
|
|
})
|
|
|
|
} else {
|
|
return Todo.find({
|
|
'userId': userId,
|
|
'category': category,
|
|
});
|
|
}
|
|
};
|
|
|
|
TodoSchema.statics.getArrCategoryInTable = function (userId) {
|
|
var Todo = this;
|
|
|
|
return Todo.find({ 'userId': userId }).distinct("category")
|
|
.then(arrcategory => {
|
|
return arrcategory
|
|
})
|
|
|
|
};
|
|
|
|
TodoSchema.statics.getAllTodo = async function (userId) {
|
|
var Todo = this;
|
|
|
|
const arralltodo = await Todo.findAllByUserIdAndCat(userId);
|
|
// console.log('arralltodo', );
|
|
|
|
let obj = [];
|
|
|
|
obj.arrcategories = await Todo.getArrCategoryInTable(userId);
|
|
|
|
const arrtodos = [];
|
|
if (obj.arrcategories.length > 0) {
|
|
for (let mycat in obj.arrcategories) {
|
|
arrtodos.push(arralltodo.filter(item => item.category === obj.arrcategories[mycat]))
|
|
}
|
|
}
|
|
|
|
obj.arrtodos = arrtodos;
|
|
|
|
// console.log('obj', obj);
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
TodoSchema.pre('save', function (next) {
|
|
// var todo = this;
|
|
|
|
// console.log('todo.expiring_at', todo.expiring_at);
|
|
|
|
next();
|
|
});
|
|
|
|
|
|
|
|
var Todo = mongoose.model('Todos', TodoSchema);
|
|
|
|
module.exports = { Todo };
|
|
|