app2-api/modules/events/event.controller.js

311 lines
18 KiB
JavaScript
Raw Normal View History

2019-07-09 08:51:00 +00:00
'use strict';
2019-07-19 19:36:20 +00:00
const httpStatus = require('http-status');
2019-07-09 08:51:00 +00:00
const generateControllers = require('../../core/controllers');
const eventService = require('./event.service');
2019-07-19 17:39:19 +00:00
const eventReservationService = require('./events_reservations.service');
2019-07-20 19:23:05 +00:00
const eventInscriptionService = require('./events_inscriptions.service');
2019-07-09 15:37:56 +00:00
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
2019-07-09 08:51:00 +00:00
2019-07-16 18:18:28 +00:00
//PRUEBA
const SchemaValidator = require('../../middlewares/schemaValidator');
const eventValidation = require('./event.validations');
const Joi = require('joi');
2019-07-19 17:39:19 +00:00
const userService = require('../auth/user.service');
2019-07-20 19:23:05 +00:00
2019-07-09 08:51:00 +00:00
// Module Name
const MODULE_NAME = '[event.controller]';
const controllerOptions = { MODULE_NAME };
2019-07-09 15:37:56 +00:00
const extraControllers = {
2019-07-18 11:23:43 +00:00
/*
2019-07-17 12:07:35 +00:00
findNext: (config) => {
config = config || {
scopes: [],
}
return async function (req, res, next) {
const params = extractParamsFromRequest(req, res, { includeAll: false });
try {
const result = await eventService.fetch(params, {
user: req.user,
scopes: config.scopes,
lapse: 'next'
});
console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
// Joi validation options
const _validationOptions = {
abortEarly: false, // abort after the last validation error
allowUnknown: true, // allow unknown keys that will be ignored
stripUnknown: true // remove unknown keys from the validated data
};
console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa222');
const data = Joi.validate(result.dataValues, eventValidation.EventsListOutputType, _validationOptions);
console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa333');
console.log(data);
return handleResultResponse(result, result.count, params, res);
} catch (error) {
handleErrorResponse(MODULE_NAME, 'findNext', error, res);
}
2019-07-10 17:26:22 +00:00
}
},
findCurrent: async (req, res, next) => {
const params = extractParamsFromRequest(req, res, {});
try {
const result = await eventService.fetch(params, { user: req.user, lapse: 'current' });
return handleResultResponse(result, result.count, params, res);
} catch (error) {
handleErrorResponse(MODULE_NAME, 'findNext', error, res);
}
},
2019-07-19 17:39:19 +00:00
if (invitationCode) {
_getLevelAndPartner()---------------------
.then(_getOrCreateUser)-------------
.then(_existsInscription)----------------
.then(_getInscriptionCount)---------
.then(_updateLevel)--------------
.then(_createInscription)--
.then(_addMember)
.then(_updateInscription)
.then(_getConference)
.then(_sendConfirmMail)
.then(_handleResponse)
.catch(_handleError);
} else {
_existsInscription()-------------
.then(_getInscriptionCount)-----------
.then(_getConference)------
.then(_updateConference)----
.then(_createInscription)--
.then(_addMember)
.then(_updateInscription)
.then(_handleResponse)
.catch(_handleError);
}
2019-07-18 11:23:43 +00:00
*/
2019-07-19 19:36:20 +00:00
checkReservationCode: async (req, res, next) => {
const params = extractParamsFromRequest(req, res, {});
const eventId = params.params.id;
const encodedInvitationCode = params.params.encodedInvitationCode;
const registrationCode = encodedInvitationCode; //Buffer.from(req.params.encodedInvitationCode, 'base64').toString('ascii');
try {
const result = await eventReservationService._getReservaByCode(eventId, registrationCode);
handleResultResponse(!!result, null, params, res, (result === null) ? httpStatus.NOT_FOUND : httpStatus.OK);
} catch(error) {
handleErrorResponse(MODULE_NAME, 'checkReservationCode', error, res)
}
},
2019-07-12 09:48:04 +00:00
createInscription: async(req, res, next) => {
const params = extractParamsFromRequest(req, res, {});
2019-07-20 16:11:43 +00:00
2019-07-21 13:30:49 +00:00
console.log('usuariooooooooooooo');
console.log(req.user);
2019-07-20 16:11:43 +00:00
let dataUser = {
2019-07-20 19:23:05 +00:00
id: null,
2019-07-21 13:30:49 +00:00
phone: '+34686333111',
2019-07-20 19:23:05 +00:00
name: 'aaaaaaaasdasdaaaaaa',
2019-07-20 16:11:43 +00:00
surname: 'bbbbbbb',
2019-07-21 13:30:49 +00:00
email: 'lqdvi333@lqdvi.com',
2019-07-20 16:11:43 +00:00
userResult: null,
}
2019-07-19 17:39:19 +00:00
2019-07-20 16:11:43 +00:00
let dataInscription = {
2019-07-20 19:23:05 +00:00
eventId: params.params.id,
encodedReservationCode: req.body.code,
reservationCode: req.body.code, //Buffer.from(req.body.code, 'base64').toString('ascii');
2019-07-21 13:30:49 +00:00
event: null,
reservation: null,
2019-07-20 19:23:05 +00:00
inscription: null,
2019-07-21 13:30:49 +00:00
ticket: null, //nº total de inscritos (libres + con reserva) - Para ticket - entrada
validated: null, //si no esta validado la inscripción es a la lista de espera
2019-07-20 19:23:05 +00:00
inscriptionsWithoutReservationCount: null, //nº total de inscritos sin reserva asignada
inscriptionsWithReservationCount: null, //nº total de inscritos a la reserva asignada
2019-07-19 17:39:19 +00:00
type : (req.body.code) ? 'reserva' : 'libre',
}
2019-07-20 19:23:05 +00:00
//SI VIENE CODIGO DE RESERVA, RECUPERAMOS LA RESERVA Y EL EVENTO
if (dataInscription.reservationCode) {
2019-07-19 19:36:20 +00:00
try {
2019-07-20 19:23:05 +00:00
dataInscription.reservation = await eventReservationService._getReservaByCode(dataInscription.eventId, dataInscription.reservationCode);
2019-07-20 16:11:43 +00:00
if (dataInscription.reservation) {
dataInscription.reservation = await dataInscription.reservation.toJSON();
dataInscription.event = dataInscription.reservation.Event;
2019-07-19 19:36:20 +00:00
} 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, 'createInscription', error, res)
}
2019-07-19 17:39:19 +00:00
}
2019-07-20 19:23:05 +00:00
//SOLO RECUPERAMOS EL EVENTO
2019-07-19 17:39:19 +00:00
else {
2019-07-19 19:36:20 +00:00
try {
2019-07-20 16:11:43 +00:00
dataInscription.event = await eventService._getEvent(dataInscription.eventId);
if (dataInscription.event) {
dataInscription.event = await dataInscription.event.toJSON();
2019-07-19 19:36:20 +00:00
} else {
// No se ha encontrado
return handleResultResponse("Evento no encontrado", null, params, res, httpStatus.NOT_FOUND);
}
} catch(error) {
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res)
}
2019-07-19 17:39:19 +00:00
}
2019-07-20 16:11:43 +00:00
console.log('>>>>>>>>>>>>>>>>>>>>>>>esta es la reserva y el evento a la que pertenece');
console.log(dataInscription.reservation);
console.log(dataInscription.event);
2019-07-19 19:36:20 +00:00
2019-07-20 16:11:43 +00:00
//creamos o recuperamos el usuario teniendo en cuenta que pude venir por APP o WEB
//si viene por web se tendra en cuenta el email y si viene por APP el phone para buscar
2019-07-19 19:36:20 +00:00
try {
2019-07-20 16:11:43 +00:00
dataUser.userResult = await userService._getOrCreateUser(dataUser);
if (!dataUser.userResult) {
// No se ha encontrado
return handleResultResponse("No se ha podido crear o encontrar el usuario dado", null, params, res, httpStatus.NOT_FOUND);
};
2019-07-19 19:36:20 +00:00
} catch(error) {
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res);
}
2019-07-20 16:11:43 +00:00
console.log('>>>>>>>>>>>>>>>>>>>>>>este es el usuario que quiere inscribirse');
console.log(dataUser.userResult);
2019-07-19 19:36:20 +00:00
2019-07-19 17:39:19 +00:00
2019-07-20 16:11:43 +00:00
try {
//Comprobamos que el usuario no tenga ya inscripcion para ese evento
2019-07-20 19:23:05 +00:00
dataInscription.inscription = await eventInscriptionService._getInscription(dataInscription.event.id, dataUser.userResult.user.id);
2019-07-20 16:11:43 +00:00
if (dataInscription.inscription) {
2019-07-21 13:30:49 +00:00
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>inscription existe, revisar si es con codigo vip y cambiarlo, su inscripcion se ha cambiado a vip');
2019-07-20 16:11:43 +00:00
console.log(dataInscription.inscription);
2019-07-21 13:30:49 +00:00
return handleResultResponse(dataInscription.inscription, null, params, res, httpStatus.OK);
2019-07-20 16:11:43 +00:00
}
2019-07-20 19:23:05 +00:00
//TENEMOS QUE CREAR INSCRIPCIÓN
2019-07-20 16:11:43 +00:00
else {
const source = (dataUser.userResult.isCreated) ? 'web' : 'app';
2019-07-20 19:23:05 +00:00
dataInscription.ticket = await eventInscriptionService._getCountInscriptionsEvent(dataInscription.event.id);
++dataInscription.ticket;
//CON CODIGO DE RESERVA SE MODIFICA EL CONFIRMED DE LA RESERVA, YA QUE SE DESCONTARA DEL AFORO DE LA RESERVA
if (dataInscription.reservation) {
dataInscription.inscriptionsWithReservationCount = await eventInscriptionService._getCountInscriptionsWithReservation(dataInscription.reservation.id);
++dataInscription.inscriptionsWithReservationCount;
2019-07-21 13:30:49 +00:00
//COMPROBAMOS SI ES VALIDO O HAY QUE APUNTARLE A LA LISTA DE ESPERA DE LA RESERVA
if (dataInscription.reservation.assistants >= dataInscription.inscriptionsWithReservationCount) {
dataInscription.validated = true;
//Actualizamos aforo de la lista de espera de la reserva y creamos inscripcion en la lista de espera de la reserva
if (await eventReservationService._updateReservationEvent(dataInscription.reservation.id, dataInscription.inscriptionsWithReservationCount))
dataInscription.inscription = await eventInscriptionService._createInscription(dataInscription.event.id,
dataUser.userResult.user.id,
dataInscription.ticket,
dataInscription.type,
dataInscription.validated,
source, dataInscription.reservation.id,
null)
else
return handleResultResponse("No se ha podido actualizar el aforo de la reserva", null, params, res, httpStatus.NOT_FOUND);
}
//LISTA DE ESPERA DE LA RESERVA
else {
if (dataInscription.reservation.allow_overflow === true) {
dataInscription.validated = false;
dataInscription.inscriptionsWithReservationCount = await eventInscriptionService._getCountInscriptionsWithReservation(dataInscription.reservation.overflow_event_reservationId);
++dataInscription.inscriptionsWithReservationCount;
// if (dataInscription.reservation.assistants >= dataInscription.inscriptionsWithReservationCount) {
//Actualizamos aforo de la reserva y creamos inscripcion
if (await eventReservationService._updateReservationEvent(dataInscription.reservation.overflow_event_reservationId, dataInscription.inscriptionsWithReservationCount))
dataInscription.inscription = await eventInscriptionService._createInscription(dataInscription.event.id,
dataUser.userResult.user.id,
dataInscription.ticket,
dataInscription.type,
dataInscription.validated,
source,
dataInscription.reservation.overflow_event_reservationId,
null)
else
return handleResultResponse("No se ha podido actualizar el aforo de la reserva", null, params, res, httpStatus.NOT_FOUND);
}
else
return handleResultResponse("Aforo completo de la reserva y no hay lista de espera", null, params, res, httpStatus.NOT_FOUND);
}
}
//SIN CODIGO DE RESERVA SE MODIFICA EL CONFIRMED DEL EVENTO, YA QUE SE DESCONTARA DEL AFORO DEL EVENTO
2019-07-20 19:23:05 +00:00
else {
dataInscription.inscriptionsWithoutReservationCount = await eventInscriptionService._getCountInscriptionsWithoutReservation(dataInscription.event.id);
++dataInscription.inscriptionsWithoutReservationCount;
2019-07-21 13:30:49 +00:00
//COMPROBAMOS SI ES VALIDO O HAY QUE APUNTARLE A LA LISTA DE ESPERA DEL EVENTO
if (dataInscription.event.assistants >= dataInscription.inscriptionsWithoutReservationCount) {
dataInscription.validated = true;
//Actualizamos aforo del evento y creamos inscripcion
if (await eventService._updateConfirmedEvent(dataInscription.event.id, dataInscription.inscriptionsWithoutReservationCount))
dataInscription.inscription = await eventInscriptionService._createInscription(dataInscription.event.id,
dataUser.userResult.user.id,
dataInscription.ticket,
dataInscription.type,
dataInscription.validated,
source,
null,
null)
else
return handleResultResponse("No se ha podido actualizar el aforo del evento", null, params, res, httpStatus.NOT_FOUND);
}
//LISTA DE ESPERA DE LA RESERVA
else {
if (dataInscription.event.allow_overflow === true) {
dataInscription.validated = false;
//Actualizamos aforo de la lista de espera del evento y creamos inscripcion
if (await eventService._updateConfirmedEvent(dataInscription.event.overflow_eventId, dataInscription.inscriptionsWithoutReservationCount))
dataInscription.inscription = await eventInscriptionService._createInscription(dataInscription.event.overflow_eventId,
dataUser.userResult.user.id,
dataInscription.ticket,
dataInscription.type,
dataInscription.validated,
source,
null,
dataInscription.overflow_eventId)
else
return handleResultResponse("No se ha podido actualizar el aforo del evento", null, params, res, httpStatus.NOT_FOUND);
}
else
return handleResultResponse("Aforo completo y no hay lista de espera", null, params, res, httpStatus.NOT_FOUND);
}
2019-07-20 19:23:05 +00:00
}
2019-07-20 16:11:43 +00:00
}
} catch (error) {
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res);
}
2019-07-19 17:39:19 +00:00
2019-07-12 09:48:04 +00:00
2019-07-20 19:23:05 +00:00
return handleResultResponse(await dataInscription.inscription.toJSON(), null, params, res, httpStatus.CREATED)
2019-07-14 16:44:59 +00:00
},
2019-07-20 16:11:43 +00:00
2019-07-09 15:37:56 +00:00
};
2019-07-09 08:51:00 +00:00
module.exports = generateControllers(eventService, extraControllers, controllerOptions);