Bump period to 7 minutes / silent feature

This commit is contained in:
Senad Uka
2019-11-26 14:00:18 +01:00
parent fe1f691b2f
commit 43c4214a23
20 changed files with 592 additions and 181 deletions

View File

@@ -274,6 +274,15 @@ const deleteBookingChangeIncidents = (incidents) => {
return Promise.all(asyncActions);
};
const deleteBookingChangeIncidentsById = (incidentIds) => {
const filters = {
id: {
[Op.in]: incidentIds
}
};
return db.bookingChangeIncident.update({deleted: true},{where: filters});
};
module.exports = {
getChargedCanceledReservations,
getIncidentsFromChanges,
@@ -281,4 +290,5 @@ module.exports = {
getShorteningIncidentsForReservationId,
getReservationsIncidentsForRemoval,
deleteBookingChangeIncidents,
deleteBookingChangeIncidentsById
};

View File

@@ -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
};

View File

@@ -65,7 +65,7 @@ const createFeeFromIncident = (incident) => {
switch (incidentTypeNumber) {
case incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION:
spacing = ' ';
spacing = ' ';
roomExplanation = resourceName || 'Unknown';
dateExplanation = bookingStartMoment.clone().startOf('day').format('MMM DD');
bookingTimeExplanation = `[${bookingStartMoment.clone().format('HH:mm')} to ${bookingEndMoment.clone().format('HH:mm')}]`;
@@ -78,7 +78,7 @@ const createFeeFromIncident = (incident) => {
quantity = 1.00;
break;
case incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION:
spacing = ' ';
spacing = ' ';
roomExplanation = resourceName || 'Unknown';
dateExplanation = bookingStartMoment.clone().startOf('day').format('MMM DD');
bookingTimeExplanation = `[${bookingStartMoment.clone().format('HH:mm')} to ${bookingEndMoment.clone().format('HH:mm')}]`;
@@ -91,7 +91,7 @@ const createFeeFromIncident = (incident) => {
quantity = +timeIntervalsToCharge.toFixed(2);
break;
case incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION:
spacing = ' ';
spacing = ' ';
roomExplanation = resourceName || 'Unknown';
dateExplanation = bookingStartMoment.clone().startOf('day').format('MMM DD');
bookingTimeExplanation = `[${bookingStartMoment.clone().format('HH:mm')} to ${bookingEndMoment.clone().format('HH:mm')}]`;
@@ -104,7 +104,7 @@ const createFeeFromIncident = (incident) => {
quantity = +timeIntervalsToCharge.toFixed(2);
break;
case incidentType.UNLOCKED_INCIDENT_STANDALONE:
spacing = ' ';
spacing = ' ';
roomExplanation = resourceName || 'Unknown';
dateExplanation = unlockMoment.clone().startOf('day').format('MMM DD');
bookingTimeExplanation = `[${unlockMoment.clone().format('HH:mm')} to ${lockMoment.clone().format('HH:mm')}]`;
@@ -117,7 +117,7 @@ const createFeeFromIncident = (incident) => {
quantity = 1.00;
break;
case incidentType.UNSCHEDULED_INCIDENT_STANDALONE:
spacing = ' ';
spacing = ' ';
roomExplanation = resourceName || 'Unknown';
dateExplanation = unlockMoment.clone().startOf('day').format('MMM DD');
bookingTimeExplanation = `[${unlockMoment.clone().format('HH:mm')} to ${lockMoment.clone().format('HH:mm')}]`;
@@ -129,7 +129,7 @@ const createFeeFromIncident = (incident) => {
quantity = +timeIntervalsToCharge.toFixed(2);
break;
case incidentType.BOOKING_MOVED_TO_ANOTHER_DAY:
spacing = ' ';
spacing = ' ';
// if (oldResourceName !== newResourceName){
// roomExplanation = `${oldResourceName} -> ${newResourceName}`;
// }else{
@@ -158,25 +158,11 @@ const createFeeFromIncident = (incident) => {
roomExplanation = newResourceName || 'Unknown';
// dateExplanation = `${oldBookingStartMoment.clone().format('ddd, MMM DD')}`;
const oldBookingDuration = oldBookingEndMoment.diff(oldBookingStartMoment, "minutes", false);
const durationInHours = Math.floor(oldBookingDuration / 60);
const durationInMinutes = Math.floor(oldBookingDuration % 60);
let durationAsText = '';
if (durationInHours !== 0){
durationAsText += durationInHours + ' hour';
if (durationInHours === 1){
durationAsText += ' ';
}else{
durationAsText += 's ';
}
}
durationAsText += durationInMinutes + ' minute';
if (durationInMinutes > 1){
durationAsText += 's';
}
const originalBookingExplanation = `${oldBookingStartMoment.clone().format('HH:mm')} to ${oldBookingEndMoment.clone().format('HH:mm')}`;
dateExplanation = `${newBookingStartMoment.clone().format('MMM DD')}`;
bookingTimeExplanation = `[${newBookingStartMoment.clone().format('HH:mm')} to ${newBookingEndMoment.clone().format('HH:mm')}]`;
incidentTimeExplanation = `reservation shortened from ${durationAsText} on : ${incidentTimestampMoment.clone().format('MMM DD, HH:mm')}`;
incidentTimeExplanation = `reservation shortened from [${originalBookingExplanation}] on [${incidentTimestampMoment.clone().format('MMM DD, HH:mm')}]`;
incidentExplanation = `${incidentTimeExplanation}`;
date = incidentTimestampMoment.clone().startOf('day').format();
@@ -185,7 +171,7 @@ const createFeeFromIncident = (incident) => {
quantity = 1.00;
break;
case incidentType.BOOKING_CANCELED_LATE:
spacing = ' ';
spacing = ' ';
roomExplanation = oldResourceName || 'Unknown';
// dateExplanation = `${oldBookingStartMoment.clone().format('ddd, MMM DD')}`;
dateExplanation = `${oldBookingStartMoment.clone().format('MMM DD')}`;

View File

@@ -18,7 +18,9 @@ const { getChargedCanceledReservations } = require('../integration/bookingChange
const getUnlockedIncidents = (startDate, endDate, memberIds) => {
const attributes = ['id', 'reservationId', 'memberId', 'resourceId', 'bookingStart', 'bookingEnd', 'unlockTimestamp', 'incidentLevel', 'incidentLevelPrice'];
const filters = {};
const filters = {
deleted: false
};
if (startDate && endDate) {
const bookingStartCondition = {
@@ -76,7 +78,9 @@ const getUnscheduledIncidents = (startDate, endDate, memberIds) => {
'totalChargeFee'
];
const filters = {};
const filters = {
deleted: false
};
if (startDate && endDate) {
const bookingStartCondition = {