2019-07-09 08:51:00 +00:00
|
|
|
'use strict';
|
2019-07-19 19:36:20 +00:00
|
|
|
const httpStatus = require('http-status');
|
2019-07-09 08:51:00 +00:00
|
|
|
const generateControllers = require('../../core/controllers');
|
|
|
|
|
const eventService = require('./event.service');
|
2019-07-19 17:39:19 +00:00
|
|
|
const eventReservationService = require('./events_reservations.service');
|
2019-07-09 15:37:56 +00:00
|
|
|
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
2019-07-09 08:51:00 +00:00
|
|
|
|
2019-07-16 18:18:28 +00:00
|
|
|
//PRUEBA
|
|
|
|
|
const SchemaValidator = require('../../middlewares/schemaValidator');
|
|
|
|
|
const eventValidation = require('./event.validations');
|
|
|
|
|
const Joi = require('joi');
|
2019-07-19 17:39:19 +00:00
|
|
|
const userService = require('../auth/user.service');
|
|
|
|
|
const inscriptionService = require('./events_inscriptions.service');
|
2019-07-09 08:51:00 +00:00
|
|
|
|
|
|
|
|
// Module Name
|
|
|
|
|
const MODULE_NAME = '[event.controller]';
|
|
|
|
|
|
|
|
|
|
const controllerOptions = { MODULE_NAME };
|
2019-07-09 15:37:56 +00:00
|
|
|
const extraControllers = {
|
2019-07-18 11:23:43 +00:00
|
|
|
/*
|
2019-07-17 12:07:35 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2019-07-10 17:26:22 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-07-19 17:39:19 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-18 11:23:43 +00:00
|
|
|
*/
|
2019-07-19 19:36:20 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
2019-07-12 09:48:04 +00:00
|
|
|
createInscription: async(req, res, next) => {
|
|
|
|
|
const params = extractParamsFromRequest(req, res, {});
|
2019-07-19 17:39:19 +00:00
|
|
|
|
|
|
|
|
let context = {
|
|
|
|
|
eventId : params.params.id,
|
2019-07-19 19:36:20 +00:00
|
|
|
encodedInvitationCode : req.body.encodedInvitationCode,
|
|
|
|
|
code: null,
|
2019-07-19 17:39:19 +00:00
|
|
|
event : null,
|
|
|
|
|
reservation : null,
|
|
|
|
|
inscriptionCount : null,
|
|
|
|
|
inscription : null,
|
|
|
|
|
assistants : null, // aforo
|
|
|
|
|
tickets : null, // nº de inscripciones
|
|
|
|
|
type : (req.body.code) ? 'reserva' : 'libre',
|
|
|
|
|
userId: '0939bb2a-d33d-4290-ac81-fc9faa1c015e',
|
|
|
|
|
user: {
|
|
|
|
|
phone: '+34686621059',
|
|
|
|
|
name: 'aaaaaaaaaaaa',
|
|
|
|
|
surname: 'bbbbbbb',
|
|
|
|
|
email: 'lqdvi@lqdvi.com',
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-19 19:36:20 +00:00
|
|
|
context.code = context.encodedInvitationCode; //Buffer.from(context.encodedInvitationCode, 'base64').toString('ascii');
|
|
|
|
|
|
2019-07-19 17:39:19 +00:00
|
|
|
//Si viene codigo invitacion _getLevelAndPartner()---------------------
|
|
|
|
|
if (context.code) {
|
2019-07-19 19:36:20 +00:00
|
|
|
try {
|
|
|
|
|
context.reservation = await eventReservationService._getReservaByCode(context.eventId, context.code);
|
|
|
|
|
if (context.reservation) {
|
|
|
|
|
context.reservation = await context.reservation.toJSON();
|
|
|
|
|
context.event = context.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)
|
|
|
|
|
}
|
2019-07-19 17:39:19 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2019-07-19 19:36:20 +00:00
|
|
|
try {
|
|
|
|
|
context.event = await eventService._getEvent(context.eventId);
|
|
|
|
|
if (context.event) {
|
|
|
|
|
context.event = await context.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)
|
|
|
|
|
}
|
2019-07-19 17:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-19 19:36:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-19 17:39:19 +00:00
|
|
|
console.log('esta es la reserva y el evento a la que pertenece');
|
|
|
|
|
console.log(context.reservation);
|
|
|
|
|
console.log(context.event);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//creamos o recuperamos el usuario .then(_getOrCreateUser)-------------
|
2019-07-19 19:36:20 +00:00
|
|
|
try {
|
|
|
|
|
context.user = await userService._getOrCreateUser(params, context);
|
|
|
|
|
context.user = await context.user.toJSON();
|
|
|
|
|
console.log('creamos o recuperamos el usuario');
|
|
|
|
|
console.log(context.user);
|
|
|
|
|
} catch(error) {
|
|
|
|
|
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-19 17:39:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
inscriptionService._existsInscription(params, context)
|
|
|
|
|
.then(inscriptionService._getInscriptionCount)
|
|
|
|
|
.then(function () { if (context.code) {return eventReservationService._updateReservation } else { return evenService._updateEvent }})
|
|
|
|
|
.then(inscriptionService._createInscription)
|
|
|
|
|
.catch(_handleError);
|
|
|
|
|
//_createInscription(user, congressId, invitationCode, source, callback);
|
2019-07-12 09:48:04 +00:00
|
|
|
|
2019-07-19 17:39:19 +00:00
|
|
|
// const result = await eventService.createIncription(params, { user: req.user, lapse: 'pass' });
|
|
|
|
|
// return handleResultResponse(result, result.count, params, res);
|
2019-07-14 16:44:59 +00:00
|
|
|
},
|
|
|
|
|
|
2019-07-18 18:13:07 +00:00
|
|
|
/*
|
|
|
|
|
findOne2: async (req, res, next) => {
|
2019-07-14 16:44:59 +00:00
|
|
|
const params = extractParamsFromRequest(req, res, { includeAll: false });
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const result = await eventService.fetchOne(params, { user: req.user });
|
|
|
|
|
return handleResultResponse(result, null, params, res, (result === null) ? httpStatus.NOT_FOUND : httpStatus.OK);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
handleErrorResponse(MODULE_NAME, 'findOne', error, res)
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-07-18 18:13:07 +00:00
|
|
|
*/
|
2019-07-09 15:37:56 +00:00
|
|
|
|
|
|
|
|
};
|
2019-07-09 08:51:00 +00:00
|
|
|
|
|
|
|
|
module.exports = generateControllers(eventService, extraControllers, controllerOptions);
|
|
|
|
|
|