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 });

View File

@@ -16,6 +16,7 @@ const { Booking } = require('../models/booking');
const { Operator } = require('../models/operator');
const { Where } = require('../models/where');
const { MyEvent } = require('../models/myevent');
const { Reaction } = require('../models/reaction');
const { Contribtype } = require('../models/contribtype');
const { PaymentType } = require('../models/paymenttype');
const { Discipline } = require('../models/discipline');
@@ -79,6 +80,10 @@ const shared_consts = require('./shared_nodejs');
module.exports = {
isTableReaction(tablename) {
return shared_consts.TABLES_REACTIONS.includes(tablename);
},
getTableByTableName(tablename) {
let mytable = '';
@@ -210,6 +215,8 @@ module.exports = {
mytable = Account;
else if (tablename === 'movements')
mytable = Movement;
else if (tablename === 'reactions')
mytable = Reaction;
else if (shared_consts.TablePickup.includes(tablename))
mytable = Pickup;
//else if (shared_consts.TableCities.includes(tablename))
@@ -562,30 +569,76 @@ module.exports = {
getNumFavoriteByIdObj: async function (idapp, numtab, id) {
const { User } = require('../models/user');
const { Reaction } = require('../models/reaction');
let query = [
{
$match: {
idapp,
"profile.favorite": {
$elemMatch:
{ id, tab: numtab }
}
idrec: id,
},
},
{
$group:
{
_id: null,
count: { $sum: 1 },
count: {
$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
}
}
},
}
},
{ $project: { _id: 0 } }
];
try {
const [result] = await User.aggregate(query);
const [result] = await Reaction.aggregate(query);
return result ? result.count : 0;
} catch (err) {
return 0;
}
return 0;
},
getNumBookByIdObj: async function (idapp, numtab, id) {
const { Reaction } = require('../models/reaction');
let query = [
{
$match: {
idapp,
idrec: id,
},
},
{
$group:
{
_id: null,
count: {
$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: { _id: 0 } }
];
try {
const [result] = await Reaction.aggregate(query);
return result ? result.count : 0;
} catch (err) {
@@ -598,16 +651,13 @@ module.exports = {
getNumAttendByIdObj: async function (idapp, numtab, id) {
const { User } = require('../models/user');
const { Reaction } = require('../models/reaction');
let query = [
{
$match: {
idapp,
"profile.attend": {
$elemMatch:
{ id, tab: numtab }
}
idrec: id,
},
},
{
@@ -615,7 +665,15 @@ module.exports = {
{
_id: null,
count: {
$sum:
$sum: {
$cond: {
if: { $ifNull: ["$attent", 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
}
},
/*$sum:
{
$reduce: {
input: "$profile.attend",
@@ -624,7 +682,7 @@ module.exports = {
$add: ["$$value", "$$this.num"]
}
}
}
}*/
},
}
},
@@ -632,7 +690,7 @@ module.exports = {
];
try {
const [result] = await User.aggregate(query);
const [result] = await Reaction.aggregate(query);
return result ? result.count : 0;
} catch (err) {
@@ -654,13 +712,14 @@ module.exports = {
let numattend = await this.getNumAttendByIdObj(idapp, numtab, id);
let numfav = await this.getNumFavoriteByIdObj(idapp, numtab, id);
let numbook = await this.getNumBookByIdObj(idapp, numtab, id);
let exist = false;
if (table === shared_consts.TABLES_MYBACHECAS)
exist = numattend > 1;
else
exist = numfav > 1;
if (recuser)
return { userId: rec.userId, username: recuser.username, descr: rec.descr, id: rec._id, table, exist, numfav, numattend };
return { userId: rec.userId, username: recuser.username, descr: rec.descr, id: rec._id, table, exist, numfav, numattend, numbook };
}
}
} catch (e) {

View File

@@ -149,7 +149,7 @@ module.exports = {
{ id: 4, table: 'mygoods' },
{ id: 5, table: 'myevents' }],
CMD_USER: {
CMD_REACTION: {
SET_FAVORITE: 1,
SET_BOOKMARK: 2,
SET_SEEN: 3,
@@ -157,6 +157,7 @@ module.exports = {
},
TABLES_ENABLE_GETREC_BYID: ['mybachecas', 'myhosps', 'myskills', 'mygoods'],
TABLES_REACTIONS: ['mybachecas', 'myhosps', 'myskills', 'mygoods'],
TABLES_USER_INCLUDE_MY: ['mygroups', 'circuits'],
TABLES_GETCOMPLETEREC: ['myskills', 'mybachecas', 'myhosps', 'mygoods'],