2022-07-10 01:25:19 +02:00
|
|
|
const express = require('express');
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
const server_constants = require('../tools/server_constants');
|
|
|
|
|
|
2024-05-09 23:36:46 +02:00
|
|
|
const { authenticate } = require('../middleware/authenticate');
|
2022-07-10 01:25:19 +02:00
|
|
|
|
2024-05-09 23:36:46 +02:00
|
|
|
const { SendNotif } = require('../models/sendnotif');
|
|
|
|
|
const { User } = require('../models/user');
|
2022-09-16 17:38:49 +02:00
|
|
|
|
|
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
2022-07-10 01:25:19 +02:00
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
2022-07-23 17:48:33 +02:00
|
|
|
router.post('/', authenticate, async (req, res) => {
|
2022-07-10 01:25:19 +02:00
|
|
|
tools.mylog('INIZIO - SendNotif');
|
|
|
|
|
// tools.mylog('req.body', req.body);
|
|
|
|
|
const body = _.pick(req.body, tools.allfieldSendNotif());
|
|
|
|
|
|
|
|
|
|
tools.mylog('crea SendNotif');
|
2022-07-23 17:48:33 +02:00
|
|
|
let myrecnotif = new SendNotif(body);
|
2022-07-10 01:25:19 +02:00
|
|
|
|
2022-07-23 17:48:33 +02:00
|
|
|
const recout = await SendNotif.saveAndSendNotif(myrecnotif, req, res);
|
|
|
|
|
if (recout) {
|
2024-05-09 23:36:46 +02:00
|
|
|
return res.send({ code: server_constants.RIS_CODE_OK, notif: '', record: recout });
|
2022-07-23 17:48:33 +02:00
|
|
|
} else {
|
2024-05-09 23:36:46 +02:00
|
|
|
return res.send({ code: server_constants.RIS_CODE_ERR, notif: '' });
|
2022-07-23 17:48:33 +02:00
|
|
|
}
|
2022-07-21 00:21:03 +02:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-16 17:38:49 +02:00
|
|
|
router.get('/setall/:username/:qualinotif/:idapp', authenticate, async (req, res) => {
|
2022-07-21 00:21:03 +02:00
|
|
|
|
|
|
|
|
const idapp = req.params.idapp;
|
|
|
|
|
const username = req.params.username;
|
2024-05-10 00:58:38 +02:00
|
|
|
const qualinotif = parseInt(req.params.qualinotif);
|
2022-07-21 00:21:03 +02:00
|
|
|
const username_call = req.user.username;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (username === username_call) {
|
2024-05-09 23:36:46 +02:00
|
|
|
let query = { idapp, dest: username, read: false };
|
2022-09-16 17:38:49 +02:00
|
|
|
|
|
|
|
|
if (qualinotif === shared_consts.QualiNotifs.CIRCUITS) {
|
2024-05-09 23:36:46 +02:00
|
|
|
query.typedir = { $eq: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS };
|
2022-09-16 17:38:49 +02:00
|
|
|
} else if (qualinotif === shared_consts.QualiNotifs.OTHERS) {
|
2024-05-09 23:36:46 +02:00
|
|
|
query.typedir = { $ne: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS };
|
2022-09-16 17:38:49 +02:00
|
|
|
}
|
|
|
|
|
const arrNotifs = await SendNotif.find(query).lean();
|
2022-07-21 00:21:03 +02:00
|
|
|
if (arrNotifs) {
|
|
|
|
|
for (const rec of arrNotifs) {
|
|
|
|
|
await SendNotif.setNotifAsRead(idapp, username_call, rec._id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-16 17:38:49 +02:00
|
|
|
|
2022-07-21 00:21:03 +02:00
|
|
|
res.send(true);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
res.status(400).send(e);
|
|
|
|
|
}
|
2022-07-10 01:25:19 +02:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-18 20:17:24 +02:00
|
|
|
router.get('/set/:_id/:idapp', authenticate, async (req, res) => {
|
|
|
|
|
|
|
|
|
|
const _id = req.params._id;
|
|
|
|
|
const username_call = req.user.username;
|
|
|
|
|
|
|
|
|
|
try {
|
2024-05-09 23:36:46 +02:00
|
|
|
let query = { _id, dest: username_call, read: false };
|
2022-09-18 20:17:24 +02:00
|
|
|
|
|
|
|
|
const rec = await SendNotif.findOne(query);
|
|
|
|
|
if (rec) {
|
|
|
|
|
rec.read = true;
|
|
|
|
|
await rec.save();
|
|
|
|
|
return res.send(true);
|
|
|
|
|
}
|
|
|
|
|
res.send(false);
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
res.status(400).send(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-23 17:48:33 +02:00
|
|
|
|
2024-10-29 02:33:29 +01:00
|
|
|
async function delNotif(res, idapp, username, id, username_call) {
|
2022-07-23 17:48:33 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (username === username_call) {
|
2022-09-16 17:38:49 +02:00
|
|
|
|
2024-05-09 23:36:46 +02:00
|
|
|
await SendNotif.findOneAndRemove({ idapp, _id: id });
|
2022-07-23 17:48:33 +02:00
|
|
|
return res.send(true);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return res.status(400).send(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.send(false);
|
|
|
|
|
|
2024-05-09 23:36:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
router.get('/del/:username/:id/:idapp', authenticate, async (req, res) => {
|
|
|
|
|
|
|
|
|
|
try {
|
2024-10-29 02:33:29 +01:00
|
|
|
return delNotif(res, req.params.idapp, req.params.username, req.params.id, req.user.username);
|
2024-05-09 23:36:46 +02:00
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return res.status(400).send(e);
|
|
|
|
|
}
|
2022-07-23 17:48:33 +02:00
|
|
|
});
|
|
|
|
|
|
2024-05-09 23:36:46 +02:00
|
|
|
|
2022-09-16 17:38:49 +02:00
|
|
|
router.get('/delall/:username/:qualinotif/:idapp', authenticate, async (req, res) => {
|
2022-07-23 17:48:33 +02:00
|
|
|
|
|
|
|
|
const idapp = req.params.idapp;
|
|
|
|
|
const username = req.params.username;
|
2024-05-10 00:58:38 +02:00
|
|
|
const qualinotif = parseInt(req.params.qualinotif);
|
2022-07-23 17:48:33 +02:00
|
|
|
const username_call = req.user.username;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (username === username_call) {
|
2024-05-09 23:36:46 +02:00
|
|
|
let query = { idapp, dest: username };
|
2022-09-16 17:38:49 +02:00
|
|
|
|
|
|
|
|
if (qualinotif === shared_consts.QualiNotifs.CIRCUITS) {
|
2024-05-09 23:36:46 +02:00
|
|
|
query.typedir = { $eq: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS };
|
2022-09-16 17:38:49 +02:00
|
|
|
} else if (qualinotif === shared_consts.QualiNotifs.OTHERS) {
|
2024-05-09 23:36:46 +02:00
|
|
|
query.typedir = { $ne: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS };
|
2022-09-16 17:38:49 +02:00
|
|
|
}
|
|
|
|
|
const ris = await SendNotif.deleteMany(query);
|
2022-07-23 17:48:33 +02:00
|
|
|
if (ris)
|
|
|
|
|
return res.send(true);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return res.status(400).send(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.send(false);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-10 01:25:19 +02:00
|
|
|
router.get('/:username/:lastdataread/:idapp', authenticate, (req, res) => {
|
2024-05-09 23:36:46 +02:00
|
|
|
return getNotif(req, res);
|
|
|
|
|
});
|
2022-07-10 01:25:19 +02:00
|
|
|
|
2024-05-10 00:58:38 +02:00
|
|
|
async function getNotif(req, res) {
|
2022-09-16 17:38:49 +02:00
|
|
|
|
2024-05-09 23:36:46 +02:00
|
|
|
try {
|
|
|
|
|
// tools.mylog('GET NotifS : ', req.params);
|
|
|
|
|
const username = req.params.username;
|
|
|
|
|
const lastdataread = req.params.lastdataread;
|
|
|
|
|
const idapp = req.params.idapp;
|
2022-09-16 17:38:49 +02:00
|
|
|
|
2024-05-09 23:36:46 +02:00
|
|
|
// var category = req.params.category;
|
2022-07-10 01:25:19 +02:00
|
|
|
|
2024-05-09 23:36:46 +02:00
|
|
|
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 });
|
|
|
|
|
}
|
2022-07-10 01:25:19 +02:00
|
|
|
|
2024-05-10 00:58:38 +02:00
|
|
|
const arrnotif = await SendNotif.findAllNotifByUsernameIdAndIdApp(username, lastdataread, idapp, shared_consts.LIMIT_NOTIF_FOR_USER, shared_consts.QualiNotifs.OTHERS);
|
2024-09-29 23:26:37 +02:00
|
|
|
let arrnotifcoins_inattesa = null;
|
|
|
|
|
|
|
|
|
|
if (await User.isAdminByUsername(idapp, req.user.username)) {
|
|
|
|
|
arrnotifcoins_inattesa = await SendNotif.findAllNotifCoinsAllIdAndIdApp(idapp);
|
|
|
|
|
} else {
|
|
|
|
|
arrnotifcoins_inattesa = await SendNotif.findAllNotifByUsernameIdAndIdApp(username, lastdataread, idapp, shared_consts.LIMIT_NOTIFCOINS_IN_ATTESA_FOR_USER, shared_consts.QualiNotifs.CIRCUITS, [{ status: 0 }]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 01:08:49 +02:00
|
|
|
const arrnotifcoins = await SendNotif.findAllNotifByUsernameIdAndIdApp(username, lastdataread, idapp, shared_consts.LIMIT_NOTIFCOINS_FOR_USER, shared_consts.QualiNotifs.CIRCUITS, [{ status: {$ne: 0 }}]);
|
|
|
|
|
|
2024-05-09 23:36:46 +02:00
|
|
|
|
2024-05-10 00:58:38 +02:00
|
|
|
//++Todo: Ottimizzare ! Non occorre inviare tutti questi dati !!! Solo per il Circuito ?!
|
|
|
|
|
const userprofile = await User.getExtraInfoByUsername(idapp, req.user.username);
|
2024-05-09 23:36:46 +02:00
|
|
|
|
2024-05-10 01:08:49 +02:00
|
|
|
return res.send({ arrnotif, arrnotifcoins: [...arrnotifcoins, ...arrnotifcoins_inattesa], userprofile });
|
2024-05-09 23:36:46 +02:00
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e.message);
|
2024-05-10 00:58:38 +02:00
|
|
|
res.status(400).send(e);
|
2024-05-09 23:36:46 +02:00
|
|
|
}
|
|
|
|
|
};
|
2022-07-10 01:25:19 +02:00
|
|
|
|
|
|
|
|
module.exports = router;
|