From 241e4255d41563ecb32f6d7e546d40f61ce028c4 Mon Sep 17 00:00:00 2001 From: paoloar77 Date: Thu, 28 Jul 2022 23:27:51 +0200 Subject: [PATCH] Notification New Group --- src/server/locales/en.json | 4 +++- src/server/locales/it.json | 4 +++- src/server/models/mygroup.js | 6 ++++-- src/server/models/sendnotif.js | 26 +++++++++++++++++--------- src/server/models/user.js | 2 +- src/server/router/index_router.js | 27 +++++++++++++++------------ src/server/router/myevent_router.js | 6 +++--- src/server/router/mygroups_router.js | 8 ++++++++ src/server/tools/shared_nodejs.js | 12 +++++++++--- 9 files changed, 63 insertions(+), 32 deletions(-) diff --git a/src/server/locales/en.json b/src/server/locales/en.json index 4254137..739598a 100755 --- a/src/server/locales/en.json +++ b/src/server/locales/en.json @@ -15,5 +15,7 @@ "%s accepted your Friendship": "%s accepted your Friendship", "%s refused your Friendship": "%s refused your Friendship", "✅ %s accepted your Friendship request !": "✅ %s ha accettato la tua richiesta di Amicizia !", - "✅ You have accepted %s' Friendship request!": "✅ You have accepted %s' Friendship request!" + "✅ You have accepted %s' Friendship request!": "✅ You have accepted %s' Friendship request!", + "GROUPS_ACCEPTED": "✅ You have been accepted by %s to join the Group %s", + "GROUP_CREATED": "✅ A new Group created by %s called %s has been created" } diff --git a/src/server/locales/it.json b/src/server/locales/it.json index 3e73550..1482a66 100755 --- a/src/server/locales/it.json +++ b/src/server/locales/it.json @@ -10,5 +10,7 @@ "%s accepted your Friendship": "%s ha accettato l'Amicizia", "%s refused your Friendship": "%s ha rifiutato l'Amicizia", "✅ %s accepted your Friendship request !": "✅ %s ha accettato la tua richiesta di Amicizia !", - "✅ You have accepted %s' Friendship request!": "✅ Hai accettato la richiesta di Amicizia di %s !" + "✅ You have accepted %s' Friendship request!": "✅ Hai accettato la richiesta di Amicizia di %s !", + "GROUPS_ACCEPTED": "✅ Sei stato accettato da %s a far parte del Gruppo %s", + "GROUP_CREATED": "✅ %s ha creato un nuovo Gruppo chiamato %s" } diff --git a/src/server/models/mygroup.js b/src/server/models/mygroup.js index 21cf604..02a8165 100755 --- a/src/server/models/mygroup.js +++ b/src/server/models/mygroup.js @@ -235,10 +235,12 @@ MyGroupSchema.statics.getInfoGroupByGroupname = async function(idapp, groupname) const whatToShow = this.getWhatToShow(idapp, groupname); - return MyGroup.findOne({ + const rec = await MyGroup.findOne({ idapp, groupname, - }, whatToShow).then((rec) => !!rec ? rec._doc : null); + }, whatToShow).lean(); + + return rec; }; diff --git a/src/server/models/sendnotif.js b/src/server/models/sendnotif.js index 3508438..f81c4d2 100755 --- a/src/server/models/sendnotif.js +++ b/src/server/models/sendnotif.js @@ -152,11 +152,11 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) { newdescr = i18n.__('%s refused your Friendship', userorig, mydescr); } } else if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) { - recnotif.openUrl = '/group/' + paramsObj.groupNameDest; + recnotif.openUrl = '/grp/' + recnotif.paramsObj.groupNameDest; if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_NEW_REC) { - newdescr = i18n.__('%s asked you for Friendship', userorig, mydescr); + newdescr = i18n.__('GROUP_CREATED', userorig, recnotif.paramsObj.groupNameDest); } else if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_ACCEPTED) { - newdescr = i18n.__('%s accepted your Friendship', userorig, mydescr); + newdescr = i18n.__('GROUPS_ACCEPTED', userorig, recnotif.paramsObj.groupNameDest); } } @@ -334,7 +334,7 @@ sendNotifSchema.statics.getDefaultRec = function(req) { }; -sendNotifSchema.statics.createNewNotification = async function(req, res, table, rec, typedir, typeid) { +sendNotifSchema.statics.createNewNotification = async function(req, res, paramsObj, table, rec, typedir, typeid) { const SendNotif = this; try { @@ -348,7 +348,7 @@ sendNotifSchema.statics.createNewNotification = async function(req, res, table, myrecnotif.typedir = typedir; myrecnotif.typeid = typeid; - await SendNotif.sendToTheDestinations(myrecnotif, req, res); + await SendNotif.sendToTheDestinations(myrecnotif, req, res, paramsObj); return true; } catch (e) { @@ -375,7 +375,7 @@ sendNotifSchema.statics.createNewNotifToSingleUser = async function(req, res, pa } }; -sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req, res) { +sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req, res, paramsObj) { const SendNotif = this; const {User} = require('../models/user'); @@ -383,6 +383,8 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req, const {City} = require('../models/city'); const {Province} = require('../models/province'); + myrecnotif.paramsObj = paramsObj; + try { // Send only to the destination to reach: @@ -466,7 +468,13 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req, } - if (send) { + if (shared_consts.TABLES_GROUPS_NOTIFICATION.includes(myrecnotif.tablerec)) { + if (tools.isBitActive(usernotifprofile.value, shared_consts.GroupsNotifs.STATUS_GROUPS_NEW)) { + send = true; + } + } + + if (send) { myrecnotif.dest = user.username; await SendNotif.saveAndSendNotif(myrecnotif, req, res, user); } @@ -484,10 +492,10 @@ sendNotifSchema.statics.sendToSingleUserDest = async function(myrecnotif, req, r try { + myrecnotif.paramsObj = paramsObj; + myrecnotif.dest = paramsObj.usernameDest ? paramsObj.usernameDest : ''; myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif); - myrecnotif.dest = paramsObj.usernameDest; - myrecnotif.paramsObj = paramsObj; if (onlysave) { await SendNotif.saveNotif(myrecnotif); } else { diff --git a/src/server/models/user.js b/src/server/models/user.js index 7293dfe..946ba61 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -1768,7 +1768,7 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD // CREATE NOTIFICATION IN TABLE SENDNOTIF const req = tools.getReqByPar(idapp, usernameOrig); - SendNotif.create(req, null, {usernameDest:'', groupnameDest}, true, shared_consts.TypeNotifs.TYPEDIR_GROUPS, shared_consts.TypeNotifs.ID_GROUP_ACCEPTED); + SendNotif.createNewNotifToSingleUser(req, null, {usernameDest:'', groupnameDest}, true, shared_consts.TypeNotifs.TYPEDIR_GROUPS, shared_consts.TypeNotifs.ID_GROUP_ACCEPTED); // Elimina la richiesta: update = {$pull: {req_users: {username: {$in: [usernameOrig]}}}}; diff --git a/src/server/router/index_router.js b/src/server/router/index_router.js index 7f2b5c8..fe79560 100755 --- a/src/server/router/index_router.js +++ b/src/server/router/index_router.js @@ -327,21 +327,12 @@ router.post('/settable', authenticate, async (req, res) => { } // tools.mylog('rec', rec); - }).then((myrec) => { - - if (params.table === shared_consts.TAB_MYGROUPS && isnewrec) { - // nuovo Record: - // aggiungi il creatore al gruppo stesso - return User.setGroupsCmd(mydata.idapp, req.user.username, - myrec.groupname, - shared_consts.GROUPSCMD.SETGROUP, true, req.user.username).then((ris) => { - return res.send(myrec); - }); - } + }).then(async (myrec) => { let setnotif = false; let typedir = 0; let typeid = 0; + let groupNameDest = ''; if (shared_consts.TABLES_ADV_NOTIFICATION.includes(params.table)) { typedir = shared_consts.TypeNotifs.TYPEDIR_BACHECA; @@ -358,13 +349,25 @@ router.post('/settable', authenticate, async (req, res) => { if (shared_consts.TABLES_GROUPS_NOTIFICATION.includes(params.table)) { typedir = shared_consts.TypeNotifs.TYPEDIR_GROUPS; typeid = shared_consts.TypeNotifs.ID_GROUP_NEW_REC; + groupNameDest = myrec ? myrec.groupname : ''; setnotif = true; } if (setnotif) { - SendNotif.createNewNotification(req, res, params.table, myrec, typedir, typeid); + await SendNotif.createNewNotification(req, res, {groupNameDest}, params.table, myrec, typedir, typeid); } + if (params.table === shared_consts.TAB_MYGROUPS && isnewrec) { + // nuovo Record: + // aggiungi il creatore al gruppo stesso + return User.setGroupsCmd(mydata.idapp, req.user.username, + myrec.groupname, + shared_consts.GROUPSCMD.SETGROUP, true, req.user.username).then((ris) => { + return res.send(myrec); + }); + } + + return res.send(myrec); }).catch((e) => { console.error('settable', e.message); diff --git a/src/server/router/myevent_router.js b/src/server/router/myevent_router.js index 1970ba3..e62b351 100755 --- a/src/server/router/myevent_router.js +++ b/src/server/router/myevent_router.js @@ -44,7 +44,7 @@ router.post('/', authenticate, (req, res) => { }).then(async (recmyevent) => { // tools.mylog('myevent:', myevent); // tools.mylog('already exist'); - SendNotif.createNewNotification(req, res, shared_consts.TABLES_MYEVENTS, recmyevent, shared_consts.TypeNotifs.TYPEDIR_EVENTS, shared_consts.TypeNotifs.ID_EVENTS_NEW_REC); + SendNotif.createNewNotification(req, res, {}, shared_consts.TABLES_MYEVENTS, recmyevent, shared_consts.TypeNotifs.TYPEDIR_EVENTS, shared_consts.TypeNotifs.ID_EVENTS_NEW_REC); return res; }).then((res) => { res.send({code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id}); @@ -59,7 +59,7 @@ router.post('/', authenticate, (req, res) => { myevent.findById(idobj).then((recmyevent) => { recmyevent.typedir = shared_consts.TypeNotifs.TYPEDIR_EVENTS recmyevent.typeid = shared_consts.TypeNotifs.ID_EVENTS_NEW_REC - SendNotif.createNewNotification(req, res, shared_consts.TABLES_MYEVENTS, recmyevent, shared_consts.TypeNotifs.TYPEDIR_EVENTS, shared_consts.TypeNotifs.ID_EVENTS_NEW_REC); + SendNotif.createNewNotification(req, res, {}, shared_consts.TABLES_MYEVENTS, recmyevent, shared_consts.TypeNotifs.TYPEDIR_EVENTS, shared_consts.TypeNotifs.ID_EVENTS_NEW_REC); return res.send({code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id}); }); }); @@ -79,7 +79,7 @@ router.delete('/:id/:notify/:idapp', authenticate, (req, res) => { } if (notify === '1') { - SendNotif.createNewNotification(req, res, shared_consts.TABLES_MYEVENTS, recmyevent, shared_consts.TypeNotifs.TYPEDIR_EVENTS, shared_consts.TypeNotifs.ID_EVENTS_REMOVE_REC); + SendNotif.createNewNotification(req, res, {}, shared_consts.TABLES_MYEVENTS, recmyevent, shared_consts.TypeNotifs.TYPEDIR_EVENTS, shared_consts.TypeNotifs.ID_EVENTS_REMOVE_REC); } tools.mylog('DELETED ', recmyevent.descr, recmyevent._id); diff --git a/src/server/router/mygroups_router.js b/src/server/router/mygroups_router.js index 1e95a6e..07c7828 100755 --- a/src/server/router/mygroups_router.js +++ b/src/server/router/mygroups_router.js @@ -29,8 +29,16 @@ async function getGroupRecAdminsInfo(idapp, data) { router.post('/load', authenticate, async (req, res) => { const idapp = req.body.idapp; const groupname = req.body.groupname; + const usernameOrig = req.user.username; try { + + const {SendNotif} = require('../models/sendnotif'); + + // Check if ìs a Notif to read + const idnotif = req.body['idnotif'] ? req.body['idnotif'] : ''; + SendNotif.setNotifAsRead(idapp, usernameOrig, idnotif); + const whatshow = MyGroup.getWhatToShow(idapp, req.user.username); let data = await MyGroup.findOne({idapp, groupname}, whatshow).lean(); diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index b057426..de79829 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -295,6 +295,12 @@ module.exports = { STATUS_GROUPS_REFUSED: 4, }, + GroupsNotifs: { + STATUS_GROUPS_NEW: 1, + STATUS_GROUPS_ACCEPTED: 2, + STATUS_GROUPS_REFUSED: 4, + }, + TypeNotifs: { TYPEDIR_BACHECA: 1, ID_BACHECA_NEW_GOOD: 1, @@ -307,13 +313,13 @@ module.exports = { TYPEDIR_FRIENDS: 3, ID_FRIENDS_NEW_REC: 1, ID_FRIENDS_ACCEPTED: 2, - ID_FRIENDS_REFUSED: 3, - ID_FRIENDS_ACCEPTED_MY_REQUEST: 4, + ID_FRIENDS_REFUSED: 4, + ID_FRIENDS_ACCEPTED_MY_REQUEST: 8, TYPEDIR_GROUPS: 4, ID_GROUP_NEW_REC: 1, ID_GROUP_ACCEPTED: 2, - ID_GROUP_REFUSED: 3, + ID_GROUP_REFUSED: 4, TYPEDIR_CIRCUITS: 5,