Notifications

Settings Notifications
User Panel
This commit is contained in:
Paolo Arena
2022-07-23 17:48:33 +02:00
parent e9ce597027
commit b06f1e4ab8
19 changed files with 472 additions and 121 deletions

View File

@@ -4,8 +4,16 @@ const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.level = 'F';
const i18n = require('i18n');
const {ObjectID} = require('mongodb');
const shared_consts = require('../tools/shared_nodejs');
const globalTables = require('../tools/globalTables');
const tools = require('../tools/general');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true;
@@ -15,7 +23,10 @@ const sendNotifSchema = new Schema({
idapp: {
type: String,
},
type: {
typedir: {
type: Number,
},
typeid: {
type: Number,
},
sender: { // mittente
@@ -27,7 +38,7 @@ const sendNotifSchema = new Schema({
descr: {
type: String,
},
link: {
openUrl: {
type: String,
},
datenotif: {
@@ -40,6 +51,12 @@ const sendNotifSchema = new Schema({
type: Boolean,
default: false,
},
tablerec: {
type: String,
},
idrec: {
type: String,
},
deleted: {
type: Boolean,
default: false,
@@ -52,7 +69,6 @@ sendNotifSchema.statics.setNotifAsRead = function(idapp, username, idnotif) {
try {
if (idnotif) {
return SendNotif.findOneAndUpdate({
$and: [
@@ -70,7 +86,7 @@ sendNotifSchema.statics.setNotifAsRead = function(idapp, username, idnotif) {
console.error('err', err);
});
}
}catch (e) {
} catch (e) {
return false;
}
};
@@ -85,7 +101,7 @@ sendNotifSchema.statics.findAllNotifByUsernameIdAndIdApp = function(username, la
{'datenotif': {$gt: new Date(lastdataread)}},
],
}).lean().sort({datenotif: -1}).then((arrnotif) => {
console.log('arrnotif', arrnotif.length);
// console.log('arrnotif', arrnotif.length);
return arrnotif;
}).catch((err) => {
console.error('err', err);
@@ -93,7 +109,24 @@ sendNotifSchema.statics.findAllNotifByUsernameIdAndIdApp = function(username, la
};
sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function(username, idapp) {
sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_BACHECA) {
if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_GOOD) {
recnotif.descr = i18n.__('Good: %s');
recnotif.openUrl = '/goods';
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_SERVICE) {
recnotif.descr = i18n.__('Service: %s');
recnotif.openUrl = '/services';
}
} else if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_FRIENDS) {
recnotif.openUrl = '/friends';
}
return recnotif;
};
sendNotifSchema.statics.findLastNotifsByUserIdAndIdApp = function(username, idapp, limit) {
const SendNotif = this;
return SendNotif.aggregate([
@@ -103,16 +136,7 @@ sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function(username, idapp
dest: username,
},
},
{
$group:
{
_id: '$dest',
descr: {$last: '$message'},
datenotif: {$last: '$datenotif'},
read: {$last: '$read'},
},
},
{$limit: limit},
{
$sort: {datenotif: -1},
},
@@ -129,6 +153,165 @@ sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function(username, idapp
};
sendNotifSchema.statics.saveAndSendNotif = function(myrecnotif, req, res, user) {
let idapp = req.body.idapp;
const check = tools.checkUserOk(myrecnotif.sender, user ? myrecnotif.sender : req.user.username, res);
if (check.exit) return check.ret;
myrecnotif._id = new ObjectID();
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
return myrecnotif.save().then((writeresult) => {
let idobj = writeresult._id;
myrecnotif._id = idobj;
return SendNotif.findById(idobj).lean().then(async (recnotif) => {
return await globalTables.sendNotif(myrecnotif.typedir, myrecnotif.typeid, res, idapp, user ? user : req.user, recnotif).then((ris) => {
return recnotif;
});
});
}).catch((e) => {
console.log(e.message);
return null;
});
};
sendNotifSchema.statics.getDefaultRec = function(req) {
return {
idapp: req.body.idapp,
typedir: '',
typeid: '',
sender: req.user ? req.user.username : '',
dest: '',
descr: '',
openUrl: '',
datenotif: new Date(),
status: 0,
read: false,
};
};
sendNotifSchema.statics.createNewNotification = async function(req, res, table, rec, typedir, typeid) {
const SendNotif = this;
try {
let myrecnotif = new SendNotif(this.getDefaultRec(req));
myrecnotif.tablerec = table;
if (rec) {
myrecnotif.idrec = rec._id;
}
myrecnotif.typedir = typedir;
myrecnotif.typeid = typeid;
await SendNotif.sendToTheDestinations(myrecnotif, req, res);
return true;
} catch (e) {
console.error('createNewNotification', e);
return false;
}
};
sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req, res) {
const SendNotif = this;
const {User} = require('../models/user');
const {City} = require('../models/city');
const {Province} = require('../models/province');
// Send only to the destination to reach:
const userlist = await User.find({
idapp: myrecnotif.idapp,
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
}, {
name: 1,
surname: 1,
lang: 1,
username: 1,
'profile.notifs': 1,
'profile.notif_idCities': 1,
'profile.notif_provinces': 1,
'profile.notif_regions': 1,
'profile.notif_sectors': 1,
'profile.notif_sector_goods': 1,
}).lean();
let arrprovinces = [];
let arrregions = [];
let idSector = 0;
const mytable = globalTables.getTableByTableName(myrecnotif.tablerec);
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotif.tablerec) || shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotif.tablerec)) {
const myrec = await mytable.findOne({_id: myrecnotif.idrec}).lean();
if (myrec) {
for (const city of myrec.idCity) {
arrprovinces.push(await City.getProvinceByIdCity(city));
arrregions.push(await City.getRegionByIdCity(city));
}
if (myrecnotif.tablerec === shared_consts.TABLES_MYGOODS) {
idSector = myrec.idSectorGood;
} else {
idSector = myrec.idSector;
}
}
}
for (const user of userlist) {
if (user.profile.notifs) {
const usernotifprofile = user.profile.notifs.find((notif) => notif.dir === myrecnotif.typedir);
let send = false;
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotif.tablerec) ||
shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotif.tablerec)) {
// Estrai la Città, la Provincia e la regione.
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_PROVINCE) &&
user.profile.notif_provinces && user.profile.notif_provinces.some(r => arrprovinces.indexOf(r) >= 0)) {
send = true;
}
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_REGION) &&
user.profile.notif_regions && user.profile.notif_regions.some(r => arrregions.indexOf(r) >= 0)) {
send = true;
}
if (idSector && usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_SECTOR)) {
// Controlla se è del settore selezionato
if (myrecnotif.tablerec === shared_consts.TABLES_MYGOODS) {
if (user.profile.notif_sector_goods) {
send = send && (user.profile.notif_sector_goods.includes(idSector));
}
} else if (user.profile.notif_sectors) {
send = send && (user.profile.notif_sectors.includes(idSector));
}
}
}
if (send) {
myrecnotif.dest = user.username;
await SendNotif.saveAndSendNotif(myrecnotif, req, res, user);
}
}
}
};
const SendNotif = mongoose.model('SendNotif', sendNotifSchema);
module.exports = {SendNotif: SendNotif};