- ++Booking List

- ++Delete a Booking also for the Admin.
This commit is contained in:
Paolo Arena
2019-10-12 23:34:32 +02:00
parent 624f929c56
commit cebe1208de
10 changed files with 290 additions and 17 deletions

View File

@@ -27,18 +27,19 @@ function sendNotif(res, idapp, user, recbooking) {
router.post('/', authenticate, (req, res) => {
tools.mylog('INIZIO - booking');
// tools.mylog('req.body', req.body);
const body = _.pick(req.body, tools.allfieldBooking());
const id = body.id_bookedevent;
const fieldtochange = _.pick(req.body, tools.allfieldBookingChange());
const myrec = _.pick(req.body, tools.allfieldBooking());
const id = myrec._id;
const fieldtochange = _.pick(myrec, tools.allfieldBookingChange());
tools.mylog('crea Booking');
const booking = new Booking(body);
const booking = new Booking(myrec);
const check = tools.checkUserOk(booking.userId, req.user._id);
if (check.exit) return check.ret;
// console.log('fieldtochange', fieldtochange);
// Modify:
return Booking.findOne({ id_bookedevent: id })
.then(trovato => {
// console.log('trovato', trovato);
@@ -49,7 +50,7 @@ router.post('/', authenticate, (req, res) => {
}).then((recbooking) => {
// tools.mylog('booking:', booking);
// tools.mylog('already exist');
sendNotif(res, body.idapp, req.user, recbooking);
sendNotif(res, myrec.idapp, req.user, recbooking);
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._id });
});
} else {
@@ -61,19 +62,42 @@ router.post('/', authenticate, (req, res) => {
Booking.findById(idobj)
.then((recbooking) => {
sendNotif(res, body.idapp, req.user, recbooking);
sendNotif(res, myrec.idapp, req.user, recbooking);
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._id });
});
});
}
})
});
router.get('/:userId/:idapp', authenticate, (req, res) => {
router.delete('/:id/:notify/:idapp', authenticate, (req, res) => {
console.log('DELETE Booking');
const id = req.params.id;
const notify = req.params.notify;
const idapp = req.params.idapp;
Booking.findByIdAndRemove(id).then((recbooking) => {
if (!recbooking) {
return res.status(404).send();
}
if (notify === '1')
sendNotif(res, idapp, req.user, recbooking);
tools.mylog('DELETED ', recbooking.descr, recbooking._id);
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._id });
}).catch((e) => {
res.status(400).send();
});
});
router.get('/:userId/:idapp/:sall', authenticate, (req, res) => {
const userId = req.params.userId;
const idapp = req.params.idapp;
const sall = req.params.sall;
// var category = req.params.category;
tools.mylog('GET BOOKINGS : ', req.params);
@@ -88,7 +112,7 @@ router.get('/:userId/:idapp', authenticate, (req, res) => {
}
// Extract all the todos of the userId only
Booking.findAllByUserIdAndIdApp(userId, idapp).then((bookedevent) => {
Booking.findAllByUserIdAndIdApp(userId, idapp, sall).then((bookedevent) => {
res.send({ bookedevent });
}).catch((e) => {
console.log(e);

View File

@@ -0,0 +1,78 @@
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;

View File

@@ -32,8 +32,8 @@ function existSubScribe(userId, access, browser) {
// POST /users
router.post('/', (req, res) => {
tools.mylog("POST /users");
var body = _.pick(req.body, ['email', 'password', 'username', 'name', 'surname', 'idapp', 'keyappid', 'lang']);
var user = new User(body);
const body = _.pick(req.body, ['email', 'password', 'username', 'name', 'surname', 'idapp', 'keyappid', 'lang']);
const user = new User(body);
// tools.mylog("LANG PASSATO = " + user.lang, "IDAPP", user.idapp);