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

396 lines
10 KiB
JavaScript
Raw Normal View History

2019-07-19 17:39:19 +00:00
/* global Venue */
2022-02-18 19:32:30 +00:00
"use strict";
const _ = require("lodash");
const moment = require("moment");
2022-02-23 18:27:58 +00:00
const { generateService, parseParamsToFindOptions } = require("../../helpers/service.helper");
2022-02-18 19:32:30 +00:00
const models = require("../../core/models");
const Sequelize = require("sequelize");
2019-08-20 16:39:05 +00:00
const xlsx = require("node-xlsx");
const fs = require("fs");
2022-02-18 19:32:30 +00:00
const cdnHelper = require("../../helpers/cdn.helper");
2019-07-30 16:24:06 +00:00
2022-02-18 19:32:30 +00:00
moment.locale("es");
2019-07-19 17:39:19 +00:00
2019-07-26 09:23:06 +00:00
function generateNewCodeTicket() {
2022-02-18 19:32:30 +00:00
let longitud = 8;
let timestamp = +new Date();
2019-07-26 09:23:06 +00:00
2022-02-18 19:32:30 +00:00
var _getRandomInt = function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
2019-07-26 09:23:06 +00:00
2022-02-18 19:32:30 +00:00
var ts = timestamp.toString();
var parts = ts.split("").reverse();
var id = "";
2019-07-26 09:23:06 +00:00
2022-02-18 19:32:30 +00:00
for (var i = 0; i < longitud; ++i) {
var index = _getRandomInt(0, longitud - 1);
id += parts[index];
}
2019-08-09 15:50:26 +00:00
2022-02-18 19:32:30 +00:00
return id;
2019-07-26 09:23:06 +00:00
}
2022-02-18 19:32:30 +00:00
const extraMethods = {
_fillInscriptionColor: (inscription) => {
if (inscription && inscription.type) {
const isVirtual = inscription.type === "online" || inscription.type === "online group";
2024-05-31 10:06:36 +00:00
if (inscription.reservationId === null && !isVirtual) {
// Inscripción normal
2024-05-31 10:06:36 +00:00
inscription.color = "green";
}
}
return inscription;
},
_fillInscriptionsColor: (inscriptions) => {
if (inscriptions && inscriptions.length) {
return inscriptions.map(extraMethods._fillInscriptionColor);
} else {
return inscriptions;
}
},
2022-02-18 19:32:30 +00:00
_getInscriptionById: (id) => {
2022-02-23 18:27:58 +00:00
return models.EventInscription.scope(["includeEventAndVenue", "includeReservation", "defaultScope"]).findOne({
2022-02-18 19:32:30 +00:00
where: {
id: id,
},
});
},
_getInscriptionByEventAndUser: (eventId, userId) => {
return models.EventInscription.scope(["includeEventAndVenue"]).findOne({
where: {
eventId: eventId,
userId: userId,
},
});
},
_getInscriptionByEvent: (eventId) => {
2022-06-01 10:13:04 +00:00
return models.EventInscription.scope("defaultScope").findAll({
where: {
eventId: eventId,
type: ["onsite", "onsite group", "regular", "reservation"],
},
});
},
_getInscriptionOnlineByEvent: (eventId) => {
2022-06-13 14:01:42 +00:00
return models.EventInscriptionOnline.scope("defaultScope").findAll({
2022-02-18 19:32:30 +00:00
where: {
eventId: eventId,
2022-06-13 14:01:42 +00:00
//type: ["online", "online group"],
2022-02-18 19:32:30 +00:00
},
});
},
_getInscriptionByEventAndValidated: (eventId, validated) => {
return models.EventInscription.scope("defaultScope").findAll({
where: {
validated: validated,
eventId: eventId,
},
});
},
_getInscriptionByEventFromPartner: (eventId) => {
return models.EventInscription.scope("defaultScope").findAll({
where: {
eventId: eventId,
reservationId: { [Sequelize.Op.ne]: null },
},
});
},
_getInscriptionsUser: (userId) => {
2022-02-23 18:27:58 +00:00
return models.EventInscription.scope("includeEventAndVenue", "includeReservation").findAll({
2022-02-18 19:32:30 +00:00
attributes: {
exclude: [
"marketing_memberId",
"overflowEventId",
"createdAt",
"updatedAt",
"userId",
"eventId",
"validateUserId",
],
},
where: {
userId: userId,
},
});
},
_getInscriptionsOfNextEventsUser: (userId) => {
return models.EventInscription.count({
include: [
{
model: models.Event,
as: "event",
where: {
end_date: { [Sequelize.Op.gte]: moment().utc() },
},
2019-08-13 18:00:27 +00:00
},
2022-02-18 19:32:30 +00:00
],
where: { userId: userId },
});
},
//Nos devuelve el número de inscripciones confirmadas para ese evento sin tener en cuenta reservas ni lista de espera
_getCountInscriptionsWithoutReservationAndOverflow: (eventId) => {
return models.EventInscription.count({
where: {
eventId: eventId,
reservationId: null,
overflowEventId: null,
type: ["onsite", "onsite group", "regular", "reservation"],
2022-02-18 19:32:30 +00:00
},
});
},
//Nos devuelve el número de inscripciones realizadas con esa reserva
_getCountInscriptionsWithReservation: (reservationId) => {
return models.EventInscription.count({
where: {
reservationId: reservationId,
},
});
},
//Nos devuelve el número de inscripciones realizadas con esa reserva
_getCountInscriptionsWithOverflowEventId: (overflowEventId) => {
return models.EventInscription.count({
where: {
overflowEventId: overflowEventId,
},
});
},
_updateReservationOfInscription: (id, reservationId) => {
return models.EventInscription.update(
{
reservationId: reservationId,
},
{
where: { id: id },
}
);
},
_updateMarketingMemberOfInscription: (id, marketingMemberId) => {
return models.EventInscription.update(
{
marketing_memberId: marketingMemberId,
},
{
where: { id: id },
}
);
},
2022-02-23 18:27:58 +00:00
_createInscription: (eventId, userId, type, validated, source, reservationId, overflowEventId) => {
2022-02-18 19:32:30 +00:00
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<valores de la inscripcion");
2022-02-23 18:27:58 +00:00
console.log(eventId, userId, type, validated, source, reservationId, overflowEventId);
2022-02-18 19:32:30 +00:00
return new Promise(function (resolve, reject) {
models.EventInscription.create({
eventId: eventId,
date: moment().utc(),
userId: userId,
type: type,
code_ticket: "ENT-" + generateNewCodeTicket(),
source: source,
validated: validated,
reservationId: reservationId,
overflowEventId: overflowEventId,
})
.then(function (result) {
resolve(result);
})
.catch(function (error) {
reject(error);
2019-08-30 10:03:56 +00:00
});
2022-02-18 19:32:30 +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,
},
});
},
_deleteInscriptionByUser: (userId) => {
return models.EventInscription.destroy({
where: {
userId: userId,
},
});
},
//Elimina todas las inscripciones de una determinada reserva/invitacion
_deleteInscriptionsByReservation: (reservationId) => {
return models.EventInscription.destroy({
where: {
reservationId: reservationId,
},
});
},
2022-02-18 19:32:30 +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,
},
}
);
},
//Validamos la inscripcion del tutor que hizo la reserva
_validateInscriptionTutorOfReservation: (reservationId, userId) => {
return models.EventInscription.update(
{
validated: true,
overflowEventId: null,
validateUserId: userId,
},
{
where: {
reservationId: reservationId,
userId: userId,
},
}
);
},
2022-03-11 11:37:03 +00:00
_getInscriptionsWithoutMemberId: (eventId) => {
2022-03-21 17:09:43 +00:00
return models.EventInscription.scope(["includeEventAndVenue", "includeReservation", "defaultScope"]).findAll({
where: {
eventId: eventId,
2024-05-31 10:06:36 +00:00
marketing_memberId: null,
2022-03-21 17:09:43 +00:00
},
});
},
2022-02-18 19:32:30 +00:00
_getInscriptionsExcel: (user, eventId, callback) => {
2022-06-01 10:13:04 +00:00
console.log("DESCARGA EXCEL INSCRIPCIONES TOTALES>>>>>>>> ");
console.log(">>>>>>>>>>>>>>>>>>>><consulta con eventId:", eventId);
2022-02-18 19:32:30 +00:00
models.EventInscription.findAll({
where: {
eventId: eventId,
type: ["onsite", "onsite group", "regular", "reservation"],
2022-02-18 19:32:30 +00:00
},
include: [
{
model: models.Event,
as: "event",
attributes: ["id", "name"],
},
/* {
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
}],
2022-02-18 19:32:30 +00:00
*/
],
order: [
// [{ model: models.Entity }, 'name', 'ASC'],
// [{ model: models.User },'name', 'ASC'],
],
})
.then(function (inscriptions) {
// console.log(inscriptions[c]);
if (inscriptions.length) {
var data = [];
2022-02-23 18:27:58 +00:00
data.push(["Centro educativo", "Número de entrada", "Nombre", "Apellidos", "Email", "Válido"]);
2022-02-18 19:32:30 +00:00
for (var c = 0; c < inscriptions.length; c++) {
var inscription = inscriptions[c];
// console.log(inscription);
var code = inscription.code_ticket;
var name = inscription.user.name;
2022-02-23 18:27:58 +00:00
var surname = inscription.user.surname ? inscription.user.surname : "";
2022-02-18 19:32:30 +00:00
var email = inscription.user.email;
2022-02-23 18:27:58 +00:00
var college = inscription.user.entityId ? inscription.user.Entity.name : "";
2022-02-18 19:32:30 +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.substr(0, 31),
data: data,
},
]);
2022-02-23 18:27:58 +00:00
var fileName = cdnHelper.sanitizeFilename(inscriptions[0].event.name + "-inscripciones.xlsx");
2022-02-18 19:32:30 +00:00
var xlsxPath = cdnHelper.getCDNPath("xlsx") + fileName;
var wstream = fs.createWriteStream(xlsxPath);
wstream.write(buffer);
wstream.end();
wstream.on("close", function () {
return callback(
{
2019-08-20 16:39:05 +00:00
messenger: {
2022-02-18 19:32:30 +00:00
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-07-19 17:39:19 +00:00
};
2022-02-18 19:32:30 +00:00
module.exports = generateService(models.EventInscription, extraMethods);