diff --git a/src/server/models/user.js b/src/server/models/user.js index c490913..f4fe753 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -1727,8 +1727,8 @@ UserSchema.statics.getUserProfileByUsername = async function ( 'profile.username_telegram': 1, 'profile.firstname_telegram': 1, 'profile.lastname_telegram': 1, - 'profile.intcode_cell': 1, - 'profile.cell': 1, + // 'profile.intcode_cell': 1, + // 'profile.cell': 1, 'profile.website': 1, 'profile.img': 1, 'profile.sex': 1, @@ -2061,7 +2061,7 @@ UserSchema.statics.addCircuitToUser = async function (idapp, usernameOrig, circu } else { // prima di aggiungerlo controlla se esiste già ! - + let update = { $addToSet: { // Utilizziamo $addToSet invece di $push per garantire che l'elemento venga aggiunto solo se non esiste già 'profile.mycircuits': { @@ -2072,7 +2072,7 @@ UserSchema.statics.addCircuitToUser = async function (idapp, usernameOrig, circu } } }; - + ris = await User.updateOne({ idapp, username: usernameOrig, 'profile.mycircuits': { $not: { $elemMatch: { circuitname } } } }, update); if (confido) { @@ -4870,27 +4870,27 @@ function calculate30DayAverage(data) { const averages = []; for (let i = 0; i < data.length; i++) { - const startDate = new Date(data[i]._id); - let sum = data[i].count; - let count = 1; + const startDate = new Date(data[i]._id); + let sum = data[i].count; + let count = 1; - for (let j = i + 1; j < data.length; j++) { - const currentDate = new Date(data[j]._id); - const diffInTime = Math.abs(startDate.getTime() - currentDate.getTime()); - const diffInDays = Math.ceil(diffInTime / (1000 * 60 * 60 * 24)); + for (let j = i + 1; j < data.length; j++) { + const currentDate = new Date(data[j]._id); + const diffInTime = Math.abs(startDate.getTime() - currentDate.getTime()); + const diffInDays = Math.ceil(diffInTime / (1000 * 60 * 60 * 24)); - if (diffInDays <= 30) { - sum += data[j].count; - count++; - } else { - break; - } + if (diffInDays <= 30) { + sum += data[j].count; + count++; + } else { + break; } + } - averages.push({ - _id: data[i]._id, - dailyAverage: sum / count - }); + averages.push({ + _id: data[i]._id, + dailyAverage: sum / count + }); } return averages; @@ -5202,6 +5202,24 @@ UserSchema.statics.getExtraInfoByUsername = async function (idapp, username) { return null; }; +UserSchema.statics.getProfilePerActivitiesByUsername = async function (idapp, username) { + const User = this; + + try { + let myuser = await User.findOne({ idapp, username }).lean(); + if (myuser) { + return { + mygroups: myuser.profile.mygroups, + mycircuits: myuser.profile.mycircuits }; + } + } catch (e) { + console.error('e', e); + } + + return null; + +}; + UserSchema.statics.addExtraInfo = async function (idapp, recUser, recUserSave, version) { try { @@ -5566,7 +5584,7 @@ UserSchema.statics.addNewSite = async function (idappPass, body) { // cerca un IdApp Libero let idapp = await Site.generateNewSite_IdApp(idappPass, body, true); - + if (idapp) { let arrSite = await Site.find({ idapp }).lean(); let numutenti = 0; @@ -5576,7 +5594,7 @@ UserSchema.statics.addNewSite = async function (idappPass, body) { if (arrSite && arrSite.length === 1 && numutenti < 2) { const MyTelegramBot = require('../telegram/telegrambot'); - + // Nessun Sito Installato e Nessun Utente installato ! let myuser = new User(); myuser._id = new ObjectID(); diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index cf7308c..22d470c 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -443,7 +443,7 @@ router.post('/receiveris', authenticate, (req, res) => { }; }); -router.post('/profile', authenticate_noerror, (req, res) => { +router.post('/profile', authenticate, (req, res) => { const usernameOrig = req.user ? req.user.username : ''; const perm = req.user ? req.user.perm : tools.Perm.PERM_NONE; const username = req.body['username']; @@ -464,7 +464,7 @@ router.post('/profile', authenticate_noerror, (req, res) => { return User.getFriendsByUsername(idapp, usernameOrig). then(async (friends) => { if (username === usernameOrig) { - const userprofile = await User.getExtraInfoByUsername(idapp, username); + const userprofile = await User.getExtraInfoByUsername(idapp, ris.username); ris.profile = userprofile; } @@ -485,6 +485,54 @@ router.post('/profile', authenticate_noerror, (req, res) => { }); +router.post('/activities', authenticate_noerror, (req, res) => { + const usernameOrig = req.user ? req.user.username : ''; + const perm = req.user ? req.user.perm : tools.Perm.PERM_NONE; + const username = req.body['username']; + const idapp = req.body.idapp; + const locale = req.body.locale; + + //++Todo: controlla che tipo di dati ha il permesso di leggere + + try { + // Check if ìs a Notif to read + const idnotif = req.body['idnotif'] ? req.body['idnotif'] : ''; + SendNotif.setNotifAsRead(idapp, usernameOrig, idnotif); + + return User.getUserProfileByUsername(idapp, username, usernameOrig, + false, perm). + then((ris) => { + + return User.getFriendsByUsername(idapp, usernameOrig). + then(async (friends) => { + let userprofile = null; + if (req.user) { + userprofile = await User.getExtraInfoByUsername(idapp, ris.username); + } else { + userprofile = await User.getProfilePerActivitiesByUsername(idapp, ris.username); + ris.aportador_solidario = ''; + ris.date_reg = ''; + ris.email = ''; + } + ris.profile = userprofile; + + return { ris, friends }; + + }).then(tot => { + return res.send({ user: tot.ris, friends: tot.friends }); + }); + + }).catch((e) => { + tools.mylog('ERRORE IN Profile: ' + e.message); + res.status(400).send(); + }); + } catch (e) { + tools.mylogserr('Error profile: ', e); + res.status(400).send(); + } + +}); + router.post('/panel', authenticate, async (req, res) => { const username = req.body['username']; idapp = req.body.idapp; diff --git a/src/server/tools/general.js b/src/server/tools/general.js index ab1257c..3efa623 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -2324,687 +2324,703 @@ module.exports = { getQueryTable: async function (idapp, params, user) { + try { - const { Search } = require('../models/search'); + const { Search } = require('../models/search'); - if (typeof params.startRow !== 'number') { - throw new Error('startRow must be number'); - } else if (typeof params.endRow !== 'number') { - throw new Error('endRow must be number'); - } - - let newvers = !!params.newvers; - - let query = []; - - if (params.filter && params.fieldsearch) { - const querytemp = this.getFilterParam(params.filter, params.fieldsearch); - if (querytemp) { - query = [...query, ...querytemp]; - } - } - - let filtriadded = []; - - // if (params.table === 'extralist') { - // if (params.filterand.includes(shared_consts.FILTER_EXTRALIST_DELETED)) - // filtriadded.push({ deleted: true }); - // else - // filtriadded.push({ deleted: { $exists: false } }); - // } - - if (params.filterand) { - - if (params.filterand.includes( - shared_consts.FILTER_EXTRALIST_NOT_REGISTERED)) - filtriadded.push({ registered: false }); - - if (params.filterand.includes( - shared_consts.FILTER_EXTRALIST_NOT_CONTACTED)) { - filtriadded.push({ contacted: { $exists: false } }); - } - if (params.filterand.includes(shared_consts.FILTER_EXTRALIST_WITH_NOTE)) - filtriadded.push({ - 'note': { $exists: true }, - '$expr': { '$gt': [{ '$strLenCP': '$note' }, 1] }, - }); - if (params.filterand.includes(shared_consts.FILTER_QUALIFIED)) - filtriadded.push({ 'profile.qualified': true }); - if (params.filterand.includes(shared_consts.FILTER_USER_NO_ZOOM)) - filtriadded.push({ 'profile.saw_zoom_presentation': false }); - if (params.filterand.includes(shared_consts.FILTER_ASK_ZOOM_VISTO)) - filtriadded.push({ 'profile.ask_zoom_partecipato': false }); - if (params.filterand.includes(shared_consts.FILTER_USER_NO_INVITANTE)) - filtriadded.push({ - aportador_solidario: { $exists: false }, - }); - if (params.filterand.includes(shared_consts.FILTER_USER_NO_TELEGRAM_ID)) - filtriadded.push({ 'profile.teleg_id': { $lt: 1 } }); - if (params.filterand.includes(shared_consts.FILTER_USER_SI_TELEGRAM_ID)) - filtriadded.push({ 'profile.teleg_id': { $gt: 1 } }); - if (params.filterand.includes( - shared_consts.FILTER_USER_CODICE_AUTH_TELEGRAM)) - filtriadded.push({ 'profile.teleg_checkcode': { $gt: 1 } }); - if (params.filterand.includes( - shared_consts.FILTER_USER_NO_EMAIL_VERIFICATA)) - filtriadded.push({ verified_email: false }); - if (params.filterand.includes(shared_consts.FILTER_USER_NO_VERIFIED_APORTADOR)) - filtriadded.push({ - verified_by_aportador: { $exists: false }, - }); - if (params.filterand.includes(shared_consts.FILTER_USER_WITHOUT_USERNAME_TELEGRAM)) - filtriadded.push({ - $or: [ - { 'profile.username_telegram': { $exists: false } }, - { 'profile.username_telegram': { $exists: true, $eq: '' } }], - - }); - if (params.filterand.includes(shared_consts.FILTER_USER_NO_DREAM)) - filtriadded.push({ - 'profile.my_dream': { - $exists: false, - }, - }); - if (params.filterand.includes(shared_consts.FILTER_USER_TELEGRAM_BLOCKED)) - filtriadded.push({ 'profile.teleg_id_old': { $gt: 1 } }); - if (params.filterand.includes(shared_consts.FILTER_USER_PROVINCE)) - filtriadded.push({ 'profile.resid_province': { $exists: true } }); - if (params.filterand.includes(shared_consts.FILTER_ATTIVI)) - filtriadded.push({ - $or: [ - { deleted: { $exists: false } }, - { deleted: { $exists: true, $eq: false } }], - }); - - // Hours - if (params.filterand.includes(shared_consts.FILTER_HOURS_ALL)) - filtriadded.push( - { - idapp, - todoId: params.codeId, - $or: [ - { deleted: { $exists: false } }, - { deleted: { $exists: true, $eq: false } }], - }, - ); - else if (params.filterand.includes(shared_consts.FILTER_HOURS_MYLIST)) - filtriadded.push( - { - idapp, - userId: params.userId, - todoId: params.codeId, - $or: [ - { deleted: { $exists: false } }, - { deleted: { $exists: true, $eq: false } }], - }, - ); - - if (params.filterand.includes(shared_consts.FILTER_NASCOSTI)) - filtriadded.push({ - deleted: { $exists: true, $eq: true }, - }); - if (params.filterand.includes(shared_consts.FILTER_NAVI_NON_PRESENTI)) - filtriadded.push({ - navinonpresenti: { $exists: true, $eq: true }, - $or: [ - { subaccount: { $exists: false } }, - { subaccount: { $exists: true, $eq: false } }], - }); - - // Iscritti Conacreis - if (params.filterand.includes(shared_consts.FILTER_REPORTED)) - filtriadded.push( - { reported: { $exists: true, $eq: true } }, - ); - - if (params.filterand.includes( - shared_consts.FILTER_TO_MAKE_MEMBERSHIP_CARD)) - filtriadded.push({ - $or: [ - { codiceConacreis: { $exists: false } }, - { codiceConacreis: { $exists: true, $eq: '' } }], - }); - - if (params.filterand.includes(shared_consts.FILTER_MEMBERSHIP_CARD_OK)) - filtriadded.push({ - codiceConacreis: { $exists: true }, - '$expr': { '$gt': [{ '$strLenCP': '$codiceConacreis' }, 0] }, - }); - - } - - if (params.filtercustom) { - let condition = {}; - for (const myfilter of params.filtercustom) { - if (myfilter['userId']) { - myfilter['userId'] = ObjectID(myfilter['userId']); - } - if (myfilter['_idOBJ']) { - filtriadded.push({ _id: ObjectID(myfilter['_idOBJ']) }); - } else if (myfilter['dateTimeStart']) { - const gte = myfilter['dateTimeStart'].$gte; - const lte = myfilter['dateTimeStart'].$lte; - condition = { - dateTimeStart: { - $gte: new Date(gte), - $lte: new Date(lte), - }, - }; - filtriadded.push(condition); - } else if (myfilter.hasOwnProperty('pub_to_share')) { - // non aggiungere niente - } else { - filtriadded.push(myfilter); - } - } - } - - if (params.filter_gte) { - for (let ind = 0; ind < params.filter_gte.length; ind++) { - for (const [key, value] of Object.entries(params.filter_gte[ind])) { - if (value > 0) { - let condition = {}; - condition[key] = { $gte: value }; - filtriadded.push(condition); - } - } - } - } - - if (params.filtersearch) { - filtriadded.push(...params.filtersearch); - } - - if (params.options) { - if (this.isBitActive(params.options, - shared_consts.OPTIONS_SEARCH_ONLY_FULL_WORDS)) { - } - } - - - if (params.filterextra) { - if (params.filterextra.length > 0) - query = [...query, ...params.filterextra] - } - - if (filtriadded) { - if (filtriadded.length > 0) - query.push({ $match: { $and: filtriadded } }); - } - - if (idapp > 0) { - query.push({ $match: { idapp } }); - } - - // console.log('QUERYMATCH', query[0].$match.or); - // console.log('filter', params.filter); - - let numrowend = params.endRow - params.startRow; - if (numrowend < 0) - numrowend = 1; - - if (params.querytype === shared_consts.QUERYTYPE_MYGROUP || params.querytype === shared_consts.QUERYTYPE_CIRCUIT) { - - const myq = this.addQueryIdMatch(params); - if (myq) - query.push(myq); - - query.push({ $project: { req_users: 1 } }); - const qa1 = this.getLookup( - { - lk_tab: 'users', - lk_LF: 'req_users.username', - lk_FF: 'username', - lk_as: 'user', - }, 0, { - 'user.idapp': 1, - 'user.username': 1, - 'user.name': 1, - 'user.surname': 1, - 'user.profile.img': 1, - 'user.profile.qualifica': 1, - }); - if (qa1) query = [...query, ...qa1]; - query.push({ $unwind: '$user' }); - - query.push({ - $match: { - $and: [ - { 'user.idapp': params.idapp }, - ], - }, - }); - - - query.push({ - $replaceRoot: { - newRoot: '$user', - }, - }, - ); - - } else if (params.querytype === shared_consts.QUERYTYPE_REFUSED_USER_GRP || params.querytype === - shared_consts.QUERYTYPE_REFUSED_USER_CIRCUIT) { - - /* - const myq = this.addQueryIdMatch(params); - if (myq) - query.push(myq); - */ - - const myq = this.addQueryIdMatch(params); - if (myq) - query.push(myq); - - query.push({ $project: { refused_users: 1 } }); - query.push({ $unwind: '$refused_users' }); - const qa1 = this.getLookupPipeLine( - { - lk_tab: 'users', - lk_LF: 'refused_users.username', - lk_FF: 'username', - lk_as: 'user', - idapp, - }, { - 'user.idapp': 1, - 'user.username': 1, - 'user.name': 1, - 'user.surname': 1, - 'user.profile.img': 1, - 'user.profile.qualifica': 1, - }); - if (qa1) query = [...query, ...qa1]; - query.push({ $unwind: '$user' }); - /* - query.push({ - $match: { - $and: [ - {'user.idapp': params.idapp}, - ], - }, - }); - */ - - query.push({ - $replaceRoot: { - newRoot: '$user', - }, - }, - ); - } else if (params.querytype === shared_consts.QUERYTYPE_GROUP_CIRCUIT) { - - const myq = this.addQueryIdMatch(params); - if (myq) - query.push(myq); - - query.push({ $project: { req_groups: 1 } }); - const qa1 = this.getLookup( - { - lk_tab: 'mygroups', - lk_LF: 'req_groups.groupname', - lk_FF: 'groupname', - lk_as: 'group', - }, 0, { - 'group.idapp': 1, - 'group.groupname': 1, - 'group.photos': 1, - }); - if (qa1) query = [...query, ...qa1]; - query.push({ $unwind: '$group' }); - - query.push({ - $match: { - $and: [ - { 'group.idapp': params.idapp }, - ], - }, - }); - - - query.push({ - $replaceRoot: { - newRoot: '$group', - }, - }, - ); - - } else if (params.querytype === shared_consts.QUERYTYPE_REFUSED_GROUP_CIRCUIT) { - - - const myq = this.addQueryIdMatch(params); - if (myq) - query.push(myq); - - query.push({ $project: { refused_groups: 1 } }); - query.push({ $unwind: '$refused_groups' }); - const qa1 = this.getGroupnameLookupPipeLine( - { - lk_tab: 'mygroups', - lk_LF: 'refused_groups.groupname', - lk_FF: 'groupname', - lk_as: 'group', - idapp, - }, { - 'group.idapp': 1, - 'group.groupname': 1, - 'group.descr': 1, - 'group.photos': 1, - }); - if (qa1) query = [...query, ...qa1]; - query.push({ $unwind: '$group' }); - /* - query.push({ - $match: { - $and: [ - {'user.idapp': params.idapp}, - ], - }, - }); - */ - - query.push({ - $replaceRoot: { - newRoot: '$group', - }, - }, - ); - - } else if (params.querytype === shared_consts.QUERYTYPE_LIST_MOVEMENTS) { - - const { Movement } = require('../models/movement'); - - const myquery = await Movement.getQueryMovsByCircuitId(params.idapp, params.username, '', '', params.myid); - - query.push(...myquery); - } else if (params.querytype === shared_consts.QUERYTYPE_LIST_MOVEMENTS_GROUPNAME) { - - const { Movement } = require('../models/movement'); - - const myquery = await Movement.getQueryMovsByCircuitId(params.idapp, '', params.groupname, '', params.myid); - - query.push(...myquery); - - } else if (params.querytype === shared_consts.QUERYTYPE_LIST_MOVEMENTS_CONTOCOM) { - - const { Movement } = require('../models/movement'); - - const myquery = await Movement.getQueryMovsByCircuitId(params.idapp, '', '', params.contocom, params.myid); - - query.push(...myquery); - - } else if (params.querytype === shared_consts.QUERYTYPE_LIST_ALLMOVEMENTS) { - - const { Movement } = require('../models/movement'); - - const myquery = await Movement.getQueryAllUsersMovsByCircuitId(params.idapp, params.myid); - - query.push(...myquery); - - } - - if (params.filterextra2) { - if (params.filterextra2.length > 0) - query = [...query, ...params.filterextra2]; - } - - if (newvers) { - // NUOVA VERSIONE - let proj = params.lookup2 ? params.lookup2.lk_proj : null; - - if (params.proj0) { - query.push({ $project: params.proj0 }); + if (typeof params.startRow !== 'number') { + throw new Error('startRow must be number'); + } else if (typeof params.endRow !== 'number') { + throw new Error('endRow must be number'); } - if (params.unwind0) { - query.push({ $unwind: params.unwind0 }); - } + let newvers = !!params.newvers; - let objadd = { query: [], proj: {} }; + let query = []; - let numtab = this.getNumTabByTable(params.table); - - - const q1 = this.getLookup(params.lookup1, 1, proj, objadd.proj); - if (q1) query = [...query, ...q1]; - - if (params.unwind1) { - query.push({ $unwind: params.unwind1 }); - } - - if (params.lookupPipeline1) { - const q1p = this.getLookupStandardPipeline(params.lookupPipeline1, 1); - if (q1p) query = [...query, ...q1p]; - } - - const q2 = this.getLookup(params.lookup2, 2, proj, objadd.proj); - if (q2) query = [...query, ...q2]; - - if (params.lookupPipeline2) { - const q2p = this.getLookupStandardPipeline(params.lookupPipeline2, 2); - if (q2p) query = [...query, ...q2p]; - } - - const q3 = this.getLookup(params.lookup3, 3, proj, objadd.proj); - if (q3) query = [...query, ...q3]; - - const q4 = this.getLookup(params.lookup4, 4, proj, objadd.proj); - if (q4) query = [...query, ...q4]; - - const q5 = this.getLookup(params.lookup5, 5, proj, objadd.proj); - if (q5) query = [...query, ...q5]; - - if (params.filtersearch2) { - if (params.filtersearch2.length > 0) { - query.push({ $match: { $and: params.filtersearch2 } }); - } - } - - - let filteradmin = false; - if (params.filtercustom) { - let condition = {}; - for (const myfilter of params.filtercustom) { - - if (!!myfilter.admins) { - filteradmin = true; - } - - if (myfilter['pub_to_share'] === shared_consts.PUBTOSHARE.ONLY_TABLE_FOLLOW && params.table === 'mygroups') { - - let arraygroups = []; - - if (user && user.profile.mygroups) { - arraygroups = user.profile.mygroups.map(rec => rec.groupname); - } - // prendere i gruppi dell'utente - - // Cerca tra i gruppi di ogni record, se combaciano almeno 1 - condition = { - 'profile.mygroups': - { - $elemMatch: { - groupname: { $in: arraygroups }, - }, - - }, - }; - - query.push({ $match: { $and: [condition] } }); - } else if (myfilter['pub_to_share'] === shared_consts.PUBTOSHARE.ONLY_TABLE_FOLLOW && params.table === 'circuits') { - - let arraycircuits = []; - - if (user && user.profile.mycircuits) { - arraycircuits = user.profile.mycircuits.map(rec => rec.circuitname); - } - // prendere i gruppi dell'utente - - // Cerca tra i gruppi di ogni record, se combaciano almeno 1 - condition = { - 'profile.mycircuits': - { - $elemMatch: { - circuitname: { $in: arraycircuits }, - }, - - }, - }; - - query.push({ $match: { $and: [condition] } }); - } else if (shared_consts.TABLES_ENABLE_GETREC_BYID.includes(params.table)) { - // Rimuovi dalla query quelli non visibili - - let arraygroups = []; - - if (user && user.profile.mygroups) { - arraygroups = user.profile.mygroups.map(rec => rec.groupname); - } - // prendere i gruppi dell'utente - - // Cerca tra i gruppi di ogni record, se combaciano almeno 1 - condition = { - 'profile.mygroups': - { - $elemMatch: { - groupname: { $in: arraygroups }, - }, - - }, - }; - - query.push( - { - $match: - { - $or: [ - { - $and: [condition, { pub_to_share: 1 }], - }, - { $or: [{ pub_to_share: { $exists: false } }, { pub_to_share: { $exists: true, $eq: shared_consts.PUBTOSHARE.ALL } }] }, - ], - }, - }, - ); - - } - - } - } - - if (params.filtersearch3or) { - if (params.filtersearch3or.length > 0) { - query.push({ $match: { $or: params.filtersearch3or } }); - } - } - - if (params.filtersearch3and) { - if (params.filtersearch3and.length > 0) { - // Se c'è statusskill allora glielo metto in OR - - if (params.table === 'myskills' && params.filtersearch && !this.contieneIdStatusSkill(params.filtersearch)) { - - query.push({ - $match: { - $or: [ - { $and: params.filtersearch3and }, - { idStatusSkill: { $in: [2] } } - ] - } - }); - } else { - query.push({ $match: { $and: params.filtersearch3and } }); - } - - - } - } - - if (params.filter && params.fieldsearch_last) { - const querytemp = this.getFilterParam(params.filter, - params.fieldsearch_last); + if (params.filter && params.fieldsearch) { + const querytemp = this.getFilterParam(params.filter, params.fieldsearch); if (querytemp) { query = [...query, ...querytemp]; } + } - // Save the search: - if (user._id) { - const mysearch = new Search({ idapp, userId: user._id, text: params.filter }); - await mysearch.save(); + let filtriadded = []; + + // if (params.table === 'extralist') { + // if (params.filterand.includes(shared_consts.FILTER_EXTRALIST_DELETED)) + // filtriadded.push({ deleted: true }); + // else + // filtriadded.push({ deleted: { $exists: false } }); + // } + + if (params.filterand) { + + if (params.filterand.includes( + shared_consts.FILTER_EXTRALIST_NOT_REGISTERED)) + filtriadded.push({ registered: false }); + + if (params.filterand.includes( + shared_consts.FILTER_EXTRALIST_NOT_CONTACTED)) { + filtriadded.push({ contacted: { $exists: false } }); + } + if (params.filterand.includes(shared_consts.FILTER_EXTRALIST_WITH_NOTE)) + filtriadded.push({ + 'note': { $exists: true }, + '$expr': { '$gt': [{ '$strLenCP': '$note' }, 1] }, + }); + if (params.filterand.includes(shared_consts.FILTER_QUALIFIED)) + filtriadded.push({ 'profile.qualified': true }); + if (params.filterand.includes(shared_consts.FILTER_USER_NO_ZOOM)) + filtriadded.push({ 'profile.saw_zoom_presentation': false }); + if (params.filterand.includes(shared_consts.FILTER_ASK_ZOOM_VISTO)) + filtriadded.push({ 'profile.ask_zoom_partecipato': false }); + if (params.filterand.includes(shared_consts.FILTER_USER_NO_INVITANTE)) + filtriadded.push({ + aportador_solidario: { $exists: false }, + }); + if (params.filterand.includes(shared_consts.FILTER_USER_NO_TELEGRAM_ID)) + filtriadded.push({ 'profile.teleg_id': { $lt: 1 } }); + if (params.filterand.includes(shared_consts.FILTER_USER_SI_TELEGRAM_ID)) + filtriadded.push({ 'profile.teleg_id': { $gt: 1 } }); + if (params.filterand.includes( + shared_consts.FILTER_USER_CODICE_AUTH_TELEGRAM)) + filtriadded.push({ 'profile.teleg_checkcode': { $gt: 1 } }); + if (params.filterand.includes( + shared_consts.FILTER_USER_NO_EMAIL_VERIFICATA)) + filtriadded.push({ verified_email: false }); + if (params.filterand.includes(shared_consts.FILTER_USER_NO_VERIFIED_APORTADOR)) + filtriadded.push({ + verified_by_aportador: { $exists: false }, + }); + if (params.filterand.includes(shared_consts.FILTER_USER_WITHOUT_USERNAME_TELEGRAM)) + filtriadded.push({ + $or: [ + { 'profile.username_telegram': { $exists: false } }, + { 'profile.username_telegram': { $exists: true, $eq: '' } }], + + }); + if (params.filterand.includes(shared_consts.FILTER_USER_NO_DREAM)) + filtriadded.push({ + 'profile.my_dream': { + $exists: false, + }, + }); + if (params.filterand.includes(shared_consts.FILTER_USER_TELEGRAM_BLOCKED)) + filtriadded.push({ 'profile.teleg_id_old': { $gt: 1 } }); + if (params.filterand.includes(shared_consts.FILTER_USER_PROVINCE)) + filtriadded.push({ 'profile.resid_province': { $exists: true } }); + if (params.filterand.includes(shared_consts.FILTER_ATTIVI)) + filtriadded.push({ + $or: [ + { deleted: { $exists: false } }, + { deleted: { $exists: true, $eq: false } }], + }); + + // Hours + if (params.filterand.includes(shared_consts.FILTER_HOURS_ALL)) + filtriadded.push( + { + idapp, + todoId: params.codeId, + $or: [ + { deleted: { $exists: false } }, + { deleted: { $exists: true, $eq: false } }], + }, + ); + else if (params.filterand.includes(shared_consts.FILTER_HOURS_MYLIST)) + filtriadded.push( + { + idapp, + userId: params.userId, + todoId: params.codeId, + $or: [ + { deleted: { $exists: false } }, + { deleted: { $exists: true, $eq: false } }], + }, + ); + + if (params.filterand.includes(shared_consts.FILTER_NASCOSTI)) + filtriadded.push({ + deleted: { $exists: true, $eq: true }, + }); + if (params.filterand.includes(shared_consts.FILTER_NAVI_NON_PRESENTI)) + filtriadded.push({ + navinonpresenti: { $exists: true, $eq: true }, + $or: [ + { subaccount: { $exists: false } }, + { subaccount: { $exists: true, $eq: false } }], + }); + + // Iscritti Conacreis + if (params.filterand.includes(shared_consts.FILTER_REPORTED)) + filtriadded.push( + { reported: { $exists: true, $eq: true } }, + ); + + if (params.filterand.includes( + shared_consts.FILTER_TO_MAKE_MEMBERSHIP_CARD)) + filtriadded.push({ + $or: [ + { codiceConacreis: { $exists: false } }, + { codiceConacreis: { $exists: true, $eq: '' } }], + }); + + if (params.filterand.includes(shared_consts.FILTER_MEMBERSHIP_CARD_OK)) + filtriadded.push({ + codiceConacreis: { $exists: true }, + '$expr': { '$gt': [{ '$strLenCP': '$codiceConacreis' }, 0] }, + }); + + } + + if (params.filtercustom) { + let condition = {}; + for (const myfilter of params.filtercustom) { + if (myfilter['userId']) { + myfilter['userId'] = ObjectID(myfilter['userId']); + } + if (myfilter['_idOBJ']) { + filtriadded.push({ _id: ObjectID(myfilter['_idOBJ']) }); + } else if (myfilter['dateTimeStart']) { + const gte = myfilter['dateTimeStart'].$gte; + const lte = myfilter['dateTimeStart'].$lte; + condition = { + dateTimeStart: { + $gte: new Date(gte), + $lte: new Date(lte), + }, + }; + filtriadded.push(condition); + } else if (myfilter.hasOwnProperty('pub_to_share')) { + // non aggiungere niente + } else { + filtriadded.push(myfilter); + } } } - if (params.table === 'mygroups' || params.table === 'circuits') { - // BINARY CHECK (?): const filter = [{ visibility: { $bitsAnyClear: [1] } }]; - // if (!User.isAdmin(req.user.perm)) { - // not Visibility_Group.HIDDEN - if (!filteradmin) { - const filter = [ - { - $or: [ + if (params.filter_gte) { + for (let ind = 0; ind < params.filter_gte.length; ind++) { + for (const [key, value] of Object.entries(params.filter_gte[ind])) { + if (value > 0) { + let condition = {}; + condition[key] = { $gte: value }; + filtriadded.push(condition); + } + } + } + } + + if (params.filtersearch) { + filtriadded.push(...params.filtersearch); + } + + if (params.options) { + if (this.isBitActive(params.options, + shared_consts.OPTIONS_SEARCH_ONLY_FULL_WORDS)) { + } + } + + + if (params.filterextra) { + if (params.filterextra.length > 0) + query = [...query, ...params.filterextra] + } + + if (filtriadded) { + if (filtriadded.length > 0) + query.push({ $match: { $and: filtriadded } }); + } + + if (idapp > 0) { + query.push({ $match: { idapp } }); + } + + // console.log('QUERYMATCH', query[0].$match.or); + // console.log('filter', params.filter); + + let numrowend = params.endRow - params.startRow; + if (numrowend < 0) + numrowend = 1; + + if (params.querytype === shared_consts.QUERYTYPE_MYGROUP || params.querytype === shared_consts.QUERYTYPE_CIRCUIT) { + + const myq = this.addQueryIdMatch(params); + if (myq) + query.push(myq); + + query.push({ $project: { req_users: 1 } }); + const qa1 = this.getLookup( + { + lk_tab: 'users', + lk_LF: 'req_users.username', + lk_FF: 'username', + lk_as: 'user', + }, 0, { + 'user.idapp': 1, + 'user.username': 1, + 'user.name': 1, + 'user.surname': 1, + 'user.profile.img': 1, + 'user.profile.qualifica': 1, + }); + if (qa1) query = [...query, ...qa1]; + query.push({ $unwind: '$user' }); + + query.push({ + $match: { + $and: [ + { 'user.idapp': params.idapp }, + ], + }, + }); + + + query.push({ + $replaceRoot: { + newRoot: '$user', + }, + }, + ); + + } else if (params.querytype === shared_consts.QUERYTYPE_REFUSED_USER_GRP || params.querytype === + shared_consts.QUERYTYPE_REFUSED_USER_CIRCUIT) { + + /* + const myq = this.addQueryIdMatch(params); + if (myq) + query.push(myq); + */ + + const myq = this.addQueryIdMatch(params); + if (myq) + query.push(myq); + + query.push({ $project: { refused_users: 1 } }); + query.push({ $unwind: '$refused_users' }); + const qa1 = this.getLookupPipeLine( + { + lk_tab: 'users', + lk_LF: 'refused_users.username', + lk_FF: 'username', + lk_as: 'user', + idapp, + }, { + 'user.idapp': 1, + 'user.username': 1, + 'user.name': 1, + 'user.surname': 1, + 'user.profile.img': 1, + 'user.profile.qualifica': 1, + }); + if (qa1) query = [...query, ...qa1]; + query.push({ $unwind: '$user' }); + /* + query.push({ + $match: { + $and: [ + {'user.idapp': params.idapp}, + ], + }, + }); + */ + + query.push({ + $replaceRoot: { + newRoot: '$user', + }, + }, + ); + } else if (params.querytype === shared_consts.QUERYTYPE_GROUP_CIRCUIT) { + + const myq = this.addQueryIdMatch(params); + if (myq) + query.push(myq); + + query.push({ $project: { req_groups: 1 } }); + const qa1 = this.getLookup( + { + lk_tab: 'mygroups', + lk_LF: 'req_groups.groupname', + lk_FF: 'groupname', + lk_as: 'group', + }, 0, { + 'group.idapp': 1, + 'group.groupname': 1, + 'group.photos': 1, + }); + if (qa1) query = [...query, ...qa1]; + query.push({ $unwind: '$group' }); + + query.push({ + $match: { + $and: [ + { 'group.idapp': params.idapp }, + ], + }, + }); + + + query.push({ + $replaceRoot: { + newRoot: '$group', + }, + }, + ); + + } else if (params.querytype === shared_consts.QUERYTYPE_REFUSED_GROUP_CIRCUIT) { + + + const myq = this.addQueryIdMatch(params); + if (myq) + query.push(myq); + + query.push({ $project: { refused_groups: 1 } }); + query.push({ $unwind: '$refused_groups' }); + const qa1 = this.getGroupnameLookupPipeLine( + { + lk_tab: 'mygroups', + lk_LF: 'refused_groups.groupname', + lk_FF: 'groupname', + lk_as: 'group', + idapp, + }, { + 'group.idapp': 1, + 'group.groupname': 1, + 'group.descr': 1, + 'group.photos': 1, + }); + if (qa1) query = [...query, ...qa1]; + query.push({ $unwind: '$group' }); + /* + query.push({ + $match: { + $and: [ + {'user.idapp': params.idapp}, + ], + }, + }); + */ + + query.push({ + $replaceRoot: { + newRoot: '$group', + }, + }, + ); + + } else if (params.querytype === shared_consts.QUERYTYPE_LIST_MOVEMENTS) { + + const { Movement } = require('../models/movement'); + + const myquery = await Movement.getQueryMovsByCircuitId(params.idapp, params.username, '', '', params.myid); + + query.push(...myquery); + } else if (params.querytype === shared_consts.QUERYTYPE_LIST_MOVEMENTS_GROUPNAME) { + + const { Movement } = require('../models/movement'); + + const myquery = await Movement.getQueryMovsByCircuitId(params.idapp, '', params.groupname, '', params.myid); + + query.push(...myquery); + + } else if (params.querytype === shared_consts.QUERYTYPE_LIST_MOVEMENTS_CONTOCOM) { + + const { Movement } = require('../models/movement'); + + const myquery = await Movement.getQueryMovsByCircuitId(params.idapp, '', '', params.contocom, params.myid); + + query.push(...myquery); + + } else if (params.querytype === shared_consts.QUERYTYPE_LIST_ALLMOVEMENTS) { + + const { Movement } = require('../models/movement'); + + const myquery = await Movement.getQueryAllUsersMovsByCircuitId(params.idapp, params.myid); + + query.push(...myquery); + + } + + if (params.filterextra2) { + if (params.filterextra2.length > 0) + query = [...query, ...params.filterextra2]; + } + + if (newvers) { + // NUOVA VERSIONE + let proj = params.lookup2 ? params.lookup2.lk_proj : null; + + if (params.proj0) { + query.push({ $project: params.proj0 }); + } + + if (params.unwind0) { + query.push({ $unwind: params.unwind0 }); + } + + let objadd = { query: [], proj: {} }; + + let numtab = this.getNumTabByTable(params.table); + + + const q1 = this.getLookup(params.lookup1, 1, proj, objadd.proj); + if (q1) query = [...query, ...q1]; + + if (params.unwind1) { + query.push({ $unwind: params.unwind1 }); + } + + if (params.lookupPipeline1) { + const q1p = this.getLookupStandardPipeline(params.lookupPipeline1, 1); + if (q1p) query = [...query, ...q1p]; + } + + const q2 = this.getLookup(params.lookup2, 2, proj, objadd.proj); + if (q2) query = [...query, ...q2]; + + if (params.lookupPipeline2) { + const q2p = this.getLookupStandardPipeline(params.lookupPipeline2, 2); + if (q2p) query = [...query, ...q2p]; + } + + const q3 = this.getLookup(params.lookup3, 3, proj, objadd.proj); + if (q3) query = [...query, ...q3]; + + const q4 = this.getLookup(params.lookup4, 4, proj, objadd.proj); + if (q4) query = [...query, ...q4]; + + const q5 = this.getLookup(params.lookup5, 5, proj, objadd.proj); + if (q5) query = [...query, ...q5]; + + if (params.filtersearch2) { + if (params.filtersearch2.length > 0) { + query.push({ $match: { $and: params.filtersearch2 } }); + } + } + + + let filteradmin = false; + if (params.filtercustom) { + let condition = {}; + for (const myfilter of params.filtercustom) { + + if (!!myfilter.admins) { + filteradmin = true; + } + + if (myfilter['pub_to_share'] === shared_consts.PUBTOSHARE.ONLY_TABLE_FOLLOW && params.table === 'mygroups') { + + let arraygroups = []; + + if (user && user.profile.mygroups) { + arraygroups = user.profile.mygroups.map(rec => rec.groupname); + } + // prendere i gruppi dell'utente + + // Cerca tra i gruppi di ogni record, se combaciano almeno 1 + condition = { + 'profile.mygroups': + { + $elemMatch: { + groupname: { $in: arraygroups }, + }, + + }, + }; + + query.push({ $match: { $and: [condition] } }); + } else if (myfilter['pub_to_share'] === shared_consts.PUBTOSHARE.ONLY_TABLE_FOLLOW && params.table === 'circuits') { + + let arraycircuits = []; + + if (user && user.profile.mycircuits) { + arraycircuits = user.profile.mycircuits.map(rec => rec.circuitname); + } + // prendere i gruppi dell'utente + + // Cerca tra i gruppi di ogni record, se combaciano almeno 1 + condition = { + 'profile.mycircuits': + { + $elemMatch: { + circuitname: { $in: arraycircuits }, + }, + + }, + }; + + query.push({ $match: { $and: [condition] } }); + } else if (shared_consts.TABLES_ENABLE_GETREC_BYID.includes(params.table)) { + // Rimuovi dalla query quelli non visibili + + let arraygroups = []; + + if (user && user.profile.mygroups) { + arraygroups = user.profile.mygroups.map(rec => rec.groupname); + } + // prendere i gruppi dell'utente + + // Cerca tra i gruppi di ogni record, se combaciano almeno 1 + condition = { + 'profile.mygroups': + { + $elemMatch: { + groupname: { $in: arraygroups }, + }, + + }, + }; + + query.push( + { + $match: + { + $or: [ + { + $and: [condition, { pub_to_share: 1 }], + }, + { $or: [{ pub_to_share: { $exists: false } }, { pub_to_share: { $exists: true, $eq: shared_consts.PUBTOSHARE.ALL } }] }, + ], + }, + }, + ); + + } + + } + } + + if (params.filtersearch3or) { + if (params.filtersearch3or.length > 0) { + query.push({ $match: { $or: params.filtersearch3or } }); + } + } + + if (params.filtersearch3and) { + if (params.filtersearch3and.length > 0) { + // Se c'è statusskill allora glielo metto in OR + + if (params.table === 'myskills' && params.filtersearch && !this.contieneIdStatusSkill(params.filtersearch)) { + + query.push({ + $match: { + $or: [ + { $and: params.filtersearch3and }, + { idStatusSkill: { $in: [2] } } + ] + } + }); + } else { + query.push({ $match: { $and: params.filtersearch3and } }); + } + + + } + } + + if (params.filter && params.fieldsearch_last) { + const querytemp = this.getFilterParam(params.filter, + params.fieldsearch_last); + if (querytemp) { + query = [...query, ...querytemp]; + } + + // Save the search: + if (user._id) { + const mysearch = new Search({ idapp, userId: user._id, text: params.filter }); + await mysearch.save(); + } + } + + if (params.table === 'mygroups' || params.table === 'circuits') { + // BINARY CHECK (?): const filter = [{ visibility: { $bitsAnyClear: [1] } }]; + // if (!User.isAdmin(req.user.perm)) { + // not Visibility_Group.HIDDEN + if (!filteradmin) { + if (user) { + const filter = [ + { + $or: [ + { + visibility: { + $nin: [shared_consts.Visibility_Group.HIDDEN], + }, + }, + { + createdBy: { + $eq: user.username, + }, + }, + ], + }, + ]; + query.push({ $match: { $and: filter } }); + } else { + const filter = [ { visibility: { $nin: [shared_consts.Visibility_Group.HIDDEN], }, }, - { - createdBy: { - $eq: user.username, - }, - }, - ], - }, - ]; - query.push({ $match: { $and: filter } }); + ]; + query.push({ $match: { $and: filter } }); + + } + } + // } } - // } + + if (params.options && this.isBitActive(params.options, + shared_consts.OPTIONS_ADD_COUNT_FAVORITE)) { + // objadd = this.addNumFavoriteAndBookmarkToQuery(idapp, numtab); + // if (Object.keys(objadd.query).length > 0) + // query = [...query, ...objadd.query]; + } + + } else { + // VECCHIA VERSIONE + const q1 = this.getLookup(params, 1); + if (q1) query = [...query, ...q1]; } - if (params.options && this.isBitActive(params.options, - shared_consts.OPTIONS_ADD_COUNT_FAVORITE)) { - // objadd = this.addNumFavoriteAndBookmarkToQuery(idapp, numtab); - // if (Object.keys(objadd.query).length > 0) - // query = [...query, ...objadd.query]; + if (params.sortBy) { + // maybe we want to sort by blog title or something + const mysort = { $sort: params.sortBy }; + // console.log('sortBy', params.sortBy); + // console.table(mysort); + query.push(mysort); } - } else { - // VECCHIA VERSIONE - const q1 = this.getLookup(params, 1); - if (q1) query = [...query, ...q1]; - } - - if (params.sortBy) { - // maybe we want to sort by blog title or something - const mysort = { $sort: params.sortBy }; - // console.log('sortBy', params.sortBy); - // console.table(mysort); - query.push(mysort); - } - - query.push( - { - $group: { - _id: null, - // get a count of every result that matches until now - count: { $sum: 1 }, - // keep our results for the next operation - results: { $push: '$$ROOT' }, + query.push( + { + $group: { + _id: null, + // get a count of every result that matches until now + count: { $sum: 1 }, + // keep our results for the next operation + results: { $push: '$$ROOT' }, + }, }, - }, - // and finally trim the results to within the range given by start/endRow - { - $project: { - count: 1, - rows: { $slice: ['$results', params.startRow, numrowend] }, + // and finally trim the results to within the range given by start/endRow + { + $project: { + count: 1, + rows: { $slice: ['$results', params.startRow, numrowend] }, + }, }, - }, - ); + ); - if (this.testing()) { - console.log('query', query); + if (this.testing()) { + console.log('query', query); + } + // console.log('query', query); + + return query; + } catch (e) { + console.error('Err query:', e); } - // console.log('query', query); - - return query; }, @@ -3769,7 +3785,7 @@ module.exports = { if (table === 'sharewithus') { return true; } - + if (table === 'hours') { return true; }