2019-06-27 05:38:45 +02:00
'use strict' ;
2019-07-08 18:23:32 +02:00
const moment = require ( 'moment-timezone' ) ;
const db = require ( '../../models/index' ) ;
2019-08-08 01:55:00 +02:00
const Op = require ( 'sequelize' ) . Op ;
2019-07-08 18:23:32 +02:00
2019-08-08 14:18:59 +02:00
const {
UI _TIMEZONE ,
BOOKING _CHANGE _PERCENTAGE _CHARGE ,
CHARGE _BOOKING _CHANGE _UNDER _TIME ,
ALLOWED _BOOKING _CANCELLATION _TIME ,
incidentType
} = require ( '../../constants/constants' ) ;
2019-07-08 18:23:32 +02:00
2019-08-21 06:45:43 +02:00
2019-07-08 18:23:32 +02:00
const bulkWriteBookingChangeIncidents = ( incidents ) => {
2019-08-21 06:49:12 +02:00
//TODO: Check if this complete method can be replaced with
// return db.bookingChangeIncident.bulkCreate(incidents)
2019-07-08 18:23:32 +02:00
return new Promise ( ( resolve , reject ) => {
const asyncJobs = [ ] ;
2019-08-21 06:45:43 +02:00
2019-07-08 18:23:32 +02:00
incidents . forEach ( ( incident ) => {
asyncJobs . push ( db . bookingChangeIncident . findOrCreate ( { where : incident , defaults : incident } ) ) ;
} ) ;
Promise . all ( asyncJobs )
2019-08-21 06:45:43 +02:00
. then ( ( ) => resolve ( ) )
2019-07-08 18:23:32 +02:00
. catch ( ( error ) => reject ( error ) ) ;
} ) ;
} ;
2019-08-21 06:45:43 +02:00
const getIncidentsFromChanges = ( changes ) => {
2019-07-08 18:23:32 +02:00
return new Promise ( ( resolve , reject ) => {
if ( Array . isArray ( changes ) ) {
const incidents = [ ] ;
changes . forEach ( ( change ) => {
const { oldReservation , newReservation } = change ;
if ( oldReservation && newReservation ) {
2019-07-09 15:05:36 +02:00
const oldResourceId = oldReservation . resourceId ;
2019-07-08 18:23:32 +02:00
const oldStart = oldReservation . start ? moment . utc ( oldReservation . start ) : null ;
const oldEnd = oldReservation . end ? moment . utc ( oldReservation . end ) : null ;
const newStart = newReservation . start ? moment . utc ( newReservation . start ) : null ;
const newEnd = newReservation . end ? moment . utc ( newReservation . end ) : null ;
2019-07-19 12:47:16 +02:00
const reservationCreationTimestamp = newReservation . createdAt ? moment . utc ( newReservation . createdAt ) : null ;
2019-07-08 18:23:32 +02:00
const reservationTimezone = newReservation . timezone ? newReservation . timezone : UI _TIMEZONE ;
2019-07-19 12:16:20 +02:00
const reservationHourlyRate = oldReservation . hourlyRate ? oldReservation . hourlyRate : newReservation . hourlyRate ;
2019-07-16 02:51:07 +02:00
const canceled = newReservation . canceled ;
2019-07-08 18:23:32 +02:00
if ( oldStart && oldEnd && newStart && newEnd && reservationHourlyRate ) {
const oldReservationLength = oldEnd . diff ( oldStart , 'hours' , true ) ;
const newReservationLength = newEnd . diff ( newStart , 'hours' , true ) ;
2019-08-08 14:18:59 +02:00
const differenceFromNow = oldStart . diff ( moment . utc ( ) , 'minutes' ) ;
2019-07-08 18:23:32 +02:00
2019-08-21 06:45:43 +02:00
console . log ( 'Change detected : ' ) ;
console . log ( '\tOld reservation :' ) ;
console . log ( '\t\tResource : ' , oldResourceId ) ;
console . log ( '\t\tStart : ' , oldStart . format ( ) ) ;
console . log ( '\t\tEnd : ' , oldEnd . format ( ) ) ;
console . log ( '\t\tLength : ' , oldReservationLength ) ;
console . log ( '\tNew Reservation :' ) ;
console . log ( '\t\tResource : ' , newReservation . resourceId ) ;
console . log ( '\t\tStart : ' , newStart . format ( ) ) ;
console . log ( '\t\tEnd : ' , newEnd . format ( ) ) ;
console . log ( '\t\tLength : ' , newReservationLength ) ;
console . log ( '\t\tCanceled : ' , canceled ) ;
console . log ( '\t---------------------------------' ) ;
console . log ( '\tDifference : ' , differenceFromNow , 'minutes' ) ;
console . log ( '' ) ;
console . log ( '\tIs booking changed too late ? ' , differenceFromNow , ' < ' , CHARGE _BOOKING _CHANGE _UNDER _TIME ) ;
2019-08-08 14:18:59 +02:00
if ( differenceFromNow < CHARGE _BOOKING _CHANGE _UNDER _TIME ) {
2019-07-16 02:51:07 +02:00
const { reservationId , memberId , resourceId } = newReservation ;
2019-08-21 06:45:43 +02:00
console . log ( '\t\tYes' ) ;
console . log ( '\tIs booking canceled ?' ) ;
2019-07-16 02:51:07 +02:00
if ( ! canceled ) {
2019-08-21 06:45:43 +02:00
console . log ( '\t\tNo' ) ;
2019-07-16 02:51:07 +02:00
// Check if new reservation is on same day
const sameDay = oldStart . tz ( reservationTimezone ) . isSame ( newStart . tz ( reservationTimezone ) , 'day' ) ;
2019-08-21 06:45:43 +02:00
console . log ( '\tIs new reservation on the same day ?' ) ;
2019-07-16 02:51:07 +02:00
if ( sameDay ) {
2019-08-21 06:45:43 +02:00
console . log ( '\t\tYes' ) ;
2019-07-16 02:51:07 +02:00
// Reservation moved in same day
// Check if member shortened the reservation
2019-08-21 06:45:43 +02:00
console . log ( '\tIs reservation shortened ? ' , newReservationLength , ' < ' , oldReservationLength ) ;
2019-07-16 02:51:07 +02:00
if ( newReservationLength < oldReservationLength ) {
2019-08-21 06:45:43 +02:00
console . log ( '\t\tYes' ) ;
2019-07-16 02:51:07 +02:00
const differenceInLength = oldReservationLength - newReservationLength ;
const chargeFee = differenceInLength * reservationHourlyRate * BOOKING _CHANGE _PERCENTAGE _CHARGE / 100 ;
2019-08-21 06:45:43 +02:00
console . log ( '\t\t\tThis is [shortened] incident ! Charge fee : ' , chargeFee ) ;
2019-07-16 02:51:07 +02:00
const incident = {
reservationId ,
memberId ,
oldResourceId : oldResourceId || resourceId ,
newResourceId : resourceId ,
oldBookingStart : oldReservation . start ,
oldBookingEnd : oldReservation . end ,
newBookingStart : newReservation . start ,
newBookingEnd : newReservation . end ,
incidentType : incidentType . BOOKING _SHORTENED ,
chargeFee ,
} ;
incidents . push ( incident ) ;
2019-08-21 06:45:43 +02:00
} else {
console . log ( '\t\tNo' ) ;
2019-07-16 02:51:07 +02:00
}
} else {
2019-08-21 06:45:43 +02:00
console . log ( '\t\tNo' ) ;
2019-07-16 02:51:07 +02:00
// Reservation moved to another day
// Add cancellation charge
const chargeFee = oldReservationLength * reservationHourlyRate * BOOKING _CHANGE _PERCENTAGE _CHARGE / 100 ;
2019-08-21 06:45:43 +02:00
console . log ( '\t\t\tThis is incident ! Charge fee : ' , chargeFee ) ;
2019-07-08 18:23:32 +02:00
const incident = {
reservationId ,
memberId ,
2019-07-16 02:51:07 +02:00
oldResourceId : oldResourceId || resourceId ,
newResourceId : resourceId ,
2019-07-08 18:23:32 +02:00
oldBookingStart : oldReservation . start ,
oldBookingEnd : oldReservation . end ,
newBookingStart : newReservation . start ,
newBookingEnd : newReservation . end ,
2019-07-16 02:51:07 +02:00
incidentType : incidentType . BOOKING _MOVED _TO _ANOTHER _DAY ,
2019-07-08 18:23:32 +02:00
chargeFee ,
} ;
incidents . push ( incident ) ;
}
} else {
2019-08-21 06:45:43 +02:00
console . log ( '\t\tYes' ) ;
2019-07-25 02:00:27 +02:00
const differenceFromCreation = moment . utc ( ) . diff ( reservationCreationTimestamp , 'minutes' ) ;
2019-07-19 12:47:16 +02:00
2019-08-21 06:45:43 +02:00
console . log ( '\tIs booking created in past ' , ALLOWED _BOOKING _CANCELLATION _TIME , ' minutes ?' , differenceFromCreation , ' > ' , ALLOWED _BOOKING _CANCELLATION _TIME ) ;
2019-07-19 12:47:16 +02:00
if ( differenceFromCreation > ALLOWED _BOOKING _CANCELLATION _TIME ) {
2019-08-21 06:45:43 +02:00
console . log ( '\t\tYes' ) ;
2019-08-08 13:18:37 +02:00
const chargeFee = reservationHourlyRate * oldReservationLength * BOOKING _CHANGE _PERCENTAGE _CHARGE / 100 ;
2019-08-21 06:45:43 +02:00
console . log ( '\t\t\tThis is [cancellation] incident ! charge fee : ' , chargeFee ) ;
2019-07-19 12:47:16 +02:00
const incident = {
reservationId ,
memberId ,
oldResourceId : oldResourceId || resourceId ,
newResourceId : null ,
oldBookingStart : oldReservation . start ,
oldBookingEnd : oldReservation . end ,
newBookingStart : null ,
newBookingEnd : null ,
incidentType : incidentType . BOOKING _CANCELED _LATE ,
chargeFee ,
} ;
incidents . push ( incident ) ;
}
2019-07-08 18:23:32 +02:00
}
}
2019-08-21 06:45:43 +02:00
else {
console . log ( '\t\tNo' ) ;
}
2019-07-08 18:23:32 +02:00
}
}
} ) ;
2019-08-21 06:45:43 +02:00
resolve ( incidents ) ;
2019-07-08 18:23:32 +02:00
} else {
reject ( 'Input argument is not an array !' ) ;
}
} ) ;
} ;
2019-08-08 01:55:00 +02:00
const getChargedCanceledReservations = ( reservationIds ) => {
const filters = {
reservationId : {
[ Op . in ] : reservationIds ,
} ,
incidentType : incidentType . BOOKING _CANCELED _LATE ,
} ;
2019-08-13 15:36:14 +02:00
const attributes = [ 'memberId' , 'oldBookingStart' , 'oldBookingEnd' , 'chargeFee' ] ;
2019-08-08 01:55:00 +02:00
return db . bookingChangeIncident . findAll ( { attributes , where : filters } ) ;
} ;
2019-07-08 18:23:32 +02:00
module . exports = {
2019-08-08 01:55:00 +02:00
getChargedCanceledReservations ,
2019-08-21 06:45:43 +02:00
getIncidentsFromChanges ,
bulkWriteBookingChangeIncidents ,
2019-07-08 18:23:32 +02:00
} ;