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

@@ -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) {