match unlock entry before first reservation in a block

This commit is contained in:
Bilal Catic
2019-12-25 16:41:05 +01:00
parent a4573a9a13
commit c1f3f42368

View File

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