2019-07-19 17:39:19 +00:00
/* global Events Reservations */
'use strict' ;
const _ = require ( 'lodash' ) ;
2019-08-19 17:54:39 +00:00
const moment = require ( 'moment' ) ;
2019-08-14 17:49:38 +00:00
const Sequelize = require ( 'sequelize' ) ;
2019-07-19 17:39:19 +00:00
const { generateService , parseParamsToFindOptions } = require ( '../../helpers/service.helper' ) ;
const models = require ( '../../core/models' ) ;
2019-08-19 17:54:39 +00:00
const xlsx = require ( "node-xlsx" ) ;
const fs = require ( "fs" ) ;
const cdnHelper = require ( '../../helpers/cdn.helper' ) ;
2019-07-19 17:39:19 +00:00
const extraMethods = {
2019-07-19 19:36:20 +00:00
_getReservaByCode : ( eventId , code ) => {
return models . EventReservation . findOne ( {
where : { reservation _code : code , eventId : eventId } ,
include : [ {
model : models . Event ,
2019-08-13 18:00:27 +00:00
} , { model : models . Entity } ] ,
2019-07-19 17:39:19 +00:00
} )
} ,
2019-08-14 17:49:38 +00:00
_getPartners : ( eventId ) => {
2019-08-15 12:33:21 +00:00
return models . EventReservation . findAll ( {
2019-08-14 17:49:38 +00:00
where : { eventId : eventId } ,
2019-08-20 19:13:30 +00:00
include : [ { model : models . Entity ,
include : [ { model : models . EntityType , as : 'types' , where : { alias : 'partner' } } ] ,
} ] ,
2019-08-15 18:09:50 +00:00
order : [ [ { model : models . Entity } , 'name' , 'asc' ] ] ,
2019-08-14 17:49:38 +00:00
} )
} ,
2019-08-20 19:13:30 +00:00
_getColleges : ( eventId ) => {
return models . EventReservation . findAll ( {
where : { eventId : eventId } ,
include : [ {
model : models . Entity ,
include : [ { model : models . EntityType , as : 'types' , where : { alias : 'college' } } ] ,
} ] ,
order : [ [ { model : models . Entity } , 'name' , 'asc' ] ] ,
} )
} ,
2019-07-25 16:39:18 +00:00
_updateConfirmedReservation : ( id , confirmed ) => {
2019-07-20 19:23:05 +00:00
return new Promise ( function ( resolve , reject ) {
models . EventReservation . update (
{
confirmed : confirmed ,
} ,
{
where : { id : id }
} )
. then ( function ( result ) {
resolve ( ( result [ 0 ] === 1 ) ) ;
} )
. catch ( function ( error ) {
reject ( error )
} ) ;
} ) ;
} ,
2019-07-19 17:39:19 +00:00
2019-07-25 16:39:18 +00:00
_updateSoldOutReservation : ( id , sold _out ) => {
return new Promise ( function ( resolve , reject ) {
models . EventReservation . update (
{
sold _out : sold _out ,
} ,
{
where : { id : id }
} )
. then ( function ( result ) {
resolve ( ( result [ 0 ] === 1 ) ) ;
} )
. catch ( function ( error ) {
reject ( error )
} ) ;
} ) ;
} ,
2019-08-19 17:54:39 +00:00
_getReservationsExcel : ( user , eventId , partnerId , callback ) => {
models . EventReservation . findAll ( {
where : {
eventId : eventId ,
} ,
include : [ {
model : models . Event ,
attributes : [ 'id' , 'name' ] ,
} , {
model : models . Entity ,
} ,
{
model : models . EventInscription ,
as : 'inscriptions' ,
required : false ,
} ] ,
order : [
[ { model : models . Entity } , 'name' , 'ASC' ] ,
[ 'reservation_code' , 'ASC' ] ,
[ { model : models . EventInscription , as : 'inscriptions' } , 'date' , 'ASC' ]
]
} ) . then ( function ( reservations ) {
if ( reservations . length ) {
var data = [ ] ;
data . push ( [ "Congreso: " + reservations [ 0 ] . Event . name ] ) ;
data . push ( [ "Fecha del listado: " + moment ( ) . toString ( ) ] ) ;
data . push ( [ ] ) ;
data . push ( [ "Nombre" , "Apellidos" , "Email" , "Válido" , "Vía" , "N. entrada" ] ) ;
var currentReservation = '' ;
var currentPartner = '' ;
reservations . forEach ( function ( reserva ) {
if ( currentPartner != reserva . Entity . name ) {
currentPartner = reserva . Entity . name ;
data . push ( [ ] ) ;
data . push ( [ ] ) ;
} ;
if ( currentReservation != reserva . reservation _code ) {
currentReservation = reserva . reservation _code ;
data . push ( [ ] ) ;
data . push ( [ "Partner: " + reserva . Entity . name + " CÓDIGO: " + reserva . reservation _code , "Tipo: " + reserva . description , "Reservadas: " + reserva . confirmed , "Aforo: " + reserva . assistants ] ) ;
data . push ( [ "Nombre" , "Apellidos" , "Email" , "Válido" , "Vía" , "N. entrada" ] ) ;
} ;
reserva . inscriptions . forEach ( function ( inscription ) {
data . push ( [ inscription . user . name , inscription . user . surname , inscription . user . email , inscription . validated ? "Confirmado" : "En lista de espera" , inscription . source , inscription . code _ticket ] ) ;
} ) ;
} ) ;
var buffer = xlsx . build ( [ {
name : reservations [ 0 ] . Event . name ,
data : data ,
} ] , {
'!cols' : [ { wch : 15 } , { wch : 25 } , { wch : 30 } , { wch : 10 } , { wch : 20 } , { wch : 20 } , { wch : 5 } ]
} ) ;
var fileName = cdnHelper . sanitizeFilename ( reservations [ 0 ] . Event . name + "-invitaciones.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 invitations.' ,
code : 'E01004'
}
} , 500 ) ;
} ) ;
} ,
2019-07-19 17:39:19 +00:00
} ;
module . exports = generateService ( models . EventReservation , extraMethods ) ;