Files
freeplanet_serverside/src/server/router/sendnotif_router.js

119 lines
3.1 KiB
JavaScript
Raw Normal View History

const express = require('express');
const router = express.Router();
const tools = require('../tools/general');
const server_constants = require('../tools/server_constants');
2022-07-21 00:21:03 +02:00
const {authenticate} = require('../middleware/authenticate');
2022-07-21 00:21:03 +02:00
const {SendNotif} = require('../models/sendnotif');
const _ = require('lodash');
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');
let myrecnotif = new SendNotif(body);
const recout = await SendNotif.saveAndSendNotif(myrecnotif, req, res);
if (recout) {
return res.send({code: server_constants.RIS_CODE_OK, notif: '', record: recout});
} else {
2022-07-21 00:21:03 +02:00
return res.send({code: server_constants.RIS_CODE_ERR, notif: ''});
}
2022-07-21 00:21:03 +02:00
});
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);
}
});
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;
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!
2022-07-21 00:21:03 +02:00
return res.status(404).send({code: server_constants.RIS_CODE_NOT_MY_USERNAME});
}
return SendNotif.findAllNotifByUsernameIdAndIdApp(username, lastdataread, idapp).then((arrnotif) => {
// const wait = new Promise((resolve, reject) => {
// setTimeout(() => {
return res.send({arrnotif});
// }, 2000);
// });
}).catch((e) => {
console.log(e.message);
res.status(400).send(e);
});
});
module.exports = router;