fix door lock charges

This commit is contained in:
Bilal Catic
2019-07-03 11:50:23 +02:00
parent fc4d374e23
commit 2b492025fb
2 changed files with 23 additions and 34 deletions

View File

@@ -190,10 +190,16 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => {
const attributes = ['memberName', 'event', 'timestamp', 'resourceId']; const attributes = ['memberName', 'event', 'timestamp', 'resourceId'];
const fromTimestamp = previousReservation && previousReservation.end ? const previousReservationEndMoment = previousReservation && previousReservation.end ?
previousReservation.end : moment.utc(reservation.start).tz(UI_TIMEZONE).startOf('Day').toISOString(); moment.utc(previousReservation.end) : null;
const reservationStartMoment = moment.utc(reservation.start);
const fromTimestamp = previousReservationEndMoment && previousReservationEndMoment.tz(UI_TIMEZONE).isSame(reservationStartMoment.tz(UI_TIMEZONE), 'day') ?
previousReservation.end : reservationStartMoment.tz(UI_TIMEZONE).startOf('day').toISOString();
const toTimestamp = reservation.end; const toTimestamp = reservation.end;
const filters = { const filters = {
memberId, memberId,
timestamp: { timestamp: {
@@ -217,21 +223,8 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => {
let pairedLockEntry = null; let pairedLockEntry = null;
let eventFound = false; let eventFound = false;
// console.log('\r\n=?== SEARCH UNLOCK ==');
// console.log('ReservatioN : ', reservation.start);
const entriesBeforeReservationStart = entries.filter((entry) => moment.utc(entry.timestamp).isBefore(reservation.start)); const entriesBeforeReservationStart = entries.filter((entry) => moment.utc(entry.timestamp).isBefore(reservation.start));
// console.log('After end : ');
/*console.log(entriesBeforeReservationStart.map(e => {
return {
timestamp: e.timestamp,
event: e.event,
}
}));
*/
entriesBeforeReservationStart.forEach((entry) => { entriesBeforeReservationStart.forEach((entry) => {
if (!eventFound) { if (!eventFound) {
if (entry.event === doorLockEvents.USER_UNLOCKED) { if (entry.event === doorLockEvents.USER_UNLOCKED) {
@@ -250,35 +243,21 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => {
}); });
if (eventFound){ if (eventFound){
// console.log('FOUND : ', candidateUnlockEntry.timestamp, candidateUnlockEntry.event);
resolve(candidateUnlockEntry); resolve(candidateUnlockEntry);
} else { } else {
// console.log('NOT FOUND IN FIRST ROUND');
candidateUnlockEntry = null; candidateUnlockEntry = null;
const numberOfEntriesLeft = entries.length - entriesBeforeReservationStart.length; const numberOfEntriesLeft = entries.length - entriesBeforeReservationStart.length;
const entriesAfterReservationStart = entries.slice(0, numberOfEntriesLeft); const entriesAfterReservationStart = entries.slice(0, numberOfEntriesLeft);
// console.log('Entries in reservation time : ');
/*console.log(entriesAfterReservationStart.map(e => {
return {
timestamp: e.timestamp,
event: e.event,
}
}));
*/
entriesAfterReservationStart.forEach((entry) => { entriesAfterReservationStart.forEach((entry) => {
if (!eventFound) { if (!eventFound) {
if (entry.event === doorLockEvents.USER_UNLOCKED) { if (entry.event === doorLockEvents.USER_UNLOCKED) {
//console.log('FOUND : ', entry.timestamp, entry.event);
eventFound = true; eventFound = true;
candidateUnlockEntry = entry; candidateUnlockEntry = entry;
} }
} }
}); });
//console.log('===??==');
resolve(candidateUnlockEntry); resolve(candidateUnlockEntry);
} }
}) })
@@ -292,9 +271,13 @@ const getLockEntryForReservation = (reservation, nextReservation) => {
const attributes = ['memberName', 'event', 'timestamp']; const attributes = ['memberName', 'event', 'timestamp'];
const nextReservationStartMoment = nextReservation && nextReservation.start ?
moment.utc(nextReservation.start) : null;
const reservationStartMoment = moment.utc(reservation.start);
const fromTimestamp = reservation.start; const fromTimestamp = reservation.start;
const toTimestamp = nextReservation && nextReservation.start ? const toTimestamp = nextReservationStartMoment && nextReservationStartMoment.tz(UI_TIMEZONE).isSame(reservationStartMoment.tz(UI_TIMEZONE), 'day') ?
nextReservation.start : moment.utc(reservation.start).tz(UI_TIMEZONE).endOf('Day').toISOString(); nextReservation.start : reservationStartMoment.tz(UI_TIMEZONE).endOf('day').toISOString();
const filters = { const filters = {
memberId, memberId,
@@ -361,6 +344,10 @@ const getLockEntryForReservation = (reservation, nextReservation) => {
}); });
}; };
const getEntriesBetween = (reservation, previousReservation) => {
};
const getLastEntryForReservation = (reservation) => { const getLastEntryForReservation = (reservation) => {
return new Promise ((resolve, reject) => { return new Promise ((resolve, reject) => {
getFirstReservationInBlock(reservation) getFirstReservationInBlock(reservation)
@@ -404,5 +391,6 @@ module.exports = {
writeDoorLockEvent, writeDoorLockEvent,
getUnlockEntryForReservation, getUnlockEntryForReservation,
getLockEntryForReservation, getLockEntryForReservation,
getEntriesBetween,
getLastEntryForReservation, getLastEntryForReservation,
}; };

View File

@@ -4,7 +4,7 @@ const moment = require('moment-timezone');
const db = require('../../models/index'); const db = require('../../models/index');
const { doorLockEvents, incidentType, unlockedIncidentLevelsPrices, UNSCHEDULED_CHARGE_PRICE, MAX_BACK_TO_BACK_DIFFERENCE, UNSCHEDULED_TIME_RESOLUTION } = require('../../constants/constants'); const { doorLockEvents, incidentType, unlockedIncidentLevelsPrices, UNSCHEDULED_CHARGE_PRICE, MAX_BACK_TO_BACK_DIFFERENCE, UNSCHEDULED_TIME_RESOLUTION } = require('../../constants/constants');
const { getUnlockEntryForReservation, getLockEntryForReservation, getLastEntryForReservation } = require('../doorLock/doorLock'); const { getUnlockEntryForReservation, getLockEntryForReservation, getEntriesBetween, getLastEntryForReservation } = require('../doorLock/doorLock');
const { getAllFinishedBookings, getFirstPreviousBooking, getFirstNextBooking } = require('../officeRnD/bookings'); const { getAllFinishedBookings, getFirstPreviousBooking, getFirstNextBooking } = require('../officeRnD/bookings');
const getSortedIncidentsForMember = (memberId) => { const getSortedIncidentsForMember = (memberId) => {
@@ -180,11 +180,12 @@ const analyseReservation = (reservation) => {
.then(([previousReservation, nextReservation]) => { .then(([previousReservation, nextReservation]) => {
const getRelatedDoorLockEntriesAsync = [ const getRelatedDoorLockEntriesAsync = [
getUnlockEntryForReservation(reservation, previousReservation), getUnlockEntryForReservation(reservation, previousReservation),
getLockEntryForReservation(reservation, nextReservation) getLockEntryForReservation(reservation, nextReservation),
getEntriesBetween(reservation, previousReservation)
]; ];
Promise.all(getRelatedDoorLockEntriesAsync) Promise.all(getRelatedDoorLockEntriesAsync)
.then(([unlockEntry, lockEntry]) => { .then(([unlockEntry, lockEntry, entriesBetween]) => {
const currentReservationStart = moment.utc(reservation.start); const currentReservationStart = moment.utc(reservation.start);
const currentReservationEnd = moment.utc(reservation.end); const currentReservationEnd = moment.utc(reservation.end);