226 lines
9.6 KiB
JavaScript
226 lines
9.6 KiB
JavaScript
'use strict';
|
|
const httpStatus = require('http-status');
|
|
const generateControllers = require('../../core/controllers');
|
|
const eventService = require('./event.service');
|
|
const eventReservationService = require('./events_reservations.service');
|
|
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
|
|
|
//PRUEBA
|
|
const SchemaValidator = require('../../middlewares/schemaValidator');
|
|
const eventValidation = require('./event.validations');
|
|
const Joi = require('joi');
|
|
const userService = require('../auth/user.service');
|
|
const inscriptionService = require('./events_inscriptions.service');
|
|
|
|
// Module Name
|
|
const MODULE_NAME = '[event.controller]';
|
|
|
|
const controllerOptions = { MODULE_NAME };
|
|
const extraControllers = {
|
|
/*
|
|
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);
|
|
}
|
|
}
|
|
},
|
|
|
|
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);
|
|
}
|
|
},
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
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)
|
|
}
|
|
},
|
|
|
|
|
|
createInscription: async(req, res, next) => {
|
|
const params = extractParamsFromRequest(req, res, {});
|
|
|
|
let dataUser = {
|
|
id: '0939bb2a-d33d-4290-ac81-fc9faa1c015e',
|
|
phone: '+34686621059',
|
|
name: 'aaaaaaaaaaaa',
|
|
surname: 'bbbbbbb',
|
|
email: 'lqdvi@lqdvi.com',
|
|
userResult: null,
|
|
}
|
|
|
|
let dataInscription = {
|
|
eventId : params.params.id,
|
|
encodedInvitationCode : req.body.encodedInvitationCode,
|
|
code: null,
|
|
event : null,
|
|
reservation : null,
|
|
inscriptionCount : null,
|
|
inscription : null,
|
|
assistants : null, // aforo
|
|
tickets : null, // nº de inscripciones
|
|
type : (req.body.code) ? 'reserva' : 'libre',
|
|
}
|
|
|
|
dataInscription.code = dataInscription.encodedInvitationCode; //Buffer.from(dataInscription.encodedInvitationCode, 'base64').toString('ascii');
|
|
|
|
//Si viene codigo invitacion _getLevelAndPartner()---------------------
|
|
if (dataInscription.code) {
|
|
try {
|
|
dataInscription.reservation = await eventReservationService._getReservaByCode(dataInscription.eventId, dataInscription.code);
|
|
if (dataInscription.reservation) {
|
|
dataInscription.reservation = await dataInscription.reservation.toJSON();
|
|
dataInscription.event = dataInscription.reservation.Event;
|
|
} 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)
|
|
}
|
|
}
|
|
else {
|
|
try {
|
|
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);
|
|
}
|
|
} catch(error) {
|
|
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res)
|
|
}
|
|
}
|
|
|
|
console.log('>>>>>>>>>>>>>>>>>>>>>>>esta es la reserva y el evento a la que pertenece');
|
|
console.log(dataInscription.reservation);
|
|
console.log(dataInscription.event);
|
|
|
|
//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
|
|
try {
|
|
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);
|
|
};
|
|
} catch(error) {
|
|
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res);
|
|
}
|
|
|
|
console.log('>>>>>>>>>>>>>>>>>>>>>>este es el usuario que quiere inscribirse');
|
|
console.log(dataUser.userResult);
|
|
|
|
|
|
try {
|
|
|
|
//Comprobamos que el usuario no tenga ya inscripcion para ese evento
|
|
dataInscription.inscription = await inscriptionService._getInscription(dataInscription.event.id, dataUser.userResult.user.id);
|
|
if (dataInscription.inscription) {
|
|
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>inscription existe, revisar si es con codigo vip y cambiarlo');
|
|
console.log(dataInscription.inscription);
|
|
return handleResultResponse(result, null, params, res, (result === null) ? httpStatus.NOT_FOUND : httpStatus.OK);
|
|
}
|
|
else {
|
|
const source = (dataUser.userResult.isCreated) ? 'web' : 'app';
|
|
if (dataInscription.reservation)
|
|
dataInscription.inscription = await inscriptionService._createInscription(dataInscription.event.id, dataUser.userResult.user.id, dataInscription.type, true, source, dataInscription.reservation.id )
|
|
|
|
else
|
|
dataInscription.inscription = await inscriptionService._createInscription(dataInscription.event.id, dataUser.userResult.user.id, dataInscription.type, true, source, null);
|
|
}
|
|
|
|
} catch (error) {
|
|
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res);
|
|
}
|
|
|
|
return handleResultResponse("FIN", null, params, res, httpStatus.NOT_FOUND);
|
|
/*
|
|
.then(inscriptionService._getInscriptionCount)
|
|
.then(function () { if (dataInscription.code) {return eventReservationService._updateReservation } else { return evenService._updateEvent }})
|
|
.then(inscriptionService._createInscription)
|
|
.catch(_handleError);
|
|
//_createInscription(user, congressId, invitationCode, source, callback);
|
|
|
|
// const result = await eventService.createIncription(params, { user: req.user, lapse: 'pass' });
|
|
// return handleResultResponse(result, result.count, params, res);
|
|
*/
|
|
},
|
|
|
|
|
|
};
|
|
|
|
module.exports = generateControllers(eventService, extraControllers, controllerOptions);
|
|
|