From d3d1fb0aac38dc497ee879462e12a4f924c6daae Mon Sep 17 00:00:00 2001 From: david Date: Sat, 20 Jul 2019 21:23:05 +0200 Subject: [PATCH] a --- modules/auth/user.service.js | 2 +- modules/events/event.controller.js | 79 +++++++++++-------- modules/events/event.service.js | 14 ++-- modules/events/events_inscriptions.service.js | 44 +++++++---- modules/events/events_reservations.service.js | 32 ++++---- 5 files changed, 100 insertions(+), 71 deletions(-) diff --git a/modules/auth/user.service.js b/modules/auth/user.service.js index aa8c01a..6587a09 100644 --- a/modules/auth/user.service.js +++ b/modules/auth/user.service.js @@ -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, diff --git a/modules/events/event.controller.js b/modules/events/event.controller.js index db7294f..095ba13 100644 --- a/modules/events/event.controller.js +++ b/modules/events/event.controller.js @@ -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) }, diff --git a/modules/events/event.service.js b/modules/events/event.service.js index 7e83c2c..9fdc91b 100644 --- a/modules/events/event.service.js +++ b/modules/events/event.service.js @@ -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) diff --git a/modules/events/events_inscriptions.service.js b/modules/events/events_inscriptions.service.js index 9268bc5..fc7cd6e 100644 --- a/modules/events/events_inscriptions.service.js +++ b/modules/events/events_inscriptions.service.js @@ -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('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>< assistants), - reservationId: reservationId + reservationId: reservationId, + code_ticket: ticket, }) .then(function (result) { - inscription = result.dataValues; - resolve(inscription); + resolve(result); }) .catch(function (error) { reject(error) diff --git a/modules/events/events_reservations.service.js b/modules/events/events_reservations.service.js index 96e48ea..c27ff09 100644 --- a/modules/events/events_reservations.service.js +++ b/modules/events/events_reservations.service.js @@ -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) + }); + }); + }, };