add special check for unscheduled use in brake between reservations

This commit is contained in:
Bilal Catic
2019-12-26 02:31:40 +01:00
parent 87d7193bb9
commit b2450faa0c

View File

@@ -15,7 +15,7 @@ const {
UI_TIMEZONE UI_TIMEZONE
} = require('../../constants/constants'); } = require('../../constants/constants');
const { getUnlockEntryForReservation, getLockEntryForReservation, getEntriesBetween, getLastEntryForReservation } = require('../doorLock/doorLock'); const { getUnlockEntryForReservation, getLockEntryForReservation, getEntriesBetween, getLastEntryForReservation } = require('../doorLock/doorLock');
const { getAllFinishedBookings, getFirstPreviousBooking, getFirstNextBooking } = require('../officeRnD/bookings'); const { getAllFinishedBookings, getFirstPreviousBooking, getFirstNextBooking, getLastReservationInBlock } = require('../officeRnD/bookings');
const getSortedIncidentsForMember = (memberId) => { const getSortedIncidentsForMember = (memberId) => {
const attributes = ['bookingStart', 'incidentLevel', 'incidentLevelPrice', 'unlockTimestamp']; const attributes = ['bookingStart', 'incidentLevel', 'incidentLevelPrice', 'unlockTimestamp'];
@@ -368,7 +368,7 @@ const getIncidentData = (reservation) => {
const { resourceId } = currentReservation; const { resourceId } = currentReservation;
// 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 === '5d240cd34a3efa00882d9526') {
// console.log('\r\n\r\n==== ANALYSE RESERVATION [GET INCIDENT DATA] ==== '); // console.log('\r\n\r\n==== ANALYSE RESERVATION [GET INCIDENT DATA] ==== ');
// console.log('\tStart : ', reservationMoment.format('DD.MM, HH:mm')); // 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('\tEnd : ', moment.tz(currentReservation.end, currentReservation.timezone).format('DD.MM, HH:mm'));
@@ -474,7 +474,89 @@ const getIncidentData = (reservation) => {
// console.log('\tThere is res after : ', nextReservationIsBackToBack); // console.log('\tThere is res after : ', nextReservationIsBackToBack);
// } // }
let forgotToLockAsyncCheck; let forgotToLockAsyncCheck;
let asyncUnscheduledUseTest = [];
if (!lockEntry && !nextReservationIsBackToBack){ if (!lockEntry && !nextReservationIsBackToBack){
// Special check
const checkForUnscheduledUseInBrakeBetweenReservations = async () => {
try {
if (nextReservation) {
const lastReservationInBlockAfterBrake = await getLastReservationInBlock(nextReservation);
const fromTimestamp = nextReservation.start;
const toTimestamp = lastReservationInBlockAfterBrake.end;
const entriesInBlock = await getEntriesBetween(fromTimestamp, toTimestamp, reservation.resourceId);
// const reservationMoment = moment.tz(reservation.start, reservation.timezone);
// if (reservation.memberId === '5d240cd34a3efa00882d9526') {
// 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(reservation.end, reservation.timezone).format('DD.MM, HH:mm'));
// console.log('\t----------------------------------');
// console.log('\tLast reservation in block after brake :');
// console.log('\t\tStart : ', moment.tz(lastReservationInBlockAfterBrake.start, UI_TIMEZONE).format('DD.MM, HH:mm'));
// console.log('\t\tEnd : ', moment.tz(lastReservationInBlockAfterBrake.end, UI_TIMEZONE).format('DD.MM, HH:mm'));
// console.log('\tSearch entries : ');
// console.log('\t\tFrom :', moment.tz(fromTimestamp, UI_TIMEZONE).format('DD.MM, HH:mm'));
// console.log('\t\tTo :', moment.tz(toTimestamp, UI_TIMEZONE).format('DD.MM, HH:mm'));
// console.log('\tFirst entry : ');
// }
const addStandaloneUnscheduledIncident = () => {
const unlockMoment = reservation.end ? moment.utc(reservation.end) : null;
const lockMoment = nextReservation.start ? moment.utc(nextReservation.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: reservation.end,
lockTimestamp: nextReservation.start,
memberId: reservation.memberId,
resourceId: reservation.resourceId,
chargePrice: UNSCHEDULED_CHARGE_PRICE,
timeIntervalsToCharge,
totalChargeFee,
});
}
}
};
if (Array.isArray(entriesInBlock) && entriesInBlock.length > 0) {
const firstEntry = entriesInBlock[0];
// console.log('\t\tTimestamp : ', moment.tz(firstEntry.timestamp, UI_TIMEZONE).format('DD.MM, HH:mm'));
// console.log('\t\tEvent : ', firstEntry.event);
if (firstEntry && firstEntry.event &&
firstEntry.memberId === reservation.memberId &&
firstEntry.event === doorLockEvents.USER_LOCKED) {
addStandaloneUnscheduledIncident();
}
} else {
const reservationAfterLastReservationInBlock = await getFirstNextBooking(nextReservation);
if (reservationAfterLastReservationInBlock) {
const lastReservationLockEntry = await getLockEntryForReservation(lastReservationInBlockAfterBrake, reservationAfterLastReservationInBlock);
if (lastReservationLockEntry) {
addStandaloneUnscheduledIncident();
}
}
}
}
}catch (e) {
console.log('ERROR while checking for unscheduled use in brake between reservations ');
console.log(e);
}
};
if (unlockEntry){ if (unlockEntry){
// if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { // if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) {
// console.log('\tIncident : YES [#1]'); // console.log('\tIncident : YES [#1]');
@@ -486,6 +568,8 @@ const getIncidentData = (reservation) => {
resourceId: unlockEntry.resourceId, resourceId: unlockEntry.resourceId,
reservation: reservation && reservation.dataValues ? reservation.dataValues : emptyReservation, reservation: reservation && reservation.dataValues ? reservation.dataValues : emptyReservation,
}); });
asyncUnscheduledUseTest.push(checkForUnscheduledUseInBrakeBetweenReservations());
} else { } else {
// No lock entry, no unlock entry and no reservation after this one // No lock entry, no unlock entry and no reservation after this one
// This is either : // This is either :
@@ -501,17 +585,18 @@ const getIncidentData = (reservation) => {
forgotToLockAsyncCheck = getLastEntryForReservation(reservation); forgotToLockAsyncCheck = getLastEntryForReservation(reservation);
forgotToLockAsyncCheck forgotToLockAsyncCheck
.then((lastEntry) => { .then((lastEntry) => {
if (lastEntry && lastEntry.event === doorLockEvents.USER_UNLOCKED){ if (lastEntry) {
// if (currentReservation.memberId === '5ce785af422bdd00967fb781' && reservationMoment.isAfter('2019-12-01 00:00:16+00')) { if (lastEntry && lastEntry.event === doorLockEvents.USER_UNLOCKED) {
// console.log('\tIncident : YES [#2]'); incidents.push({
// } incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION,
incidents.push({ unlockTimestamp: lastEntry.timestamp,
incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION, memberId: lastEntry.memberId,
unlockTimestamp: lastEntry.timestamp, resourceId: lastEntry.resourceId,
memberId: lastEntry.memberId, reservation: reservation && reservation.dataValues ? reservation.dataValues : emptyReservation,
resourceId: lastEntry.resourceId, });
reservation: reservation && reservation.dataValues ? reservation.dataValues : emptyReservation,
}); asyncUnscheduledUseTest.push(checkForUnscheduledUseInBrakeBetweenReservations());
}
} }
}) })
.catch((error) => reject(error)); .catch((error) => reject(error));
@@ -539,35 +624,36 @@ const getIncidentData = (reservation) => {
const previousReservationUnlockEntry = previousReservationResults.unlockEntry; const previousReservationUnlockEntry = previousReservationResults.unlockEntry;
//Special check for bookings with break between //Special check for bookings with break between
if (previousReservation && // Check this for duplication, previous special check in step 3. can catch this scenario
!previousReservationIsBackToBack && // if (previousReservation &&
previousReservation.memberId === currentReservation.memberId && // !previousReservationIsBackToBack &&
previousReservationUnlockEntry && !previousReservationLockEntry && // previousReservation.memberId === currentReservation.memberId &&
!unlockEntry && lockEntry //current reservation unlock / lock entries // 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; // 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'); // if (unlockMoment && lockMoment){
const timeIntervalsToCharge = Math.floor(timeDifference / UNSCHEDULED_TIME_RESOLUTION); // const timeDifference = lockMoment.diff(unlockMoment, 'minutes');
const totalChargeFee = timeIntervalsToCharge * UNSCHEDULED_CHARGE_PRICE; // const timeIntervalsToCharge = Math.floor(timeDifference / UNSCHEDULED_TIME_RESOLUTION);
if (timeIntervalsToCharge > 0) { // const totalChargeFee = timeIntervalsToCharge * UNSCHEDULED_CHARGE_PRICE;
incidents.push({ // if (timeIntervalsToCharge > 0) {
incidentType: incidentType.UNSCHEDULED_INCIDENT_STANDALONE, // incidents.push({
reservation: emptyReservation, // incidentType: incidentType.UNSCHEDULED_INCIDENT_STANDALONE,
unlockTimestamp: previousReservation.end, // reservation: emptyReservation,
lockTimestamp: currentReservation.start, // unlockTimestamp: previousReservation.end,
memberId: currentReservation.memberId, // lockTimestamp: currentReservation.start,
resourceId, // memberId: currentReservation.memberId,
chargePrice: UNSCHEDULED_CHARGE_PRICE, // resourceId,
timeIntervalsToCharge, // chargePrice: UNSCHEDULED_CHARGE_PRICE,
totalChargeFee, // timeIntervalsToCharge,
}); // totalChargeFee,
} // });
} // }
} // }
// }
fromTimestamp = previousReservationLockEntry && previousReservationLockEntry.timestamp ? fromTimestamp = previousReservationLockEntry && previousReservationLockEntry.timestamp ?
previousReservationLockEntry.timestamp : previousReservation.end; previousReservationLockEntry.timestamp : previousReservation.end;
@@ -588,7 +674,7 @@ const getIncidentData = (reservation) => {
} }
//Now wait for "forgotToLockAsyncCheck", and possible "getEntriesBetween" to finish //Now wait for "forgotToLockAsyncCheck", and possible "getEntriesBetween" to finish
Promise.all([forgotToLockAsyncCheck, getEntriesAsyncCheck]) Promise.all([forgotToLockAsyncCheck, getEntriesAsyncCheck, asyncUnscheduledUseTest])
.then((asyncActionResults) => { .then((asyncActionResults) => {
const entriesBetween = asyncActionResults[1]; const entriesBetween = asyncActionResults[1];
if (Array.isArray(entriesBetween)){ if (Array.isArray(entriesBetween)){