correzione numseen, numfav, ...: ora li ho aggiunti alle tabelle...

This commit is contained in:
Surya Paolo
2023-10-01 01:24:47 +02:00
parent 142dcadca9
commit b6579832b6
27 changed files with 8952 additions and 8875 deletions

View File

@@ -48,16 +48,99 @@ const reactionSchema = new Schema({
},
});
reactionSchema.statics.getFieldsForSearch = function() {
return [
{field: 'username', type: tools.FieldType.string}];
reactionSchema.statics.getFieldsForReactions = function () {
let reactionsField = {
numseen: {
type: Number,
},
numbook: {
type: Number,
},
numfav: {
type: Number,
},
numattend: {
type: Number,
},
};
return reactionsField;
};
reactionSchema.statics.executeQueryTable = function(idapp, params) {
reactionSchema.statics.getFieldsForSearch = function () {
return [
{ field: 'username', type: tools.FieldType.string }];
};
reactionSchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, 0, params);
};
reactionSchema.statics.calcReactions = async function (idapp, id, tab) {
try {
let myquerycountreaction = [
{
$match: {
idapp,
idrec: id,
tab,
},
},
{
$group: {
_id: null,
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
}
}
},
numattend: {
$sum: {
$cond: {
if: { $ifNull: ["$attend", 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
}
}
}
},
},
];
return await Reaction.aggregate(myquerycountreaction)
.then((ris) => {
return ris ? ris[0] : null;
});
} catch (err) {
console.error(err);
}
};
// Aggiungo il Favorite
reactionSchema.statics.addFavorite = async function (req, idapp, username, id, tab) {
@@ -94,11 +177,11 @@ reactionSchema.statics.addFavorite = async function (req, idapp, username, id, t
}
return {ris, ok};
return { ris, ok };
} catch (e) {
console.error('Err addFavorite', e);
return {ris: null, ok: 0};
return { ris: null, ok: 0 };
}
};
@@ -111,7 +194,7 @@ reactionSchema.statics.addSeen = async function (req, idapp, username, id, tab)
return await myrec.save()
.then((ris) => {
// console.log('salvato proj!');
return {ris, ok: ris ? 1 : 0};
return { ris, ok: ris ? 1 : 0 };
})
.catch(err => {
console.log("Error addSeen", err.message);
@@ -133,7 +216,7 @@ reactionSchema.statics.addBookmark = async function (req, idapp, username, id, t
const myrec = new Reaction({ idrec: id, idapp, userId: req.user.id, username, tab, book: true });
return await myrec.save()
.then((ris) => {
return {ris, ok: ris ? 1 : 0};
return { ris, ok: ris ? 1 : 0 };
})
.catch(err => {
console.log("Error addBookmark", err.message);
@@ -155,7 +238,7 @@ reactionSchema.statics.addAttend = async function (req, idapp, username, id, tab
const myrec = new Reaction({ idrec: id, idapp, userId: req.user.id, username, tab, attend: true });
return await myrec.save()
.then((ris) => {
return {ris, ok: ris ? 1 : 0};
return { ris, ok: ris ? 1 : 0 };
})
.catch(err => {
console.log("Error addAttend", err.message);
@@ -175,7 +258,7 @@ reactionSchema.statics.removeFavorite = async function (
const myrec = await Reaction.findOne({ idrec: id, idapp, username });
if (myrec) {
return Reaction.updateOne({ _id: myrec._id}, {
return Reaction.updateOne({ _id: myrec._id }, {
$set: {
fav: false,
}
@@ -193,7 +276,7 @@ reactionSchema.statics.removeBookmark = async function (
const myrec = await Reaction.findOne({ idrec: id, idapp, username });
if (myrec) {
return Reaction.updateOne({ _id: myrec._id}, {
return Reaction.updateOne({ _id: myrec._id }, {
$set: {
book: false,
}