2022-02-18 19:32:30 +00:00
"use strict" ;
const moment = require ( "moment" ) ;
const generateControllers = require ( "../../core/controllers" ) ;
2022-02-23 18:27:58 +00:00
const eventService = require ( "./event.service" ) ;
2022-02-18 19:32:30 +00:00
const eventReservationService = require ( "./events_reservations.service" ) ;
2022-02-23 18:27:58 +00:00
const eventInscriptionService = require ( "./events_inscriptions.service" ) ;
2022-03-11 11:37:03 +00:00
const mailService = require ( "./mail.service" ) ;
2022-06-07 08:33:00 +00:00
const userService = require ( "../auth/user.service" ) ;
2022-03-11 11:37:03 +00:00
const marketingListService = require ( "./marketing_list.service" ) ;
2022-02-18 19:32:30 +00:00
const {
extractParamsFromRequest ,
handleErrorResponse ,
handleResultResponse ,
2022-02-23 18:27:58 +00:00
httpStatus ,
2022-02-18 19:32:30 +00:00
} = require ( "../../helpers/controller.helper" ) ;
const emailHelper = require ( "../../helpers/mail.helper" ) ;
2019-10-15 11:01:25 +00:00
const path = require ( "path" ) ;
2023-02-28 16:41:33 +00:00
const responseTime = require ( "response-time" ) ;
2019-08-14 17:49:38 +00:00
// Module Name
2022-02-18 19:32:30 +00:00
const MODULE _NAME = "[eventReservation.controller]" ;
2019-08-14 17:49:38 +00:00
const controllerOptions = { MODULE _NAME } ;
2019-10-15 10:56:17 +00:00
2022-06-07 08:33:00 +00:00
/ * * * * A c t i v a u n a r e s e r v a q u e e s t á e n b o r r a d o r ( D r a f t ) , d e s c o n t a n d o a s i s t e n t e s d e l a f o r o t o t a l d e l e v e n t o
* Devuelve la reserva pasada por parametro publicada ( publish ) o null en el caso de error
* /
2022-03-24 10:54:59 +00:00
async function activeReservation ( reservation ) {
if ( ! reservation )
throw new Error ( "activeReservation: reservation should be an object" ) ;
2023-03-08 17:49:18 +00:00
//En el caso de ya estar publicada no hacemos nada se devuelve tal cual
2022-06-07 08:33:00 +00:00
if ( reservation . state === 'publish' )
return reservation ;
2022-03-24 10:54:59 +00:00
2023-03-08 17:49:18 +00:00
eventService . _updateAforoOfEventReservation ( reservation , reservation . assistants ) ;
2022-03-24 10:54:59 +00:00
2022-06-07 08:33:00 +00:00
//REvisar confirmados en lista de espera
//Finalmente publicamos la reserva solo si no está asociada a la lista de espera
if ( ! reservation . overflow _reservationId ) {
if ( ! ( await eventReservationService . _updatePublishReservation ( reservation . id ) ) ) {
console . log ( "No se ha podido publicar la reserva del evento" ) ;
return null ;
}
reservation . state = "publish" ;
2022-03-24 10:54:59 +00:00
2022-06-07 08:33:00 +00:00
//Finalmente hay que validar la inscripción del tutor
if ( ( await eventInscriptionService . _validateInscriptionTutorOfReservation ( reservation . id , reservation . userId ) ) <= 0 ) {
console . log ( "No se ha podido validar la inscripción del tutor" ) ;
return null ;
}
2022-03-24 10:54:59 +00:00
2022-06-07 09:45:41 +00:00
console . log ( 'reservation.userId>>>>>> ' , reservation . userId ) ;
2022-06-07 08:33:00 +00:00
//Mandamos el código de reserva para que registra a sus alumnos
const user = await userService . _getUserById ( reservation . userId ) ;
reservation . contact _email = user . email ;
reservation . contact _name = user . name ;
2022-06-07 09:45:41 +00:00
console . log ( 'reservation.userId>>>>>> ' , user . email ) ;
console . log ( 'reservation.userId>>>>>> ' , user . email ) ;
2022-03-24 10:54:59 +00:00
2022-06-07 08:33:00 +00:00
try {
2022-06-07 09:40:16 +00:00
console . log ( 'envio correo>>>>>>>>>>> ' , user ) ;
2022-06-07 08:33:00 +00:00
mailService . sendReservationCollegeEmail ( reservation ) ;
} catch ( error ) {
console . log ( error ) ;
} ;
2022-03-24 10:54:59 +00:00
2022-06-07 08:33:00 +00:00
return reservation ;
}
}
async function activeReservationById ( id ) {
//Buscar reserva con evento y entidad
let reservation = await eventReservationService . _getReservaByIdWithEntityAndEvent ( id ) ;
if ( await activeReservation ( reservation ) === null ) {
console . log ( "Error activeReservationById" )
return false ;
}
2022-02-23 18:27:58 +00:00
return true ;
}
2019-10-15 10:56:17 +00:00
const extraControllers = {
2022-02-23 18:27:58 +00:00
checkReservationCode : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
const appVersion = req && req . headers && req . headers [ "accept-version" ] ? req . headers [ "accept-version" ] : null ;
console . log ( "checkReservationCode - appVersion: " , appVersion ) ;
console . log ( "checkReservationCode - PARAMS " , params ) ;
const eventId = params . params . id ;
const encodedInvitationCode = params . params . encodedInvitationCode ;
const registrationCode = Buffer . from ( req . params . encodedInvitationCode , "base64" ) . toString ( "ascii" ) ;
try {
const result = await eventReservationService . _getReservaByCode ( eventId , registrationCode ) ;
if ( appVersion ) {
if ( appVersion == "1.0.0" || appVersion == "1.0.1" || appVersion == "1.0.2" )
return handleResultResponse ( ! ! result , null , params , res , httpStatus . OK ) ;
else return handleResultResponse ( result , null , params , res , httpStatus . OK ) ;
} else return handleResultResponse ( ! ! result , null , params , res , httpStatus . OK ) ;
} catch ( error ) {
return handleErrorResponse ( MODULE _NAME , "checkReservationCode" , error , res ) ;
}
} ,
2022-02-18 19:32:30 +00:00
getReservationsExcel : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
const eventId = params . params . id ;
const userId = req . user . id ;
2022-03-24 10:54:59 +00:00
let entityType = null ;
let entityId = null
2022-03-24 11:35:07 +00:00
// console.log('params.params.typeOrId>>>>++++++++++++++++++++++++++ 1 ', params.params.typeOrId);
2022-03-24 10:54:59 +00:00
//typeOrId puede ser college, partner o el id de la entidad de la reserva
const typeOrId = params . params . typeOrId ;
2022-03-24 11:35:07 +00:00
if ( typeOrId !== null ) {
if ( ( typeOrId === "colleges" ) || ( params . params . typeOrId === "partners" ) )
2022-03-24 10:54:59 +00:00
entityType = typeOrId
else
2022-03-24 11:35:07 +00:00
entityId = typeOrId ;
2022-03-24 10:54:59 +00:00
} ;
2022-03-24 11:35:07 +00:00
// console.log('params.params.typeOrId>>>>++++++++++++++++++++++++++ 2 ', entityId);
// console.log('params.params.typeOrId>>>>++++++++++++++++++++++++++ 3 ', entityType);
2022-02-18 19:32:30 +00:00
const reservations = await eventReservationService . _getReservationsExcel (
req . user ,
eventId ,
2022-03-24 10:54:59 +00:00
entityId ,
entityType ,
2022-02-18 19:32:30 +00:00
function ( result , status ) {
if ( result . messenger . code == "S99001" ) {
console . log ( result ) ;
2022-02-23 18:27:58 +00:00
res . setHeader ( "Content-Type" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) ;
res . setHeader ( "Content-Disposition" , "attachment; filename=" + result . data . name ) ;
2022-02-18 19:32:30 +00:00
res . attachment ( result . data . name ) ;
res . download ( path . resolve ( result . data . path ) , result . data . name ) ;
} else {
res . status ( status ) . json ( result ) ;
2019-10-15 10:56:17 +00:00
}
2022-02-18 19:32:30 +00:00
}
) ;
} ,
sendMailReservation : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
const reservationId = params . params . id ;
const user = req . user ;
try {
2022-02-23 18:27:58 +00:00
const reservation = await eventReservationService . _getReservaByIdWithEntityAndEvent ( reservationId ) ;
if ( ! reservation ) return handleResultResponse ( "Reserva no encontrada" , null , params , res , httpStatus . NOT _FOUND ) ;
2022-02-18 19:32:30 +00:00
try {
if ( reservation . Entity . contact _email )
2022-03-11 11:37:03 +00:00
emailHelper . sendReservationCode ( mailService . generateHeaderMailReservation ( reservation ) , mailService . generateBodyMailReservation ( reservation ) ) ;
2022-02-18 19:32:30 +00:00
} catch ( error ) {
// console.log(error);
2022-02-23 18:27:58 +00:00
console . log ( "No se ha podido mandar email con los códigos de invitación" ) ;
2022-02-18 19:32:30 +00:00
}
return handleResultResponse ( null , null , params , res , httpStatus . OK ) ;
} catch ( error ) {
2022-02-23 18:27:58 +00:00
return handleResultResponse ( "Error al buscar la reserva" , null , params , res , httpStatus . NOT _FOUND ) ;
2022-02-18 19:32:30 +00:00
}
} ,
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 {
2022-02-23 18:27:58 +00:00
if ( ! entityId ) reservations = await eventReservationService . _getReservasByEventAndType ( eventId , type ) ;
else reservations = await eventReservationService . _getReservasByEventAndEntity ( eventId , entityId ) ;
2022-02-18 19:32:30 +00:00
if ( ! reservations )
2022-02-23 18:27:58 +00:00
return handleResultResponse ( "Reservas no encontradas" , null , params , res , httpStatus . NOT _FOUND ) ;
2022-02-18 19:32:30 +00:00
try {
reservations . forEach ( function ( reservation ) {
console . log ( "mando correo: " , reservation . Entity . name ) ;
2022-02-23 18:27:58 +00:00
if ( reservation . Entity . contact _email && reservation . Entity . contact _email . length !== 0 ) {
2022-02-18 19:32:30 +00:00
console . log ( "correo: " , reservation . Entity . contact _email ) ;
2022-03-11 11:37:03 +00:00
emailHelper . sendReservationCode ( mailService . generateHeaderMailReservation ( reservation ) , mailService . generateBodyMailReservation ( reservation ) ) ;
2022-02-18 19:32:30 +00:00
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);
2022-02-23 18:27:58 +00:00
console . log ( "No se ha podido mandar email con los códigos de invitación" ) ;
2022-02-18 19:32:30 +00:00
}
return handleResultResponse ( result , null , params , res , httpStatus . OK ) ;
} catch ( error ) {
2022-02-23 18:27:58 +00:00
return handleResultResponse ( "Error al buscar las reservas" , null , params , res , httpStatus . NOT _FOUND ) ;
2022-02-18 19:32:30 +00:00
}
} ,
recuperateReservationByCode : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
let dataInscription = res . locals . dataInscription ;
if ( ! dataInscription )
return handleResultResponse (
2022-03-11 11:37:03 +00:00
"Error recuperateReservationByCode, prepareDataInscription requerida" ,
2022-02-18 19:32:30 +00:00
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
2022-03-21 17:09:43 +00:00
console . log ( ">>>>>>>>>>>>>>>>>>>> recuperateReservationByCode" , dataInscription . type ) ;
2022-02-18 19:32:30 +00:00
//SI VIENE CODIGO DE RESERVA, RECUPERAMOS LA RESERVA Y EL EVENTO
if ( dataInscription . reservationCode ) {
try {
2022-02-23 18:27:58 +00:00
dataInscription . reservation = await eventReservationService . _getReservaByCode (
dataInscription . eventId ,
dataInscription . reservationCode
) ;
2022-02-18 19:32:30 +00:00
if ( dataInscription . reservation ) {
2022-02-23 18:27:58 +00:00
dataInscription . reservation = await dataInscription . reservation . toJSON ( ) ;
2022-02-18 19:32:30 +00:00
dataInscription . event = dataInscription . reservation . Event ;
} else {
// No se ha encontrado
2022-02-23 18:27:58 +00:00
return handleResultResponse ( "Código de reserva no encontrado" , null , params , res , httpStatus . NOT _FOUND ) ;
2019-10-15 10:56:17 +00:00
}
2022-02-23 18:27:58 +00:00
res . locals . dataInscription = dataInscription ;
2022-02-18 19:32:30 +00:00
} catch ( error ) {
2022-03-11 11:37:03 +00:00
return handleErrorResponse ( MODULE _NAME , "recuperateReservationByCode" , error , res ) ;
2022-02-18 19:32:30 +00:00
}
}
next ( ) ;
} ,
createReservationToEntity : async ( req , res , next ) => {
2022-02-23 18:27:58 +00:00
console . log ( ">>>>>>>>>>>>>>>>>>>> createReservationToEntity" ) ;
2022-02-18 19:32:30 +00:00
const params = extractParamsFromRequest ( req , res , { } ) ;
let dataInscription = res . locals . dataInscription ;
if ( ! dataInscription || ! dataInscription . event )
return handleResultResponse (
2022-03-11 11:37:03 +00:00
"Error createReservationToEntity, prepareDataInscription, requerida" ,
2022-02-18 19:32:30 +00:00
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
let dataUser = res . locals . dataUser ;
if ( ! dataUser )
return handleResultResponse (
2022-03-11 11:37:03 +00:00
"Error createReservationToEntity, prepareDataInscription, getOrCreateUser requerida" ,
2022-02-18 19:32:30 +00:00
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
2022-03-21 17:09:43 +00:00
console . log ( ">>>>>>>>>>>>>>>>>>>> createReservationToEntity>>>>>>" , dataInscription . type ) ;
2022-02-18 19:32:30 +00:00
//Si viene group_size crearemos un código de reserva
2022-02-23 18:27:58 +00:00
if ( dataInscription . groupSize > 1 ) {
2022-03-21 17:09:43 +00:00
2022-03-11 11:37:03 +00:00
if ( ! dataUser . entityId ) {
return handleResultResponse (
2022-03-21 17:09:43 +00:00
"Error No es posible crear reserva grupal si no pertences a una entidad educativa" ,
null ,
params ,
res ,
httpStatus . NOT _FOUND
)
} ;
2022-02-18 19:32:30 +00:00
const reservationData = {
2022-02-23 18:27:58 +00:00
reservation _code : eventReservationService . _generateReservatioCode ( dataInscription . event , dataUser . entityName ) ,
state : "draft" , //sin confirmar, publish es cuando se descuenta del aforo del evento
2022-09-15 08:44:55 +00:00
color : "orange" ,
2022-03-21 17:09:43 +00:00
description : ( dataInscription . type === 'online group' ) ? 'Incripción online en grupo' : 'Reserva' ,
2022-02-18 19:32:30 +00:00
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 ,
2022-02-23 18:27:58 +00:00
assistants : dataInscription . groupSize ,
virtual : dataInscription . type === "online" || dataInscription . type === "online group" ,
userId : dataUser . id ,
allow _overflow : false ,
2022-02-18 19:32:30 +00:00
} ;
2022-03-11 11:37:03 +00:00
//Comprobamos aforo si no es online, si no es posible apuntarse iria a lista de espera
2022-03-21 17:09:43 +00:00
if ( dataInscription . type !== "online" && dataInscription . type !== "online group" ) {
const plazasDisponibles = dataInscription . event . assistants - dataInscription . event . confirmed ;
if ( plazasDisponibles < reservationData . assistants )
if ( dataInscription . event . allow _overflow ) {
reservationData . overflowEventId = dataInscription . event . overflow _eventId ;
reservationData . description = reservationData . description + ' en lista de espera'
console . log ( 'Asigno lista de espera>>>>>>>>>>>>>>>>>>>>>>>' , reservationData . eventId ) ;
}
else return handleResultResponse ( "Aforo lleno no es posible efectuar la reserva" , null , params , res , httpStatus . NOT _FOUND ) ;
2022-03-11 11:37:03 +00:00
}
2022-02-18 19:32:30 +00:00
///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 , { } )
) ;
2022-03-11 11:37:03 +00:00
2022-02-18 19:32:30 +00:00
dataInscription . reservation = dataInscription . reservation . toJSON ( ) ;
res . locals . dataInscription = dataInscription ;
2022-02-23 18:27:58 +00:00
console . log ( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>RESERVATION CREADA" , dataInscription . reservation ) ;
2022-03-21 17:09:43 +00:00
}
2022-03-11 11:37:03 +00:00
else console . log ( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>no hago nada" ) ;
2022-02-18 19:32:30 +00:00
next ( ) ;
} ,
activeReservationToEntity : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
let dataInscription = res . locals . dataInscription ;
2022-03-11 11:37:03 +00:00
let dataUser = res . locals . dataUser ;
if ( ! dataUser || ! dataInscription || ! dataInscription . event )
2022-02-18 19:32:30 +00:00
return handleResultResponse (
2022-03-11 11:37:03 +00:00
"Error createReservationToEntity, prepareDataInscription, ActiveReservationToEntity requerida" ,
2022-02-18 19:32:30 +00:00
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
2022-03-11 11:37:03 +00:00
//En caso de ser online no descontamos ningun aforo ya que no se controla de momento
if ( dataInscription . type === "online" || dataInscription . type === "online group" ) {
console . log ( ">> No se aplica descuento de aforo" ) ;
next ( ) ;
return ;
2022-03-21 17:09:43 +00:00
} ;
2022-03-11 11:37:03 +00:00
//En caso de ser una inscripción normal o una reserva ya publicada no descontamos ningun aforo al evento
2022-03-21 17:09:43 +00:00
if ( ! dataInscription . reservation || ( dataInscription . reservation . state === 'publish' ) ) { // || (!dataInscription.reservationCode) || (dataInscription.reservationCode === "")) {
2022-03-11 11:37:03 +00:00
console . log ( ">> No se aplica descuento de aforo ya que es una reserva publicada" ) ;
next ( ) ;
return ;
2022-03-21 17:09:43 +00:00
} ;
2022-03-11 11:37:03 +00:00
//Si es centro aliado
if ( dataUser . entityLevel === "aliado" ) {
2022-06-07 09:26:28 +00:00
//Aseguramos que la reserva vaya con su evento
dataInscription . reservation . Event = dataInscription . event ;
2022-06-07 08:33:00 +00:00
const reservationPublicada = await activeReservation ( dataInscription . reservation ) ;
if ( reservationPublicada === null )
2022-03-11 11:37:03 +00:00
return handleResultResponse (
2022-06-07 08:33:00 +00:00
"Error activeReservationToEntity requerida" ,
2022-03-11 11:37:03 +00:00
null ,
params ,
res ,
2022-06-07 08:33:00 +00:00
httpStatus . NOT _FOUND ) ;
else dataInscription . reservation = reservationPublicada ;
} ;
2022-03-11 11:37:03 +00:00
2022-02-23 18:27:58 +00:00
next ( ) ;
} ,
activarReservation : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
const idResevation = params . params . id ;
if ( ! idResevation )
return handleResultResponse ( "Error id de reservation necesario" , null , params , res , httpStatus . NOT _FOUND ) ;
try {
let result = await activeReservationById ( idResevation ) ;
return handleResultResponse ( result , null , params , res , httpStatus . OK ) ;
} catch ( error ) {
return handleResultResponse ( "Error al buscar las reservas" , null , params , res , httpStatus . NOT _FOUND ) ;
}
} ,
2023-02-28 16:41:33 +00:00
deleteReservationById : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
const idResevation = params . params . id ;
if ( ! idResevation )
return handleResultResponse ( "Error id de reservation necesario" , null , params , res , httpStatus . NOT _FOUND ) ;
2023-03-08 17:49:18 +00:00
const eventReservation = await eventReservationService . _getReservaById ( idResevation ) ;
2023-02-28 16:41:33 +00:00
if ( ! eventReservation )
return handleResultResponse ( "Error reserva no existente" , null , params , res , httpStatus . NOT _FOUND ) ;
try {
//Antes de borrar una reserva debemos borrar todas las inscripciones asociadas.
eventInscriptionService . _deleteInscriptionsByReservation ( eventReservation . id ) ;
if ( eventReservation . state === 'publish' ) {
2023-03-08 17:49:18 +00:00
const cantidad = - 1 * eventReservation . assistants ;
eventService . _updateAforoOfEventReservation ( eventReservation , cantidad ) ;
2023-02-28 16:41:33 +00:00
}
let result = eventReservationService . _deleteReservation ( eventReservation . id ) ;
return handleResultResponse ( result , null , params , res , httpStatus . OK ) ;
} catch ( error ) {
return handleResultResponse ( "Error al eliminar la reserva" , null , params , res , httpStatus . NOT _FOUND ) ;
}
} ,
/ * * * * M o d i f i c a u n a r e s e r v a , s i e s t á p u b l i c a d a , a c t u a l i z a l a d i f e r e n e c i a d e l o s a s i s t e n t e s , e n e l c a s o d e s e r m o d i f i c a d o , e n e l a f o r o d e l e v e n t o ( a s s i t a n t s ) .
* /
checkAssitantsUpdate : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
const idResevation = params . params . id ;
const NewReservarionAssistants = req . body . assistants ;
if ( ! idResevation )
return handleResultResponse ( "Error id de reservation necesario" , null , params , res , httpStatus . NOT _FOUND ) ;
2023-03-08 17:49:18 +00:00
const eventReservation = await eventReservationService . _getReservaById ( idResevation ) ; //
2023-02-28 16:41:33 +00:00
if ( ! eventReservation )
return handleResultResponse ( "Error reserva no existente" , null , params , res , httpStatus . NOT _FOUND ) ;
try {
2023-03-08 17:49:18 +00:00
if ( eventReservation . state === 'publish' && eventReservation . assistants != NewReservarionAssistants ) {
if ( NewReservarionAssistants < eventReservation . confirmed ) {
2023-02-28 16:41:33 +00:00
return handleResultResponse ( "Error el número de asistentes no puede ser menor que el de confirmados" , null , params , res , httpStatus . NOT _FOUND ) ;
2023-03-08 17:49:18 +00:00
} ;
const cantidad = ( NewReservarionAssistants - eventReservation . assistants ) ;
eventService . _updateAforoOfEventReservation ( eventReservation , cantidad ) ;
}
2023-02-28 16:41:33 +00:00
next ( ) ;
} catch ( error ) {
return handleResultResponse ( "Error al checkAssitantsUpdate de la reserva" , null , params , res , httpStatus . NOT _FOUND ) ;
}
} ,
2022-02-23 18:27:58 +00:00
///////////////////////////////////////////////////////////////////
//Esta función se llama desde APP y desde WEB
//Inscripción con CODIGO DE RESERVA, SE MODIFICA EL CONFIRMED DE LA RESERVA, YA QUE SE DESCONTARA DEL AFORO DE LA RESERVA
// en caso de online, funciona igual ya que descontará la inscripción de la persona del aforo quedando la inscripción online asociada a la reserva
///////////////////////////////////////////////////////////////////
createInscriptionReservation : async ( req , res , next ) => {
console . log ( ">>>>>>>>>>>>>>>>>>>> createInscriptionReservation (event_reservations.controller)" ) ;
const params = extractParamsFromRequest ( req , res , { } ) ;
2022-03-21 17:09:43 +00:00
2022-02-23 18:27:58 +00:00
let dataInscription = res . locals . dataInscription ;
if ( ! dataInscription || ! dataInscription . event || ! dataInscription . reservation )
2022-02-18 19:32:30 +00:00
return handleResultResponse (
2022-03-11 11:37:03 +00:00
"Error prepareDataInscription, recuperateReservationByCode o createReservationToEntity requerida" ,
2022-02-18 19:32:30 +00:00
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
2022-03-21 17:09:43 +00:00
2022-02-23 18:27:58 +00:00
let dataUser = res . locals . dataUser ;
if ( ! dataUser )
return handleResultResponse ( "Error getOrCreateUser requerida" , null , params , res , httpStatus . NOT _FOUND ) ;
2022-02-18 19:32:30 +00:00
2022-02-23 18:27:58 +00:00
try {
//CON CODIGO DE RESERVA SE MODIFICA EL CONFIRMED DE LA RESERVA, YA QUE SE DESCONTARA DEL AFORO DE LA RESERVA
dataInscription . inscriptionsWithReservationCount =
await eventInscriptionService . _getCountInscriptionsWithReservation ( dataInscription . reservation . id ) ;
++ dataInscription . inscriptionsWithReservationCount ;
2022-03-11 11:37:03 +00:00
/ * c o n s o l e . l o g (
2022-02-23 18:27:58 +00:00
"me inscribo por reserva>>>>>>>>>>>>>>>>>>>>>>>>>>><< con asistentes: " ,
2022-03-11 11:37:03 +00:00
dataInscription
2022-02-23 18:27:58 +00:00
) ;
console . log ( dataInscription . reservation . sold _out ) ;
console . log ( dataInscription . inscriptionsWithReservationCount ) ;
2022-03-11 11:37:03 +00:00
* /
2022-02-23 18:27:58 +00:00
//COMPROBAMOS SI ES VALIDO O HAY QUE APUNTARLE A LA LISTA DE ESPERA DE LA RESERVA
if (
dataInscription . reservation . sold _out == 0 &&
dataInscription . reservation . assistants >= dataInscription . inscriptionsWithReservationCount
) {
2022-03-21 17:09:43 +00:00
dataInscription . validated = false ;
2022-03-11 11:37:03 +00:00
//Es la inscripción automática del tutor o alguien que tiene el código y eso no puede ser
2022-03-16 18:12:54 +00:00
if ( dataInscription . type === "online" || dataInscription . type === "online group" || dataInscription . reservation . state === 'publish' )
2022-03-21 17:09:43 +00:00
dataInscription . validated = true ;
2022-03-16 18:12:54 +00:00
2022-03-11 11:37:03 +00:00
2022-02-23 18:27:58 +00:00
console . log ( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" , dataInscription . type ) ;
//Actualizamos aforo de la lista de espera de la reserva y creamos inscripcion en la lista de espera de la reserva
if (
await eventReservationService . _updateConfirmedReservation (
dataInscription . reservation . id ,
dataInscription . inscriptionsWithReservationCount
)
)
dataInscription . inscription = await eventInscriptionService . _createInscription (
dataInscription . event . id ,
2022-03-11 11:37:03 +00:00
dataUser . id ,
2022-02-23 18:27:58 +00:00
dataInscription . type ,
dataInscription . validated ,
dataInscription . source ,
dataInscription . reservation . id ,
null
) ;
else
return handleResultResponse (
"No se ha podido actualizar el aforo de la reserva" ,
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
//Ponemos la reserva en SOLD_OUT para que no se pueda apuntar nadie más
if ( dataInscription . reservation . assistants == dataInscription . inscriptionsWithReservationCount )
await eventReservationService . _updateSoldOutReservation ( dataInscription . reservation . id , true ) ;
}
// APUNTARSE A LISTA DE ESPERA SI SE PUEDE
else {
if ( dataInscription . reservation . allow _overflow === true ) {
dataInscription . validated = false ;
dataInscription . inscriptionsWithReservationCount =
await eventInscriptionService . _getCountInscriptionsWithReservation (
dataInscription . reservation . overflow _reservationId
) ;
++ dataInscription . inscriptionsWithReservationCount ;
// if (dataInscription.reservation.assistants >= dataInscription.inscriptionsWithReservationCount) {
//Actualizamos aforo de la reserva y creamos inscripcion
if (
await eventReservationService . _updateConfirmedReservation (
dataInscription . reservation . overflow _reservationId ,
dataInscription . inscriptionsWithReservationCount
)
)
dataInscription . inscription = await eventInscriptionService . _createInscription (
dataInscription . event . id ,
dataUser . userResult . user . id ,
dataInscription . type ,
dataInscription . validated ,
dataInscription . source ,
dataInscription . reservation . overflow _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
) ;
}
let Result = await dataInscription . inscription . toJSON ( ) ;
Result = {
reservation : dataInscription . reservation ,
... Result ,
} ;
2022-03-11 11:37:03 +00:00
2022-03-21 17:09:43 +00:00
// Incluimos correo en sendinblue
2022-03-11 11:37:03 +00:00
try {
2022-03-21 17:09:43 +00:00
// marketingListService.addMarketingList(dataUser, dataInscription);
} catch ( error ) { console . log ( 'Se ha producido un error al añadir a SenINBlue>>>>>>>>>>>>>>>>><<' , error ) ; }
2022-03-11 11:37:03 +00:00
//Mandamos correo con entrada o lista de espera
try {
mailService . sendEmailConfirm ( dataUser , dataInscription ) ;
2022-03-21 17:09:43 +00:00
} catch ( error ) { console . log ( 'Se ha producido un error al enviar mail>>>>>>>>>>>>>>>>><<' , error ) ; }
2022-03-11 11:37:03 +00:00
2022-02-23 18:27:58 +00:00
return handleResultResponse ( Result , null , params , res , httpStatus . CREATED ) ;
} catch ( error ) {
return handleResultResponse ( "Error al crear la incripción online" , null , params , res , httpStatus . NOT _FOUND ) ;
}
2022-02-18 19:32:30 +00:00
} ,
2019-10-15 10:56:17 +00:00
} ;
2019-08-14 17:49:38 +00:00
2022-02-23 18:27:58 +00:00
module . exports = generateControllers ( eventReservationService , extraControllers , controllerOptions ) ;