Notifiche - Tutti - non letti

This commit is contained in:
Paolo Arena
2022-07-21 00:21:03 +02:00
parent cbd65ccdbe
commit e9ce597027
10 changed files with 235 additions and 177 deletions

View File

@@ -4,13 +4,13 @@ const router = express.Router();
const tools = require('../tools/general');
const server_constants = require('../tools/server_constants');
const { authenticate } = require('../middleware/authenticate');
const {authenticate} = require('../middleware/authenticate');
const { User } = require('../models/user');
const { Operator } = require('../models/operator');
const { SendNotif } = require('../models/sendnotif');
const {User} = require('../models/user');
const {Operator} = require('../models/operator');
const {SendNotif} = require('../models/sendnotif');
const { ObjectID } = require('mongodb');
const {ObjectID} = require('mongodb');
const sendemail = require('../sendemail');
@@ -18,33 +18,6 @@ const shared_consts = require('../tools/shared_nodejs');
const _ = require('lodash');
async function sendNotif(res, idapp, user, recnotif) {
// 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 ')
//++Todo: tools.sendNotificationToUser
}
}
// 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);
// Send Msg by EMAIL
if (emaildest && tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByEmail))
await sendemail.sendEmail_Msg(res, user.lang, emaildest, user, idapp, recnotif);
return true
}
router.post('/', authenticate, (req, res) => {
tools.mylog('INIZIO - SendNotif');
// tools.mylog('req.body', req.body);
@@ -59,25 +32,45 @@ router.post('/', authenticate, (req, res) => {
// console.log('fieldtochange', fieldtochange);
myrecnotif._id = new ObjectID();
return myrecnotif.save()
.then((writeresult) => {
let idobj = writeresult._id;
return myrecnotif.save().then((writeresult) => {
let idobj = writeresult._id;
myrecnotif._id = idobj;
myrecnotif._id = idobj;
return SendNotif.findById(idobj)
.then(async (recnotif) => {
// Add this field because I don't want to add into the database
return SendNotif.findById(idobj).then(async (recnotif) => {
// Add this field because I don't want to add into the database
return await 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);
return res.send({ code: server_constants.RIS_CODE_ERR, notif: '' });
})
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);
return res.send({code: server_constants.RIS_CODE_ERR, notif: ''});
});
});
router.get('/setall/: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 arrNotifs = await SendNotif.find({idapp, dest: username, read: false}).lean();
if (arrNotifs) {
for (const rec of arrNotifs) {
await SendNotif.setNotifAsRead(idapp, username_call, rec._id);
}
}
res.send(true);
}
} catch (e) {
res.status(400).send(e);
}
});
@@ -90,15 +83,14 @@ router.get('/:username/:lastdataread/:idapp', authenticate, (req, res) => {
if (req.user.idapp !== idapp) {
// I'm trying to get something not mine!
return res.status(404).send({ code: server_constants.RIS_CODE_NOT_MY_USERNAME });
return res.status(404).send({code: server_constants.RIS_CODE_NOT_MY_USERNAME});
}
// Extract all the todos of the userId only
return SendNotif.findAllNotifByUsernameIdAndIdApp(username, lastdataread, idapp).then((arrnotif) => {
// const wait = new Promise((resolve, reject) => {
// setTimeout(() => {
res.send({ arrnotif });
res.send({arrnotif});
// }, 2000);
// });
@@ -109,5 +101,4 @@ router.get('/:username/:lastdataread/:idapp', authenticate, (req, res) => {
});
module.exports = router;