This commit is contained in:
David Arranz 2019-07-20 21:23:05 +02:00
parent c2882887c9
commit d3d1fb0aac
5 changed files with 100 additions and 71 deletions

View File

@ -17,7 +17,7 @@ const extraMethods = {
} else {
await models.User.findOrCreate({
where: {
[Sequelize.or] : [{ iphone: dataUser.phone }, { email: dataUser.email }]
[Sequelize.Op.or] : [ {phone: dataUser.phone}, {email: dataUser.email} ]
},
defaults: {
phone: dataUser.phone,

View File

@ -3,6 +3,7 @@ const httpStatus = require('http-status');
const generateControllers = require('../../core/controllers');
const eventService = require('./event.service');
const eventReservationService = require('./events_reservations.service');
const eventInscriptionService = require('./events_inscriptions.service');
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
//PRUEBA
@ -10,7 +11,7 @@ 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]';
@ -111,33 +112,31 @@ const extraControllers = {
const params = extractParamsFromRequest(req, res, {});
let dataUser = {
id: '0939bb2a-d33d-4290-ac81-fc9faa1c015e',
phone: '+34686621059',
name: 'aaaaaaaaaaaa',
id: null,
phone: '+34686621049',
name: 'aaaaaaaasdasdaaaaaa',
surname: 'bbbbbbb',
email: 'lqdvi@lqdvi.com',
email: 'lqdvi2@lqdvi.com',
userResult: null,
}
let dataInscription = {
eventId : params.params.id,
encodedInvitationCode : req.body.encodedInvitationCode,
code: null,
eventId: params.params.id,
encodedReservationCode: req.body.code,
reservationCode: req.body.code, //Buffer.from(req.body.code, 'base64').toString('ascii');
event : null,
reservation : null,
inscriptionCount : null,
inscription : null,
assistants : null, // aforo
tickets : null, // nº de inscripciones
inscription: null,
ticket : null, //nº total de inscritos (libres + con reserva) - Para ticket - entrada
inscriptionsWithoutReservationCount: null, //nº total de inscritos sin reserva asignada
inscriptionsWithReservationCount: null, //nº total de inscritos a la reserva asignada
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) {
//SI VIENE CODIGO DE RESERVA, RECUPERAMOS LA RESERVA Y EL EVENTO
if (dataInscription.reservationCode) {
try {
dataInscription.reservation = await eventReservationService._getReservaByCode(dataInscription.eventId, dataInscription.code);
dataInscription.reservation = await eventReservationService._getReservaByCode(dataInscription.eventId, dataInscription.reservationCode);
if (dataInscription.reservation) {
dataInscription.reservation = await dataInscription.reservation.toJSON();
dataInscription.event = dataInscription.reservation.Event;
@ -149,6 +148,7 @@ const extraControllers = {
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res)
}
}
//SOLO RECUPERAMOS EL EVENTO
else {
try {
dataInscription.event = await eventService._getEvent(dataInscription.eventId);
@ -186,36 +186,51 @@ const extraControllers = {
try {
//Comprobamos que el usuario no tenga ya inscripcion para ese evento
dataInscription.inscription = await inscriptionService._getInscription(dataInscription.event.id, dataUser.userResult.user.id);
dataInscription.inscription = await eventInscriptionService._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);
}
//TENEMOS QUE CREAR INSCRIPCIÓN
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 )
dataInscription.ticket = await eventInscriptionService._getCountInscriptionsEvent(dataInscription.event.id);
++dataInscription.ticket;
//CON CODIGO DE RESERVA SE MODIFICA EL CONFIRMED DE LA RESERVA, YA QUE SE DESCONTARA DEL AFORO DE LA RESERVA
if (dataInscription.reservation) {
dataInscription.inscriptionsWithReservationCount = await eventInscriptionService._getCountInscriptionsWithReservation(dataInscription.reservation.id);
++dataInscription.inscriptionsWithReservationCount;
//??????????????????????????????????????????????
//En la reserva lo tengo pero me fio mas del count reservation.confirmed++ ??????????????????????????????
else
dataInscription.inscription = await inscriptionService._createInscription(dataInscription.event.id, dataUser.userResult.user.id, dataInscription.type, true, source, null);
//Actualizamos aforo y creamos inscripcion
if (await eventReservationService._updateReservationEvent(dataInscription.reservation.id, dataInscription.inscriptionsWithReservationCount))
dataInscription.inscription = await eventInscriptionService._createInscription(dataInscription.event.id, dataUser.userResult.user.id, dataInscription.ticket, dataInscription.type, true, source, null)
else
return handleResultResponse("No se ha podido actualizar el aforo de la reserva", null, params, res, httpStatus.NOT_FOUND);
}
//SIN CODIGO DE RESERVA SE MODIFICA EL CONFIRMED DEL EVENTO, YA QUE SE DESCONTARA DEL AFORO DEL EVENTO
else {
dataInscription.inscriptionsWithoutReservationCount = await eventInscriptionService._getCountInscriptionsWithoutReservation(dataInscription.event.id);
++dataInscription.inscriptionsWithoutReservationCount;
//Actualizamos aforo y creamos inscripcion
if (await eventService._updateConfirmedEvent(dataInscription.event.id, dataInscription.inscriptionsWithoutReservationCount))
dataInscription.inscription = await eventInscriptionService._createInscription(dataInscription.event.id, dataUser.userResult.user.id, dataInscription.ticket, dataInscription.type, true, source, null)
else
return handleResultResponse("No se ha podido actualizar el aforo del evento", null, params, res, httpStatus.NOT_FOUND);
}
}
} 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);
*/
return handleResultResponse(await dataInscription.inscription.toJSON(), null, params, res, httpStatus.CREATED)
},

View File

@ -86,20 +86,20 @@ const extraMethods = {
});
},
_updateEvent: (params, context) => {
const assistants = context.event.assistants; // <- aforo
const tickets = context.inscriptionCount + 1; // <- nº de inscritos + 1
_updateConfirmedEvent: (eventId, confirmed) => {
return new Promise(function (resolve, reject) {
models.Event.update(
{
confirmed : tickets,
confirmed : confirmed,
},
{
where : { id: context.EventId }
where: { id: eventId }
})
.then(function (result) {
resolve(result);
const aaa = result[0];
console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaa');
console.log(aaa);
resolve((result[0] === 1));
})
.catch(function (error) {
reject(error)

View File

@ -16,25 +16,37 @@ const extraMethods = {
})
},
_getInscriptionCount: (params, context) => {
//Nos devuelve el número total de inscripciones realizadas para ese evento (para el codigo de ticket-entrada)
_getCountInscriptionsEvent: (eventId) => {
return models.EventInscription.count({
where: {
eventId: eventId,
},
})
},
return new Promise(function (resolve, reject) {
models.Inscription.count({
//Nos devuelve el número de inscripciones realizadas para ese evento
_getCountInscriptionsWithoutReservation: (eventId) => {
return models.EventInscription.count({
where: {
eventId: context.eventId,
reservationId: (context.code) ? context.reservation.id : null,
eventId: eventId,
reservationId : null,
},
})
.then(function (count) {
context.inscriptionCount = count;
resolve(count);
}).catch(function (error) {
reject(error)
});
})
},
//Nos devuelve el número de inscripciones realizadas con esa reserva
_getCountInscriptionsWithReservation: (reservationId) => {
return models.EventInscription.count({
where: {
reservationId: reservationId,
},
})
},
_createInscription: (eventId, userId, type, valid, source, reservationId) => {
_createInscription: (eventId, userId, ticket, type, valid, source, reservationId) => {
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<valores de la inscripcion');
console.log(eventId, userId, ticket, type, valid, source, reservationId);
return new Promise(function (resolve, reject) {
models.EventInscription.create({
@ -44,11 +56,11 @@ const extraMethods = {
source: source,
valid: valid,
// valid: !(tickets > assistants),
reservationId: reservationId
reservationId: reservationId,
code_ticket: ticket,
})
.then(function (result) {
inscription = result.dataValues;
resolve(inscription);
resolve(result);
})
.catch(function (error) {
reject(error)

View File

@ -16,21 +16,23 @@ const extraMethods = {
})
},
_updateReservation: (params, context) => {
const tickets = inscriptionCount + 1; // <- nº de inscritos + 1
return new Promise (function (resolve, reject) {
models.EventReservation.update({
confirmed: tickets
}, {
where: {
id: eventReservationid
}
})
})
}
_updateReservationEvent: (id, confirmed) => {
return new Promise(function (resolve, reject) {
models.EventReservation.update(
{
confirmed: confirmed,
},
{
where: { id: id }
})
.then(function (result) {
resolve((result[0] === 1));
})
.catch(function (error) {
reject(error)
});
});
},
};