app2-api/modules/events/events_inscriptions.service.js

104 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-07-19 17:39:19 +00:00
/* global Venue */
'use strict';
const _ = require('lodash');
2019-07-21 13:30:49 +00:00
const moment = require('moment');
2019-07-19 17:39:19 +00:00
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
const models = require('../../core/models');
const extraMethods = {
2019-07-25 16:39:18 +00:00
_getInscriptionById: (Id) => {
2019-07-20 16:11:43 +00:00
return models.EventInscription.findOne({
where: {
2019-07-25 16:39:18 +00:00
id: Id,
2019-07-20 16:11:43 +00:00
},
2019-07-19 17:39:19 +00:00
})
},
2019-07-25 16:39:18 +00:00
_getInscriptionByEventAndUser: (eventId, userId) => {
return models.EventInscription.findOne({
2019-07-20 19:23:05 +00:00
where: {
eventId: eventId,
2019-07-25 16:39:18 +00:00
userId: userId
2019-07-20 19:23:05 +00:00
},
})
},
2019-07-19 17:39:19 +00:00
2019-07-25 16:39:18 +00:00
//Nos devuelve el número de inscripciones confirmadas para ese evento sin tener en cuenta reservas
_getCountInscriptionsWithoutReservationAndOverflow: (eventId) => {
2019-07-20 19:23:05 +00:00
return models.EventInscription.count({
2019-07-19 17:39:19 +00:00
where: {
2019-07-20 19:23:05 +00:00
eventId: eventId,
2019-07-25 16:39:18 +00:00
reservationId: null,
overflowEventId: null
2019-07-19 17:39:19 +00:00
},
2019-07-20 19:23:05 +00:00
})
},
//Nos devuelve el número de inscripciones realizadas con esa reserva
_getCountInscriptionsWithReservation: (reservationId) => {
return models.EventInscription.count({
where: {
reservationId: reservationId,
},
2019-07-19 17:39:19 +00:00
})
},
2019-07-25 16:39:18 +00:00
//Nos devuelve el número de inscripciones realizadas con esa reserva
_getCountInscriptionsWithOverflowEventId: (overflowEventId) => {
return models.EventInscription.count({
where: {
overflowEventId: overflowEventId,
},
})
},
2019-07-19 17:39:19 +00:00
2019-07-25 16:39:18 +00:00
_updateReservationOfInscription: (id, reservationId) => {
return models.EventInscription.update ({
reservationId: reservationId,
},
{
where: { id: id }
});
},
_createInscription: (eventId, userId, type, validated, source, reservationId, overflowEventId) => {
2019-07-20 19:23:05 +00:00
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<valores de la inscripcion');
2019-07-25 16:39:18 +00:00
console.log(eventId, userId, type, validated, source, reservationId, overflowEventId);
2019-07-19 17:39:19 +00:00
return new Promise(function (resolve, reject) {
2019-07-20 16:11:43 +00:00
models.EventInscription.create({
eventId: eventId,
2019-07-21 13:30:49 +00:00
date: moment().utc(),
2019-07-20 16:11:43 +00:00
userId: userId,
type: type,
2019-07-25 16:39:18 +00:00
code_ticket: ('ENT-' + (Date.now() + Math.random()).toString()),
2019-07-20 16:11:43 +00:00
source: source,
2019-07-21 13:30:49 +00:00
validated: validated,
2019-07-20 19:23:05 +00:00
reservationId: reservationId,
2019-07-21 13:30:49 +00:00
overflowEventId: overflowEventId,
2019-07-19 17:39:19 +00:00
})
.then(function (result) {
2019-07-20 19:23:05 +00:00
resolve(result);
2019-07-19 17:39:19 +00:00
})
.catch(function (error) {
reject(error)
});
});
},
2019-07-25 16:39:18 +00:00
_deleteInscription: (id) => {
//habria que poner el idusuario para asegurar que otro usuario no borra una inscripcion de otro
return models.EventInscription.destroy({
where: {
id: id,
}
});
},
2019-07-19 17:39:19 +00:00
};
module.exports = generateService(models.EventInscription, extraMethods);