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

@@ -94,6 +94,9 @@ const MySkillSchema = new Schema({
},
});
MySkillSchema.index({ 'idapp': 1 });
MySkillSchema.pre('save', async function(next) {
if (this.isNew) {
if (!this.date_created)

View File

@@ -20,12 +20,12 @@ const PermissionSchema = new Schema({
type: String,
default: ''
}
},{ _id : false });
}, { _id: false });
PermissionSchema.pre('save', async function (next) {
if (this.isNew) {
const myrec = await Permission.findOne().limit(1).sort({_id:-1});
const myrec = await Permission.findOne().limit(1).sort({ _id: -1 });
if (!!myrec) {
if (myrec._doc._id === 0)
this._id = 1;
@@ -48,7 +48,7 @@ PermissionSchema.statics.executeQueryTable = function (idapp, params) {
PermissionSchema.statics.findAllIdApp = async function () {
const Permission = this;
const myfind = { };
const myfind = {};
return await Permission.find(myfind, (err, arrrec) => {
return arrrec

210
src/server/models/reaction.js Executable file
View File

@@ -0,0 +1,210 @@
const mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;
const i18n = require('i18n');
const tools = require('../tools/general');
const shared_consts = require('../tools/shared_nodejs');
mongoose.Promise = global.Promise;
mongoose.level = "F";
const { ObjectID } = require('mongodb');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const reactionSchema = new Schema({
idapp: {
type: String,
},
userId: {
type: String,
},
username: {
type: String,
},
idrec: {
type: String,
},
tab: {
type: Number,
},
fav: {
type: Boolean,
},
book: {
type: Boolean,
},
seen: {
type: Boolean,
},
attend: {
type: Boolean,
},
});
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);
};
// Aggiungo il Favorite
reactionSchema.statics.addFavorite = async function (req, idapp, username, id, tab) {
let ris = null;
try {
let ok = false;
const myrec = await Reaction.findOne({ idrec: id, idapp, username });
if (!myrec) {
const myrec = new Reaction({ idrec: id, idapp, userId: req.user.id, username, tab, fav: true });
ris = await myrec.save();
ok = ris ? 1 : 0;
} else {
ris = await Reaction.updateOne({ idrec: id, idapp, username }, {
$set: {
fav: true,
}
})
ok = ris.ok;
}
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, ok};
} catch (e) {
console.error('Err addFavorite', e);
return {ris: null, ok: 0};
}
};
// Aggiungo il Seen
reactionSchema.statics.addSeen = async function (req, idapp, username, id, tab) {
const myrec = await Reaction.findOne({ idrec: id, idapp, username });
if (!myrec) {
const myrec = new Reaction({ idrec: id, idapp, userId: req.user.id, username, tab, seen: true });
return await myrec.save()
.then((ris) => {
// console.log('salvato proj!');
return {ris, ok: ris ? 1 : 0};
})
.catch(err => {
console.log("Error addSeen", err.message);
});
} else {
return Reaction.updateOne({ _id: myrec._id }, {
$set: {
seen: true,
}
})
}
};
// Aggiungo il Bookmark
reactionSchema.statics.addBookmark = async function (req, idapp, username, id, tab) {
const myrec = await Reaction.findOne({ idrec: id, idapp, username });
if (!myrec) {
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};
})
.catch(err => {
console.log("Error addBookmark", err.message);
});
} else {
return Reaction.updateOne({ _id: myrec._id }, {
$set: {
book: true,
}
})
}
};
// Aggiungo il Attend
reactionSchema.statics.addAttend = async function (req, idapp, username, id, tab) {
const myrec = await Reaction.findOne({ idrec: id, idapp, username });
if (!myrec) {
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};
})
.catch(err => {
console.log("Error addAttend", err.message);
});
} else {
return Reaction.updateOne({ _id: myrec._id }, {
$set: {
attend: true,
}
})
}
};
// Rimuovo il Favorite
reactionSchema.statics.removeFavorite = async function (
idapp, username, id, tab) {
const myrec = await Reaction.findOne({ idrec: id, idapp, username });
if (myrec) {
return Reaction.updateOne({ _id: myrec._id}, {
$set: {
fav: false,
}
})
}
return false;
};
// Rimuovo il Bookmark
reactionSchema.statics.removeBookmark = async function (
idapp, username, id, tab) {
const myrec = await Reaction.findOne({ idrec: id, idapp, username });
if (myrec) {
return Reaction.updateOne({ _id: myrec._id}, {
$set: {
book: false,
}
})
}
return false;
};
const Reaction = mongoose.model('Reaction', reactionSchema);
module.exports = { Reaction };

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