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

309 lines
10 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');
2019-08-20 16:39:05 +00:00
const xlsx = require("node-xlsx");
const fs = require("fs");
const cdnHelper = require('../../helpers/cdn.helper');
2019-07-30 16:24:06 +00:00
2019-08-20 16:39:05 +00:00
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-08-29 18:08:22 +00:00
const extraMethods = {
2019-07-19 17:39:19 +00:00
2019-07-28 20:08:15 +00:00
_getInscriptionById: (id) => {
2019-09-12 12:09:55 +00:00
return models.EventInscription.scope(['includeEventAndVenue', 'includeReservation', 'defaultScope']).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-08-30 18:04:21 +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-08-30 16:48:02 +00:00
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-08-30 18:04:21 +00:00
return models.EventInscription.scope('includeEventAndVenue', 'includeReservation').findAll({
2019-07-26 09:13:33 +00:00
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-09-11 11:01:16 +00:00
//Nos devuelve el número de inscripciones confirmadas para ese evento sin tener en cuenta reservas ni lista de espera
2019-07-25 16:39:18 +00:00
_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
2019-09-12 11:50:39 +00:00
//Validamos la inscripcion la quitamos de las lista de espera y asignamos el usuario que la ha validado
_validateInscription: (inscriptionId, userId) => {
return models.EventInscription.update({
validated: true,
overflowEventId: null,
validateUserId: userId,
},
{where: {
id: inscriptionId,
}});
},
2019-08-29 18:08:22 +00:00
_addMember: (marketingListId, member) => {
// console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasddddddmemberrrrr1');
// console.log(member);
2019-07-29 12:59:01 +00:00
return new Promise(function (resolve, reject) {
2019-08-13 18:00:27 +00:00
if (!marketingListId) { // || !member.validated) {
2019-09-13 14:55:07 +00:00
resolve(member)
2019-07-29 12:59:01 +00:00
} else {
marketing.addMember(marketingListId, member)
.then(function (result) {
2019-08-29 18:08:22 +00:00
resolve(result.ID);
2019-07-29 12:59:01 +00:00
})
.catch(function (error) {
reject(error)
});
}
2019-08-13 18:00:27 +00:00
});
2019-07-29 12:59:01 +00:00
},
2019-08-30 10:03:56 +00:00
_deleteMember: (marketingListId, marketingMemberId) => {
console.log('Elimino miembro de la lista de milchimp>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<', marketingListId, marketingMemberId);
return new Promise(function (resolve, reject) {
if ((!marketingListId) || (!marketingMemberId)) {
resolve();
} else {
resolve(marketing.deleteMember(marketingListId, marketingMemberId))
}
});
},
2019-08-20 16:39:05 +00:00
_getInscriptionsExcel: (user, eventId, callback) => {
models.EventInscription.findAll({
where: {
eventId: eventId,
type: 'regular'
},
include: [{
model: models.Event,
as: 'event',
attributes: ['id', 'name'],
2019-10-02 09:59:29 +00:00
},
2019-10-02 10:27:49 +00:00
/* {
2019-10-02 10:08:55 +00:00
model: models.User,
2019-10-02 09:59:29 +00:00
as: 'user' ,
2019-10-02 10:23:24 +00:00
include: [{ model: models.Entity, attributes: ['id', 'name'], required: false}]
2019-10-02 10:01:20 +00:00
}],
2019-10-02 10:27:49 +00:00
*/ ],
2019-08-20 16:39:05 +00:00
order:[
// [{ model: models.Entity }, 'name', 'ASC'],
// [{ model: models.User },'name', 'ASC'],
],
}).then(function (inscriptions) {
2019-10-02 10:14:43 +00:00
// console.log(inscriptions[c]);
2019-08-20 16:39:05 +00:00
if (inscriptions.length) {
var data = [];
data.push(["Centro educativo", "Número de entrada", "Nombre", "Apellidos", "Email", "Válido"]);
for (var c = 0; c < inscriptions.length; c++) {
var inscription = inscriptions[c];
2019-10-02 08:27:56 +00:00
// console.log(inscription);
2019-10-02 09:07:12 +00:00
if (inscription.user.email == 'marta.lopezbenitez.2003@monaita-mulhacen2222.com')
2019-10-02 09:09:36 +00:00
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>< ', inscriptions[c]);
2019-08-20 16:39:05 +00:00
var code = inscription.code_ticket;
var name = inscription.user.name;
var surname = inscription.user.surname ? inscription.user.surname : "";
var email = inscription.user.email;
2019-10-02 10:24:59 +00:00
var college = inscription.user.entityId ? inscription.user.Entity.name : "";
2019-08-20 16:39:05 +00:00
var valid = inscription.validated ? "Válido" : "No válido";
data.push([college, code, name, surname, email, valid]);
}
var buffer = xlsx.build([{
name: inscriptions[0].event.name,
data: data
}]);
var fileName = cdnHelper.sanitizeFilename(inscriptions[0].event.name + "-inscripciones.xlsx");
var xlsxPath = cdnHelper.getCDNPath('xlsx') + fileName;
var wstream = fs.createWriteStream(xlsxPath);
wstream.write(buffer);
wstream.end();
wstream.on("close", function () {
return callback({
messenger: {
success: true,
message: 'Ok',
code: 'S99001'
},
data: {
path: xlsxPath,
name: fileName
}
}, 200);
});
} else {
return callback({
messenger: {
success: true,
message: 'Ok',
code: 'S99002'
}
}, 200);
}
}).catch(function (error) {
console.log(error);
return callback({
messenger: {
success: false,
message: 'Database error getting inscription.',
code: 'E01004'
}
}, 500);
});
},
2019-08-19 17:54:39 +00:00
2019-07-19 17:39:19 +00:00
};
module.exports = generateService(models.EventInscription, extraMethods);