2022-02-18 19:32:30 +00:00
|
|
|
"use strict";
|
2019-08-16 17:49:25 +00:00
|
|
|
|
2022-02-18 19:32:30 +00:00
|
|
|
const generateControllers = require("../../core/controllers");
|
|
|
|
|
const eventInscriptionService = require("./events_inscriptions.service");
|
|
|
|
|
const eventService = require("./event.service");
|
2019-08-16 17:49:25 +00:00
|
|
|
|
2022-02-18 19:32:30 +00:00
|
|
|
const {
|
|
|
|
|
extractParamsFromRequest,
|
|
|
|
|
handleResultResponse,
|
|
|
|
|
httpStatus,
|
|
|
|
|
} = require("../../helpers/controller.helper");
|
2019-08-16 17:49:25 +00:00
|
|
|
|
|
|
|
|
// Module Name
|
2022-02-18 19:32:30 +00:00
|
|
|
const MODULE_NAME = "[eventInscription.controller]";
|
2019-08-16 17:49:25 +00:00
|
|
|
|
|
|
|
|
const controllerOptions = { MODULE_NAME };
|
2022-02-17 12:12:13 +00:00
|
|
|
const extraControllers = {
|
2022-02-18 19:32:30 +00:00
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
//Prepara la estructura de datos para el registro de inscripciones
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
prepareInscription: async (req, res, next) => {
|
|
|
|
|
console.log(">>>>>>>>>>>>>>>>>>>> prepareInscription");
|
|
|
|
|
const params = extractParamsFromRequest(req, res, {});
|
2022-02-17 12:12:13 +00:00
|
|
|
|
2022-02-18 19:32:30 +00:00
|
|
|
let typeInscription = "presencial";
|
|
|
|
|
//online
|
|
|
|
|
if (req.body.type === "online") {
|
|
|
|
|
if (req.body.code) typeInscription = "reservation online";
|
|
|
|
|
else if (req.body.group_size > 1) typeInscription = "reservation online";
|
|
|
|
|
else typeInscription = "online";
|
|
|
|
|
}
|
|
|
|
|
//onsite
|
|
|
|
|
else {
|
|
|
|
|
if (req.body.code) typeInscription = "reservation presencial";
|
|
|
|
|
else if (req.body.group_size > 1)
|
|
|
|
|
typeInscription = "reservation presencial";
|
|
|
|
|
}
|
2022-02-17 12:12:13 +00:00
|
|
|
|
2022-02-18 19:32:30 +00:00
|
|
|
let dataInscription = {
|
|
|
|
|
eventId: params.params.id,
|
|
|
|
|
reservationCode: req.user
|
|
|
|
|
? req.body.code
|
|
|
|
|
: Buffer.from(req.body.code, "base64").toString("ascii"),
|
|
|
|
|
type: typeInscription,
|
|
|
|
|
source: req.user ? "app" : "web", //En el caso de tener ya usuario viene por APP sino viene por web
|
|
|
|
|
validated: null, //si no esta validado la inscripción es a la lista de espera
|
|
|
|
|
inscriptionsWithoutReservationAndOverflowCount: null, //nº total de inscritos sin reserva y sin overflow asignada
|
|
|
|
|
inscriptionsWithReservationCount: null, //nº total de inscritos a la reserva asignada
|
|
|
|
|
event: null,
|
|
|
|
|
reservation: null,
|
|
|
|
|
inscription: null,
|
|
|
|
|
};
|
|
|
|
|
res.locals.dataInscription = dataInscription;
|
|
|
|
|
next();
|
|
|
|
|
},
|
2022-02-17 12:12:13 +00:00
|
|
|
|
2022-02-18 19:32:30 +00:00
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
//Esta función comprueba si el usuario ya tiene una inscripción para el evento
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
checkInscription: async (req, res, next) => {
|
|
|
|
|
console.log(
|
|
|
|
|
">>>>>>>>>>>>>>>>>>>> checkInscription (event_inscriptions.controller)"
|
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
|
);
|
2022-02-17 12:12:13 +00:00
|
|
|
|
2022-02-18 19:32:30 +00:00
|
|
|
//Comprobamos que el usuario no tenga ya inscripcion para ese evento
|
|
|
|
|
dataInscription.inscription =
|
|
|
|
|
await eventInscriptionService._getInscriptionByEventAndUser(
|
|
|
|
|
dataInscription.event.id,
|
|
|
|
|
dataUser.userResult.user.id
|
|
|
|
|
);
|
|
|
|
|
if (dataInscription.inscription) {
|
|
|
|
|
console.log("esta es la inscripcion que ya tengo>>>>>>>>>>>>>>>>>>>>><");
|
|
|
|
|
console.log(dataInscription.inscription);
|
|
|
|
|
//Devuelvo la reserva que ya tiene hecha el usuario
|
|
|
|
|
if (
|
|
|
|
|
!dataInscription.inscription.reservationId ||
|
|
|
|
|
dataInscription.inscription.reservationId ==
|
|
|
|
|
dataInscription.reservation.id
|
|
|
|
|
)
|
|
|
|
|
return handleResultResponse(
|
|
|
|
|
dataInscription.inscription,
|
|
|
|
|
null,
|
|
|
|
|
params,
|
|
|
|
|
res,
|
|
|
|
|
httpStatus.OK
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
},
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
//Esta función se puede llamar desde APP
|
|
|
|
|
//SIN CODIGO DE RESERVA SE MODIFICA EL CONFIRMED DEL EVENTO, YA QUE SE DESCONTARA DEL AFORO DEL EVENTO
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
createInscription: async (req, res, next) => {
|
|
|
|
|
console.log(
|
|
|
|
|
">>>>>>>>>>>>>>>>>>>> createInscription (event_inscriptions.controller)"
|
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
|
);
|
2022-02-17 12:12:13 +00:00
|
|
|
|
2022-02-18 19:32:30 +00:00
|
|
|
dataInscription.inscriptionsWithoutReservationAndOverflowCount =
|
|
|
|
|
await eventInscriptionService._getCountInscriptionsWithoutReservationAndOverflow(
|
|
|
|
|
dataInscription.event.id
|
|
|
|
|
);
|
|
|
|
|
++dataInscription.inscriptionsWithoutReservationAndOverflowCount;
|
|
|
|
|
|
|
|
|
|
//COMPROBAMOS SI ES VALIDO O HAY QUE APUNTARLE A LA LISTA DE ESPERA DEL EVENTO
|
|
|
|
|
if (
|
|
|
|
|
dataInscription.event.sold_out == 0 &&
|
|
|
|
|
dataInscription.event.assistants >=
|
|
|
|
|
dataInscription.inscriptionsWithoutReservationAndOverflowCount
|
|
|
|
|
) {
|
|
|
|
|
dataInscription.validated = true;
|
|
|
|
|
//Actualizamos aforo del evento y creamos inscripcion
|
|
|
|
|
if (
|
|
|
|
|
await eventService._updateConfirmedEvent(
|
|
|
|
|
dataInscription.event.id,
|
|
|
|
|
dataInscription.inscriptionsWithoutReservationAndOverflowCount
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
dataInscription.inscription =
|
|
|
|
|
await eventInscriptionService._createInscription(
|
|
|
|
|
dataInscription.event.id,
|
|
|
|
|
dataUser.userResult.user.id,
|
|
|
|
|
dataInscription.type,
|
|
|
|
|
dataInscription.validated,
|
|
|
|
|
dataInscription.source,
|
|
|
|
|
null,
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
else
|
|
|
|
|
return handleResultResponse(
|
|
|
|
|
"No se ha podido actualizar el aforo del evento",
|
|
|
|
|
null,
|
|
|
|
|
params,
|
|
|
|
|
res,
|
|
|
|
|
httpStatus.NOT_FOUND
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//Ponemos el evento en SOLD_OUT
|
|
|
|
|
if (
|
|
|
|
|
dataInscription.event.assistants ==
|
|
|
|
|
dataInscription.inscriptionsWithoutReservationAndOverflowCount
|
|
|
|
|
)
|
|
|
|
|
await eventService._updateSoldOutEvent(dataInscription.event.id, true);
|
|
|
|
|
}
|
2019-08-16 17:49:25 +00:00
|
|
|
|
2022-02-18 19:32:30 +00:00
|
|
|
// APUNTARSE A la lista de espera si se puede
|
|
|
|
|
else {
|
|
|
|
|
if (dataInscription.event.allow_overflow === true) {
|
|
|
|
|
dataInscription.validated = false;
|
|
|
|
|
|
|
|
|
|
//Actualizamos aforo de la lista de espera del evento y creamos inscripcion
|
|
|
|
|
console.log(
|
|
|
|
|
"evento de lista de espera que debo actulizar sus confirmados>>>>>>>>>>>>>>>>>>>>>",
|
|
|
|
|
dataInscription.event.overflow_eventId
|
|
|
|
|
);
|
|
|
|
|
//recuperamos la cantidad de apuntados al evento overflow a lista de espera
|
|
|
|
|
const ConfirmedWaitList =
|
|
|
|
|
await eventInscriptionService._getCountInscriptionsWithOverflowEventId(
|
|
|
|
|
dataInscription.event.overflow_eventId
|
|
|
|
|
);
|
|
|
|
|
console.log(
|
|
|
|
|
"cantidad apuntados al evento padre>>>>>>>>>>>>>>>>>>>>>",
|
|
|
|
|
dataInscription.inscriptionsWithoutReservationAndOverflowCount
|
|
|
|
|
);
|
|
|
|
|
console.log(
|
|
|
|
|
"cantidad apuntados al evento de lista de espera asociado>>>>>>>>>>>>>>>>>>>>>",
|
|
|
|
|
ConfirmedWaitList
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
await eventService._updateConfirmedEvent(
|
|
|
|
|
dataInscription.event.overflow_eventId,
|
|
|
|
|
ConfirmedWaitList
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
//console.log('voy a crearrrrrr la inscripcion');
|
|
|
|
|
dataInscription.inscription =
|
|
|
|
|
await eventInscriptionService._createInscription(
|
|
|
|
|
dataInscription.event.id,
|
|
|
|
|
dataUser.userResult.user.id,
|
|
|
|
|
dataInscription.type,
|
|
|
|
|
dataInscription.validated,
|
|
|
|
|
dataInscription.source,
|
|
|
|
|
null,
|
|
|
|
|
dataInscription.event.overflow_eventId
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.log("No se ha podido actualizar el aforo del evento");
|
|
|
|
|
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-08-16 17:49:25 +00:00
|
|
|
|
2022-02-18 19:32:30 +00:00
|
|
|
module.exports = generateControllers(
|
|
|
|
|
eventInscriptionService,
|
|
|
|
|
extraControllers,
|
|
|
|
|
controllerOptions
|
|
|
|
|
);
|