synchronize incidents detection and save incidents to the DB

This commit is contained in:
Bilal Catic
2019-12-02 20:57:46 +01:00
parent fb959491d9
commit c30fb1adf6

View File

@@ -273,9 +273,10 @@ const analyseReservation = (reservation) => {
const totalChargeFeeAfter = timeIntervalsToChargeAfter * UNSCHEDULED_CHARGE_PRICE;
const chargeAfter = totalChargeFeeAfter > 0;
// if (reservation.memberId === '5ce785af422bdd00967fb781') {
// const reservationMoment = moment.tz(reservation.start, reservation.timezone);
// if (reservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-11-21 00:00:16+00')) {
// console.log('\r\n\r\n==== ANALYSE RESERVATION ==== ');
// console.log('\tStart : ', moment.tz(reservation.start, reservation.timezone).format('DD.MM, HH:mm'));
// console.log('\tStart : ', reservationMoment.format('DD.MM, HH:mm'));
// console.log('\tEnd : ', moment.tz(reservation.end, reservation.timezone).format('DD.MM, HH:mm'));
// console.log('\t----------------------------------');
// console.log('\tFirst previous reservation : ');
@@ -366,10 +367,145 @@ const getIncidentData = (reservation) => {
timeIntervalsToChargeAfter,
} = result;
const incidents = [];
const incidentsAsyncJobs = [];
const { resourceId } = currentReservation;
// 0a. Check for unscheduled use between current and previous reservation
// 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----------------------------------');
// if (unlockEntry) {
// console.log('\tUnlock entry : ', moment.tz(unlockEntry.timestamp, reservation.timezone).format('DD.MM, HH:mm'));
// } else {
// console.log('\tUnlock entry : NO UNLOCK ENTRY');
// }
// if (lockEntry) {
// console.log('\tLock entry : ', moment.tz(lockEntry.timestamp, reservation.timezone).format('DD.MM, HH:mm'));
// } else {
// console.log('\tLock entry : NO LOCK ENTRY');
// }
// }
//**********************
// 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 (unlockEntry && chargeBefore && !previousReservationIsBackToBack) {
// console.log('\tIncident : YES');
incidents.push({
incidentType: incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION,
reservation,
unlockTimestamp: unlockEntry.timestamp,
lockTimestamp: null,
memberId: reservation.memberId,
resourceId: reservation.resourceId,
chargePrice: UNSCHEDULED_CHARGE_PRICE,
timeIntervalsToCharge: timeIntervalsToChargeBefore,
totalChargeFee: totalChargeFeeBefore,
});
}else{
// 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 (lockEntry && chargeAfter && !nextReservationIsBackToBack) {
// console.log('\tIncident : YES');
incidents.push({
incidentType: incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION,
reservation,
unlockTimestamp: null,
lockTimestamp: lockEntry.timestamp,
memberId: reservation.memberId,
resourceId: reservation.resourceId,
chargePrice: UNSCHEDULED_CHARGE_PRICE,
timeIntervalsToCharge: timeIntervalsToChargeAfter,
totalChargeFee: totalChargeFeeAfter,
});
}else{
// 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);
let forgotToLockAsyncCheck;
if (!lockEntry && !nextReservationIsBackToBack){
const emptyReservation = {
reservationId: null,
start: null,
end: null,
};
if (unlockEntry){
// console.log('\tIncident : YES [#1]');
incidents.push({
incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION,
unlockTimestamp: unlockEntry.timestamp,
memberId: reservation.memberId,
resourceId: unlockEntry.resourceId,
reservation: reservation && reservation.dataValues ? reservation.dataValues : emptyReservation,
});
} else {
// No lock entry, no unlock entry and no reservation after this one
// This is either :
// 1. Last reservation in block of N reservations
// 1a. Member locked before entering this reservation
// 1b. Member forgot to lock the door <<< Only this is real incident
// 2. One reservation, but member never entered
if (previousReservationIsBackToBack){
// To ensure that this is last reservation in block (there is previous but no next reservation back to back)
// Now, just check if member actually locked before in the reservation time
forgotToLockAsyncCheck = getLastEntryForReservation(reservation);
forgotToLockAsyncCheck
.then((lastEntry) => {
if (lastEntry && lastEntry.event === doorLockEvents.USER_UNLOCKED){
// console.log('\tIncident : YES [#2]');
incidents.push({
incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION,
unlockTimestamp: lastEntry.timestamp,
memberId: lastEntry.memberId,
resourceId: lastEntry.resourceId,
reservation: reservation && reservation.dataValues ? reservation.dataValues : emptyReservation,
});
}
})
.catch((error) => reject(error));
}
}
}
// 4. Check for unscheduled use between current and previous reservation
const analysePreviousJob = [];
if (previousReservation && !previousReservationIsBackToBack){
analysePreviousJob.push(analyseReservation(previousReservation));
@@ -377,7 +513,6 @@ const getIncidentData = (reservation) => {
analysePreviousJob.push(undefined);
}
incidentsAsyncJobs.push(
Promise.all(analysePreviousJob)
.then(([previousReservationResults]) => {
let fromTimestamp;
@@ -396,15 +531,13 @@ const getIncidentData = (reservation) => {
unlockEntry.timestamp : reservation.start;
}
incidentsAsyncJobs.push(
getEntriesBetween(fromTimestamp, toTimestamp, resourceId)
.then((entriesBetween) => {
incidentsAsyncJobs.push(
new Promise((resolve, reject) => {
let pairUnlockEntry = null;
let pairLockEntry = null;
let pairUnlockEntry = null;
let pairLockEntry = null;
entriesBetween.forEach((entry) => {
//Inspect all entries and insert detected incidents
entriesBetween.forEach((entry) => {
if (entry && entry.event){
switch(entry.event){
case doorLockEvents.USER_UNLOCKED:
@@ -486,114 +619,30 @@ const getIncidentData = (reservation) => {
}
}
});
resolve(null);
}));
//Now wait also for "forgotToLockAsyncCheck" to finish
Promise.all([forgotToLockAsyncCheck])
.then(() => {
resolve(incidents);
})
.catch(error => reject(error));
})
.catch((error) => reject(error)));
.catch((error) => reject(error))
})
.catch((error) => reject(error)));
// 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 (unlockEntry && chargeBefore && !previousReservationIsBackToBack) {
// console.log('\tIncident : YES');
incidents.push({
incidentType: incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION,
reservation,
unlockTimestamp: unlockEntry.timestamp,
lockTimestamp: null,
memberId: reservation.memberId,
resourceId: reservation.resourceId,
chargePrice: UNSCHEDULED_CHARGE_PRICE,
timeIntervalsToCharge: timeIntervalsToChargeBefore,
totalChargeFee: totalChargeFeeBefore,
});
}else{
// 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 (lockEntry && chargeAfter && !nextReservationIsBackToBack) {
// console.log('\tIncident : YES');
incidents.push({
incidentType: incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION,
reservation,
unlockTimestamp: null,
lockTimestamp: lockEntry.timestamp,
memberId: reservation.memberId,
resourceId: reservation.resourceId,
chargePrice: UNSCHEDULED_CHARGE_PRICE,
timeIntervalsToCharge: timeIntervalsToChargeAfter,
totalChargeFee: totalChargeFeeAfter,
});
}else{
// 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 (!lockEntry && !nextReservationIsBackToBack){
const emptyReservation = {
reservationId: null,
start: null,
end: null,
};
if (unlockEntry){
// console.log('\tIncident : YES [#1]');
incidents.push({
incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION,
unlockTimestamp: unlockEntry.timestamp,
memberId: reservation.memberId,
resourceId: unlockEntry.resourceId,
reservation: reservation && reservation.dataValues ? reservation.dataValues : emptyReservation,
});
} else {
// No lock entry, no unlock entry and no reservation after this one
// This is either :
// 1. Last reservation in block of N reservations
// 1a. Member locked before entering this reservation
// 1b. Member forgot to lock the door <<< Only this is real incident
// 2. One reservation, but member never entered
if (previousReservationIsBackToBack){
// To ensure that this is last reservation in block (there is previous but no next reservation back to back)
// Now, just check if member actually locked before in the reservation time
incidentsAsyncJobs.push(
getLastEntryForReservation(reservation)
.then((lastEntry) => {
if (lastEntry && lastEntry.event === doorLockEvents.USER_UNLOCKED){
// console.log('\tIncident : YES [#2]');
incidents.push({
incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION,
unlockTimestamp: lastEntry.timestamp,
memberId: lastEntry.memberId,
resourceId: lastEntry.resourceId,
reservation: reservation && reservation.dataValues ? reservation.dataValues : emptyReservation,
});
}
})
.catch((error) => reject(error)));
}
}
}
Promise.all(incidentsAsyncJobs)
.then(() => {
resolve(incidents);
})
.catch((error) => reject(error));
.catch(error => reject(error));
//
// Promise.all(incidentsAsyncJobs)
// .then(() => {
// // console.log('\tDone with Async jobs for reservation ');
// // console.log('\t\tStart : ', reservationMoment.format('DD.MM, HH:mm'));
// // console.log('\t\tEnd : ', moment.tz(currentReservation.end, currentReservation.timezone).format('DD.MM, HH:mm'));
//
// setTimeout(() => {resolve(incidents)}, 10000);
// // resolve(incidents);
// })
// .catch((error) => reject(error));
})
.catch((error) => reject(error));