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

@@ -14,6 +14,7 @@ const { Graduatoria } = require('../models/graduatoria');
// const { ExtraList } = require('../models/extralist');
const { Reaction } = require('../models/reaction');
const { MyGroup } = require('../models/mygroup');
const { Circuit } = require('../models/circuit');
@@ -485,6 +486,8 @@ const UserSchema = new mongoose.Schema({
},
});
UserSchema.methods.toJSON = function () {
const user = this;
const userObject = user.toObject();
@@ -1620,10 +1623,6 @@ UserSchema.statics.getUserProfileByUsername = async function (
'profile.calc': 1,
'profile.handshake': 1,
'profile.friends': 1,
'profile.favorite': 1,
'profile.bookmark': 1,
'profile.attend': 1,
'profile.seen': 1,
email: 1,
date_reg: 1,
'useraport.username': 1,
@@ -1668,10 +1667,6 @@ UserSchema.statics.getUserProfileByUsername = async function (
'profile.calc': 1,
'profile.handshake': 1,
'profile.friends': 1,
'profile.favorite': 1,
'profile.bookmark': 1,
'profile.attend': 1,
'profile.seen': 1,
email: 1,
date_reg: 1,
'useraport.username': 1,
@@ -1717,10 +1712,6 @@ UserSchema.statics.getUserProfileByUsername = async function (
'profile.calc': 1,
'profile.handshake': 1,
'profile.friends': 1,
'profile.favorite': 1,
'profile.bookmark': 1,
'profile.attend': 1,
'profile.seen': 1,
'mycities': 1,
'comune': 1,
email: 1,
@@ -1978,34 +1969,7 @@ UserSchema.statics.removeReqFriend = async function (
{ $pull: { 'profile.req_friends': { username: { $in: [usernameDest] } } } });
};
// Rimuovo il Favorite
UserSchema.statics.removeFavorite = async function (
idapp, username, id, tab) {
return await User.updateOne({ idapp, username },
{ $pull: { 'profile.favorite': { id: { $in: [id] }, tab } } });
};
// Aggiungo il Favorite
UserSchema.statics.addFavorite = async function (
req, idapp, username, id, tab) {
const ris = await User.updateOne({ idapp, username },
{ $push: { 'profile.favorite': { id, tab } } });
const { SendNotif } = require('../models/sendnotif');
const globalTables = require('../tools/globalTables');
// Invia una Notifica al Destinatario
const recObjCreator = await globalTables.getUserCreatorByNumTabAndId(idapp, id, tab);
if (recObjCreator) {
await SendNotif.createNewNotifToSingleUser(req, null, { usernameDest: recObjCreator.username, recObjCreator, username_action: req.user.username }, false, shared_consts.TypeNotifs.TYPEDIR_FAVORITE,
shared_consts.TypeNotifs.ID_FAVORITE_ADDED);
}
return ris;
};
// Aggiungo il Partecipa
UserSchema.statics.addAttend = async function (
@@ -2934,10 +2898,6 @@ function getWhatToShow(idapp, username) {
date_reg: 1,
'profile.friends': 1,
'profile.handshake': 1,
'profile.favorite': 1,
'profile.bookmark': 1,
'profile.attend': 1,
'profile.seen': 1,
};
}
@@ -4571,11 +4531,18 @@ UserSchema.statics.calcRegWeekly = async function (idapp) {
if (tools.INITDB_FIRSTIME) {
console.log(' createIndex User Index...');
UserSchema.index({ userId: 1 });
UserSchema.index({ 'idapp': 1 });
// UserSchema.index({ username: 'text', name: 'text', surname: 'text', email: 'text' });
// UserSchema.index({ name: 'name' });
// UserSchema.index({ name: 1 });
// UserSchema.index({ surname: 1 });
// ter
tools.INITDB_FIRSTIME = false;
}
UserSchema.statics.getUsernameByIndex = async function (idapp, index) {
@@ -4860,7 +4827,7 @@ UserSchema.statics.addExtraInfo = async function (idapp, recUser, recUserSave, v
versattualeuser = recUser.profile.version;
}
versattualeuser = 0;
// versattualeuser = 0; // TOGLIERE!
if (versattualeuser < version) {
// Aggiornamento versione
@@ -4922,6 +4889,8 @@ UserSchema.statics.addExtraInfo = async function (idapp, recUser, recUserSave, v
{ deleted: { $exists: true, $eq: false } }],
}).lean();
recUser.profile.reaction = await Reaction.find({idapp, username: recUser.username}).lean();
recUser.profile.manage_mygroups = listManageGroups
? listManageGroups
: [];
@@ -4955,6 +4924,109 @@ UserSchema.statics.addExtraInfo = async function (idapp, recUser, recUserSave, v
return recUser;
};
addRecordReaction = async function (arr, fields, fieldtoset) {
let newrecord = {};
try {
if (tools.isArray(arr)) {
for (let myrec of arr) {
if (myrec.id) {
let myfields = { ...{ idrec: myrec.id }, ...fields };
newrecord = await Reaction.findOne(myfields)
if (!newrecord) {
newrecord = new Reaction(myfields);
// console.log('Nuovo Record: ', newrecord._doc);
}
newrecord[fieldtoset] = true;
await newrecord.save();
let newrecfound = await Reaction.findOne(myfields)
}
// if (newrecfound)
// console.log('trovato')
}
}
} catch (e) {
console.error(e.message);
}
};
UserSchema.statics.moverecordsFavorite = async function (tab) {
const { MySkill } = require('../models/myskill');
const { MyGood } = require('../models/mygood');
console.log('INIZIO - moverecordsFavorite tab', tab);
const arrusers = await User.find({ idapp: '13' }).lean();
let index = 0;
try {
let index = 0;
let totali = 0;
for (let user of arrusers) {
if (user.profile) {
let arrfav = user.profile.favorite;
let arrseen = user.profile.seen;
let arrbookmark = user.profile.bookmark;
let arrattend = user.profile.attend;
totali++;
if (arrfav || arrseen) {
console.log('utente n.', index, 'su', totali, user.username);
index++;
let fields = {
idapp: user.idapp,
userId: user._id,
username: user.username,
tab
};
await addRecordReaction(arrfav, fields, 'fav');
await addRecordReaction(arrseen, fields, 'seen');
await addRecordReaction(arrbookmark, fields, 'book:');
await addRecordReaction(arrattend, fields, 'attend');
}
}
}
// Rimuovi i record del vecchio schema
const attivacanc = false;
if (attivacanc) {
const queryfind = { idapp: '13' };
await User.findOneAndUpdate(queryfind, {
$set:
{
'profile.favorite': [],
'profile.bookmark': [],
'profile.attend': [],
'profile.seen': []
},
});
}
} catch (e) {
console.error(e.message);
}
console.log('fine moverecordsFavorite');
};
UserSchema.statics.updateVersion = async function (userversion, recUser) {
const { MySkill } = require('../models/myskill');
@@ -5015,6 +5087,10 @@ UserSchema.statics.updateVersion = async function (userversion, recUser) {
await User.findOneAndUpdate({ _id: recUser._id }, { $set: { 'profile.notifs': recUser.profile.notifs } });
}
if (userversion < 1016) {
}
return recUser;
};