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 { } else {
await models.User.findOrCreate({ await models.User.findOrCreate({
where: { where: {
[Sequelize.or] : [{ iphone: dataUser.phone }, { email: dataUser.email }] [Sequelize.Op.or] : [ {phone: dataUser.phone}, {email: dataUser.email} ]
}, },
defaults: { defaults: {
phone: dataUser.phone, phone: dataUser.phone,

View File

@ -3,6 +3,7 @@ const httpStatus = require('http-status');
const generateControllers = require('../../core/controllers'); const generateControllers = require('../../core/controllers');
const eventService = require('./event.service'); const eventService = require('./event.service');
const eventReservationService = require('./events_reservations.service'); const eventReservationService = require('./events_reservations.service');
const eventInscriptionService = require('./events_inscriptions.service');
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper'); const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
//PRUEBA //PRUEBA
@ -10,7 +11,7 @@ const SchemaValidator = require('../../middlewares/schemaValidator');
const eventValidation = require('./event.validations'); const eventValidation = require('./event.validations');
const Joi = require('joi'); const Joi = require('joi');
const userService = require('../auth/user.service'); const userService = require('../auth/user.service');
const inscriptionService = require('./events_inscriptions.service');
// Module Name // Module Name
const MODULE_NAME = '[event.controller]'; const MODULE_NAME = '[event.controller]';
@ -111,33 +112,31 @@ const extraControllers = {
const params = extractParamsFromRequest(req, res, {}); const params = extractParamsFromRequest(req, res, {});
let dataUser = { let dataUser = {
id: '0939bb2a-d33d-4290-ac81-fc9faa1c015e', id: null,
phone: '+34686621059', phone: '+34686621049',
name: 'aaaaaaaaaaaa', name: 'aaaaaaaasdasdaaaaaa',
surname: 'bbbbbbb', surname: 'bbbbbbb',
email: 'lqdvi@lqdvi.com', email: 'lqdvi2@lqdvi.com',
userResult: null, userResult: null,
} }
let dataInscription = { let dataInscription = {
eventId : params.params.id, eventId: params.params.id,
encodedInvitationCode : req.body.encodedInvitationCode, encodedReservationCode: req.body.code,
code: null, reservationCode: req.body.code, //Buffer.from(req.body.code, 'base64').toString('ascii');
event : null, event : null,
reservation : null, reservation : null,
inscriptionCount : null, inscription: null,
inscription : null, ticket : null, //nº total de inscritos (libres + con reserva) - Para ticket - entrada
assistants : null, // aforo inscriptionsWithoutReservationCount: null, //nº total de inscritos sin reserva asignada
tickets : null, // nº de inscripciones inscriptionsWithReservationCount: null, //nº total de inscritos a la reserva asignada
type : (req.body.code) ? 'reserva' : 'libre', type : (req.body.code) ? 'reserva' : 'libre',
} }
dataInscription.code = dataInscription.encodedInvitationCode; //Buffer.from(dataInscription.encodedInvitationCode, 'base64').toString('ascii'); //SI VIENE CODIGO DE RESERVA, RECUPERAMOS LA RESERVA Y EL EVENTO
if (dataInscription.reservationCode) {
//Si viene codigo invitacion _getLevelAndPartner()---------------------
if (dataInscription.code) {
try { try {
dataInscription.reservation = await eventReservationService._getReservaByCode(dataInscription.eventId, dataInscription.code); dataInscription.reservation = await eventReservationService._getReservaByCode(dataInscription.eventId, dataInscription.reservationCode);
if (dataInscription.reservation) { if (dataInscription.reservation) {
dataInscription.reservation = await dataInscription.reservation.toJSON(); dataInscription.reservation = await dataInscription.reservation.toJSON();
dataInscription.event = dataInscription.reservation.Event; dataInscription.event = dataInscription.reservation.Event;
@ -149,6 +148,7 @@ const extraControllers = {
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res) return handleErrorResponse(MODULE_NAME, 'createInscription', error, res)
} }
} }
//SOLO RECUPERAMOS EL EVENTO
else { else {
try { try {
dataInscription.event = await eventService._getEvent(dataInscription.eventId); dataInscription.event = await eventService._getEvent(dataInscription.eventId);
@ -186,36 +186,51 @@ const extraControllers = {
try { try {
//Comprobamos que el usuario no tenga ya inscripcion para ese evento //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) { if (dataInscription.inscription) {
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>inscription existe, revisar si es con codigo vip y cambiarlo'); console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>inscription existe, revisar si es con codigo vip y cambiarlo');
console.log(dataInscription.inscription); console.log(dataInscription.inscription);
return handleResultResponse(result, null, params, res, (result === null) ? httpStatus.NOT_FOUND : httpStatus.OK); return handleResultResponse(result, null, params, res, (result === null) ? httpStatus.NOT_FOUND : httpStatus.OK);
} }
//TENEMOS QUE CREAR INSCRIPCIÓN
else { else {
const source = (dataUser.userResult.isCreated) ? 'web' : 'app'; const source = (dataUser.userResult.isCreated) ? 'web' : 'app';
if (dataInscription.reservation) dataInscription.ticket = await eventInscriptionService._getCountInscriptionsEvent(dataInscription.event.id);
dataInscription.inscription = await inscriptionService._createInscription(dataInscription.event.id, dataUser.userResult.user.id, dataInscription.type, true, source, dataInscription.reservation.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 //Actualizamos aforo y creamos inscripcion
dataInscription.inscription = await inscriptionService._createInscription(dataInscription.event.id, dataUser.userResult.user.id, dataInscription.type, true, source, null); 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) { } catch (error) {
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res); 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(await dataInscription.inscription.toJSON(), null, params, res, httpStatus.CREATED)
// return handleResultResponse(result, result.count, params, res);
*/
}, },

View File

@ -86,20 +86,20 @@ const extraMethods = {
}); });
}, },
_updateEvent: (params, context) => { _updateConfirmedEvent: (eventId, confirmed) => {
const assistants = context.event.assistants; // <- aforo
const tickets = context.inscriptionCount + 1; // <- nº de inscritos + 1
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
models.Event.update( models.Event.update(
{ {
confirmed : tickets, confirmed : confirmed,
}, },
{ {
where : { id: context.EventId } where: { id: eventId }
}) })
.then(function (result) { .then(function (result) {
resolve(result); const aaa = result[0];
console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaa');
console.log(aaa);
resolve((result[0] === 1));
}) })
.catch(function (error) { .catch(function (error) {
reject(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) { //Nos devuelve el número de inscripciones realizadas para ese evento
models.Inscription.count({ _getCountInscriptionsWithoutReservation: (eventId) => {
return models.EventInscription.count({
where: { where: {
eventId: context.eventId, eventId: eventId,
reservationId: (context.code) ? context.reservation.id : null, reservationId : null,
}, },
}) })
.then(function (count) { },
context.inscriptionCount = count;
resolve(count); //Nos devuelve el número de inscripciones realizadas con esa reserva
}).catch(function (error) { _getCountInscriptionsWithReservation: (reservationId) => {
reject(error) 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) { return new Promise(function (resolve, reject) {
models.EventInscription.create({ models.EventInscription.create({
@ -44,11 +56,11 @@ const extraMethods = {
source: source, source: source,
valid: valid, valid: valid,
// valid: !(tickets > assistants), // valid: !(tickets > assistants),
reservationId: reservationId reservationId: reservationId,
code_ticket: ticket,
}) })
.then(function (result) { .then(function (result) {
inscription = result.dataValues; resolve(result);
resolve(inscription);
}) })
.catch(function (error) { .catch(function (error) {
reject(error) reject(error)

View File

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