diff --git a/src/server/locales/en.json b/src/server/locales/en.json index 4598ad9..b4a43a4 100755 --- a/src/server/locales/en.json +++ b/src/server/locales/en.json @@ -3,5 +3,9 @@ "Email Verificata!": "Email Verificada! Cierre esta ventana y regrese a la otra.", "Nuova Registrazione": "Nuevo Registro", "Effettuata una Nuova Registrazione": "Se ha realizado un nuevo registro", - "partecipanti": "participantes" + "partecipanti": "participantes", + "Hello": "Hello", + "Hello %s": "Ciao %s", + "Good: %s": "Good: %s", + "Service: %s": "Service: %s" } diff --git a/src/server/locales/it.json b/src/server/locales/it.json index 2c63c08..5393b9d 100755 --- a/src/server/locales/it.json +++ b/src/server/locales/it.json @@ -1,2 +1,6 @@ { + "Hello": "Ciao", + "Hello %s": "Ciao %s", + "Good: %s": "Bene: %s", + "Service: %s": "Servizio: %s" } diff --git a/src/server/models/city.js b/src/server/models/city.js index 37db3f5..8ae837c 100755 --- a/src/server/models/city.js +++ b/src/server/models/city.js @@ -68,6 +68,24 @@ CitySchema.pre('save', async function(next) { next(); }); +CitySchema.statics.getProvinceByIdCity = async function(idcity) { + const myrec = await City.findOne({_id: idcity}).lean(); + if (myrec) { + return myrec.prov; + } + + return ''; +} + +CitySchema.statics.getRegionByIdCity = async function(idcity) { + const myrec = await City.findOne({_id: idcity}).lean(); + if (myrec) { + return myrec.reg; + } + + return ''; +} + CitySchema.statics.findByCity = function(mycity) { let myregexp = new RegExp(mycity.trim().replace(' ', '|'), 'ig'); diff --git a/src/server/models/province.js b/src/server/models/province.js index 6f2c640..3c9e6ff 100755 --- a/src/server/models/province.js +++ b/src/server/models/province.js @@ -37,6 +37,16 @@ const ProvinceSchema = new Schema({ }, }, { _id : false }); +ProvinceSchema.statics.getRegionByStrProvince = async function(strprovince) { + const myrec = await Province.findOne({prov: strprovince}).lean(); + if (myrec) { + return myrec.reg; + } + + return ''; +} + + ProvinceSchema.statics.getFieldsForSearch = function() { return [ diff --git a/src/server/models/sendnotif.js b/src/server/models/sendnotif.js index e7bdccb..c1cef52 100755 --- a/src/server/models/sendnotif.js +++ b/src/server/models/sendnotif.js @@ -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}; diff --git a/src/server/models/site.js b/src/server/models/site.js index 470eac8..3ff3087 100755 --- a/src/server/models/site.js +++ b/src/server/models/site.js @@ -131,7 +131,7 @@ module.exports.findAll = async function () { const myfind = { active: true }; - return Site.find(myfind).lean(); + return await Site.find(myfind).lean(); }; diff --git a/src/server/models/user.js b/src/server/models/user.js index da79dbc..d5ea7d9 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -365,6 +365,26 @@ const UserSchema = new mongoose.Schema({ value: {type: Number}, }, ], + notif_idCities: [ + { + type: Number, + }], + notif_provinces: [ + { + type: String, + }], + notif_regions: [ + { + type: String, + }], + notif_sectors: [ + { + type: Number, + }], + notif_sector_goods: [ + { + type: Number, + }] }, }) ; diff --git a/src/server/populate/populate.js b/src/server/populate/populate.js index f2ffa9f..b10e6c8 100644 --- a/src/server/populate/populate.js +++ b/src/server/populate/populate.js @@ -6,7 +6,6 @@ const mongoose = require('mongoose').set('debug', false) const shared_consts = require('../tools/shared_nodejs'); -const globalTables = require('../tools/globalTables'); module.exports = { @@ -146,6 +145,8 @@ module.exports = { const scrivi_citta = false; const scrivi_contribtype = false; + const globalTables = require('../tools/globalTables'); + let ris = null; try { diff --git a/src/server/router/index_router.js b/src/server/router/index_router.js index 561985e..facea35 100755 --- a/src/server/router/index_router.js +++ b/src/server/router/index_router.js @@ -253,6 +253,7 @@ router.post('/settable', authenticate, async (req, res) => { } } + if (params.table === shared_consts.TAB_MYGROUPS) { if (shared_consts.MYGROUPS_KEY_TO_CRYPTED in mydata) { if (mydata[shared_consts.MYGROUPS_KEY_TO_CRYPTED]) { @@ -338,6 +339,11 @@ router.post('/settable', authenticate, async (req, res) => { }); } + if (shared_consts.TABLES_ADV_NOTIFICATION.includes(params.table)) { + // E' un annuncio, pertanto mando una notifica a "tutti" + SendNotif.createNewNotification(req, res, params.table, myrec, shared_consts.TypeNotifs.TYPEDIR_BACHECA, shared_consts.TypeNotifs.ID_BACHECA_NEW_GOOD); + } + return res.send(myrec); }).catch((e) => { console.error('settable', e.message); @@ -1301,7 +1307,7 @@ router.get(process.env.LINK_CHECK_UPDATES, authenticate, async (req, res) => { // msgs = SendMsg.findAllByUserIdAndIdApp(userId, req.user.username, req.user.idapp); let last_msgs = SendMsg.findLastGroupByUserIdAndIdApp(userId, req.user.username, idapp); - let last_notifs = SendNotif.findLastGroupByUserIdAndIdApp(userId, req.user.username, idapp); + let last_notifs = SendNotif.findLastNotifsByUserIdAndIdApp(req.user.username, idapp, 20); let usersList = null; diff --git a/src/server/router/myevent_router.js b/src/server/router/myevent_router.js index 6f2a12e..45bb1d6 100755 --- a/src/server/router/myevent_router.js +++ b/src/server/router/myevent_router.js @@ -14,6 +14,10 @@ const sendemail = require('../sendemail'); const globalTables = require('../tools/globalTables'); +const {SendNotif} = require('../models/sendnotif'); + +const shared_consts = require('../tools/shared_nodejs'); + const _ = require('lodash'); router.post('/', authenticate, (req, res) => { @@ -37,10 +41,12 @@ router.post('/', authenticate, (req, res) => { return myevent.findOneAndUpdate({id}, {$set: fieldtochange}, { new: false, upsert: true, - }).then((recmyevent) => { + }).then(async (recmyevent) => { // tools.mylog('myevent:', myevent); // tools.mylog('already exist'); - globalTables.sendNotif(res, myrec.idapp, req.user, recmyevent); + recmyevent.typedir = shared_consts.TypeNotifs.TYPEDIR_EVENTS + recmyevent.typeid = shared_consts.TypeNotifs.ID_EVENTS_NEW_REC + await SendNotif.saveAndSendNotif(recmyevent, req, res); return res; }).then((res) => { res.send({code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id}); @@ -53,8 +59,11 @@ router.post('/', authenticate, (req, res) => { let idobj = writeresult._id; myevent.findById(idobj).then((recmyevent) => { - globalTables.sendNotif(res, myrec.idapp, req.user, recmyevent); - res.send({code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id}); + recmyevent.typedir = shared_consts.TypeNotifs.TYPEDIR_EVENTS + recmyevent.typeid = shared_consts.TypeNotifs.ID_EVENTS_NEW_REC + return SendNotif.saveAndSendNotif(recmyevent, req, res).then((ris) => { + return res.send({code: server_constants.RIS_CODE_OK, msg: '', id: recmyevent._id}); + }); }); }); } @@ -72,8 +81,11 @@ router.delete('/:id/:notify/:idapp', authenticate, (req, res) => { return res.status(404).send(); } - if (notify === '1') - globalTables.sendNotif(res, idapp, req.user, recmyevent); + if (notify === '1') { + recmyevent.typedir = shared_consts.TypeNotifs.TYPEDIR_EVENTS + recmyevent.typeid = shared_consts.TypeNotifs.ID_EVENTS_REMOVE_REC + SendNotif.saveAndSendNotif(recmyevent, req, res); + } tools.mylog('DELETED ', recmyevent.descr, recmyevent._id); diff --git a/src/server/router/sendmsg_router.js b/src/server/router/sendmsg_router.js index 91d2162..e331e87 100755 --- a/src/server/router/sendmsg_router.js +++ b/src/server/router/sendmsg_router.js @@ -10,6 +10,8 @@ const { User } = require('../models/user'); const { Operator } = require('../models/operator'); const { SendMsg } = require('../models/sendmsg'); +const {SendNotif} = require('../models/sendnotif'); + const { ObjectID } = require('mongodb'); const sendemail = require('../sendemail'); @@ -46,9 +48,15 @@ router.post('/', authenticate, (req, res) => { // Add this field because I don't want to add into the database recmsg.source.infoevent = body.source.infoevent; - return await globalTables.sendNotif(res, body.idapp, req.user, recmsg).then((ris) => { - return res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmsg._id }); + recmsg.typedir = shared_consts.TypeNotifs.TYPEDIR_EVENTS; + recmsg.typeid = shared_consts.TypeNotifs.ID_EVENTS_REMOVE_REC; + return SendNotif.saveAndSendNotif(recmyevent, req, res).then((out) => { + if (out) + return res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmsg._id }); + else + return res.send({ code: server_constants.RIS_CODE_ERR, msg: '' }); }) + }); }).catch((e) => { console.log(e.message); diff --git a/src/server/router/sendnotif_router.js b/src/server/router/sendnotif_router.js index 7d05929..d759315 100755 --- a/src/server/router/sendnotif_router.js +++ b/src/server/router/sendnotif_router.js @@ -6,49 +6,24 @@ const server_constants = require('../tools/server_constants'); const {authenticate} = require('../middleware/authenticate'); -const {User} = require('../models/user'); -const {Operator} = require('../models/operator'); const {SendNotif} = require('../models/sendnotif'); -const {ObjectID} = require('mongodb'); - -const sendemail = require('../sendemail'); - -const shared_consts = require('../tools/shared_nodejs'); - const _ = require('lodash'); -router.post('/', authenticate, (req, res) => { +router.post('/', authenticate, async (req, res) => { tools.mylog('INIZIO - SendNotif'); // tools.mylog('req.body', req.body); const body = _.pick(req.body, tools.allfieldSendNotif()); tools.mylog('crea SendNotif'); - const myrecnotif = new SendNotif(body); + let myrecnotif = new SendNotif(body); - const check = tools.checkUserOk(myrecnotif.sender, req.user.username, res); - if (check.exit) return check.ret; - - // console.log('fieldtochange', fieldtochange); - - myrecnotif._id = new ObjectID(); - return myrecnotif.save().then((writeresult) => { - let idobj = writeresult._id; - - myrecnotif._id = idobj; - - return SendNotif.findById(idobj).then(async (recnotif) => { - // Add this field because I don't want to add into the database - - return await globalTables.sendNotif(res, body.idapp, req.user, recnotif).then((ris) => { - return res.send({code: server_constants.RIS_CODE_OK, notif: '', id: recnotif._id}); - }); - }); - }).catch((e) => { - console.log(e.message); - // res.status(400).send(e); + const recout = await SendNotif.saveAndSendNotif(myrecnotif, req, res); + if (recout) { + return res.send({code: server_constants.RIS_CODE_OK, notif: '', record: recout}); + } else { return res.send({code: server_constants.RIS_CODE_ERR, notif: ''}); - }); + } }); @@ -74,6 +49,46 @@ router.get('/setall/:username/:idapp', authenticate, async (req, res) => { }); +router.get('/del/:username/:id/:idapp', authenticate, async (req, res) => { + + const idapp = req.params.idapp; + const username = req.params.username; + const myid = req.params.id; + const username_call = req.user.username; + + try { + if (username === username_call) { + await SendNotif.findOneAndRemove({idapp, _id: myid}); + return res.send(true); + } + } catch (e) { + return res.status(400).send(e); + } + + return res.send(false); + +}); + +router.get('/delall/:username/:idapp', authenticate, async (req, res) => { + + const idapp = req.params.idapp; + const username = req.params.username; + const username_call = req.user.username; + + try { + if (username === username_call) { + const ris = await SendNotif.deleteMany({idapp, dest: username}); + if (ris) + return res.send(true); + } + } catch (e) { + return res.status(400).send(e); + } + + return res.send(false); + +}); + router.get('/:username/:lastdataread/:idapp', authenticate, (req, res) => { tools.mylog('GET NotifS : ', req.params); const username = req.params.username; diff --git a/src/server/router/subscribe_router.js b/src/server/router/subscribe_router.js index 811d9d3..f608af2 100755 --- a/src/server/router/subscribe_router.js +++ b/src/server/router/subscribe_router.js @@ -56,6 +56,7 @@ router.post('/', authenticate, (req, res) => { } else { myitem.endpoint = subscriptionModel.endpoint; myitem.keys = subscriptionModel.keys; + myitem.createDate = new Date(); } return myitem.save((err, subscription) => { diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index 6b5e6f4..ff8c048 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -422,10 +422,12 @@ router.post('/panel', authenticate, async (req, res) => { try { const myuser = await User.findOne({idapp, username}, - {username: 1, email: 1, verified_by_aportador: 1, aportador_solidario: 1, + { + username: 1, email: 1, verified_by_aportador: 1, aportador_solidario: 1, lasttimeonline: 1, deleted: 1, - profile: 1}).lean(); + profile: 1, + }).lean(); if (!!myuser) { res.send(myuser); } else { @@ -450,7 +452,6 @@ router.post('/notifs', authenticate, async (req, res) => { send({code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: ''}); } - try { if (!!myuser) { if (tools.isArray(notifs) && notifs.length >= 0) { @@ -465,7 +466,6 @@ router.post('/notifs', authenticate, async (req, res) => { }); - router.post('/login', (req, res) => { var body = _.pick(req.body, ['username', 'password', 'idapp', 'keyappid', 'lang']); diff --git a/src/server/server.js b/src/server/server.js index 21f7cb2..3ffc02f 100755 --- a/src/server/server.js +++ b/src/server/server.js @@ -28,7 +28,6 @@ require('./db/mongoose'); const {Settings} = require('./models/settings'); -const globalTables = require('./tools/globalTables'); const i18n = require('i18n'); @@ -133,8 +132,13 @@ myLoad().then(ris => { i18n.configure({ locales: ['it', 'enUs', 'es', 'fr', 'pt', 'si'], + defaultLocale: 'it', // cookie: 'cook', directory: __dirname + '/locales', + api: { + '__': 'translate', + '__n': 'translateN' + }, }); app.use(cors({ @@ -245,6 +249,8 @@ async function mystart() { // await estraiTutteLeImmagini(); + await tools.getApps(); + if (process.env.PROD !== 1) { testmsgwebpush(); @@ -266,6 +272,7 @@ async function mystart() { } } + telegrambot = require('./telegram/telegrambot'); await inizia(); @@ -437,6 +444,9 @@ async function estraiImmagini(table) { let arrlist; + const globalTables = require('./tools/globalTables'); + + const mytable = globalTables.getTableByTableName(table); if (!mytable) return; diff --git a/src/server/telegram/telegrambot.js b/src/server/telegram/telegrambot.js index 6c90f6d..e27257b 100755 --- a/src/server/telegram/telegrambot.js +++ b/src/server/telegram/telegrambot.js @@ -3701,6 +3701,8 @@ if (true) { arrTeleg = appTelegram_TEST; console.log('TELEGRAM STARTING.... NODE_ENV:' + process.env.NODE_ENV); + + for (const idapp of arrTeleg) { try { diff --git a/src/server/tools/general.js b/src/server/tools/general.js index b322e7e..fa1ef41 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -574,13 +574,17 @@ module.exports = { 'type', 'userId', 'sender', + 'typedir', + 'typeid', 'dest', 'descr', - 'link', + 'openUrl', 'datenotif', 'read', 'deleted', 'idapp', + 'tablerec', + 'idrec', 'status']; }, @@ -723,7 +727,7 @@ module.exports = { }; // Pass object into sendNotification - return webpush.sendNotification(subscription, JSON.stringify(payload), + return webpush.sendNotification(subscription._doc, JSON.stringify(payload), pushOptions). catch(err => { if (err.statusCode === 410) { @@ -734,7 +738,7 @@ module.exports = { } }). then(ris => { - // console.log('ris', ris) + console.log('sendNotification', ris) }). catch(err => { console.error(err); @@ -1141,8 +1145,7 @@ module.exports = { getNomeAppByIdApp: function(idapp) { - const myapp = - this.getApps().find(item => item.idapp === idapp); + const myapp = this.MYAPPS.find(item => item.idapp === idapp); if (myapp) return ((process.env.NODE_ENV === 'test') ? 'Test: ' : '') + myapp.name; else @@ -1161,7 +1164,7 @@ module.exports = { getHostByIdApp: function(idapp) { - const myapp = this.getApps().find(item => item.idapp === idapp); + const myapp = this.MYAPPS.find(item => item.idapp === idapp); if (myapp) { let siteport = (myapp.portapp && myapp.portapp !== '0') ? (':' + myapp.portapp) : ''; @@ -1185,7 +1188,7 @@ module.exports = { getConfSiteOptionEnabledByIdApp: function(idapp, option) { - const myapp = this.getApps().find(item => item.idapp === idapp); + const myapp = this.MYAPPS.find(item => item.idapp === idapp); if (myapp) { if (myapp.hasOwnProperty('confsite')) { @@ -1200,7 +1203,7 @@ module.exports = { getConfParamSiteByIdApp: function(idapp, field) { - const myapp = this.getApps().find(item => item.idapp === idapp); + const myapp = this.MYAPPS.find(item => item.idapp === idapp); if (myapp) { if (myapp.hasOwnProperty('confsite')) { @@ -1214,7 +1217,7 @@ module.exports = { }, isAbilitaNave: function(idapp) { - const myapp = this.getApps().find(item => item.idapp === idapp); + const myapp = this.MYAPPS.find(item => item.idapp === idapp); return myapp.abilitanave; }, @@ -1222,7 +1225,7 @@ module.exports = { let mypath = ''; const myapp = - this.getApps().find(item => item.idapp === idapp); + this.MYAPPS.find(item => item.idapp === idapp); if (myapp) { if (process.env.NODE_ENV === 'test') mypath = (myapp) ? myapp.dir_test : ''; @@ -1240,7 +1243,7 @@ module.exports = { }, getAdminEmailByIdApp: function(idapp) { - const myapp = this.getApps().find((item) => item.idapp === idapp); + const myapp = this.MYAPPS.find((item) => item.idapp === idapp); if (myapp) return myapp.adminemail; else @@ -1248,7 +1251,7 @@ module.exports = { }, getreplyToEmailByIdApp: function(idapp) { - const myapp = this.getApps().find((item) => item.idapp === idapp); + const myapp = this.MYAPPS.find((item) => item.idapp === idapp); if (myapp) return myapp.replyTo; else @@ -1256,7 +1259,7 @@ module.exports = { }, getpathregByIdApp: function(idapp, lang) { - const myapp = this.getApps().find((item) => item.idapp === idapp); + const myapp = this.MYAPPS.find((item) => item.idapp === idapp); if (myapp) { const addstr = myapp.pathreg_add ? myapp.pathreg_add : ''; @@ -1276,7 +1279,7 @@ module.exports = { }, getManagerEmailByIdApp: function(idapp) { - const myapp = this.getApps().find((item) => item.idapp === idapp); + const myapp = this.MYAPPS.find((item) => item.idapp === idapp); if (myapp) return !!myapp.manageremail ? myapp.manageremail : ''; else @@ -1284,17 +1287,17 @@ module.exports = { }, getEmailByIdApp: function(idapp) { - const myapp = this.getApps().find((item) => item.idapp === idapp); + const myapp = this.MYAPPS.find((item) => item.idapp === idapp); return (myapp) ? myapp.email_from : ''; }, getPwdByIdApp: function(idapp) { - const myapp = this.getApps().find((item) => item.idapp === idapp); + const myapp = this.MYAPPS.find((item) => item.idapp === idapp); return (myapp) ? this.decryptdata(myapp.email_pwd) : ''; }, getTelegramBotNameByIdApp: function(idapp) { - const myapp = this.getApps().find((item) => item.idapp === idapp); + const myapp = this.MYAPPS.find((item) => item.idapp === idapp); if (process.env.NODE_ENV === 'test') return (myapp) ? myapp.telegram_bot_name_test : ''; else @@ -1303,7 +1306,7 @@ module.exports = { getTelegramSupportChat: function(idapp) { try { - const myapp = this.getApps().find((item) => item.idapp === idapp); + const myapp = this.MYAPPS.find((item) => item.idapp === idapp); return (myapp && myapp.telegram_support_chat) ? myapp.telegram_support_chat : ''; @@ -1313,8 +1316,7 @@ module.exports = { }, getTelegramKeyByIdApp: function(idapp) { - const myarr = this.getApps(); - const myapp = myarr.find((item) => item.idapp === idapp); + const myapp = this.MYAPPS.find((item) => item.idapp === idapp); if (process.env.NODE_ENV === 'test') return (myapp) ? myapp.telegram_key_test : ''; else @@ -2750,12 +2752,16 @@ module.exports = { }, async loadApps() { - this.MYAPPS = await Site.findAll(0); - // console.log('this.MYAPPS', this.MYAPPS); + try { + this.MYAPPS = await Site.findAll(0); + console.log('this.MYAPPS', this.MYAPPS); + }catch (e) { + console.error('loadApps', e); + } }, - getApps() { + async getApps() { if (this.MYAPPS.length <= 0) - this.loadApps(); + await this.loadApps(); return this.MYAPPS; }, @@ -2936,6 +2942,8 @@ module.exports = { }, updateQueryStringParameter(uri, key, value) { + if (uri === '') + return ''; var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i"); var separator = uri.indexOf('?') !== -1 ? "&" : "?"; if (uri.match(re)) { diff --git a/src/server/tools/globalTables.js b/src/server/tools/globalTables.js index b7a4a6c..a00c9ca 100755 --- a/src/server/tools/globalTables.js +++ b/src/server/tools/globalTables.js @@ -74,7 +74,6 @@ const tools = require('./general'); const shared_consts = require('./shared_nodejs'); -const sendemail = require('../sendemail'); module.exports = { @@ -217,46 +216,60 @@ module.exports = { return process.env.ENABLE_PUSHNOTIFICATION === '1'; }, - async sendNotif(res, idapp, user, recnotif, cmd) { + async sendNotif(typenotif, idnotif, res, idapp, user, recnotif, cmd) { // Controlla nelle impostazioni che tipo di Notifica visualizzare - if (tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByPushNotification)) { - if (this.checkifSendPushNotification) { - console.log('SEND PUSH NOTIFICATION '); + const sendemail = require('../sendemail'); - const params = { - sendreally: true, - typesend: shared_consts.TypeSend.PUSH_NOTIFICATION, - title: this.getNomeAppByIdApp(idapp), - content: recnotif.descr ? recnotif.descr : tools.getContentByTypeMsg(recnotif.typemsg), - openUrl: tools.updateQueryStringParameter(recnotif.link, 'idnotif', recnotif._id), - typemsg: recnotif.typemsg ? recnotif.typemsg : shared_consts.TypeMsg.SEND_TO_USER, - } - - ris = await this.SendMsgToParam(idapp, params); - - recnotif.link = mylink; + try { + if (!recnotif.options) { + recnotif.options = shared_consts.MessageOptions.Notify_ByPushNotification; } + + if (tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByPushNotification)) { + if (this.checkifSendPushNotification) { + + const params = { + sendreally: true, + typesend: shared_consts.TypeSend.PUSH_NOTIFICATION + shared_consts.TypeSend.TELEGRAM, + title: tools.getNomeAppByIdApp(idapp), + content: recnotif.descr ? recnotif.descr : tools.getContentByTypeMsg(recnotif.typemsg), + openUrl: tools.updateQueryStringParameter(recnotif.openUrl, 'idnotif', recnotif._id), + typemsg: recnotif.typemsg ? recnotif.typemsg : shared_consts.TypeMsg.SEND_TO_USER, + typenotif, + idnotif, + usernameDest: recnotif.usernameDest ? recnotif.usernameDest : recnotif.dest, + }; + + ris = await this.SendMsgToParam(idapp, params); + + } + } + + // Send Msg by EMAIL + if (tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByEmail)) { + // Read from the operator table first + let emaildest = await Operator.getEmailByUsername(recnotif.dest.idapp, recnotif.dest.username); + if (!emaildest) + emaildest = await User.getEmailByUsername(recnotif.dest.idapp, recnotif.dest.username); + + console.log('emaildest', emaildest); + + await sendemail.sendEmail_Msg(res, user.lang, emaildest, user, idapp, recnotif); + } + + return true; + } catch (e) { + console.error('sendNotif', e, typenotif, recnotif); + return false; } - - // Send Msg by EMAIL - if (emaildest && tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByEmail)) { - // Read from the operator table first - let emaildest = await Operator.getEmailByUsername(recnotif.dest.idapp, recnotif.dest.username); - if (!emaildest) - emaildest = await User.getEmailByUsername(recnotif.dest.idapp, recnotif.dest.username); - - console.log('emaildest', emaildest); - - await sendemail.sendEmail_Msg(res, user.lang, emaildest, user, idapp, recnotif); - } - - return true; }, SendMsgToParam: async function(idapp, params) { try { + console.log('SendMsgToParam'); + const telegrambot = require('../telegram/telegrambot'); if (params.typesend === 0) @@ -343,7 +356,8 @@ module.exports = { console.error(e.message); }); - } else if (tools.isBitActive(params.typesend, shared_consts.TypeSend.TELEGRAM)) { + } + if (tools.isBitActive(params.typesend, shared_consts.TypeSend.TELEGRAM)) { const telegid = user.profile.teleg_id; if (telegid > 0) { diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index b0f94c8..f83d8f0 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -108,6 +108,9 @@ module.exports = { TABLES_VISU_STAT_IN_HOME: ['myskills', 'mybachecas', 'myhosps', 'mygoods', 'mygroups'], + TABLES_ADV_NOTIFICATION: ['myskills', 'myhosps', 'mygoods'], + TABLES_EVENTS_NOTIFICATION: ['mybachecas'], + TABLES_ID_NUMBER: [ 'permissions', 'levels', @@ -273,8 +276,38 @@ module.exports = { PUSH_NOTIFICATION: 1, TELEGRAM: 2, }, + UsersNotif: { + NEW_ADV_CITY: 1, + NEW_ADV_PROVINCE: 2, + NEW_ADV_REGION: 4, + NEW_ADV_MY_GROUPS: 8, + NEW_ADV_MY_RIS_CIRCUIT: 16, + NEW_ADV_SECTOR: 32, + }, + + TypeNotifs: { + TYPEDIR_BACHECA: 1, + ID_BACHECA_NEW_GOOD: 1, + ID_BACHECA_NEW_SERVICE: 2, + + TYPEDIR_EVENTS: 2, + ID_EVENTS_NEW_REC: 1, + ID_EVENTS_REMOVE_REC: 2, + + TYPEDIR_FRIENDS: 3, + ID_FRIENDS_NEW_REC: 1, + + TYPEDIR_CIRCUITS: 4, + + TYPEDIR_BOOKING: 5, + + TYPEDIR_MSGS: 6, + ID_MSGS_NEW_REC: 1, + }, + // Tipi di Notifiche: + /* Notif: { UPDATE_APP: 1, NEW_GOOD_MY_PROVINCE: 12, @@ -292,6 +325,8 @@ module.exports = { RIS_SENT_REFUSED: 52, }, + */ + fieldsUserToChange() { return [