From 6a0b7db73cdd95782225c41bfadc1ee7dcdc766f Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Sat, 13 Apr 2019 03:04:49 +0200 Subject: [PATCH] - Modified privacywrite query with graphLookup: $graphLookup: { from: "projects", startWith: "$id_main_project", connectFromField: "id_main_project", connectToField: "_id", as: "ris", restrictSearchWithMatch: { $or: [{ privacyread: server_constants.Privacy.all }, { userId: userId }] } } }, { $match: { "ris.privacyread": { $exists: true } } }, --- server/models/project.js | 88 +++++++++++++++++++++++++------- server/models/todo.js | 10 ++-- server/router/projects_router.js | 8 ++- server/tests/seed/seed.js | 8 +-- server/tools/general.js | 10 +++- server/tools/server_constants.js | 2 +- 6 files changed, 94 insertions(+), 32 deletions(-) diff --git a/server/models/project.js b/server/models/project.js index 2a5ee9c..58b21d1 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -32,12 +32,8 @@ var ProjectSchema = new mongoose.Schema({ typeproj: { type: Number, }, - id_main_project: { - type: String, - }, - id_parent: { - type: String, - }, + id_main_project: mongoose.Schema.Types.ObjectId, + id_parent: mongoose.Schema.Types.ObjectId, priority: { type: Number, }, @@ -61,9 +57,7 @@ var ProjectSchema = new mongoose.Schema({ type: Boolean, default: false }, - id_prev: { - type: String, - }, + id_prev: mongoose.Schema.Types.ObjectId, modified: { type: Boolean, }, @@ -156,18 +150,47 @@ ProjectSchema.statics.findProjectByUserId = function (userId, idproj) { }; -ProjectSchema.statics.findAllByUserId = function (userId) { +ProjectSchema.statics.findAllProjByUserId = async 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 + return Project.aggregate([ + { + $graphLookup: { + from: "projects", + startWith: "$id_main_project", + connectFromField: "id_main_project", + connectToField: "_id", + as: "ris", + restrictSearchWithMatch: { $or: [{ privacyread: server_constants.Privacy.all }, { userId: userId }] } + } + }, + { $match: { "ris.privacyread": { $exists: true } } }, + + // { + // $project: { + // "descr": 1, + // "typeproj": 1, + // "id_main_project": 1, + // } + // } + ]).then(ris1 => { + + console.log('findAllProjByUserId', ris1); + + return ris1; }) + // return Project.find({ + // $or: [ + // { 'userId': userId }, + // {'privacyread' : server_constants.Privacy.all} + // ], + // $or: [ + // { 'typeproj': server_constants.TypeProj.TYPE_PROJECT }, + // { 'typeproj': server_constants.TypeProj.TYPE_SUBDIR } + // ], + + }; ProjectSchema.statics.getArrIdParentInTable = function (userId) { @@ -197,15 +220,44 @@ ProjectSchema.statics.getIdParentByIdProj = function (idProj) { ProjectSchema.statics.getAllProjects = async function (userId) { var Project = this; + console.log('getAllProjects'); let obj = []; - obj.arrproj = await Project.findAllByUserId(userId); + const projbase = await Project.findById(process.env.PROJECT_ID_MAIN) + .then(ris => { + console.log('ris', ris); + if (!!ris._doc) + return ris._doc; + else + return null; + }); + + obj.arrproj = await Project.findAllProjByUserId(userId); + obj.arrproj.push(projbase); return obj; }; +ProjectSchema.statics.enabletoModify = async function (userId, idProj) { + var Project = this; + console.log('getAllProjects'); + + let obj = []; + + return Project.findOne({ + '_id': idProj, + $or: [{ + 'privacywrite': server_constants.Privacy.all, + 'userId': userId + }] + }).then(ris => { + return (!!ris); + }); +}; + + ProjectSchema.statics.updateCalc = async function (userId, idproj, objdatacalc, recIn) { return new Promise((resolve, reject) => { diff --git a/server/models/todo.js b/server/models/todo.js index f24dfd5..58f4e51 100644 --- a/server/models/todo.js +++ b/server/models/todo.js @@ -55,9 +55,7 @@ var TodoSchema = new mongoose.Schema({ type: Boolean, default: false }, - id_prev: { - type: String, - }, + id_prev: mongoose.Schema.Types.ObjectId, progress: { type: Number, }, @@ -148,8 +146,10 @@ TodoSchema.statics.getAllTodo = async function (userId) { const arrtodos = []; if (obj.arrcategories.length > 0) { - for (let mycat in obj.arrcategories) { - arrtodos.push(tools.mapSort(arralltodo.filter(item => item.category === obj.arrcategories[mycat]))) + for (const mycat of obj.arrcategories) { + const arrfiltrato = arralltodo.filter(item => item.category === mycat); + // console.log('arrfiltrato ', mycat, arrfiltrato); + arrtodos.push(tools.mapSort(arrfiltrato)) } } diff --git a/server/router/projects_router.js b/server/router/projects_router.js index ac68b45..ec45037 100644 --- a/server/router/projects_router.js +++ b/server/router/projects_router.js @@ -70,13 +70,16 @@ router.patch('/:id', authenticate, (req, res) => { var id = req.params.id; var body = _.pick(req.body, tools.allfieldProject()); - tools.mylogshow('PATCH PROJECT: ', id) + tools.mylogshow('PATCH PROJECT: ', id); if (!ObjectID.isValid(id)) { tools.mylog('ERROR: id not VALID', id); return res.status(404).send(); } + // check if you are able to modify this project + if (!Project.enabletoModify(String(req.user._id), id)) + return res.status(404).send(); Project.findByIdAndUpdate(id, { $set: body }, { new: true }).then((project) => { tools.mylogshow(' PROJECT TO MODIFY: ', project.descr); @@ -128,7 +131,8 @@ router.get('/:userId', authenticate, (req, res) => { // Extract all the projects of the userId only // Project.findAllByUserIdAndCat(userId, category).then((projects) => { return Project.getAllProjects(userId).then((objprojects) => { - tools.mylog('projects', objprojects.arrproj.length); + if (!!objprojects.arrproj) + tools.mylog('projects', objprojects.arrproj.length); return objprojects diff --git a/server/tests/seed/seed.js b/server/tests/seed/seed.js index 009c452..5fe8785 100644 --- a/server/tests/seed/seed.js +++ b/server/tests/seed/seed.js @@ -81,7 +81,7 @@ const todos = [{ descr: "Primo Task Esempio", enableExpiring: false, expiring_at: new Date(), - id_prev: "0", + id_prev: null, modified: false, modify_at: new Date(), pos: 1, @@ -97,7 +97,7 @@ const todos = [{ descr: "Secondo Task Esempio", enableExpiring: false, expiring_at: new Date(), - id_prev: "1", + // id_prev: "1", modified: false, modify_at: new Date(), pos: 2, @@ -113,7 +113,7 @@ const todos = [{ descr: "Terzo Task Esempio", enableExpiring: false, expiring_at: new Date(), - id_prev: "1", + // id_prev: "1", modified: false, modify_at: new Date(), pos: 3, @@ -129,7 +129,7 @@ const todos = [{ descr: "Nuovo Quarto Task Esempio da Inserire", enableExpiring: false, expiring_at: new Date(), - id_prev: "2", + // id_prev: "2", modified: false, modify_at: new Date(), pos: 4, diff --git a/server/tools/general.js b/server/tools/general.js index c7fc8b5..201afbb 100644 --- a/server/tools/general.js +++ b/server/tools/general.js @@ -215,19 +215,25 @@ module.exports = { // console.log('currentId', currentId); sortedList.push(item); } else { - map.set(item.id_prev, i); + map.set(String(item.id_prev), i); } } while (sortedList.length < linkedList.length) { // get the item with a previous item ID referencing the current item var nextItem = linkedList[map.get(currentId)]; - if (nextItem === undefined) + if (nextItem === undefined) { break; + } sortedList.push(nextItem); currentId = String(nextItem._id); } + if (sortedList.length < linkedList.length) { + console.log('ATTENZIONE !!! ', sortedList.length, linkedList.length); + } + + // console.log('DOPO sortedList', sortedList); return sortedList; diff --git a/server/tools/server_constants.js b/server/tools/server_constants.js index 0d9283d..01e483d 100644 --- a/server/tools/server_constants.js +++ b/server/tools/server_constants.js @@ -15,7 +15,7 @@ module.exports = Object.freeze({ LIST_END: '10000000', - LIST_START: '0', + LIST_START: null, Privacy: { all: 'all',