From c1f3f423686a3706ba101f13379eb5c4be7fa761 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 25 Dec 2019 16:41:05 +0100 Subject: [PATCH] match unlock entry before first reservation in a block --- services/doorLock/doorLock.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/services/doorLock/doorLock.js b/services/doorLock/doorLock.js index 1171261..c50b2cd 100644 --- a/services/doorLock/doorLock.js +++ b/services/doorLock/doorLock.js @@ -19,7 +19,7 @@ const { const { fetchAllMembers } = require('../officeRnD/members'); const { getMappingsFromDatabase } = require('../officeRnD/resources'); -const { getFirstReservationInBlock } = require('../officeRnD/bookings'); +const { getFirstReservationInBlock, getFirstPreviousBooking } = require('../officeRnD/bookings'); const extractMappingFromFileName = (fileName) => { const contentBetweenBracketsRegex = /\[(.*?)\]/; @@ -425,11 +425,25 @@ const getLastEntryForReservation = (reservation) => { const order = [['timestamp', 'DESC']]; db.doorLockEvent.findAll({where: filters, order}) - .then((entries) => { + .then(async(entries) => { if (entries && entries.length > 0){ resolve(entries[0]); } else { - resolve (undefined); + //No entry found in this block of reservations, now check if there is unlock entry for the first reservation in block, before reservation start time + + try { + const firstPreviousBookingBeforeFirstInBlock = await getFirstPreviousBooking(firstReservationInBlock); + const unlockEntryForFirstInBlock = await getUnlockEntryForReservation(firstReservationInBlock, firstPreviousBookingBeforeFirstInBlock); + + if (unlockEntryForFirstInBlock){ + resolve(unlockEntryForFirstInBlock); + }else{ + resolve(undefined); + } + + }catch (e) { + reject(e); + } } }) .catch((error) => reject(error));