Files
old-psihologija/services/integration/bookingChangeCharges.js

193 lines
10 KiB
JavaScript
Raw Normal View History

'use strict';
2019-07-08 18:23:32 +02:00
const moment = require('moment-timezone');
const db = require('../../models/index');
const Op = require('sequelize').Op;
2019-07-08 18:23:32 +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) => {
//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){
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;
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);
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);
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-08-21 06:45:43 +02:00
console.log('\tIs booking created in past ', ALLOWED_BOOKING_CANCELLATION_TIME, ' minutes ?', differenceFromCreation, ' > ', ALLOWED_BOOKING_CANCELLATION_TIME);
if (differenceFromCreation > ALLOWED_BOOKING_CANCELLATION_TIME){
2019-08-21 06:45:43 +02:00
console.log('\t\tYes');
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);
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 !');
}
});
};
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'];
return db.bookingChangeIncident.findAll({attributes, where: filters});
};
2019-07-08 18:23:32 +02:00
module.exports = {
getChargedCanceledReservations,
2019-08-21 06:45:43 +02:00
getIncidentsFromChanges,
bulkWriteBookingChangeIncidents,
2019-07-08 18:23:32 +02:00
};