From 7cda5dbb8bf622b504afd165e45d6b0c4b17ef1e Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Thu, 28 Jul 2022 21:47:23 +0200 Subject: [PATCH] Friends Notification Starting Group Create Notification... --- src/server/locales/en.json | 4 ++- src/server/locales/it.json | 4 ++- src/server/models/sendnotif.js | 53 +++++++++++++++++------------- src/server/models/user.js | 35 +++++++++++++++++--- src/server/router/index_router.js | 18 ++++++---- src/server/router/push_router.js | 2 +- src/server/telegram/telegrambot.js | 9 ----- src/server/tools/shared_nodejs.js | 6 +++- 8 files changed, 85 insertions(+), 46 deletions(-) diff --git a/src/server/locales/en.json b/src/server/locales/en.json index 00e5eb6..4254137 100755 --- a/src/server/locales/en.json +++ b/src/server/locales/en.json @@ -13,5 +13,7 @@ "OPEN PAGE": "OPEN PAGE", "%s asked you for Friendship": "%s asked you for Friendship", "%s accepted your Friendship": "%s accepted your Friendship", - "%s refused your Friendship": "%s refused 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!" } diff --git a/src/server/locales/it.json b/src/server/locales/it.json index 529c5da..3e73550 100755 --- a/src/server/locales/it.json +++ b/src/server/locales/it.json @@ -8,5 +8,7 @@ "OPEN PAGE": "APRI PAGINA", "%s asked you for Friendship": "%s ti ha chiesto l'Amicizia", "%s accepted your Friendship": "%s ha accettato l'Amicizia", - "%s refused your Friendship": "%s ha rifiutato 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 !" } diff --git a/src/server/models/sendnotif.js b/src/server/models/sendnotif.js index 3e7058f..3508438 100755 --- a/src/server/models/sendnotif.js +++ b/src/server/models/sendnotif.js @@ -145,7 +145,8 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) { recnotif.openUrl = '/my/' + userorig; if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_NEW_REC) { newdescr = i18n.__('%s asked you for Friendship', userorig, mydescr); - } else if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED) { + } else if ((recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED) || + (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED_MY_REQUEST)) { newdescr = i18n.__('%s accepted your Friendship', userorig, mydescr); } else if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REFUSED) { newdescr = i18n.__('%s refused your Friendship', userorig, mydescr); @@ -258,30 +259,38 @@ sendNotifSchema.statics.saveNotif = async function(myrecnotif) { } else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REFUSED) { newstatus = shared_consts.StatusNotifs.STATUS_FRIENDS_REFUSED; } - if (newstatus) { - const fields_to_update = { - status: newstatus, - read: true, - }; + } else if (myrecnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) { + typeidsearch = shared_consts.TypeNotifs.ID_GROUP_NEW_REC; + if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_ACCEPTED) { + newstatus = shared_consts.StatusNotifs.STATUS_GROUPS_ACCEPTED; + } else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_REFUSED) { + newstatus = shared_consts.StatusNotifs.STATUS_GROUPS_REFUSED; + } + } - const query = { - idapp: myrecnotif.idapp, - typedir: myrecnotif.typedir, - typeid: typeidsearch, - dest: myrecnotif.sender, - deleted: false, - status: 0, - }; + if (newstatus) { + const fields_to_update = { + status: newstatus, + read: true, + }; - // Cerca il record e se lo trova lo aggiorna - const myrec = await SendNotif.findOneAndUpdate(query, {$set: fields_to_update}, { - new: false, - returnNewDocument: true - }); + const query = { + idapp: myrecnotif.idapp, + typedir: myrecnotif.typedir, + typeid: typeidsearch, + dest: myrecnotif.sender, + deleted: false, + status: 0, + }; - if (myrec) { - return myrec._doc; - } + // Cerca il record e se lo trova lo aggiorna + const myrec = await SendNotif.findOneAndUpdate(query, {$set: fields_to_update}, { + new: false, + returnNewDocument: true, + }); + + if (myrec) { + return myrec._doc; } } diff --git a/src/server/models/user.js b/src/server/models/user.js index cd47404..7293dfe 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -17,6 +17,8 @@ const {MyGroup} = require('../models/mygroup'); const {ObjectID} = require('mongodb'); +const i18n = require('i18n'); + const shared_consts = require('../tools/shared_nodejs'); mongoose.Promise = global.Promise; @@ -476,7 +478,7 @@ UserSchema.statics.canHavePower = function(perm) { try { let consentito = false; if (User.isAdmin(perm) || User.isManager(perm) || - User.isEditor(perm) || User.isTutor(perm)) { + User.isEditor(perm) || User.isFacilitatore(perm)) { consentito = true; } @@ -531,7 +533,7 @@ UserSchema.statics.isDepartment = function(perm) { } }; -UserSchema.statics.isTutor = function(perm) { +UserSchema.statics.isFacilitatore = function(perm) { try { return ((perm & shared_consts.Permissions.Facilitatore) === shared_consts.Permissions.Facilitatore); @@ -1561,6 +1563,8 @@ UserSchema.statics.setFriendsCmd = async function(idapp, usernameOrig, usernameD const {SendNotif} = require('../models/sendnotif'); + const telegrambot = require('../telegram/telegrambot'); + let ris = null; let update = {}; try { @@ -1595,12 +1599,31 @@ UserSchema.statics.setFriendsCmd = async function(idapp, usernameOrig, usernameD }; ris = await User.updateOne({idapp, username: usernameOrig}, update); - // CREATE NOTIFICATION IN TABLE SENDNOTIF - const req = tools.getReqByPar(idapp, usernameOrig); + // Send a notification to the DESTINATION FRIENDSHIP ! + let req = tools.getReqByPar(idapp, usernameOrig); await SendNotif.createNewNotifToSingleUser(req, null, {usernameDest}, true, shared_consts.TypeNotifs.TYPEDIR_FRIENDS, shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED); + // Send a notification to the SENDER FRIENDSHIP ! + req = tools.getReqByPar(idapp, usernameOrig); + await SendNotif.createNewNotifToSingleUser(req, null, {usernameDest}, true, shared_consts.TypeNotifs.TYPEDIR_FRIENDS, shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED_MY_REQUEST); + update = {$pull: {'profile.req_friends': {username: {$in: [usernameDest]}}}}; ris = await User.updateOne({idapp, username: usernameOrig}, update); + + if (ris) { + try { + const userDest = await User.getRecLangAndIdByUsername(idapp, usernameDest); + const user = await User.getRecLangAndIdByUsername(idapp, usernameOrig); + const msgOrig = i18n.__({phrase: '✅ %s accepted your Friendship request !', locale: user.lang}, usernameOrig) + const msgDest = i18n.__({phrase: "✅ You have accepted %s' Friendship request!", locale: userDest.lang}, usernameDest); + + await telegrambot.sendMsgTelegram(idapp, usernameDest, msgOrig); + await telegrambot.sendMsgTelegram(idapp, usernameOrig, msgDest); + }catch (e) { + console.error('Notification : ', e); + } + } + } // Controlla se lui aveva già la mia amicizia @@ -1723,6 +1746,8 @@ UserSchema.statics.ifAlreadyInGroup = async function(idapp, usernameOrig, groupn UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameDest, cmd, value, username_action) { + const {SendNotif} = require('../models/sendnotif'); + let ris = null; let update = {}; try { @@ -1743,7 +1768,7 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD // CREATE NOTIFICATION IN TABLE SENDNOTIF const req = tools.getReqByPar(idapp, usernameOrig); - SendNotif.createNewNotifToSingleUser(req, null, {usernameDest, groupnameDest}, true, shared_consts.TypeNotifs.TYPEDIR_GROUPS, shared_consts.TypeNotifs.ID_GROUP_ACCEPTED); + SendNotif.create(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 e604c34..7f2b5c8 100755 --- a/src/server/router/index_router.js +++ b/src/server/router/index_router.js @@ -230,12 +230,12 @@ router.post('/settable', authenticate, async (req, res) => { try { if (User.isAdmin(req.user.perm) || User.isManager(req.user.perm) || - User.isEditor(req.user.perm) || User.isTutor(req.user.perm)) { + User.isEditor(req.user.perm) || User.isFacilitatore(req.user.perm)) { consentito = true; } if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm) && - !User.isEditor(req.user.perm) && !User.isTutor(req.user.perm)) && + !User.isEditor(req.user.perm) && !User.isFacilitatore(req.user.perm)) && !tools.ModificheConsentite(params.table, fieldsvalue)) { // If without permissions, exit return res.status(404). @@ -355,6 +355,12 @@ router.post('/settable', authenticate, async (req, res) => { setnotif = true; } + if (shared_consts.TABLES_GROUPS_NOTIFICATION.includes(params.table)) { + typedir = shared_consts.TypeNotifs.TYPEDIR_GROUPS; + typeid = shared_consts.TypeNotifs.ID_GROUP_NEW_REC; + setnotif = true; + } + if (setnotif) { SendNotif.createNewNotification(req, res, params.table, myrec, typedir, typeid); } @@ -475,7 +481,7 @@ router.post('/getexp', authenticate, (req, res) => { } if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm) && - !User.isTutor(req.user.perm))) { + !User.isFacilitatore(req.user.perm))) { // If without permissions, exit return res.status(404). send({code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: ''}); @@ -591,7 +597,7 @@ router.patch('/chval', authenticate, async (req, res) => { // If I change my record... if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm) && - !User.isEditor(req.user.perm) && !User.isTutor(req.user.perm)) && + !User.isEditor(req.user.perm) && !User.isFacilitatore(req.user.perm)) && (req.user._id.toString() !== id) && !tools.ModificheConsentite(mydata.table, fieldsvalue)) { // If without permissions, exit @@ -798,7 +804,7 @@ router.patch('/askfunz', authenticate, async (req, res) => { if (!entra) { // If I change my record... if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm) && - !User.isTutor(req.user.perm)) && (req.user._id.toString() !== id)) { + !User.isFacilitatore(req.user.perm)) && (req.user._id.toString() !== id)) { // If without permissions, exit return res.status(404). send({code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: ''}); @@ -854,7 +860,7 @@ router.patch('/callfunz', authenticate, async (req, res) => { if (!entra) { // If I change my record... if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm) && - !User.isTutor(req.user.perm)) && (req.user._id.toString() !== id)) { + !User.isFacilitatore(req.user.perm)) && (req.user._id.toString() !== id)) { // If without permissions, exit return res.status(404). send({code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: ''}); diff --git a/src/server/router/push_router.js b/src/server/router/push_router.js index b2bc756..b950e18 100755 --- a/src/server/router/push_router.js +++ b/src/server/router/push_router.js @@ -119,7 +119,7 @@ router.post('/send', authenticate, async (req, res) => { let nummsg = 0; if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm) && - !User.isTutor(req.user.perm))) { + !User.isFacilitatore(req.user.perm))) { // If without permissions, exit return res.status(404). send({code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: ''}); diff --git a/src/server/telegram/telegrambot.js b/src/server/telegram/telegrambot.js index 80444e6..e48716c 100755 --- a/src/server/telegram/telegrambot.js +++ b/src/server/telegram/telegrambot.js @@ -494,8 +494,6 @@ const txt = { ' Si è appena Registrato "%s" (n. %s)\nInvitato da %s', MSG_APORTADOR_ASK_CONFIRM: '🆕💥 🧍‍♂️ Abilita Nuova Registrazione:', MSG_ACCEPT_NEWENTRY_INGROUP: '❇️👥 🧍‍♂️ Accetta Ingresso nel GRUPPO %s:', - MSG_FRIENDS_ACCEPTED: '✅ %s ha accettato la tua richiesta di Amicizia !', - MSG_FRIENDS_ACCEPTED_CONFIRMED: '✅ Hai accettato la richiesta di Amicizia di %s !', MSG_FRIENDS_NOT_ACCEPTED_CONFIRMED: '🚫 Hai rifiutato la richiesta di Amicizia di %s !', MSG_APORTADOR_CONFIRMED: '✅ %s è stato Abilitato correttamente (da %s)!', MSG_APORTADOR_DEST_CONFIRMED: '✅ Sei stato Abilitato correttamente da %s!\n' + @@ -3901,13 +3899,6 @@ if (true) { if (!foundIfAlreadyFriend) { // Aggiungilo nelle Amicizie const ris = await User.setFriendsCmd(user.idapp, data.username, data.userDest, cmd); - if (ris) { - const msgOrig = printf(getstr(userDest.lang, 'MSG_FRIENDS_ACCEPTED', data.userDest)); - const msgDest = printf(getstr(user.lang, 'MSG_FRIENDS_ACCEPTED_CONFIRMED'), data.username); - - await local_sendMsgTelegram(user.idapp, data.username, msgOrig); - await local_sendMsgTelegram(user.idapp, data.userDest, msgDest); - } } diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index e11afe6..b057426 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -111,6 +111,7 @@ module.exports = { TABLES_ADV_NOTIFICATION: ['myskills', 'myhosps', 'mygoods'], TABLES_EVENTS_NOTIFICATION: ['mybachecas'], + TABLES_GROUPS_NOTIFICATION: ['mygroups'], TABLES_ID_NUMBER: [ 'permissions', @@ -290,7 +291,8 @@ module.exports = { StatusNotifs: { STATUS_FRIENDS_ACCEPTED: 1, STATUS_FRIENDS_REFUSED: 2, - + STATUS_GROUPS_ACCEPTED: 3, + STATUS_GROUPS_REFUSED: 4, }, TypeNotifs: { @@ -306,10 +308,12 @@ module.exports = { ID_FRIENDS_NEW_REC: 1, ID_FRIENDS_ACCEPTED: 2, ID_FRIENDS_REFUSED: 3, + ID_FRIENDS_ACCEPTED_MY_REQUEST: 4, TYPEDIR_GROUPS: 4, ID_GROUP_NEW_REC: 1, ID_GROUP_ACCEPTED: 2, + ID_GROUP_REFUSED: 3, TYPEDIR_CIRCUITS: 5,