fix how unlocked incident fees are applied

This commit is contained in:
Bilal Catic
2019-12-06 03:16:24 +01:00
parent e462f076d0
commit 8944f5181c

View File

@@ -238,7 +238,7 @@ const analyseReservation = (reservation) => {
const previousReservationEnd = moment.utc(previousReservation.end); const previousReservationEnd = moment.utc(previousReservation.end);
timeDifferenceFromPreviousReservation = currentReservationStart.diff(previousReservationEnd, 'minutes'); 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; let timeDifferenceBeforeNextReservation = MAX_BACK_TO_BACK_DIFFERENCE + 100;
@@ -246,7 +246,7 @@ const analyseReservation = (reservation) => {
const nextReservationStart = moment.utc(nextReservation.start); const nextReservationStart = moment.utc(nextReservation.start);
timeDifferenceBeforeNextReservation = nextReservationStart.diff(currentReservationEnd, 'minutes'); timeDifferenceBeforeNextReservation = nextReservationStart.diff(currentReservationEnd, 'minutes');
} }
const nextReservationIsBackToBack = timeDifferenceBeforeNextReservation < MAX_BACK_TO_BACK_DIFFERENCE; const nextReservationIsBackToBack = timeDifferenceBeforeNextReservation <= MAX_BACK_TO_BACK_DIFFERENCE;
let timeDifferenceFromUnlockEntry = 0; let timeDifferenceFromUnlockEntry = 0;
if (unlockEntry) { if (unlockEntry) {
@@ -406,12 +406,16 @@ const getIncidentData = (reservation) => {
//********************** //**********************
// 1. Check if member entered before reservation start time // 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 :'); // 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('\tunlockEntry : ', unlockEntry && unlockEntry.timestamp ? unlockEntry.timestamp : 'NO UNLOCK ENTRY');
// console.log('\tCharge before : ', chargeBefore); // console.log('\tCharge before : ', chargeBefore);
// console.log('\tThere is prev. res : ', previousReservationIsBackToBack); // console.log('\tThere is prev. res : ', previousReservationIsBackToBack);
// }
if (unlockEntry && chargeBefore && !previousReservationIsBackToBack) { if (unlockEntry && chargeBefore && !previousReservationIsBackToBack) {
// if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) {
// console.log('\tIncident : YES'); // console.log('\tIncident : YES');
// }
incidents.push({ incidents.push({
incidentType: incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION, incidentType: incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION,
reservation, reservation,
@@ -424,16 +428,22 @@ const getIncidentData = (reservation) => {
totalChargeFee: totalChargeFeeBefore, totalChargeFee: totalChargeFeeBefore,
}); });
}else{ }else{
// if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) {
// console.log('\tIncident : NO'); // console.log('\tIncident : NO');
// }
} }
// 2. Check if member left after reservation end time // 2. Check if member left after reservation end time
// 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('\r\n\r\nChecking if member left after reservation end time :');
// console.log('\tlockEntry : ', lockEntry && lockEntry.timestamp ? lockEntry.timestamp : 'NO LOCK ENTRY'); // console.log('\tlockEntry : ', lockEntry && lockEntry.timestamp ? lockEntry.timestamp : 'NO LOCK ENTRY');
// console.log('\tCharge after : ', chargeAfter); // console.log('\tCharge after : ', chargeAfter);
// console.log('\tThere is res after : ', nextReservationIsBackToBack); // console.log('\tThere is res after : ', nextReservationIsBackToBack);
// }
if (lockEntry && chargeAfter && !nextReservationIsBackToBack) { if (lockEntry && chargeAfter && !nextReservationIsBackToBack) {
// if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) {
// console.log('\tIncident : YES'); // console.log('\tIncident : YES');
// }
incidents.push({ incidents.push({
incidentType: incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION, incidentType: incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION,
reservation, reservation,
@@ -446,15 +456,19 @@ const getIncidentData = (reservation) => {
totalChargeFee: totalChargeFeeAfter, totalChargeFee: totalChargeFeeAfter,
}); });
}else{ }else{
// if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) {
// console.log('\tIncident : NO'); // console.log('\tIncident : NO');
// }
} }
// 3. Check if member forgot to lock the door // 3. Check if member forgot to lock the door
// 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('\r\n\r\nChecking if member left unlocked door :');
// console.log('\tunlockEntry : ', unlockEntry && unlockEntry.timestamp ? unlockEntry.timestamp : 'NO UNLOCK ENTRY'); // console.log('\tunlockEntry : ', unlockEntry && unlockEntry.timestamp ? unlockEntry.timestamp : 'NO UNLOCK ENTRY');
// console.log('\tlockEntry : ', lockEntry && lockEntry.timestamp ? lockEntry.timestamp : 'NO LOCK ENTRY'); // console.log('\tlockEntry : ', lockEntry && lockEntry.timestamp ? lockEntry.timestamp : 'NO LOCK ENTRY');
// console.log('\tThere is res before: ', previousReservationIsBackToBack); // console.log('\tThere is res before: ', previousReservationIsBackToBack);
// console.log('\tThere is res after : ', nextReservationIsBackToBack); // console.log('\tThere is res after : ', nextReservationIsBackToBack);
// }
let forgotToLockAsyncCheck; let forgotToLockAsyncCheck;
if (!lockEntry && !nextReservationIsBackToBack){ if (!lockEntry && !nextReservationIsBackToBack){
const emptyReservation = { const emptyReservation = {
@@ -464,7 +478,9 @@ const getIncidentData = (reservation) => {
}; };
if (unlockEntry){ if (unlockEntry){
// if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) {
// console.log('\tIncident : YES [#1]'); // console.log('\tIncident : YES [#1]');
// }
incidents.push({ incidents.push({
incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION, incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION,
unlockTimestamp: unlockEntry.timestamp, unlockTimestamp: unlockEntry.timestamp,
@@ -488,7 +504,9 @@ const getIncidentData = (reservation) => {
forgotToLockAsyncCheck forgotToLockAsyncCheck
.then((lastEntry) => { .then((lastEntry) => {
if (lastEntry && lastEntry.event === doorLockEvents.USER_UNLOCKED){ if (lastEntry && lastEntry.event === doorLockEvents.USER_UNLOCKED){
// if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) {
// console.log('\tIncident : YES [#2]'); // console.log('\tIncident : YES [#2]');
// }
incidents.push({ incidents.push({
incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION, incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION,
unlockTimestamp: lastEntry.timestamp, unlockTimestamp: lastEntry.timestamp,
@@ -531,7 +549,13 @@ const getIncidentData = (reservation) => {
unlockEntry.timestamp : reservation.start; unlockEntry.timestamp : reservation.start;
} }
getEntriesBetween(fromTimestamp, toTimestamp, resourceId) // 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) => { .then((entriesBetween) => {
// const reservationMoment = moment.tz(currentReservation.start, currentReservation.timezone); // const reservationMoment = moment.tz(currentReservation.start, currentReservation.timezone);
// if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-11-23 00:00:16+00')) { // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-11-23 00:00:16+00')) {
@@ -651,16 +675,42 @@ const getIncidentData = (reservation) => {
} }
}); });
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 //Now wait also for "forgotToLockAsyncCheck" to finish
Promise.all([forgotToLockAsyncCheck]) // 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(() => { .then(() => {
resolve(incidents); resolve(incidents);
}) })
.catch(error => reject(error)); .catch(error => reject(error));
})
.catch((error) => reject(error))
}) })
.catch(error => reject(error)); .catch(error => reject(error));
// //