From fa1fa35986360f1970a888f1c394bd390b5587ee Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 6 Dec 2019 05:39:01 +0100 Subject: [PATCH] add custom check for incident between two, not back-to-back bookings --- services/integration/doorLockCharges.js | 68 ++++++++++++++----------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/services/integration/doorLockCharges.js b/services/integration/doorLockCharges.js index 850ceb3..14ee642 100644 --- a/services/integration/doorLockCharges.js +++ b/services/integration/doorLockCharges.js @@ -405,6 +405,12 @@ const getIncidentData = (reservation) => { //********************** + const emptyReservation = { + reservationId: null, + start: null, + end: null, + }; + // 1. Check if member entered before reservation start time // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { // console.log('\r\n\r\nChecking if member entered before reservation start time :'); @@ -471,12 +477,6 @@ const getIncidentData = (reservation) => { // } let forgotToLockAsyncCheck; if (!lockEntry && !nextReservationIsBackToBack){ - const emptyReservation = { - reservationId: null, - start: null, - end: null, - }; - if (unlockEntry){ // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { // console.log('\tIncident : YES [#1]'); @@ -538,6 +538,38 @@ const getIncidentData = (reservation) => { if (previousReservationResults){ const previousReservationLockEntry = previousReservationResults.lockEntry; + const previousReservationUnlockEntry = previousReservationResults.unlockEntry; + + //Special check for bookings with break between + if (previousReservation && + !previousReservationIsBackToBack && + previousReservation.memberId === currentReservation.memberId && + previousReservationUnlockEntry && !previousReservationLockEntry && + !unlockEntry && lockEntry //current reservation unlock / lock entries + ) { + + const unlockMoment = previousReservation.end ? moment.utc(previousReservation.end) : null; + const lockMoment = currentReservation.start ? moment.utc(currentReservation.start) : null; + + if (unlockMoment && lockMoment){ + const timeDifference = lockMoment.diff(unlockMoment, 'minutes'); + const timeIntervalsToCharge = Math.floor(timeDifference / UNSCHEDULED_TIME_RESOLUTION); + const totalChargeFee = timeIntervalsToCharge * UNSCHEDULED_CHARGE_PRICE; + if (timeIntervalsToCharge > 0) { + incidents.push({ + incidentType: incidentType.UNSCHEDULED_INCIDENT_STANDALONE, + reservation: emptyReservation, + unlockTimestamp: previousReservation.end, + lockTimestamp: currentReservation.start, + memberId: currentReservation.memberId, + resourceId, + chargePrice: UNSCHEDULED_CHARGE_PRICE, + timeIntervalsToCharge, + totalChargeFee, + }); + } + } + } fromTimestamp = previousReservationLockEntry && previousReservationLockEntry.timestamp ? previousReservationLockEntry.timestamp : previousReservation.end; @@ -599,12 +631,6 @@ const getIncidentData = (reservation) => { if (!pairUnlockEntry){ pairUnlockEntry = entry; }else{ - const emptyReservation = { - reservationId: null, - start: null, - end: null, - }; - incidents.push({ incidentType: incidentType.UNLOCKED_INCIDENT_STANDALONE, reservation: emptyReservation, @@ -620,11 +646,7 @@ const getIncidentData = (reservation) => { case doorLockEvents.USER_LOCKED: if (pairUnlockEntry && !pairLockEntry){ pairLockEntry = entry; - const emptyReservation = { - reservationId: null, - start: null, - end: null, - }; + const unlockMoment = moment.utc(pairUnlockEntry.timestamp); const lockMoment = moment.utc(pairLockEntry.timestamp); @@ -646,12 +668,6 @@ const getIncidentData = (reservation) => { }); } }else{ - const emptyReservation = { - reservationId: null, - start: null, - end: null, - }; - incidents.push({ incidentType: incidentType.UNLOCKED_INCIDENT_STANDALONE, reservation: emptyReservation, @@ -676,12 +692,6 @@ const getIncidentData = (reservation) => { }); if (pairUnlockEntry){ - const emptyReservation = { - reservationId: null, - start: null, - end: null, - }; - incidents.push({ incidentType: incidentType.UNLOCKED_INCIDENT_STANDALONE, reservation: emptyReservation,