- 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 } } },
This commit is contained in:
Paolo Arena
2019-04-13 03:04:49 +02:00
parent 1deba2a5ff
commit 6a0b7db73c
6 changed files with 94 additions and 32 deletions

View File

@@ -32,12 +32,8 @@ var ProjectSchema = new mongoose.Schema({
typeproj: { typeproj: {
type: Number, type: Number,
}, },
id_main_project: { id_main_project: mongoose.Schema.Types.ObjectId,
type: String, id_parent: mongoose.Schema.Types.ObjectId,
},
id_parent: {
type: String,
},
priority: { priority: {
type: Number, type: Number,
}, },
@@ -61,9 +57,7 @@ var ProjectSchema = new mongoose.Schema({
type: Boolean, type: Boolean,
default: false default: false
}, },
id_prev: { id_prev: mongoose.Schema.Types.ObjectId,
type: String,
},
modified: { modified: {
type: Boolean, 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; var Project = this;
return Project.find({ 'typeproj': server_constants.TypeProj.TYPE_PROJECT, return Project.aggregate([
$or: [ {
{ 'userId': userId }, $graphLookup: {
{'privacyread' : server_constants.Privacy.all} from: "projects",
] startWith: "$id_main_project",
}).then(ris => { connectFromField: "id_main_project",
return ris 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) { ProjectSchema.statics.getArrIdParentInTable = function (userId) {
@@ -197,15 +220,44 @@ ProjectSchema.statics.getIdParentByIdProj = function (idProj) {
ProjectSchema.statics.getAllProjects = async function (userId) { ProjectSchema.statics.getAllProjects = async function (userId) {
var Project = this; var Project = this;
console.log('getAllProjects');
let obj = []; 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; 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) { ProjectSchema.statics.updateCalc = async function (userId, idproj, objdatacalc, recIn) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@@ -55,9 +55,7 @@ var TodoSchema = new mongoose.Schema({
type: Boolean, type: Boolean,
default: false default: false
}, },
id_prev: { id_prev: mongoose.Schema.Types.ObjectId,
type: String,
},
progress: { progress: {
type: Number, type: Number,
}, },
@@ -148,8 +146,10 @@ TodoSchema.statics.getAllTodo = async function (userId) {
const arrtodos = []; const arrtodos = [];
if (obj.arrcategories.length > 0) { if (obj.arrcategories.length > 0) {
for (let mycat in obj.arrcategories) { for (const mycat of obj.arrcategories) {
arrtodos.push(tools.mapSort(arralltodo.filter(item => item.category === obj.arrcategories[mycat]))) const arrfiltrato = arralltodo.filter(item => item.category === mycat);
// console.log('arrfiltrato ', mycat, arrfiltrato);
arrtodos.push(tools.mapSort(arrfiltrato))
} }
} }

View File

@@ -70,13 +70,16 @@ router.patch('/:id', authenticate, (req, res) => {
var id = req.params.id; var id = req.params.id;
var body = _.pick(req.body, tools.allfieldProject()); var body = _.pick(req.body, tools.allfieldProject());
tools.mylogshow('PATCH PROJECT: ', id) tools.mylogshow('PATCH PROJECT: ', id);
if (!ObjectID.isValid(id)) { if (!ObjectID.isValid(id)) {
tools.mylog('ERROR: id not VALID', id); tools.mylog('ERROR: id not VALID', id);
return res.status(404).send(); 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) => { Project.findByIdAndUpdate(id, { $set: body }, { new: true }).then((project) => {
tools.mylogshow(' PROJECT TO MODIFY: ', project.descr); 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 // Extract all the projects of the userId only
// Project.findAllByUserIdAndCat(userId, category).then((projects) => { // Project.findAllByUserIdAndCat(userId, category).then((projects) => {
return Project.getAllProjects(userId).then((objprojects) => { return Project.getAllProjects(userId).then((objprojects) => {
tools.mylog('projects', objprojects.arrproj.length); if (!!objprojects.arrproj)
tools.mylog('projects', objprojects.arrproj.length);
return objprojects return objprojects

View File

@@ -81,7 +81,7 @@ const todos = [{
descr: "Primo Task Esempio", descr: "Primo Task Esempio",
enableExpiring: false, enableExpiring: false,
expiring_at: new Date(), expiring_at: new Date(),
id_prev: "0", id_prev: null,
modified: false, modified: false,
modify_at: new Date(), modify_at: new Date(),
pos: 1, pos: 1,
@@ -97,7 +97,7 @@ const todos = [{
descr: "Secondo Task Esempio", descr: "Secondo Task Esempio",
enableExpiring: false, enableExpiring: false,
expiring_at: new Date(), expiring_at: new Date(),
id_prev: "1", // id_prev: "1",
modified: false, modified: false,
modify_at: new Date(), modify_at: new Date(),
pos: 2, pos: 2,
@@ -113,7 +113,7 @@ const todos = [{
descr: "Terzo Task Esempio", descr: "Terzo Task Esempio",
enableExpiring: false, enableExpiring: false,
expiring_at: new Date(), expiring_at: new Date(),
id_prev: "1", // id_prev: "1",
modified: false, modified: false,
modify_at: new Date(), modify_at: new Date(),
pos: 3, pos: 3,
@@ -129,7 +129,7 @@ const todos = [{
descr: "Nuovo Quarto Task Esempio da Inserire", descr: "Nuovo Quarto Task Esempio da Inserire",
enableExpiring: false, enableExpiring: false,
expiring_at: new Date(), expiring_at: new Date(),
id_prev: "2", // id_prev: "2",
modified: false, modified: false,
modify_at: new Date(), modify_at: new Date(),
pos: 4, pos: 4,

View File

@@ -215,19 +215,25 @@ module.exports = {
// console.log('currentId', currentId); // console.log('currentId', currentId);
sortedList.push(item); sortedList.push(item);
} else { } else {
map.set(item.id_prev, i); map.set(String(item.id_prev), i);
} }
} }
while (sortedList.length < linkedList.length) { while (sortedList.length < linkedList.length) {
// get the item with a previous item ID referencing the current item // get the item with a previous item ID referencing the current item
var nextItem = linkedList[map.get(currentId)]; var nextItem = linkedList[map.get(currentId)];
if (nextItem === undefined) if (nextItem === undefined) {
break; break;
}
sortedList.push(nextItem); sortedList.push(nextItem);
currentId = String(nextItem._id); currentId = String(nextItem._id);
} }
if (sortedList.length < linkedList.length) {
console.log('ATTENZIONE !!! ', sortedList.length, linkedList.length);
}
// console.log('DOPO sortedList', sortedList); // console.log('DOPO sortedList', sortedList);
return sortedList; return sortedList;

View File

@@ -15,7 +15,7 @@ module.exports = Object.freeze({
LIST_END: '10000000', LIST_END: '10000000',
LIST_START: '0', LIST_START: null,
Privacy: { Privacy: {
all: 'all', all: 'all',