2022-02-18 19:32:30 +00:00
"use strict" ;
2019-08-16 17:49:25 +00:00
2022-02-23 18:27:58 +00:00
const httpStatus = require ( "http-status" ) ;
2022-02-18 19:32:30 +00:00
const generateControllers = require ( "../../core/controllers" ) ;
const eventInscriptionService = require ( "./events_inscriptions.service" ) ;
2022-03-11 11:37:03 +00:00
const eventReservationService = require ( "./events_reservations.service" ) ;
const eventService = require ( "./event.service" ) ;
2022-02-23 18:27:58 +00:00
const mailService = require ( "./mail.service" ) ;
const marketingListService = require ( "./marketing_list.service" ) ;
2022-03-11 11:37:03 +00:00
const QRHelper = require ( "../../helpers/qr.helper" ) ;
2022-03-21 17:09:43 +00:00
const { extractParamsFromRequest , handleResultResponse , handleErrorResponse } = require ( "../../helpers/controller.helper" ) ;
2022-03-11 11:37:03 +00:00
const { data } = require ( "../../core/logger" ) ;
const lodash = require ( "lodash" ) ;
2022-03-21 17:09:43 +00:00
const userService = require ( "../auth/user.service" ) ;
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-23 18:27:58 +00:00
2022-03-11 11:37:03 +00:00
2022-03-21 17:09:43 +00:00
async function refreshConfirmed ( inscription ) {
2022-03-11 11:37:03 +00:00
2022-03-21 17:09:43 +00:00
if ( ! inscription ) {
throw new Error ( "Error al eliminar inscripción, no puedo cambiar confirmados a la reserva asociada" ) ;
2022-03-11 11:37:03 +00:00
} ;
if ( inscription . type === "online" )
return true ;
//En caso de inscripciones
const EventOrReservationChangeId = inscription . reservationId
2022-03-21 17:09:43 +00:00
? inscription . reservationId
: inscription . overflowEventId
? inscription . overflowEventId
: inscription . eventId ;
2022-03-11 11:37:03 +00:00
let NewConfirmed = 0 ;
//Si la inscripción viene por una reserva modificamos los confirmados de la reserva
if ( inscription . reservationId != null ) {
console . log ( "Tengo reservation>>>>>>>>>>>>>>>>>>" , inscription . reservationId ) ;
NewConfirmed = await eventInscriptionService . _getCountInscriptionsWithReservation (
2022-03-21 17:09:43 +00:00
EventOrReservationChangeId
2022-03-11 11:37:03 +00:00
) ;
//No se tienen en cuenta los marketinglist de las otras estructuras si lo tuviera seria esto
// marketingListId = (await eventReservationService._getReservaById(EventOrReservationChangeId))
// .marketing_list;
2022-03-21 17:09:43 +00:00
//Inscripcion de lista de espera bien de reserva o de evento de lista de espera
2022-03-11 11:37:03 +00:00
} else if ( inscription . overflowEventId != null ) {
console . log ( "Tengo overflow>>>>>>>>>>>>>>>>>>" , inscription . overflowEventId ) ;
NewConfirmed = await eventInscriptionService . _getCountInscriptionsWithOverflowEventId (
EventOrReservationChangeId
) ;
//No se tienen en cuenta los marketinglist de las otras estructuras si lo tuviera seria esto
/// marketingListId = (await eventService._getEvent(EventOrReservationChangeId)).marketing_list;
2022-03-21 17:09:43 +00:00
//Inscripción al evento (ni reserva ni lista de espera)
2022-03-11 11:37:03 +00:00
} else if ( inscription . eventId != null ) {
NewConfirmed = await eventInscriptionService . _getCountInscriptionsWithoutReservationAndOverflow (
EventOrReservationChangeId
) ;
//No se tienen en cuenta los marketinglist de las otras estructuras si lo tuviera seria esto
2022-03-21 17:09:43 +00:00
//marketingListId = (await eventService._getEvent(EventOrReservationChangeId)).marketing_list;
2022-03-11 11:37:03 +00:00
} ;
2022-03-21 17:09:43 +00:00
//Actualizamos aforo del evento o de la reserva
if ( inscription . reservationId != null ) {
console . log ( ">>>>>>>>>>>>>>Voy a actualizar aforo reserva" , EventOrReservationChangeId ) ;
console . log ( ">>>>>>>>>>>>>> " , NewConfirmed ) ;
if ( ! ( await eventReservationService . _updateConfirmedReservation ( EventOrReservationChangeId , NewConfirmed ) ) ) {
throw new Error ( "Error al eliminar inscripción, no puedo cambiar confirmados a la reserva asociada" ) ;
}
} else {
console . log ( ">>>>>>>>>>>>>>Voy a actualizar aforo evento" , EventOrReservationChangeId ) ;
console . log ( ">>>>>>>>>>>>>> " , NewConfirmed ) ;
if ( ! ( await eventService . _updateConfirmedEvent ( EventOrReservationChangeId , NewConfirmed ) ) ) {
throw new Error ( "Error al eliminar inscripción, no puedo cambiar confirmados a la inscripcion" ) ;
}
} ;
2022-03-11 11:37:03 +00:00
}
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
///////////////////////////////////////////////////////////////////
2022-03-11 11:37:03 +00:00
prepareDataInscription : async ( req , res , next ) => {
2022-02-18 19:32:30 +00:00
const params = extractParamsFromRequest ( req , res , { } ) ;
2022-02-17 12:12:13 +00:00
2022-03-11 11:37:03 +00:00
//Si no viene type es porque es una inscripción con la app antigua y el valor por defecto es onsite
2022-03-21 17:09:43 +00:00
let typeInscription = "onsite" ;
2022-03-11 11:37:03 +00:00
if ( ( req . body . type ) && ( req . body . type === "online" ) )
2022-03-21 17:09:43 +00:00
typeInscription = "online" ;
2022-03-11 11:37:03 +00:00
//Si viene code es la appa antigua o la nueva
2022-03-21 17:09:43 +00:00
if ( ( ( req . body . code ) && ( req . body . code !== "" ) )
|| ( ( req . body . group _size ) && ( req . body . group _size > 1 ) ) )
typeInscription = typeInscription + " group" ;
2022-02-18 19:32:30 +00:00
let dataInscription = {
eventId : params . params . id ,
2022-02-23 18:27:58 +00:00
reservationCode : req . user ? req . body . code : Buffer . from ( req . body . code , "base64" ) . toString ( "ascii" ) ,
2022-02-18 19:32:30 +00:00
type : typeInscription ,
2022-03-11 11:37:03 +00:00
groupSize : req . body . group _size ? req . body . group _size : 1 , //Si no viene group_size será uno porque es una inscripcion de la APP antigua
2022-02-18 19:32:30 +00:00
source : req . user ? "app" : "web" , //En el caso de tener ya usuario viene por APP sino viene por web
2022-03-11 11:37:03 +00:00
validated : false , //si no esta validado la inscripción es a la lista de espera
2022-02-18 19:32:30 +00:00
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 ,
} ;
2022-03-11 11:37:03 +00:00
try {
2022-03-21 17:09:43 +00:00
dataInscription . event = await eventService . _getEvent ( dataInscription . eventId ) ;
if ( dataInscription . event ) {
dataInscription . event = await dataInscription . event . toJSON ( ) ;
} else {
// No se ha encontrado
return handleResultResponse ( "Evento no encontrado" , null , params , res , httpStatus . NOT _FOUND ) ;
2022-03-11 11:37:03 +00:00
}
} catch ( error ) {
return handleErrorResponse ( MODULE _NAME , "encontrado" , error , res ) ;
}
2022-03-21 17:09:43 +00:00
2022-02-18 19:32:30 +00:00
res . locals . dataInscription = dataInscription ;
2022-03-21 17:09:43 +00:00
console . log ( ">>>>>>>>>>>>>>>>>>>> prepareDataInscription" , dataInscription . type ) ;
2022-02-18 19:32:30 +00:00
next ( ) ;
} ,
2022-02-17 12:12:13 +00:00
2022-03-11 11:37:03 +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
2022-03-11 11:37:03 +00:00
//si es así se comprueba que no quiera cambiar de codigo de reserva y se actualiza
////////////////////////////////////////////////////////////////////////////////
checkInscriptionByUser : async ( req , res , next ) => {
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 checkInscriptionByUser, 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 checkInscriptionByUser, prepareDataInscription, getOrCreateUser requerida" ,
2022-02-18 19:32:30 +00:00
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
2022-02-17 12:12:13 +00:00
2022-03-11 11:37:03 +00:00
try {
//Comprobamos que el usuario no tenga ya inscripcion para ese evento
dataInscription . inscription = await eventInscriptionService . _getInscriptionByEventAndUser (
dataInscription . event . id ,
dataUser . userResult . user . id
2022-02-23 18:27:58 +00:00
) ;
2022-03-11 11:37:03 +00:00
if ( dataInscription . inscription ) {
console . log ( "esta es la inscripcion que ya tengo>>>>" ,
dataInscription . inscription
) ;
//Si la inscripcion no tiene reserva o la tiene y es la misma de la insripcion devuelvo la inscripcion
2022-03-21 17:09:43 +00:00
if ( ( ! dataInscription . inscription . reservationId )
|| ( ( dataInscription . reservation ) && ( dataInscription . inscription . reservationId == dataInscription . reservation . id ) ) )
return handleResultResponse ( dataInscription . inscription , null , params , res , httpStatus . OK ) ;
2022-03-11 11:37:03 +00:00
//En caso contrario devuelvo la plaza a la reserva que tenia la inscripción anterior y apunto la inscripción a la nueva reserva
//ACTUALIZAMOS LA RESERVA DE LA INSCRIPCION CON LA NUEVA Y CAMBIAMOS COMFIRMADOS DEVOLVIENDO LA INSCRIPCIÓN CON LA NUEVA RESERVA
let CountConfirmedOldReservation = await eventInscriptionService . _getCountInscriptionsWithReservation (
2022-03-21 17:09:43 +00:00
dataInscription . inscription . reservationId
2022-03-11 11:37:03 +00:00
) ;
console . log ( "actualizo confirmados de la reserva anterior" ) ;
await eventReservationService . _updateConfirmedReservation (
2022-03-21 17:09:43 +00:00
dataInscription . inscription . reservationId ,
-- CountConfirmedOldReservation
2022-03-11 11:37:03 +00:00
) ;
let CountConfirmedNewReservation = await eventInscriptionService . _getCountInscriptionsWithReservation (
2022-03-21 17:09:43 +00:00
dataInscription . reservation . id
2022-03-11 11:37:03 +00:00
) ;
console . log ( "actualizo confirmados de la nueva reserva" ) ;
await eventReservationService . _updateConfirmedReservation (
2022-03-21 17:09:43 +00:00
dataInscription . reservation . id ,
++ CountConfirmedNewReservation
2022-03-11 11:37:03 +00:00
) ;
await eventInscriptionService . _updateReservationOfInscription (
2022-03-21 17:09:43 +00:00
dataInscription . inscription . id ,
dataInscription . reservation . id
2022-03-11 11:37:03 +00:00
) ;
dataInscription . inscription = await eventInscriptionService . _getInscriptionById ( dataInscription . inscription . id ) ;
return handleResultResponse ( dataInscription . inscription , null , params , res , httpStatus . OK ) ;
} ;
2022-03-21 17:09:43 +00:00
} catch ( error ) {
2022-03-11 11:37:03 +00:00
return handleResultResponse ( "Error checkInscriptionByUser" , error , params , res , httpStatus . NOT _FOUND ) ;
} ;
2022-02-18 19:32:30 +00:00
next ( ) ;
} ,
2022-03-11 11:37:03 +00:00
getInscription : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
const inscriptionId = params . params . id ;
const userId = req . user . id ;
try {
let inscription = await eventInscriptionService . _getInscriptionById ( inscriptionId ) ;
if ( ! inscription ) {
return handleResultResponse ( "Inscripción no encontrada" , null , params , res , httpStatus . NOT _FOUND ) ;
} else if ( inscription . userId !== userId ) {
return handleResultResponse ( "Inscripción no encontrada" , null , params , res , httpStatus . NOT _FOUND ) ;
}
//console.log("inscripcion encontrada>>>>>>>>>>>>>>>>>>>>>>>>>>>", inscription);
inscription = await inscription . toJSON ( ) ;
2022-03-21 17:09:43 +00:00
// console.log(">>>>>>>voy a dar inscription>>><", inscription.user);
2022-03-11 11:37:03 +00:00
var member = marketingListService . _generateMarketingDTO ( inscription ) ;
member . qrConfig = QRHelper . generateQRConfig ( member ) ;
inscription . code _ticket _qr = await QRHelper . getInscriptionQRCode ( member . qrConfig ) ;
//Si el usuario de la inscripción no es tutor limpiamos la información de la reserva
if ( inscription . user . profile !== 'tutor' )
2022-03-21 17:09:43 +00:00
if ( inscription . reservation ) inscription . reservation . assistants = null ;
2022-03-11 11:37:03 +00:00
console . log ( ">>>>>>>voy a dar inscription" , inscription ) ;
return handleResultResponse ( inscription , null , params , res , httpStatus . OK ) ;
} catch ( error ) {
return handleResultResponse ( "Error al buscar la inscripción" , null , params , res , httpStatus . NOT _FOUND ) ;
}
} ,
////////////////////////////////////////////////////////////////////////////////
//Esta función comprueba si el email ya tiene una inscripción para el evento
////////////////////////////////////////////////////////////////////////////////
checkInscriptionByMail : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
var eventId = params . params . id ;
var email = params . params . email ;
try {
const user = await userService . _getUserByEmail ( email ) ;
if ( user ) {
const result = await eventInscriptionService . _getInscriptionByEventAndUser ( eventId , user . id ) ;
if ( result ) return handleResultResponse ( result . stateText , null , params , res , httpStatus . OK ) ;
}
return handleResultResponse ( "No hay inscripción con ese email" , null , params , res , httpStatus . NOT _FOUND ) ;
} catch ( error ) {
return handleErrorResponse ( MODULE _NAME , "checkInscription" , error , res ) ;
}
} ,
2022-02-18 19:32:30 +00:00
///////////////////////////////////////////////////////////////////
2022-02-23 18:27:58 +00:00
//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
2022-02-18 19:32:30 +00:00
///////////////////////////////////////////////////////////////////
createInscription : async ( req , res , next ) => {
2022-02-23 18:27:58 +00:00
console . log ( ">>>>>>>>>>>>>>>>>>>> createInscription (event_inscriptions.controller)" ) ;
2022-02-18 19:32:30 +00:00
const params = extractParamsFromRequest ( req , res , { } ) ;
2022-03-21 17:09:43 +00:00
2022-02-18 19:32:30 +00:00
let dataInscription = res . locals . dataInscription ;
if ( ! dataInscription || ! dataInscription . event )
return handleResultResponse (
2022-03-11 11:37:03 +00:00
"Error prepareDataInscription requerida" ,
2022-02-18 19:32:30 +00:00
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
2022-03-11 11:37:03 +00:00
2022-02-18 19:32:30 +00:00
let dataUser = res . locals . dataUser ;
if ( ! dataUser )
2022-02-23 18:27:58 +00:00
return handleResultResponse ( "Error getOrCreateUser requerida" , null , params , res , httpStatus . NOT _FOUND ) ;
if ( dataInscription . reservation )
2022-02-18 19:32:30 +00:00
return handleResultResponse (
2022-02-23 18:27:58 +00:00
"Error existe una reserva por lo que debe llamar a createInscriptionReservation" ,
2022-02-18 19:32:30 +00:00
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
2022-02-17 12:12:13 +00:00
2022-03-21 17:09:43 +00:00
try {
2022-03-11 11:37:03 +00:00
//ONLINE
//Si es una inscripcion online no se validan aforos se crea inscripción y ya esta
if ( dataInscription . type === "online" || dataInscription . type === "online group" ) {
2022-02-23 18:27:58 +00:00
//creamos inscripcion
2022-03-21 17:09:43 +00:00
dataInscription . inscription = await eventInscriptionService . _createInscription (
2022-02-18 19:32:30 +00:00
dataInscription . event . id ,
2022-02-23 18:27:58 +00:00
dataUser . userResult . user . id ,
dataInscription . type ,
2022-03-12 11:03:31 +00:00
true , //validated,
2022-02-23 18:27:58 +00:00
dataInscription . source ,
2022-02-18 19:32:30 +00:00
null ,
2022-02-23 18:27:58 +00:00
null
2022-02-18 19:32:30 +00:00
) ;
2022-02-23 18:27:58 +00:00
console . log ( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ENTRADA ONLINE" , dataInscription . inscription ) ;
2022-03-11 11:37:03 +00:00
} ;
2022-03-21 17:09:43 +00:00
2022-03-11 11:37:03 +00:00
//ONSITE INDIVIDUAL
if ( ! dataInscription . inscription ) {
let inscriptionsWithoutReservationAndOverflowCount =
await eventInscriptionService . _getCountInscriptionsWithoutReservationAndOverflow ( dataInscription . event . id ) ;
++ inscriptionsWithoutReservationAndOverflowCount ;
console . log ( 'Vamos a ver si hay aforo>>>>' , 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 >= inscriptionsWithoutReservationAndOverflowCount
) {
2022-03-21 17:09:43 +00:00
dataInscription . validated = true ;
console . log ( 'actualizamos afoorooo>>>' ) ;
//Actualizamos aforo del evento y creamos inscripcion
if (
await eventService . _updateConfirmedEvent (
dataInscription . event . id ,
inscriptionsWithoutReservationAndOverflowCount
)
) {
try {
dataInscription . inscription = await eventInscriptionService . _createInscription (
dataInscription . event . id ,
dataUser . id ,
dataInscription . type ,
dataInscription . validated ,
dataInscription . source ,
null ,
null
)
} catch ( error ) { console . log ( 'SSSSSSSSSSSSSSSSSSSSSSSSS' , error ) }
2022-03-11 11:37:03 +00:00
}
2022-03-21 17:09:43 +00:00
else
return handleResultResponse (
"No se ha podido actualizar el aforo del evento" ,
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
console . log ( 'Inscripcion hecha>>>' , dataInscription . inscription ) ;
//Ponemos el evento en SOLD_OUT
if ( dataInscription . event . assistants == inscriptionsWithoutReservationAndOverflowCount )
await eventService . _updateSoldOutEvent ( dataInscription . event . id , true ) ;
}
2022-03-11 11:37:03 +00:00
// APUNTARSE A la lista de espera si se puede
else {
2022-03-21 17:09:43 +00:00
dataInscription . validated = false ;
if ( dataInscription . event . allow _overflow === false ) {
console . log ( "Aforo completo y no hay lista de espera" ) ;
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-03-21 17:09:43 +00:00
//recuperamos la cantidad de apuntados al evento overflow (lista de espera)
let ConfirmedWaitList = await eventInscriptionService . _getCountInscriptionsWithOverflowEventId (
dataInscription . event . overflow _eventId
) ;
//recuperamos aforo de la lista de espera
dataInscription . overflow _event = await eventService . _getEvent ( dataInscription . event . overflow _eventId ) ;
console . log ( "cantidad apuntados a lista de espera asociado, aforo >>>>>>>>>>>>>>>>>>>>>" ,
ConfirmedWaitList , dataInscription . overflow _event . assistants
) ;
//Si no hay espacio a lista de espera damos el mismo error que no hay lista de espera
if ( dataInscription . overflow _event . assistants < ++ ConfirmedWaitList ) {
console . log ( "Aforo completo de lista de espera" ) ;
return handleResultResponse (
"Aforo completo y no hay lista de espera" ,
null ,
params ,
res ,
httpStatus . NOT _FOUND
) ;
} ;
//Creamos inscripción a lista de espera
if (
await eventService . _updateConfirmedEvent ( dataInscription . event . overflow _eventId , ConfirmedWaitList )
) {
dataInscription . inscription = await eventInscriptionService . _createInscription (
dataInscription . event . id ,
dataUser . userResult . user . id ,
dataInscription . type ,
dataInscription . validated ,
dataInscription . source ,
null ,
2022-03-11 11:37:03 +00:00
dataInscription . event . overflow _eventId
) ;
2022-03-21 17:09:43 +00:00
} else {
console . log ( "No se ha podido actualizar el aforo de la lista de espera del evento" ) ;
return handleResultResponse (
"o se ha podido actualizar el aforo de la lista de espera del evento" ,
null ,
params ,
res ,
httpStatus . NOT _FOUND
2022-03-11 11:37:03 +00:00
) ;
2022-03-21 17:09:43 +00:00
}
} //FIN APUNTARSE A la lista de espera si se puede
2022-03-11 11:37:03 +00:00
} ;
dataInscription . inscription = await dataInscription . inscription . toJSON ( ) ;
//Incluimos correo en sendinblue
try {
marketingListService . addMarketingList ( dataUser , dataInscription ) ;
2022-03-21 17:09:43 +00:00
} 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
return handleResultResponse ( await dataInscription . inscription , null , params , res , httpStatus . CREATED ) ;
} catch ( Error ) {
return handleResultResponse ( "Error al crear la incripción createInscription" , null , params , res , httpStatus . NOT _FOUND ) ;
}
} ,
deleteInscription : async ( req , res , next ) => {
const params = extractParamsFromRequest ( req , res , { } ) ;
const user = req . user ;
const inscriptionId = params . params . id ;
2022-03-21 17:09:43 +00:00
let marketingListId = null ;
2022-03-11 11:37:03 +00:00
try {
const inscription = await eventInscriptionService . _getInscriptionById ( inscriptionId ) ;
console . log ( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>><Inscripcion a borrar:" ) ;
console . log ( inscription ) ;
//Solo se tiene en cuenta el marketinglist del evento
marketingListId = inscription . event . marketing _list ;
//Esto no seria posible si es asi damos error
if ( ! inscription || inscription . userId !== user . id )
return handleResultResponse ( "Inscription no encontrada" , null , params , res , httpStatus . NOT _FOUND ) ;
//En el caso de ser una inscripción grupal y el tutor se quiere quitar, se podrá hacer solo en el caso de que en la reserva no haya ya confirmados
if ( inscription . user . profile === "tutor" && inscription . reservation && inscription . reservation . confirmed > 1 )
2022-03-21 17:09:43 +00:00
return handleResultResponse ( "No se pudo eliminar inscripción por ser tutor de grupo y tener alumnos apuntados, pongase en contacto con nosotros" , null , params , res , httpStatus . NOT _FOUND )
2022-03-11 11:37:03 +00:00
//Borramos inscripción
if ( ( await eventInscriptionService . _deleteInscription ( inscription . id ) ) > 0 ) {
2022-03-21 17:09:43 +00:00
console . log ( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>><Inscripcion borrada" ) ;
//Actualizamos confirmados asistentes
refreshConfirmed ( inscription ) ;
if ( inscription . user . profile === "tutor" && inscription . reservation && inscription . reservation . confirmed === 1 ) {
//Eliminamos la reserva hecha del centro aliado
if ( ! ( ( await eventReservationService . _deleteReservation ( inscription . reservation . id ) ) > 0 ) )
return handleResultResponse ( "No se pudo eliminar inscripción por ser tutor de grupo online y tener alumnos apuntados, pongase en contacto con nosotros" , null , params , res , httpStatus . NOT _FOUND ) ;
if ( lodash . words ( inscription . type ) . includes ( "onsite" ) ) {
const eventOfReservation = await eventService . _getEvent ( inscription . reservation . eventId ) ;
//Modificamos los asistentes de evento (AFORO) para añadir las plazas de la reserva eliminada
const newAforo = eventOfReservation . assistants + inscription . reservation . assistants ;
if ( ! ( await eventService . _updateAssistantsEvent ( eventOfReservation . id , newAforo ) ) )
return handleResultResponse ( "No se ha podido actualizar el aforo del evento, para reservar las plazas solicitadas" , null , params , res , httpStatus . NOT _FOUND ) ;
}
} ;
2022-02-23 18:27:58 +00:00
}
2022-03-21 17:09:43 +00:00
else
return handleResultResponse ( "No se pudo eliminar inscripción" , null , params , res , httpStatus . NOT _FOUND ) ;
2022-03-11 11:37:03 +00:00
//Quitamos correo en sendinblue
try {
marketingListService . _deleteMember ( marketingListId , inscription . user . email ) ;
2022-03-21 17:09:43 +00:00
} catch ( error ) { console . log ( 'Se ha producido un error al eliminar de SenINBlue>>>>>>>>>>>>>>>>><<' , error ) ; }
2022-03-11 11:37:03 +00:00
//Mandamos correo de confirmación de eliminación
try {
mailService . sendEmailCancelate ( inscription ) ;
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-03-21 17:09:43 +00:00
console . log ( ">>>>>>>>>>>>>>Inscripcion eliminada con todos los pasos" ) ;
return handleResultResponse ( "Inscripción eliminada" , null , params , res , httpStatus . DELETEOK ) ;
2022-03-11 11:37:03 +00:00
} catch ( error ) {
console . log ( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrorrrr" , error ) ;
return handleResultResponse ( "Error al eliminar inscripción" , null , params , res , httpStatus . NOT _FOUND ) ;
2022-02-18 19:32:30 +00:00
}
} ,
2022-03-11 11:37:03 +00:00
2022-02-18 19:32:30 +00:00
} ;
2019-08-16 17:49:25 +00:00
2022-02-23 18:27:58 +00:00
module . exports = generateControllers ( eventInscriptionService , extraControllers , controllerOptions ) ;