- fix RIS in pendenti, se troppi msg, non compariva piu

- cataloghi, ricerca pickup
This commit is contained in:
Surya Paolo
2024-05-09 23:36:46 +02:00
parent 54443e784e
commit 3b1b2b9c83
13 changed files with 296 additions and 94 deletions

View File

@@ -4,10 +4,10 @@ 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 {SendNotif} = require('../models/sendnotif');
const {User} = require('../models/user');
const { SendNotif } = require('../models/sendnotif');
const { User } = require('../models/user');
const shared_consts = require('../tools/shared_nodejs');
@@ -23,9 +23,9 @@ router.post('/', authenticate, async (req, res) => {
const recout = await SendNotif.saveAndSendNotif(myrecnotif, req, res);
if (recout) {
return res.send({code: server_constants.RIS_CODE_OK, notif: '', record: recout});
return res.send({ code: server_constants.RIS_CODE_OK, notif: '', record: recout });
} else {
return res.send({code: server_constants.RIS_CODE_ERR, notif: ''});
return res.send({ code: server_constants.RIS_CODE_ERR, notif: '' });
}
});
@@ -39,12 +39,12 @@ router.get('/setall/:username/:qualinotif/:idapp', authenticate, async (req, res
try {
if (username === username_call) {
let query = {idapp, dest: username, read: false};
let query = { idapp, dest: username, read: false };
if (qualinotif === shared_consts.QualiNotifs.CIRCUITS) {
query.typedir = {$eq: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS};
query.typedir = { $eq: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS };
} else if (qualinotif === shared_consts.QualiNotifs.OTHERS) {
query.typedir = {$neq: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS};
query.typedir = { $ne: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS };
}
const arrNotifs = await SendNotif.find(query).lean();
if (arrNotifs) {
@@ -67,7 +67,7 @@ router.get('/set/:_id/:idapp', authenticate, async (req, res) => {
const username_call = req.user.username;
try {
let query = {_id, dest: username_call, read: false};
let query = { _id, dest: username_call, read: false };
const rec = await SendNotif.findOne(query);
if (rec) {
@@ -83,17 +83,13 @@ router.get('/set/:_id/: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;
async function delNotif(idapp, username, id, username_call) {
try {
if (username === username_call) {
await SendNotif.findOneAndRemove({idapp, _id: myid});
await SendNotif.findOneAndRemove({ idapp, _id: id });
return res.send(true);
}
} catch (e) {
@@ -102,8 +98,19 @@ router.get('/del/:username/:id/:idapp', authenticate, async (req, res) => {
return res.send(false);
};
router.get('/del/:username/:id/:idapp', authenticate, async (req, res) => {
try {
return delNotif(req.params.idapp, req.params.username, req.params.id, req.user.username);
} catch (e) {
return res.status(400).send(e);
}
});
router.get('/delall/:username/:qualinotif/:idapp', authenticate, async (req, res) => {
const idapp = req.params.idapp;
@@ -113,12 +120,12 @@ router.get('/delall/:username/:qualinotif/:idapp', authenticate, async (req, res
try {
if (username === username_call) {
let query = {idapp, dest: username};
let query = { idapp, dest: username };
if (qualinotif === shared_consts.QualiNotifs.CIRCUITS) {
query.typedir = {$eq: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS};
query.typedir = { $eq: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS };
} else if (qualinotif === shared_consts.QualiNotifs.OTHERS) {
query.typedir = {$neq: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS};
query.typedir = { $ne: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS };
}
const ris = await SendNotif.deleteMany(query);
if (ris)
@@ -133,31 +140,45 @@ router.get('/delall/:username/:qualinotif/:idapp', authenticate, async (req, res
});
router.get('/:username/:lastdataread/:idapp', authenticate, (req, res) => {
// tools.mylog('GET NotifS : ', req.params);
const username = req.params.username;
const lastdataread = req.params.lastdataread;
const idapp = req.params.idapp;
// var category = req.params.category;
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 SendNotif.findAllNotifByUsernameIdAndIdApp(username, lastdataread, idapp, shared_consts.LIMIT_NOTIF_FOR_USER).then(async (arrnotif) => {
// const wait = new Promise((resolve, reject) => {
// setTimeout(() => {
//++Todo: Ottimizzare ! Non occorre inviare tutti questi dati !!! Solo per il Circuito ?!
const userprofile = await User.getExtraInfoByUsername(idapp, req.user.username);
return res.send({arrnotif, userprofile});
}).catch((e) => {
console.log(e.message);
res.status(400).send(e);
});
return getNotif(req, res);
});
router.get('/:username/:lastdataread/:idapp/:qualinotif', authenticate, (req, res) => {
return getNotif(req, res);
});
function getNotif(req, res) {
try {
// tools.mylog('GET NotifS : ', req.params);
const username = req.params.username;
const lastdataread = req.params.lastdataread;
const idapp = req.params.idapp;
const qualinotif = req.params.qualinotif;
// var category = req.params.category;
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 SendNotif.findAllNotifByUsernameIdAndIdApp(username, lastdataread, idapp, shared_consts.LIMIT_NOTIF_FOR_USER, qualinotif).then(async (arrnotif) => {
// const wait = new Promise((resolve, reject) => {
// setTimeout(() => {
//++Todo: Ottimizzare ! Non occorre inviare tutti questi dati !!! Solo per il Circuito ?!
const userprofile = await User.getExtraInfoByUsername(idapp, req.user.username);
return res.send({ arrnotif, userprofile });
}).catch((e) => {
console.log(e.message);
res.status(400).send(e);
});
} catch (e) {
console.log(e.message);
}
};
module.exports = router;