Bump period to 7 minutes / silent feature
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
const moment = require('moment-timezone');
|
||||
const db = require('../../models/index');
|
||||
const Op = require('sequelize').Op;
|
||||
|
||||
const {
|
||||
doorLockEvents,
|
||||
@@ -9,6 +10,7 @@ const {
|
||||
unlockedIncidentLevelsPrices,
|
||||
UNSCHEDULED_CHARGE_PRICE,
|
||||
MAX_BACK_TO_BACK_DIFFERENCE,
|
||||
UNSCHEDULED_USE_INITIAL_TIME_SEGMENT_LENGTH,
|
||||
UNSCHEDULED_TIME_RESOLUTION,
|
||||
UI_TIMEZONE
|
||||
} = require('../../constants/constants');
|
||||
@@ -18,7 +20,8 @@ const { getAllFinishedBookings, getFirstPreviousBooking, getFirstNextBooking } =
|
||||
const getSortedIncidentsForMember = (memberId) => {
|
||||
const attributes = ['bookingStart', 'incidentLevel', 'incidentLevelPrice', 'unlockTimestamp'];
|
||||
const filters = {
|
||||
memberId
|
||||
memberId,
|
||||
deleted: false
|
||||
};
|
||||
const order = [['unlockTimestamp', 'DESC']];
|
||||
|
||||
@@ -59,6 +62,7 @@ const insertUnscheduledIncidents = (incidents) => {
|
||||
bookingEnd: end,
|
||||
unlockTimestamp,
|
||||
lockTimestamp,
|
||||
deleted: false
|
||||
},
|
||||
defaults: {...incidentForDB},
|
||||
}));
|
||||
@@ -67,6 +71,15 @@ const insertUnscheduledIncidents = (incidents) => {
|
||||
return Promise.all(asyncJobs);
|
||||
};
|
||||
|
||||
const deleteUnscheduledIncidentsById = (incidentIds) => {
|
||||
const filters = {
|
||||
id: {
|
||||
[Op.in]: incidentIds
|
||||
}
|
||||
};
|
||||
return db.unscheduledIncident.update({deleted: true},{where: filters});
|
||||
};
|
||||
|
||||
const insertUnlockedIncidents = (incidents) => {
|
||||
const asyncJobs = [];
|
||||
incidents.forEach((incident) => {
|
||||
@@ -93,6 +106,7 @@ const insertUnlockedIncidents = (incidents) => {
|
||||
bookingEnd: end,
|
||||
unlockTimestamp,
|
||||
incidentLevel,
|
||||
deleted: false
|
||||
},
|
||||
defaults: {...incidentForDB},
|
||||
}));
|
||||
@@ -101,6 +115,15 @@ const insertUnlockedIncidents = (incidents) => {
|
||||
return Promise.all(asyncJobs);
|
||||
};
|
||||
|
||||
const deleteUnlockedIncidentsById = (incidentIds) => {
|
||||
const filters = {
|
||||
id: {
|
||||
[Op.in]: incidentIds
|
||||
}
|
||||
};
|
||||
return db.unlockedIncident.update({deleted: true},{where: filters});
|
||||
};
|
||||
|
||||
const setUnlockedIncidentsLevel = (incidents) => {
|
||||
return new Promise ((resolve, reject) => {
|
||||
const sortingFunction = (incidentA, incidentB) => {
|
||||
@@ -230,7 +253,11 @@ const analyseReservation = (reservation) => {
|
||||
const unlockTime = moment.utc(unlockEntry.timestamp);
|
||||
timeDifferenceFromUnlockEntry = currentReservationStart.diff(unlockTime, 'minutes');
|
||||
}
|
||||
const timeIntervalsToChargeBefore = Math.floor(timeDifferenceFromUnlockEntry / UNSCHEDULED_TIME_RESOLUTION);
|
||||
|
||||
let timeIntervalsToChargeBefore = Math.floor(timeDifferenceFromUnlockEntry / UNSCHEDULED_TIME_RESOLUTION);
|
||||
if (timeDifferenceFromUnlockEntry < UNSCHEDULED_USE_INITIAL_TIME_SEGMENT_LENGTH){
|
||||
timeIntervalsToChargeBefore = 0;
|
||||
}
|
||||
const totalChargeFeeBefore = timeIntervalsToChargeBefore * UNSCHEDULED_CHARGE_PRICE;
|
||||
const chargeBefore = totalChargeFeeBefore > 0;
|
||||
|
||||
@@ -239,10 +266,54 @@ const analyseReservation = (reservation) => {
|
||||
const lockTime = moment.utc(lockEntry.timestamp);
|
||||
timeDifferenceFromLockEntry = lockTime.diff(currentReservationEnd, 'minutes');
|
||||
}
|
||||
const timeIntervalsToChargeAfter = Math.floor(timeDifferenceFromLockEntry / UNSCHEDULED_TIME_RESOLUTION);
|
||||
let timeIntervalsToChargeAfter = Math.floor(timeDifferenceFromLockEntry / UNSCHEDULED_TIME_RESOLUTION);
|
||||
if (timeDifferenceFromLockEntry < UNSCHEDULED_USE_INITIAL_TIME_SEGMENT_LENGTH){
|
||||
timeIntervalsToChargeAfter = 0;
|
||||
}
|
||||
const totalChargeFeeAfter = timeIntervalsToChargeAfter * UNSCHEDULED_CHARGE_PRICE;
|
||||
const chargeAfter = totalChargeFeeAfter > 0;
|
||||
|
||||
// if (reservation.memberId === '5ce785af422bdd00967fb781') {
|
||||
// console.log('\r\n\r\n==== ANALYSE RESERVATION ==== ');
|
||||
// console.log('\tStart : ', moment.tz(reservation.start, reservation.timezone).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 : ');
|
||||
// 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 : ');
|
||||
// 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');
|
||||
// }
|
||||
//
|
||||
// console.log('\t----------------------------------');
|
||||
// console.log('\tTime before : ');
|
||||
// console.log('\t\tOriginal : ', timeDifferenceFromUnlockEntry);
|
||||
// console.log('\t\tModified : ', timeIntervalsToChargeBefore);
|
||||
// console.log('\tTime after : ');
|
||||
// console.log('\t\tOriginal : ', timeDifferenceFromLockEntry);
|
||||
// console.log('\t\tModified : ', timeIntervalsToChargeAfter);
|
||||
// }
|
||||
|
||||
const result = {
|
||||
currentReservation: reservation,
|
||||
previousReservation,
|
||||
@@ -423,7 +494,12 @@ const getIncidentData = (reservation) => {
|
||||
.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,
|
||||
@@ -435,10 +511,17 @@ const getIncidentData = (reservation) => {
|
||||
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,
|
||||
@@ -450,9 +533,16 @@ const getIncidentData = (reservation) => {
|
||||
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,
|
||||
@@ -461,7 +551,7 @@ const getIncidentData = (reservation) => {
|
||||
};
|
||||
|
||||
if (unlockEntry){
|
||||
|
||||
// console.log('\tIncident : YES [#1]');
|
||||
incidents.push({
|
||||
incidentType: incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION,
|
||||
unlockTimestamp: unlockEntry.timestamp,
|
||||
@@ -484,6 +574,7 @@ const getIncidentData = (reservation) => {
|
||||
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,
|
||||
@@ -573,5 +664,7 @@ const calculateDoorLockCharges = () => {
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
calculateDoorLockCharges
|
||||
calculateDoorLockCharges,
|
||||
deleteUnlockedIncidentsById,
|
||||
deleteUnscheduledIncidentsById
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user