From 0c92febe52544501870924ef2f718a0c6686ea14 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Fri, 19 Jul 2019 22:25:22 +0200 Subject: [PATCH] Bug fix for bookings --- constants/constants.js | 3 ++ environment.env | 2 ++ services/integration/bookingChangeCharges.js | 37 +++++++++++--------- services/officeRnD/bookings.js | 2 +- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/constants/constants.js b/constants/constants.js index 03443e5..d339663 100644 --- a/constants/constants.js +++ b/constants/constants.js @@ -81,6 +81,8 @@ const UNSCHEDULED_CHARGE_PRICE = parseFloat(process.env.UNSCHEDULED_USE_CHARGE_P const BOOKING_CHANGE_PERCENTAGE_CHARGE = parseInt(process.env.BOOKING_CHANGE_PERCENTAGE_CHARGE) || 100; +const ALLOWED_BOOKING_CANCELLATION_TIME= parseInt(process.env.ALLOWED_BOOKING_CANCELLATION_TIME) || 30; + module.exports = { VALID_CSV_HEADERS, USER_ENTRY_EVENT, @@ -98,4 +100,5 @@ module.exports = { UNSCHEDULED_TIME_RESOLUTION, UNSCHEDULED_CHARGE_PRICE, BOOKING_CHANGE_PERCENTAGE_CHARGE, + ALLOWED_BOOKING_CANCELLATION_TIME, }; diff --git a/environment.env b/environment.env index 1ea3b01..b4f3c76 100644 --- a/environment.env +++ b/environment.env @@ -20,3 +20,5 @@ UNLOCK_5=Price for unlocked door, sixth month UNLOCK_STREAK_REPAIR_AFTER=Number of months without incidents to reset user incident level BOOKING_CHANGE_PERCENTAGE_CHARGE=Percentage of hourly reate to apply for cancellation-like charges + +ALLOWED_BOOKING_CANCELLATION_TIME=Time from creation (in minutes) in which cancellation is not charged diff --git a/services/integration/bookingChangeCharges.js b/services/integration/bookingChangeCharges.js index 5163d74..cbe26c4 100644 --- a/services/integration/bookingChangeCharges.js +++ b/services/integration/bookingChangeCharges.js @@ -3,7 +3,7 @@ const moment = require('moment-timezone'); const db = require('../../models/index'); -const { UI_TIMEZONE, BOOKING_CHANGE_PERCENTAGE_CHARGE, incidentType } = require('../../constants/constants'); +const { UI_TIMEZONE, BOOKING_CHANGE_PERCENTAGE_CHARGE, ALLOWED_BOOKING_CANCELLATION_TIME, incidentType } = require('../../constants/constants'); const bulkWriteBookingChangeIncidents = (incidents) => { return new Promise((resolve, reject) => { @@ -33,9 +33,10 @@ const chargeBookingChanges = (changes) => { 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; const reservationTimezone = newReservation.timezone ? newReservation.timezone : UI_TIMEZONE; - const reservationHourlyRate = oldReservation.hourlyRate ? oldReservation.hourlyRate : undefined; + const reservationHourlyRate = oldReservation.hourlyRate ? oldReservation.hourlyRate : newReservation.hourlyRate; const canceled = newReservation.canceled; if (oldStart && oldEnd && newStart && newEnd && reservationHourlyRate){ @@ -96,21 +97,25 @@ const chargeBookingChanges = (changes) => { incidents.push(incident); } }else{ - const chargeFee = 2 * reservationHourlyRate * oldReservationLength * BOOKING_CHANGE_PERCENTAGE_CHARGE / 100; - 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, - }; + const differenceFromCreation = reservationCreationTimestamp.diff(moment.utc(), 'minutes'); - incidents.push(incident); + if (differenceFromCreation > ALLOWED_BOOKING_CANCELLATION_TIME){ + const chargeFee = 2 * reservationHourlyRate * oldReservationLength * BOOKING_CHANGE_PERCENTAGE_CHARGE / 100; + 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); + } } } } diff --git a/services/officeRnD/bookings.js b/services/officeRnD/bookings.js index d63d991..94549d1 100644 --- a/services/officeRnD/bookings.js +++ b/services/officeRnD/bookings.js @@ -184,7 +184,7 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => { } }); - instance.setDataValue('hourlyRate', previous.hourlyRate); + instance.setDataValue('hourlyRate', instance.previous('hourlyRate')); if (realChange){ changes.push({