165 lines
6.9 KiB
JavaScript
165 lines
6.9 KiB
JavaScript
"use strict";
|
|
|
|
const httpStatus = require("http-status");
|
|
const generateControllers = require("../../core/controllers");
|
|
const eventInscriptionService = require("./events_inscriptions.service");
|
|
const mailService = require("./mail.service");
|
|
const marketingListService = require("./marketing_list.service");
|
|
|
|
const { extractParamsFromRequest, handleResultResponse } = require("../../helpers/controller.helper");
|
|
|
|
// Module Name
|
|
const MODULE_NAME = "[eventInscription.controller]";
|
|
const controllerOptions = { MODULE_NAME };
|
|
|
|
const extraControllers = {
|
|
///////////////////////////////////////////////////////////////////
|
|
//Prepara la estructura de datos para el registro de inscripciones
|
|
///////////////////////////////////////////////////////////////////
|
|
prepareInscription: async (req, res, next) => {
|
|
console.log(">>>>>>>>>>>>>>>>>>>> prepareInscription");
|
|
const params = extractParamsFromRequest(req, res, {});
|
|
|
|
let typeInscription = "onsite";
|
|
//online
|
|
if (req.body.type === "online") {
|
|
// reservation online
|
|
if (req.body.code) typeInscription = "online group";
|
|
//reservation online
|
|
else if (req.body.group_size > 1) typeInscription = "online group";
|
|
else typeInscription = "online";
|
|
}
|
|
//onsite
|
|
else {
|
|
// reservation presencial
|
|
if (req.body.code) typeInscription = "onsite group";
|
|
// reservation presencial
|
|
else if (req.body.group_size > 1) typeInscription = "onsite group";
|
|
}
|
|
|
|
let dataInscription = {
|
|
eventId: params.params.id,
|
|
reservationCode: req.user ? req.body.code : Buffer.from(req.body.code, "base64").toString("ascii"),
|
|
type: typeInscription,
|
|
groupSize: req.body.group_size ? req.body.group_size : 1,
|
|
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();
|
|
},
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
//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
|
|
);
|
|
|
|
//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>>>>>>>>>>>>>>>>>>>>><",
|
|
dataInscription.inscription.reservationId
|
|
);
|
|
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 llama solo desde APP
|
|
//Inscripción sin CODIGO DE RESERVA, SE MODIFICA EL CONFIRMED DEL EVENTO, YA QUE SE DESCONTARA DEL AFORO DEL EVENTO
|
|
// en caso de online no afectará a los aforos
|
|
///////////////////////////////////////////////////////////////////
|
|
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 prepareInscription, recuperateEvent requerida",
|
|
null,
|
|
params,
|
|
res,
|
|
httpStatus.NOT_FOUND
|
|
);
|
|
let dataUser = res.locals.dataUser;
|
|
if (!dataUser)
|
|
return handleResultResponse("Error getOrCreateUser requerida", null, params, res, httpStatus.NOT_FOUND);
|
|
|
|
if (dataInscription.reservation)
|
|
return handleResultResponse(
|
|
"Error existe una reserva por lo que debe llamar a createInscriptionReservation",
|
|
null,
|
|
params,
|
|
res,
|
|
httpStatus.NOT_FOUND
|
|
);
|
|
|
|
//Si es una inscripcion online no se validan aforos se crea inscripción y ya esta
|
|
if (dataInscription.type === "online" || dataInscription.type === "online group") {
|
|
try {
|
|
//creamos inscripcion
|
|
dataInscription.inscription = await eventInscriptionService._createInscription(
|
|
dataInscription.event.id,
|
|
dataUser.userResult.user.id,
|
|
dataInscription.type,
|
|
true, //validated,
|
|
dataInscription.source,
|
|
null,
|
|
null
|
|
);
|
|
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ENTRADA ONLINE", dataInscription.inscription);
|
|
|
|
let member = marketingListService.addMarketingList(dataUser, dataInscription);
|
|
|
|
console.log("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS", member);
|
|
// member = mailService._sendInscriptionEmail(dataInscription, member);
|
|
|
|
//************************************************************************************************************************************************
|
|
//EN UN FUTURO SE METERÄ AQUI LAS INSCRIPCIONES NORMALES************************************
|
|
//************************************************************************************************************************************************
|
|
|
|
return handleResultResponse(await dataInscription.inscription.toJSON(), null, params, res, httpStatus.CREATED);
|
|
} catch (Error) {
|
|
return handleResultResponse("Error al crear la incripción online", null, params, res, httpStatus.NOT_FOUND);
|
|
}
|
|
}
|
|
},
|
|
};
|
|
|
|
module.exports = generateControllers(eventInscriptionService, extraControllers, controllerOptions);
|