From 3d7db8b15696906164dbb03f6b4454b716caf3e1 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 11 Dec 2019 20:47:07 +0100 Subject: [PATCH 1/2] fix wrong entrance entry detection after reservation start --- services/doorLock/doorLock.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/services/doorLock/doorLock.js b/services/doorLock/doorLock.js index 08c7fb6..95ef497 100644 --- a/services/doorLock/doorLock.js +++ b/services/doorLock/doorLock.js @@ -238,7 +238,25 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => { const entriesBeforeReservationStart = entries.filter((entry) => moment.utc(entry.timestamp).isBefore(reservation.start)); + // if (memberId === '5ce785af422bdd00967fb781') { + // console.log('Start : ', moment.tz(reservation.start, UI_TIMEZONE).format('DD.MM HH:mm')); + // console.log('End : ', moment.tz(reservation.end, UI_TIMEZONE).format('DD.MM HH:mm')); + // console.log('\tPrevious reservation '); + // console.log('\tStart : ', previousReservation ? moment.tz(previousReservation.start, UI_TIMEZONE).format('DD.MM HH:mm') : '-'); + // console.log('\tEnd : ', previousReservation ? moment.tz(previousReservation.end, UI_TIMEZONE).format('DD.MM HH:mm') : '-'); + // console.log('\t---------------------------'); + // console.log('\tSearch for entries : '); + // console.log('\tFrom : ', fromTimestamp ? moment.tz(fromTimestamp, UI_TIMEZONE).format('DD.MM HH:mm') : '-'); + // console.log('\tTo : ', toTimestamp ? moment.tz(toTimestamp, UI_TIMEZONE).format('DD.MM HH:mm') : '-'); + // console.log('\t---------------------------'); + // console.log('\tEntries before reservation start : '); + // } + entriesBeforeReservationStart.forEach((entry) => { + // if (memberId === '5ce785af422bdd00967fb781') { + // console.log('\t', entry.event, '\t', moment.tz(entry.timestamp, UI_TIMEZONE).format('DD.MM HH:mm')); + // } + if (!eventFound) { if (entry.event === doorLockEvents.USER_UNLOCKED) { if (pairedLockEntry) { @@ -256,13 +274,25 @@ const getUnlockEntryForReservation = (reservation, previousReservation) => { }); if (eventFound){ + // if (memberId === '5ce785af422bdd00967fb781') { + // console.log('\t=> FOUND UNLOCK ENTRY - NO NEED TO LOOK AFTER <='); + // } resolve(candidateUnlockEntry); } else { candidateUnlockEntry = null; const numberOfEntriesLeft = entries.length - entriesBeforeReservationStart.length; const entriesAfterReservationStart = entries.slice(0, numberOfEntriesLeft); + const invertedEntriesAfterReservationStart = entriesAfterReservationStart.reverse(); + + // if (memberId === '5ce785af422bdd00967fb781') { + // console.log('\t-----------------------------'); + // console.log('\tEntries after reservation start : '); + // } + invertedEntriesAfterReservationStart.forEach((entry) => { + // if (memberId === '5ce785af422bdd00967fb781') { + // console.log('\t', entry.event, '\t', moment.tz(entry.timestamp, UI_TIMEZONE).format('DD.MM HH:mm')); + // } - entriesAfterReservationStart.forEach((entry) => { if (!eventFound) { if (entry.event === doorLockEvents.USER_UNLOCKED) { eventFound = true; -- 2.47.3 From b71d869f490aad6f56e3354d967d24b3244a9d6c Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 12 Dec 2019 09:19:33 +0100 Subject: [PATCH 2/2] fix wrong exit entry detection --- services/doorLock/doorLock.js | 60 +++++++++++++---------------------- 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/services/doorLock/doorLock.js b/services/doorLock/doorLock.js index 95ef497..1171261 100644 --- a/services/doorLock/doorLock.js +++ b/services/doorLock/doorLock.js @@ -333,7 +333,7 @@ const getLockEntryForReservation = (reservation, nextReservation) => { resourceId, }; - const order = [['timestamp', 'ASC']]; + const order = [['timestamp', 'DESC']]; db.doorLockEvent.findAll({ attributes, @@ -341,47 +341,31 @@ const getLockEntryForReservation = (reservation, nextReservation) => { order, }) .then((entries) => { - let candidateLockEntry = null; - let pairedUnlockEntry = null; - let eventFound = false; - const entriesAfterReservationEnd = entries.filter((entry) => moment.utc(entry.timestamp).isAfter(reservation.end)); + const entriesBeforeReservationEnd = entries.filter(entry => moment.utc(entry.timestamp).isBefore(reservation.end)); - entriesAfterReservationEnd.forEach((entry) => { - if (!eventFound) { - if (entry.event === doorLockEvents.USER_LOCKED) { - if (pairedUnlockEntry) { - pairedUnlockEntry = null; - candidateLockEntry = null; - } else { - candidateLockEntry = entry; - eventFound = true; - } - } - if (entry.event === doorLockEvents.USER_UNLOCKED){ - pairedUnlockEntry = entry; - } + if (entriesBeforeReservationEnd.length > 0){ + const lastEntryInReservationTime = entriesBeforeReservationEnd[0]; + if (lastEntryInReservationTime.event === doorLockEvents.USER_LOCKED){ + resolve(lastEntryInReservationTime); + return; } - }); - - if (eventFound){ - resolve(candidateLockEntry); - } else { - candidateLockEntry = null; - const numberOfEntriesLeft = entries.length - entriesAfterReservationEnd.length; - const entriesBeforeReservationEnd = entries.slice(0, numberOfEntriesLeft); - - entriesBeforeReservationEnd.reverse().forEach((entry) => { - if (!eventFound) { - if (entry.event === doorLockEvents.USER_LOCKED) { - eventFound = true; - candidateLockEntry = entry; - } - } - }); - - resolve(candidateLockEntry); } + + + + // Phase 2 + const numberOfEntriesLeft = entries.length - entriesBeforeReservationEnd.length; + const entriesAfterReservationEnd = entries.slice(0, numberOfEntriesLeft).reverse(); + if (entriesAfterReservationEnd.length > 0){ + const firstEntryAfterReservation = entriesAfterReservationEnd[0]; + if (firstEntryAfterReservation.event === doorLockEvents.USER_LOCKED){ + resolve(firstEntryAfterReservation); + return; + } + } + + resolve(null); }) .catch((error) => reject(error)); }); -- 2.47.3