Fixed: le reactions devono stare in una tabella a parte (reactions).

- cambiata la gestione dei seen, fav, book, attend
This commit is contained in:
Surya Paolo
2023-09-27 18:38:57 +02:00
parent ad6b4c2bfa
commit 142dcadca9
15 changed files with 826 additions and 264 deletions

View File

@@ -1818,6 +1818,49 @@ module.exports = {
return myquery;
},
getLookupStandardPipeline: function (params, num) {
const query = [];
if (!params)
return;
query.push(
{
$lookup: {
from: params.lk_tab,
let: { searchId: { $toObjectId: "$" + params.lk_LF } },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$" + params.lk_FF, "$$searchId"] },
],
},
},
},
{
$project: params.lk_proj
},
],
as: params.lk_as + num,
},
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
{
$arrayElemAt: [
'$' + params.lk_as + num, 0],
}, '$$ROOT'],
},
},
}
)
return query;
},
getLookup: function (params, num, pass_proj, proj_add) {
const query = [];
@@ -1963,144 +2006,179 @@ module.exports = {
addNumFavoriteAndBookmarkToQuery(idapp, numtab) {
let query = {};
try {
query =
[{
$lookup: {
from: 'users',
let: {
tab: numtab,
id: '$_id',
},
pipeline: [
{
$unwind: '$profile.favorite',
[
{
$lookup: {
from: "reactions",
let: {
tab: numtab,
id: '$_id',
},
{
$match: {
$expr: {
$and: [
{ $eq: ['$profile.favorite.id', '$$id'] },
{ $eq: ['$profile.favorite.tab', '$$tab'] },
{ $eq: ['$idapp', idapp] },
],
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$idrec', '$$id'] },
{ $eq: ['$tab', numtab] },
{ $eq: ['$idapp', idapp] },
],
},
},
},
},
{ $project: { username: 1, name: 1, surname: 1, 'profile.resid_province': 1, 'profile.img': 1, 'profile.qualifica': 1 } },
],
as: 'myfav',
},
},
{
$lookup: {
from: "users",
let: {
tab: numtab,
id: '$_id',
},
pipeline: [
{
$unwind: '$profile.bookmark',
},
{
$match: {
$expr: {
$and: [
{ $eq: ['$profile.bookmark.id', '$$id'] },
{ $eq: ['$profile.bookmark.tab', '$$tab'] },
{ $eq: ['$idapp', idapp] },
],
{
$group: {
_id: "$idrec",
numseen: {
$sum: {
$cond: {
if: { $ifNull: ["$seen", false] }, // Check if the field exists and is not null
then: 1, // Increment count by 1 if the field exists
else: 0, // Otherwise, keep the count unchanged
}
}
},
numfav: {
$sum: {
$cond: {
if: { $ifNull: ["$fav", false] }, // Check if the field exists and is not null
then: 1, // Increment count by 1 if the field exists
else: 0, // Otherwise, keep the count unchanged
}
}
},
numbook: {
$sum: {
$cond: {
if: { $ifNull: ["$book", false] }, // Check if the field exists and is not null
then: 1, // Increment count by 1 if the field exists
else: 0, // Otherwise, keep the count unchanged
}
}
}
},
},
},
{
$project: {
username: 1, name: 1, surname: 1, 'profile.resid_province': 1, 'profile.img': 1,
'profile.qualifica': 1,
}
},
],
as: 'mybook',
},
},
{
$lookup: {
from: "users",
let: {
tab: numtab,
id: '$_id',
],
as: 'myreact',
},
pipeline: [
{
$unwind: '$profile.seen',
},
{
$match: {
$expr: {
$and: [
{ $eq: ['$profile.seen.id', '$$id'] },
{ $eq: ['$profile.seen.tab', '$$tab'] },
{ $eq: ['$idapp', idapp] },
],
},
},
},
{
$project: {
username: 1, name: 1, surname: 1, 'profile.resid_province': 1, 'profile.img': 1,
'profile.qualifica': 1,
}
},
],
as: 'myseen',
},
},
{
$lookup: {
from: "users",
let: {
tab: numtab,
id: '$_id',
},
pipeline: [
{
$unwind: '$profile.attend',
},
{
$match: {
$expr: {
$and: [
{ $eq: ['$profile.attend.id', '$$id'] },
{ $eq: ['$profile.attend.tab', '$$tab'] },
{ $eq: ['$idapp', idapp] },
],
},
},
},
{
$project: {
username: 1, name: 1, surname: 1, 'profile.resid_province': 1, 'profile.img': 1,
'profile.qualifica': 1,
}
},
],
as: 'myattend',
},
}];
const numtabbacheca = this.getNumTabByTable(shared_consts.TABLES_MYBACHECAS);
if (numtab === numtabbacheca) {
const queryadd = this.getQueryMyBacheca(idapp);
query = [...query, ...queryadd];
{
$unwind: '$myreact',
},
/*{
$lookup: {
from: "reactions",
let: {
tab: 2,
id: '$_id',
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$idrec', '$$id'] },
{ $eq: ['$tab', 2] },
{ $eq: ['$idapp', '13'] },
],
},
},
},
{
$project: {
myid: '$_id',
_id: 0,
idapp: 1,
userId: 1,
username: 1,
fav: 1,
seen: 1,
},
},
],
as: 'items',
},
},
{
$unwind: "$items"
},
{
$lookup: {
from: "users",
let: {
itemId: { $toObjectId: "$items.userId" },
items: "$items"
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$_id",
"$$itemId"
]
}
}
},
{
$project: {
username: 1,
name: 1,
surname: 1,
'profile.img': 1,
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
"$$items",
"$$ROOT"
]
}
}
}
],
as: "myreact"
}
},
{
$group: {
_id: "$_id",
userId: {
$first: "$userId"
},
descr: {
$first: "$descr"
},
myreact: {
$push: {
$first: "$myreact"
},
},
firstDoc: {
$first: "$$ROOT"
},
}
} */
];
if (Object.keys(query).length > 0) {
const numtabbacheca = this.getNumTabByTable(shared_consts.TABLES_MYBACHECAS);
if (numtab === numtabbacheca) {
const queryadd = this.getQueryMyBacheca(idapp);
query = [...query, ...queryadd];
}
}
proj = {
myfav: 1,
mybook: 1,
myseen: 1,
myattend: 1,
mybookings: 1,
};
} catch (e) {
console.error(e);
@@ -2639,12 +2717,6 @@ module.exports = {
let numtab = this.getNumTabByTable(params.table);
if (params.options && this.isBitActive(params.options,
shared_consts.OPTIONS_ADD_COUNT_FAVORITE)) {
objadd = this.addNumFavoriteAndBookmarkToQuery(idapp, numtab);
query = [...query, ...objadd.query];
}
const q1 = this.getLookup(params.lookup1, 1, proj, objadd.proj);
if (q1) query = [...query, ...q1];
@@ -2653,9 +2725,19 @@ module.exports = {
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];
@@ -2816,6 +2898,13 @@ module.exports = {
// }
}
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);
@@ -2849,17 +2938,35 @@ module.exports = {
},
);
// console.log('query', query);
if (this.testing()) {
console.log('query', query);
}
return query;
},
startTimeLog(name) {
if (this.testing()) {
console.log('inizio', name);
console.time(name);
}
},
endTimeLog(name) {
if (this.testing()) {
console.log(' ... fine', name);
console.timeEnd(name);
}
},
async executeQueryTable(mythistable, idapp, params, user) {
let query = await this.getQueryTable(idapp, params, user);
try {
// console.log('query', query);
this.startTimeLog('Query 1');
const [ris] = await mythistable.aggregate(query);
if (ris) {
@@ -2874,6 +2981,8 @@ module.exports = {
}
}
this.endTimeLog('Query 1');
// console.table(ris.rows);
// console.log('ROW ', ris.count);
return ({ count: ris.count, rows: ris.rows });