102 lines
3.0 KiB
JavaScript
102 lines
3.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 { Booking } = require('../models/booking');
|
|
|
|
const { ObjectID } = require('mongodb');
|
|
|
|
const sendemail = require('../sendemail');
|
|
|
|
const _ = require('lodash');
|
|
|
|
function sendNotif(res, idapp, user, recbooking) {
|
|
//++Todo: tools.sendNotificationToUser
|
|
|
|
// Send Email
|
|
if (recbooking.booked)
|
|
sendemail.sendEmail_Booking(res, user.lang, user.email, user, idapp, recbooking);
|
|
else
|
|
sendemail.sendEmail_CancelBooking(res, user.lang, user.email, user, idapp, 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());
|
|
|
|
tools.mylog('crea Booking');
|
|
const booking = new Booking(body);
|
|
|
|
const check = tools.checkUserOk(booking.userId, req.user._id);
|
|
if (check.exit) return check.ret;
|
|
|
|
// console.log('fieldtochange', fieldtochange);
|
|
|
|
return Booking.findOne({ id_bookedevent: id })
|
|
.then(trovato => {
|
|
// console.log('trovato', trovato);
|
|
if (trovato) {
|
|
return Booking.findOneAndUpdate({ id_bookedevent: id }, { $set: fieldtochange }, {
|
|
new: true,
|
|
upsert: true
|
|
}).then((recbooking) => {
|
|
// tools.mylog('booking:', booking);
|
|
// tools.mylog('already exist');
|
|
sendNotif(res, body.idapp, req.user, recbooking);
|
|
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._id });
|
|
});
|
|
} else {
|
|
// save to database a new record
|
|
tools.mylog('save to database a new record');
|
|
booking._id = new ObjectID();
|
|
return booking.save().then((writeresult) => {
|
|
let idobj = writeresult._id;
|
|
|
|
Booking.findById(idobj)
|
|
.then((recbooking) => {
|
|
sendNotif(res, body.idapp, req.user, recbooking);
|
|
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._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 BOOKINGS : ', 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
|
|
Booking.findAllByUserIdAndIdApp(userId, idapp).then((bookedevent) => {
|
|
res.send({ bookedevent });
|
|
}).catch((e) => {
|
|
console.log(e);
|
|
res.status(400).send(e);
|
|
});
|
|
|
|
});
|
|
|
|
|
|
module.exports = router;
|