diff --git a/server/models/project.js b/server/models/project.js index 58b21d1..757cbf5 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -9,6 +9,8 @@ var server_constants = require('../tools/server_constants'); mongoose.Promise = global.Promise; mongoose.level = "F"; +const { ObjectID } = require('mongodb'); + // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true @@ -144,7 +146,7 @@ ProjectSchema.statics.findProjectByUserId = function (userId, idproj) { } else { return Project.findOne({ 'userId': userId, - '_id': idproj, + '_id': ObjectId(idproj), }); } }; @@ -175,7 +177,7 @@ ProjectSchema.statics.findAllProjByUserId = async function (userId) { // } ]).then(ris1 => { - console.log('findAllProjByUserId', ris1); + // console.log('findAllProjByUserId', ris1); return ris1; }) @@ -205,7 +207,7 @@ ProjectSchema.statics.getIdParentByIdProj = function (idProj) { console.log('INIT getIdParentByIdProj', idProj); - return Project.findOne({ '_id': idProj }).then(rec => { + return Project.findOne({ '_id': ObjectId(idProj) }).then(rec => { if (!!rec) { console.log('getIdParentByIdProj', rec.id_parent); return rec.id_parent; @@ -220,13 +222,13 @@ ProjectSchema.statics.getIdParentByIdProj = function (idProj) { ProjectSchema.statics.getAllProjects = async function (userId) { var Project = this; - console.log('getAllProjects'); + // console.log('getAllProjects'); let obj = []; const projbase = await Project.findById(process.env.PROJECT_ID_MAIN) .then(ris => { - console.log('ris', ris); + // console.log('ris', ris); if (!!ris._doc) return ris._doc; else @@ -242,7 +244,6 @@ ProjectSchema.statics.getAllProjects = async function (userId) { ProjectSchema.statics.enabletoModify = async function (userId, idProj) { var Project = this; - console.log('getAllProjects'); let obj = []; diff --git a/server/models/todo.js b/server/models/todo.js index 58f4e51..b6ae167 100644 --- a/server/models/todo.js +++ b/server/models/todo.js @@ -6,6 +6,8 @@ const tools = require('../tools/general'); var { Project } = require('./project'); +var server_constants = require('../tools/server_constants'); + mongoose.Promise = global.Promise; mongoose.level = "F"; @@ -23,9 +25,7 @@ var TodoSchema = new mongoose.Schema({ pos: { type: Number, }, - category: { - type: String, - }, + category: mongoose.Schema.Types.ObjectId, descr: { type: String, }, @@ -105,16 +105,54 @@ TodoSchema.statics.findByUserIdAndIdParent = function (userId, category, phase = }; +// User.find({ admin: true }).where('created_at').gt(monthAgo).exec(function(err, users) { +// if (err) throw err; + +function getQueryFilterTodo(userId) { + myobj = [{ privacyread: server_constants.Privacy.all }, { userId: userId }]; + return myobj; +} + +function getQueryTodo(filterMatchBefore = {}, userId) { + + let filterMatchAfter = { + $or: getQueryFilterTodo(userId) + }; + + let myobjField = getobjFieldTodo(true); + + const query = [ + { $match: filterMatchBefore }, + { + $lookup: { + from: "projects", + localField: "category", // field in the Todo collection + foreignField: "_id", // field in the projects collection + as: "fromItems" + } + }, + { + $replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ["$fromItems", 0] }, "$$ROOT"] } } + }, + { $match: filterMatchAfter }, + { $project: myobjField }]; + + return query; +} + TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') { var Todo = this; if (category === '') { - return Todo.find({ - 'userId': userId, - }).then(ris => { - //return tools.mapSort(ris) - return ris - }) + + const query = getQueryTodo({}, userId); + + return Todo.aggregate(query) + .then(ris => { + // console.log('ris TODO:', ris); + //return tools.mapSort(ris) + return ris + }); } else { return Todo.find({ @@ -127,13 +165,47 @@ TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') { TodoSchema.statics.getArrIdParentInTable = function (userId) { var Todo = this; - return Todo.find({ 'userId': userId }).distinct("category") + return Todo.find(getQueryFilterTodo(userId)).distinct("category") .then(arrcategory => { return arrcategory }) }; +function getobjFieldTodo(withprojectfield) { + let objfield = {}; + for (const field of tools.allfieldTodo()) { + objfield[field] = 1; + } + + if (withprojectfield) { + objfield["privacyread"] = 1; + objfield["privacywrite"] = 1; + } + + // console.log("getobjFieldTodo", objfield); + + return objfield +} + +TodoSchema.statics.enabletoModify = async function (userId, idTodo) { + var Todo = this; + + let params = {}; + params['_id'] = ObjectId(idTodo); + const query = getQueryTodo(params, userId); + + return Todo.aggregate(query) + .then(ris => { + return (!!ris); + }) + .catch(err => { + console.log("ERR: ".err); + return false; + }); +}; + + TodoSchema.statics.getAllTodo = async function (userId) { var Todo = this; @@ -144,18 +216,25 @@ TodoSchema.statics.getAllTodo = async function (userId) { obj.arrcategories = await Todo.getArrIdParentInTable(userId); - const arrtodos = []; + console.log('obj.arrcategories', obj.arrcategories); + + let arrtodos = []; if (obj.arrcategories.length > 0) { for (const mycat of obj.arrcategories) { - const arrfiltrato = arralltodo.filter(item => item.category === mycat); - // console.log('arrfiltrato ', mycat, arrfiltrato); - arrtodos.push(tools.mapSort(arrfiltrato)) + if (!!arralltodo) { + const arrfiltrato = arralltodo.filter(item => item.category.toString() === mycat.toString()); + if (arrfiltrato.length > 0) { + const arrmap = tools.mapSort(arrfiltrato); + arrtodos.push(arrmap); + console.log('AGGIUNGI RECORDS TODO! cat: ', mycat, 'da aggiungere:', arrfiltrato.length, 'attuali', arrtodos.length); + } + } } } obj.arrtodos = arrtodos; - // console.log('obj', obj); + console.log('obj.arrtodos', obj.arrtodos); return obj; diff --git a/server/tools/general.js b/server/tools/general.js index 201afbb..b3788c3 100644 --- a/server/tools/general.js +++ b/server/tools/general.js @@ -200,7 +200,8 @@ module.exports = { // SORT WITH PREV_ID // ********************** mapSort: function (linkedList) { - var sortedList = []; + let sortedList = []; + let remainingList = []; var map = new Map(); var currentId = null; @@ -219,21 +220,30 @@ module.exports = { } } - while (sortedList.length < linkedList.length) { + let conta = 0; + while (conta < linkedList.length) { // get the item with a previous item ID referencing the current item var nextItem = linkedList[map.get(currentId)]; if (nextItem === undefined) { - break; + + } else { + sortedList.push(nextItem); + currentId = String(nextItem._id); } - sortedList.push(nextItem); - currentId = String(nextItem._id); + conta++; } - if (sortedList.length < linkedList.length) { + if (linkedList.length > sortedList.length) { + // If are not in the list, I'll put at the bottom of the list console.log('ATTENZIONE !!! ', sortedList.length, linkedList.length); + for (const itemlinked of linkedList) { + const elemtrov = sortedList.find((item) => item._id === itemlinked._id); + if (elemtrov === undefined) { + sortedList.push(itemlinked); + } + } } - // console.log('DOPO sortedList', sortedList); return sortedList;