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

207 lines
6.3 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');
2019-07-29 12:59:01 +00:00
const marketing = require('../../helpers/mailchimp.helper')
2019-07-30 16:24:06 +00:00
const Sequelize = require('sequelize');
moment.locale('es');
2019-07-19 17:39:19 +00:00
2019-07-26 09:23:06 +00:00
function generateNewCodeTicket() {
2019-08-09 16:03:49 +00:00
let longitud = 8;
2019-08-09 15:48:04 +00:00
let timestamp = +new Date;
2019-07-26 09:23:06 +00:00
var _getRandomInt = function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
2019-08-09 15:50:26 +00:00
var ts = timestamp.toString();
var parts = ts.split("").reverse();
var id = "";
2019-08-09 16:03:49 +00:00
for (var i = 0; i < longitud; ++i) {
var index = _getRandomInt(0, longitud - 1);
2019-08-09 15:50:26 +00:00
id += parts[index];
2019-07-26 09:23:06 +00:00
}
2019-08-09 15:50:26 +00:00
return id;
2019-07-26 09:23:06 +00:00
}
2019-07-19 17:39:19 +00:00
const extraMethods = {
2019-07-28 20:08:15 +00:00
_getInscriptionById: (id) => {
return models.EventInscription.scope('includeEventAndVenue').findOne({
2019-07-20 16:11:43 +00:00
where: {
2019-07-28 20:08:15 +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) => {
2019-07-26 09:13:33 +00:00
return models.EventInscription.scope('includeEventAndVenue').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-08-20 15:37:53 +00:00
_getInscriptionByEvent: (eventId) => {
return models.EventInscription.scope('defaultScope').findAll({
where: {
eventId: eventId,
},
})
},
2019-07-25 18:43:03 +00:00
_getInscriptionsUser: (userId) => {
2019-07-26 09:13:33 +00:00
return models.EventInscription.scope('includeEventAndVenue').findAll({
attributes: {
exclude: ['marketing_memberId', 'overflowEventId', 'createdAt', 'updatedAt', 'userId', 'eventId', 'validateUserId']
},
2019-07-25 18:43:03 +00:00
where: {
userId: userId
},
})
},
2019-07-30 16:24:06 +00:00
_getInscriptionsOfNextEventsUser: (userId) => {
return models.EventInscription.count({
include: [{ model: models.Event,
as: 'event',
where: {
end_date: {[Sequelize.Op.gte]: moment().utc()},
}
}],
where: { userId: userId },
});
},
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 }
});
},
2019-08-13 18:00:27 +00:00
_updateMarketingMemberOfInscription: (id, marketingMemberId) => {
return models.EventInscription.update({
marketing_memberId: marketingMemberId,
},
{
where: { id: id }
});
},
2019-07-25 16:39:18 +00:00
_createInscription: (eventId, userId, type, validated, source, reservationId, overflowEventId) => {
2019-07-26 09:23:06 +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-26 09:23:06 +00:00
code_ticket: ('ENT-' + generateNewCodeTicket()),
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-29 12:59:01 +00:00
_addMember: (marketingListId, user, inscription, reservation) => {
console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasddddddmemberrrrr1');
2019-08-13 18:00:27 +00:00
console.log(user, reservation);
2019-07-29 12:59:01 +00:00
var member = {
2019-08-13 18:00:27 +00:00
marketing_memberId: null,
2019-07-29 12:59:01 +00:00
email: user.email,
name: user.name,
surname: user.surname,
source: inscription.source,
reservation_code: (reservation) ? reservation.reservation_code : null,
code_ticket: inscription.code_ticket,
validated: inscription.validated,
color: (reservation) ? reservation.color : 'white',
description: (reservation) ? reservation.description : 'entrada libre',
2019-08-13 18:00:27 +00:00
entity: (reservation) ? reservation.Entity.name : user.entityId,
2019-07-29 12:59:01 +00:00
userId: user.id
}
return new Promise(function (resolve, reject) {
2019-08-13 18:00:27 +00:00
if (!marketingListId) { // || !member.validated) {
2019-07-29 12:59:01 +00:00
resolve(member)
} else {
marketing.addMember(marketingListId, member)
.then(function (result) {
2019-08-13 18:00:27 +00:00
member.marketing_memberId = result.ID;
2019-07-29 12:59:01 +00:00
resolve(member);
})
.catch(function (error) {
reject(error)
});
}
2019-08-13 18:00:27 +00:00
});
2019-07-29 12:59:01 +00:00
},
2019-08-19 17:54:39 +00:00
2019-07-19 17:39:19 +00:00
};
module.exports = generateService(models.EventInscription, extraMethods);