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.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 = [];

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;