216 lines
10 KiB
JavaScript
216 lines
10 KiB
JavaScript
'use strict';
|
|
|
|
const moment = require('moment');
|
|
const httpStatus = require('http-status');
|
|
const generateControllers = require('../../core/controllers');
|
|
const eventReservationService = require('./events_reservations.service');
|
|
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
|
const emailHelper = require('../../helpers/mail.helper');
|
|
const path = require("path");
|
|
const responseTime = require('response-time');
|
|
|
|
|
|
// Module Name
|
|
const MODULE_NAME = '[eventReservation.controller]';
|
|
|
|
const controllerOptions = { MODULE_NAME };
|
|
|
|
function generateHeaderMail(reservation) {
|
|
let headerMail = null;
|
|
if (reservation) {
|
|
headerMail = {
|
|
to: reservation.Entity.contact_email,
|
|
name: reservation.Entity.name,
|
|
bcc: "cbarrantes@loquedeverdadimporta.org",
|
|
bccName: "Carolina Barrantes",
|
|
subject: 'Códigos de invitación para congreso LQDVI'
|
|
}
|
|
};
|
|
return headerMail;
|
|
}
|
|
|
|
function generateBodyMail(reservation) {
|
|
let bodyMail = null;
|
|
if (reservation) {
|
|
bodyMail = {
|
|
entityName: reservation.Entity.name,
|
|
eventName: reservation.Event.name,
|
|
dateEvent: moment(reservation.Event.init_date).format('D [de] MMMM [de] YYYY'),
|
|
reservationCode: reservation.reservation_code,
|
|
reservationDescription: reservation.description,
|
|
}
|
|
};
|
|
return bodyMail;
|
|
}
|
|
|
|
const extraControllers = {
|
|
|
|
getReservationsExcel: async (req, res, next) => {
|
|
const params = extractParamsFromRequest(req, res, {});
|
|
const eventId = params.params.id;
|
|
const type = params.params.type;
|
|
const userId = req.user.id;
|
|
|
|
const reservations = await eventReservationService._getReservationsExcel(req.user, eventId, null, type, function (result, status) {
|
|
if (result.messenger.code == "S99001") {
|
|
console.log(result);
|
|
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
res.setHeader('Content-Disposition', 'attachment; filename=' + result.data.name);
|
|
res.attachment(result.data.name);
|
|
res.download(path.resolve(result.data.path), result.data.name);
|
|
} else {
|
|
res.status(status).json(result);
|
|
}
|
|
})
|
|
},
|
|
|
|
|
|
sendMailReservation: async (req, res, next) => {
|
|
const params = extractParamsFromRequest(req, res, {});
|
|
const reservationId = params.params.id;
|
|
const user = req.user;
|
|
try {
|
|
const reservation = await eventReservationService._getReservaByIdWithEntityAndEvent(reservationId);
|
|
if (!reservation)
|
|
return handleResultResponse("Reserva no encontrada", null, params, res, httpStatus.NOT_FOUND);
|
|
|
|
try {
|
|
if (reservation.Entity.contact_email)
|
|
emailHelper.sendReservationCode(generateHeaderMail(reservation), generateBodyMail(reservation));
|
|
} catch (error) {
|
|
// console.log(error);
|
|
console.log('No se ha podido mandar email con los códigos de invitación');
|
|
};
|
|
|
|
return handleResultResponse(null, null, params, res, httpStatus.OK);
|
|
|
|
} catch (error) {
|
|
return handleResultResponse("Error al buscar la reserva", null, params, res, httpStatus.NOT_FOUND);
|
|
}
|
|
},
|
|
|
|
sendMailReservationsEvent: async (req, res, next) => {
|
|
const params = extractParamsFromRequest(req, res, {});
|
|
const eventId = params.params.id;
|
|
const entityId = params.params.entityId;
|
|
const type = params.params.type;
|
|
const user = req.user;
|
|
let reservations = null;
|
|
let result = "";
|
|
|
|
try {
|
|
if (!entityId)
|
|
reservations = await eventReservationService._getReservasByEventAndType(eventId, type);
|
|
else
|
|
reservations = await eventReservationService._getReservasByEventAndEntity(eventId, entityId);
|
|
|
|
if (!reservations)
|
|
return handleResultResponse("Reservas no encontradas", null, params, res, httpStatus.NOT_FOUND);
|
|
|
|
try {
|
|
reservations.forEach(function (reservation) {
|
|
console.log('mando correo: ', reservation.Entity.name);
|
|
if (reservation.Entity.contact_email && reservation.Entity.contact_email.length !== 0) {
|
|
console.log('correo: ', reservation.Entity.contact_email);
|
|
emailHelper.sendReservationCode(generateHeaderMail(reservation), generateBodyMail(reservation));
|
|
result = result + 'Invitación con código ' + reservation.reservation_code + ' enviada a ' + reservation.Entity.name + ' al destinatario ' + reservation.Entity.contact_email + '\n'
|
|
}
|
|
else result = result + 'Invitación con código ' + reservation.reservation_code + ' no se ha enviado proque el correo (' + reservation.Entity.contact_email + ') no es válido\n'
|
|
});
|
|
} catch (error) {
|
|
// console.log(error);
|
|
console.log('No se ha podido mandar email con los códigos de invitación');
|
|
};
|
|
|
|
return handleResultResponse(result, null, params, res, httpStatus.OK);
|
|
|
|
} catch (error) {
|
|
return handleResultResponse("Error al buscar las reservas", null, params, res, httpStatus.NOT_FOUND);
|
|
}
|
|
},
|
|
|
|
|
|
recuperateReservation: async (req, res, next) => {
|
|
console.log('>>>>>>>>>>>>>>>>>>>> recuperateReservation');
|
|
const params = extractParamsFromRequest(req, res, {});
|
|
let dataInscription = res.locals.dataInscription;
|
|
if (!dataInscription)
|
|
return handleResultResponse("Error recuperateReservation, prepareInscription, recuperateEvent requerida", null, params, res, httpStatus.NOT_FOUND);
|
|
|
|
//SI VIENE CODIGO DE RESERVA, RECUPERAMOS LA RESERVA Y EL EVENTO
|
|
if (dataInscription.reservationCode) {
|
|
try {
|
|
dataInscription.reservation = await eventReservationService._getReservaByCode(dataInscription.eventId, dataInscription.reservationCode);
|
|
if (dataInscription.reservation) {
|
|
dataInscription.reservation = await dataInscription.reservation.toJSON();
|
|
dataInscription.event = dataInscription.reservation.Event;
|
|
} else {
|
|
// No se ha encontrado
|
|
return handleResultResponse("Código de reserva no encontrado", null, params, res, httpStatus.NOT_FOUND);
|
|
}
|
|
} catch (error) {
|
|
return handleErrorResponse(MODULE_NAME, 'recuperateEventAndReservation', error, res)
|
|
}
|
|
}
|
|
res.locals.dataInscription = dataInscription;
|
|
next();
|
|
},
|
|
|
|
|
|
createReservationToEntity: async (req, res, next) => {
|
|
console.log('>>>>>>>>>>>>>>>>>>>> getOrCreateUser');
|
|
const params = extractParamsFromRequest(req, res, {});
|
|
let dataInscription = res.locals.dataInscription;
|
|
if ((!dataInscription) || (!dataInscription.event))
|
|
return handleResultResponse("Error createReservationToEntity, prepareInscription, recuperateEvent requerida", null, params, res, httpStatus.NOT_FOUND);
|
|
let dataUser = res.locals.dataUser;
|
|
if (!dataUser)
|
|
return handleResultResponse("Error createReservationToEntity, prepareInscription, recuperateEvent, getOrCreateUser requerida", null, params, res, httpStatus.NOT_FOUND);
|
|
|
|
//Si viene group_size crearemos un código de reserva
|
|
if (req.body.group_size > 1) {
|
|
|
|
const reservationData = {
|
|
reservation_code: eventReservationService._generateReservatioCode(dataInscription.event, dataUser.entityName),
|
|
state: 'draft', //borrador no estaría activa, publish es cuando se descuenta del aforo del evento
|
|
color: 'gray',
|
|
description: 'Reserva',
|
|
init_available_date: dataInscription.event.init_available_date,
|
|
end_available_date: dataInscription.event.end_available_date,
|
|
entityId: dataUser.entityId,
|
|
eventId: dataInscription.event.id,
|
|
gmt: dataInscription.event.gmt,
|
|
assistants: req.body.group_size,
|
|
confirmed: '0',
|
|
};
|
|
|
|
///Aqui podríamos validar si ya hay reserva y dar error ya que no pueden meter codigo de reserva y darnos un group_size superior a 1.
|
|
dataInscription.reservation = await eventReservationService.create(reservationData, generateControllers.buildContext(req, {}));
|
|
dataInscription.reservation = dataInscription.reservation.toJSON();
|
|
res.locals.dataInscription = dataInscription;
|
|
};
|
|
req.body.group_size = 1;
|
|
req.body.code = dataInscription.reservation.reservation_code;
|
|
next();
|
|
},
|
|
|
|
activeReservationToEntity: async (req, res, next) => {
|
|
console.log('>>>>>>>>>>>>>>>>>>>> ActiveReservationToEntity');
|
|
const params = extractParamsFromRequest(req, res, {});
|
|
let dataInscription = res.locals.dataInscription;
|
|
if ((!dataInscription) || (!dataInscription.reservation))
|
|
return handleResultResponse("Error activeReservationToEntity, prepareInscription, recuperateEvent requerida", null, params, res, httpStatus.NOT_FOUND);
|
|
|
|
///Aqui podríamos validar si ya hay reserva y dar error ya que no pueden meter codigo de reserva y darnos un group_size superior a 1.
|
|
if (!await eventReservationService._updatePublishReservation(dataInscription.reservation.id))
|
|
return handleResultResponse("No se ha podido publicar la reserva del evento", null, params, res, httpStatus.NOT_FOUND);
|
|
|
|
next();
|
|
},
|
|
|
|
|
|
};
|
|
|
|
module.exports = generateControllers(eventReservationService, extraControllers, controllerOptions);
|
|
|