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

@@ -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;