From 8944f5181c0134fd7dd542ecb8ecbc346e4d703f Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 6 Dec 2019 03:16:24 +0100 Subject: [PATCH] fix how unlocked incident fees are applied --- services/integration/doorLockCharges.js | 302 ++++++++++++++---------- 1 file changed, 176 insertions(+), 126 deletions(-) diff --git a/services/integration/doorLockCharges.js b/services/integration/doorLockCharges.js index 0f08363..850ceb3 100644 --- a/services/integration/doorLockCharges.js +++ b/services/integration/doorLockCharges.js @@ -238,7 +238,7 @@ const analyseReservation = (reservation) => { const previousReservationEnd = moment.utc(previousReservation.end); timeDifferenceFromPreviousReservation = currentReservationStart.diff(previousReservationEnd, 'minutes'); } - const previousReservationIsBackToBack = timeDifferenceFromPreviousReservation < MAX_BACK_TO_BACK_DIFFERENCE; + const previousReservationIsBackToBack = timeDifferenceFromPreviousReservation <= MAX_BACK_TO_BACK_DIFFERENCE; let timeDifferenceBeforeNextReservation = MAX_BACK_TO_BACK_DIFFERENCE + 100; @@ -246,7 +246,7 @@ const analyseReservation = (reservation) => { const nextReservationStart = moment.utc(nextReservation.start); timeDifferenceBeforeNextReservation = nextReservationStart.diff(currentReservationEnd, 'minutes'); } - const nextReservationIsBackToBack = timeDifferenceBeforeNextReservation < MAX_BACK_TO_BACK_DIFFERENCE; + const nextReservationIsBackToBack = timeDifferenceBeforeNextReservation <= MAX_BACK_TO_BACK_DIFFERENCE; let timeDifferenceFromUnlockEntry = 0; if (unlockEntry) { @@ -406,12 +406,16 @@ const getIncidentData = (reservation) => { //********************** // 1. Check if member entered before reservation start time - // console.log('\r\n\r\nChecking if member entered before reservation start time :'); - // console.log('\tunlockEntry : ', unlockEntry && unlockEntry.timestamp ? unlockEntry.timestamp : 'NO UNLOCK ENTRY'); - // console.log('\tCharge before : ', chargeBefore); - // console.log('\tThere is prev. res : ', previousReservationIsBackToBack); + // 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 :'); + // console.log('\tunlockEntry : ', unlockEntry && unlockEntry.timestamp ? unlockEntry.timestamp : 'NO UNLOCK ENTRY'); + // console.log('\tCharge before : ', chargeBefore); + // console.log('\tThere is prev. res : ', previousReservationIsBackToBack); + // } if (unlockEntry && chargeBefore && !previousReservationIsBackToBack) { - // console.log('\tIncident : YES'); + // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { + // console.log('\tIncident : YES'); + // } incidents.push({ incidentType: incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION, reservation, @@ -424,16 +428,22 @@ const getIncidentData = (reservation) => { totalChargeFee: totalChargeFeeBefore, }); }else{ - // console.log('\tIncident : NO'); + // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { + // console.log('\tIncident : NO'); + // } } // 2. Check if member left after reservation end time - // console.log('\r\n\r\nChecking if member left after reservation end time :'); - // console.log('\tlockEntry : ', lockEntry && lockEntry.timestamp ? lockEntry.timestamp : 'NO LOCK ENTRY'); - // console.log('\tCharge after : ', chargeAfter); - // console.log('\tThere is res after : ', nextReservationIsBackToBack); + // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { + // console.log('\r\n\r\nChecking if member left after reservation end time :'); + // console.log('\tlockEntry : ', lockEntry && lockEntry.timestamp ? lockEntry.timestamp : 'NO LOCK ENTRY'); + // console.log('\tCharge after : ', chargeAfter); + // console.log('\tThere is res after : ', nextReservationIsBackToBack); + // } if (lockEntry && chargeAfter && !nextReservationIsBackToBack) { - // console.log('\tIncident : YES'); + // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { + // console.log('\tIncident : YES'); + // } incidents.push({ incidentType: incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION, reservation, @@ -446,15 +456,19 @@ const getIncidentData = (reservation) => { totalChargeFee: totalChargeFeeAfter, }); }else{ - // console.log('\tIncident : NO'); + // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { + // console.log('\tIncident : NO'); + // } } // 3. Check if member forgot to lock the door - // console.log('\r\n\r\nChecking if member left unlocked door :'); - // console.log('\tunlockEntry : ', unlockEntry && unlockEntry.timestamp ? unlockEntry.timestamp : 'NO UNLOCK ENTRY'); - // console.log('\tlockEntry : ', lockEntry && lockEntry.timestamp ? lockEntry.timestamp : 'NO LOCK ENTRY'); - // console.log('\tThere is res before: ', previousReservationIsBackToBack); - // console.log('\tThere is res after : ', nextReservationIsBackToBack); + // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { + // console.log('\r\n\r\nChecking if member left unlocked door :'); + // console.log('\tunlockEntry : ', unlockEntry && unlockEntry.timestamp ? unlockEntry.timestamp : 'NO UNLOCK ENTRY'); + // console.log('\tlockEntry : ', lockEntry && lockEntry.timestamp ? lockEntry.timestamp : 'NO LOCK ENTRY'); + // console.log('\tThere is res before: ', previousReservationIsBackToBack); + // console.log('\tThere is res after : ', nextReservationIsBackToBack); + // } let forgotToLockAsyncCheck; if (!lockEntry && !nextReservationIsBackToBack){ const emptyReservation = { @@ -464,7 +478,9 @@ const getIncidentData = (reservation) => { }; if (unlockEntry){ - // console.log('\tIncident : YES [#1]'); + // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { + // console.log('\tIncident : YES [#1]'); + // } incidents.push({ incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION, unlockTimestamp: unlockEntry.timestamp, @@ -488,7 +504,9 @@ const getIncidentData = (reservation) => { forgotToLockAsyncCheck .then((lastEntry) => { if (lastEntry && lastEntry.event === doorLockEvents.USER_UNLOCKED){ - // console.log('\tIncident : YES [#2]'); + // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { + // console.log('\tIncident : YES [#2]'); + // } incidents.push({ incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION, unlockTimestamp: lastEntry.timestamp, @@ -531,96 +549,55 @@ const getIncidentData = (reservation) => { unlockEntry.timestamp : reservation.start; } - getEntriesBetween(fromTimestamp, toTimestamp, resourceId) - .then((entriesBetween) => { - // const reservationMoment = moment.tz(currentReservation.start, currentReservation.timezone); - // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-11-23 00:00:16+00')) { - // console.log('\r\n\r\n==== ANALYSE RESERVATION [GET INCIDENT DATA] ==== '); - // console.log('\tStart : ', reservationMoment.format('DD.MM, HH:mm')); - // console.log('\tEnd : ', moment.tz(currentReservation.end, currentReservation.timezone).format('DD.MM, HH:mm')); - // console.log('\t----------------------------------'); - // console.log('\tFirst previous reservation : is back to back [', previousReservationIsBackToBack ? 'T' : 'F', ']'); - // if (previousReservation) { - // console.log('\t\tStart : ', moment.tz(previousReservation.start, previousReservation.timezone).format('DD.MM, HH:mm')); - // console.log('\t\tEnd : ', moment.tz(previousReservation.end, previousReservation.timezone).format('DD.MM, HH:mm')); - // } else { - // console.log('\t\tNO PREVIOUS RESERVATION'); - // } - // - // console.log('\tFirst next reservation : is back to back [', nextReservationIsBackToBack ? 'T' : 'F', ']'); - // if (nextReservation) { - // console.log('\t\tStart : ', moment.tz(nextReservation.start, nextReservation.timezone).format('DD.MM, HH:mm')); - // console.log('\t\tEnd : ', moment.tz(nextReservation.end, nextReservation.timezone).format('DD.MM, HH:mm')); - // } else { - // console.log('\t\tNO NEXT RESERVATION'); - // } - // - // console.log('\t--------------------'); - // - // console.log('\tFrom timestamp : ', fromTimestamp ? moment.tz(fromTimestamp, UI_TIMEZONE).format('DD.MM, HH:mm') : '-'); - // console.log('\tTo timestamp : ', toTimestamp ? moment.tz(toTimestamp, UI_TIMEZONE).format('DD.MM, HH:mm') : '-'); - // console.log('\t--------------------'); - // } + // Let's skip this if previous reservation is back-to-back + let getEntriesAsyncCheck; + if (previousReservation && previousReservationIsBackToBack){ + //Skip + }else{ + getEntriesAsyncCheck = getEntriesBetween(fromTimestamp, toTimestamp, resourceId); + getEntriesAsyncCheck + .then((entriesBetween) => { + // const reservationMoment = moment.tz(currentReservation.start, currentReservation.timezone); + // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-11-23 00:00:16+00')) { + // console.log('\r\n\r\n==== ANALYSE RESERVATION [GET INCIDENT DATA] ==== '); + // console.log('\tStart : ', reservationMoment.format('DD.MM, HH:mm')); + // console.log('\tEnd : ', moment.tz(currentReservation.end, currentReservation.timezone).format('DD.MM, HH:mm')); + // console.log('\t----------------------------------'); + // console.log('\tFirst previous reservation : is back to back [', previousReservationIsBackToBack ? 'T' : 'F', ']'); + // if (previousReservation) { + // console.log('\t\tStart : ', moment.tz(previousReservation.start, previousReservation.timezone).format('DD.MM, HH:mm')); + // console.log('\t\tEnd : ', moment.tz(previousReservation.end, previousReservation.timezone).format('DD.MM, HH:mm')); + // } else { + // console.log('\t\tNO PREVIOUS RESERVATION'); + // } + // + // console.log('\tFirst next reservation : is back to back [', nextReservationIsBackToBack ? 'T' : 'F', ']'); + // if (nextReservation) { + // console.log('\t\tStart : ', moment.tz(nextReservation.start, nextReservation.timezone).format('DD.MM, HH:mm')); + // console.log('\t\tEnd : ', moment.tz(nextReservation.end, nextReservation.timezone).format('DD.MM, HH:mm')); + // } else { + // console.log('\t\tNO NEXT RESERVATION'); + // } + // + // console.log('\t--------------------'); + // + // console.log('\tFrom timestamp : ', fromTimestamp ? moment.tz(fromTimestamp, UI_TIMEZONE).format('DD.MM, HH:mm') : '-'); + // console.log('\tTo timestamp : ', toTimestamp ? moment.tz(toTimestamp, UI_TIMEZONE).format('DD.MM, HH:mm') : '-'); + // console.log('\t--------------------'); + // } - let pairUnlockEntry = null; - let pairLockEntry = null; + let pairUnlockEntry = null; + let pairLockEntry = null; - //Inspect all entries and insert detected incidents - entriesBetween.forEach((entry) => { - // console.log('\tEvent : ', entry.event); - // console.log('\tEvent : ', entry.timestamp ? moment.tz(entry.timestamp, UI_TIMEZONE).format('DD.MM, HH:mm') : '-'); - if (entry && entry.event){ - switch(entry.event){ - case doorLockEvents.USER_UNLOCKED: - if (!pairUnlockEntry){ - pairUnlockEntry = entry; - }else{ - const emptyReservation = { - reservationId: null, - start: null, - end: null, - }; - - incidents.push({ - incidentType: incidentType.UNLOCKED_INCIDENT_STANDALONE, - reservation: emptyReservation, - unlockTimestamp: pairUnlockEntry.timestamp, - memberId: pairUnlockEntry.memberId, - resourceId, - }); - - pairLockEntry = null; - pairUnlockEntry = entry; - } - break; - 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); - - if (lockMoment.tz(UI_TIMEZONE).isSame(unlockMoment.tz(UI_TIMEZONE), 'day')){ - 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: pairUnlockEntry.timestamp, - lockTimestamp: pairLockEntry.timestamp, - memberId: pairUnlockEntry.memberId, - resourceId, - chargePrice: UNSCHEDULED_CHARGE_PRICE, - timeIntervalsToCharge, - totalChargeFee, - }); - } + //Inspect all entries and insert detected incidents + entriesBetween.forEach((entry) => { + // console.log('\tEvent : ', entry.event); + // console.log('\tEvent : ', entry.timestamp ? moment.tz(entry.timestamp, UI_TIMEZONE).format('DD.MM, HH:mm') : '-'); + if (entry && entry.event){ + switch(entry.event){ + case doorLockEvents.USER_UNLOCKED: + if (!pairUnlockEntry){ + pairUnlockEntry = entry; }else{ const emptyReservation = { reservationId: null, @@ -635,32 +612,105 @@ const getIncidentData = (reservation) => { memberId: pairUnlockEntry.memberId, resourceId, }); - } - pairUnlockEntry = null; - pairLockEntry = null; - }else{ - if (!pairUnlockEntry){ + pairLockEntry = null; + pairUnlockEntry = entry; + } + break; + case doorLockEvents.USER_LOCKED: + if (pairUnlockEntry && !pairLockEntry){ pairLockEntry = entry; - //Only lock entry, ignore now + const emptyReservation = { + reservationId: null, + start: null, + end: null, + }; + const unlockMoment = moment.utc(pairUnlockEntry.timestamp); + const lockMoment = moment.utc(pairLockEntry.timestamp); + + if (lockMoment.tz(UI_TIMEZONE).isSame(unlockMoment.tz(UI_TIMEZONE), 'day')){ + 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: pairUnlockEntry.timestamp, + lockTimestamp: pairLockEntry.timestamp, + memberId: pairUnlockEntry.memberId, + resourceId, + chargePrice: UNSCHEDULED_CHARGE_PRICE, + timeIntervalsToCharge, + totalChargeFee, + }); + } + }else{ + const emptyReservation = { + reservationId: null, + start: null, + end: null, + }; + + incidents.push({ + incidentType: incidentType.UNLOCKED_INCIDENT_STANDALONE, + reservation: emptyReservation, + unlockTimestamp: pairUnlockEntry.timestamp, + memberId: pairUnlockEntry.memberId, + resourceId, + }); + } + + pairUnlockEntry = null; + pairLockEntry = null; + }else{ + if (!pairUnlockEntry){ + pairLockEntry = entry; + //Only lock entry, ignore now + } + pairLockEntry = null; + pairUnlockEntry = null; } - pairLockEntry = null; - pairUnlockEntry = null; - } + } } + }); + + if (pairUnlockEntry){ + const emptyReservation = { + reservationId: null, + start: null, + end: null, + }; + + incidents.push({ + incidentType: incidentType.UNLOCKED_INCIDENT_STANDALONE, + reservation: emptyReservation, + unlockTimestamp: pairUnlockEntry.timestamp, + memberId: pairUnlockEntry.memberId, + resourceId, + }); + + pairLockEntry = null; + pairUnlockEntry = null; } - }); - //Now wait also for "forgotToLockAsyncCheck" to finish - Promise.all([forgotToLockAsyncCheck]) - .then(() => { - resolve(incidents); - }) - .catch(error => reject(error)); + //Now wait also for "forgotToLockAsyncCheck" to finish + // Promise.all([forgotToLockAsyncCheck]) + // .then(() => { + // resolve(incidents); + // }) + // .catch(error => reject(error)); + }) + .catch((error) => reject(error)) + } + //Now wait for "forgotToLockAsyncCheck", and possible "getEntriesBetween" to finish + Promise.all([forgotToLockAsyncCheck, getEntriesAsyncCheck]) + .then(() => { + resolve(incidents); }) - .catch((error) => reject(error)) + .catch(error => reject(error)); }) .catch(error => reject(error)); //