79 lines
2.0 KiB
JavaScript
79 lines
2.0 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
|
|
const tools = require('../tools/general');
|
|
const server_constants = require('../tools/server_constants');
|
|
|
|
const { authenticate } = require('../middleware/authenticate');
|
|
|
|
const { Sendmsg } = require('../models/Sendmsg');
|
|
|
|
const { ObjectID } = require('mongodb');
|
|
|
|
const sendemail = require('../sendemail');
|
|
|
|
const _ = require('lodash');
|
|
|
|
function sendNotif(res, idapp, user, recmsg) {
|
|
//++Todo: tools.sendNotificationToUser
|
|
|
|
// Send Msg
|
|
sendemail.sendEmail_Msg(res, user.lang, user.email, user, idapp, recmsg);
|
|
}
|
|
|
|
router.post('/', authenticate, (req, res) => {
|
|
tools.mylog('INIZIO - Sendmsg');
|
|
// tools.mylog('req.body', req.body);
|
|
const body = _.pick(req.body, tools.allfieldSendmsg());
|
|
|
|
tools.mylog('crea Sendmsg');
|
|
const Sendmsg = new Sendmsg(body);
|
|
|
|
const check = tools.checkUserOk(Sendmsg.userId, req.user._id);
|
|
if (check.exit) return check.ret;
|
|
|
|
// console.log('fieldtochange', fieldtochange);
|
|
|
|
Sendmsg._id = new ObjectID();
|
|
return Sendmsg.save().then((writeresult) => {
|
|
let idobj = writeresult._id;
|
|
|
|
Sendmsg.findById(idobj)
|
|
.then((recmsg) => {
|
|
sendNotif(res, body.idapp, req.user, recmsg);
|
|
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmsg._id });
|
|
});
|
|
});
|
|
|
|
|
|
});
|
|
|
|
router.get('/:userId/:idapp', authenticate, (req, res) => {
|
|
const userId = req.params.userId;
|
|
const idapp = req.params.idapp;
|
|
// var category = req.params.category;
|
|
|
|
tools.mylog('GET SendmsgS : ', req.params);
|
|
|
|
if (!ObjectID.isValid(userId)) {
|
|
return res.status(404).send();
|
|
}
|
|
|
|
if (userId !== String(req.user._id)) {
|
|
// I'm trying to write something not mine!
|
|
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
|
|
}
|
|
|
|
// Extract all the todos of the userId only
|
|
Sendmsg.findAllByUserIdAndIdApp(userId, idapp).then((msgall) => {
|
|
res.send({ msgall });
|
|
}).catch((e) => {
|
|
console.log(e);
|
|
res.status(400).send(e);
|
|
});
|
|
|
|
});
|
|
|
|
|
|
module.exports = router;
|