Privacy about Project: what to see, what you can modify...

This commit is contained in:
Paolo Arena
2019-04-20 13:59:07 +02:00
parent 6a0b7db73c
commit e0e48f7eb2
3 changed files with 118 additions and 28 deletions

View File

@@ -9,6 +9,8 @@ var server_constants = require('../tools/server_constants');
mongoose.Promise = global.Promise; mongoose.Promise = global.Promise;
mongoose.level = "F"; mongoose.level = "F";
const { ObjectID } = require('mongodb');
// Resolving error Unknown modifier: $pushAll // Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => { mongoose.plugin(schema => {
schema.options.usePushEach = true schema.options.usePushEach = true
@@ -144,7 +146,7 @@ ProjectSchema.statics.findProjectByUserId = function (userId, idproj) {
} else { } else {
return Project.findOne({ return Project.findOne({
'userId': userId, 'userId': userId,
'_id': idproj, '_id': ObjectId(idproj),
}); });
} }
}; };
@@ -175,7 +177,7 @@ ProjectSchema.statics.findAllProjByUserId = async function (userId) {
// } // }
]).then(ris1 => { ]).then(ris1 => {
console.log('findAllProjByUserId', ris1); // console.log('findAllProjByUserId', ris1);
return ris1; return ris1;
}) })
@@ -205,7 +207,7 @@ ProjectSchema.statics.getIdParentByIdProj = function (idProj) {
console.log('INIT getIdParentByIdProj', idProj); console.log('INIT getIdParentByIdProj', idProj);
return Project.findOne({ '_id': idProj }).then(rec => { return Project.findOne({ '_id': ObjectId(idProj) }).then(rec => {
if (!!rec) { if (!!rec) {
console.log('getIdParentByIdProj', rec.id_parent); console.log('getIdParentByIdProj', rec.id_parent);
return rec.id_parent; return rec.id_parent;
@@ -220,13 +222,13 @@ 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'); // console.log('getAllProjects');
let obj = []; let obj = [];
const projbase = await Project.findById(process.env.PROJECT_ID_MAIN) const projbase = await Project.findById(process.env.PROJECT_ID_MAIN)
.then(ris => { .then(ris => {
console.log('ris', ris); // console.log('ris', ris);
if (!!ris._doc) if (!!ris._doc)
return ris._doc; return ris._doc;
else else
@@ -242,7 +244,6 @@ ProjectSchema.statics.getAllProjects = async function (userId) {
ProjectSchema.statics.enabletoModify = async function (userId, idProj) { ProjectSchema.statics.enabletoModify = async function (userId, idProj) {
var Project = this; var Project = this;
console.log('getAllProjects');
let obj = []; let obj = [];

View File

@@ -6,6 +6,8 @@ const tools = require('../tools/general');
var { Project } = require('./project'); var { Project } = require('./project');
var server_constants = require('../tools/server_constants');
mongoose.Promise = global.Promise; mongoose.Promise = global.Promise;
mongoose.level = "F"; mongoose.level = "F";
@@ -23,9 +25,7 @@ var TodoSchema = new mongoose.Schema({
pos: { pos: {
type: Number, type: Number,
}, },
category: { category: mongoose.Schema.Types.ObjectId,
type: String,
},
descr: { descr: {
type: String, 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 = '') { TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') {
var Todo = this; var Todo = this;
if (category === '') { if (category === '') {
return Todo.find({
'userId': userId, const query = getQueryTodo({}, userId);
}).then(ris => {
//return tools.mapSort(ris) return Todo.aggregate(query)
return ris .then(ris => {
}) // console.log('ris TODO:', ris);
//return tools.mapSort(ris)
return ris
});
} else { } else {
return Todo.find({ return Todo.find({
@@ -127,13 +165,47 @@ TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') {
TodoSchema.statics.getArrIdParentInTable = function (userId) { TodoSchema.statics.getArrIdParentInTable = function (userId) {
var Todo = this; var Todo = this;
return Todo.find({ 'userId': userId }).distinct("category") return Todo.find(getQueryFilterTodo(userId)).distinct("category")
.then(arrcategory => { .then(arrcategory => {
return 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) { TodoSchema.statics.getAllTodo = async function (userId) {
var Todo = this; var Todo = this;
@@ -144,18 +216,25 @@ TodoSchema.statics.getAllTodo = async function (userId) {
obj.arrcategories = await Todo.getArrIdParentInTable(userId); obj.arrcategories = await Todo.getArrIdParentInTable(userId);
const arrtodos = []; console.log('obj.arrcategories', obj.arrcategories);
let arrtodos = [];
if (obj.arrcategories.length > 0) { if (obj.arrcategories.length > 0) {
for (const mycat of obj.arrcategories) { for (const mycat of obj.arrcategories) {
const arrfiltrato = arralltodo.filter(item => item.category === mycat); if (!!arralltodo) {
// console.log('arrfiltrato ', mycat, arrfiltrato); const arrfiltrato = arralltodo.filter(item => item.category.toString() === mycat.toString());
arrtodos.push(tools.mapSort(arrfiltrato)) 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; obj.arrtodos = arrtodos;
// console.log('obj', obj); console.log('obj.arrtodos', obj.arrtodos);
return obj; return obj;

View File

@@ -200,7 +200,8 @@ module.exports = {
// SORT WITH PREV_ID // SORT WITH PREV_ID
// ********************** // **********************
mapSort: function (linkedList) { mapSort: function (linkedList) {
var sortedList = []; let sortedList = [];
let remainingList = [];
var map = new Map(); var map = new Map();
var currentId = null; 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 // 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;
} else {
sortedList.push(nextItem);
currentId = String(nextItem._id);
} }
sortedList.push(nextItem); conta++;
currentId = String(nextItem._id);
} }
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); 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); // console.log('DOPO sortedList', sortedList);
return sortedList; return sortedList;