From 87d7193bb91f5eac1028863e53c068fe34640ed7 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 26 Dec 2019 02:30:03 +0100 Subject: [PATCH] add method for finding last reservation in a block --- services/officeRnD/bookings.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/services/officeRnD/bookings.js b/services/officeRnD/bookings.js index 35b4fa5..5d390a6 100644 --- a/services/officeRnD/bookings.js +++ b/services/officeRnD/bookings.js @@ -244,6 +244,32 @@ const getFirstReservationInBlock = (reservation) => { }); }; +const getLastReservationInBlock = async (reservation) => { + const { resourceId, memberId, end } = reservation; + + const toTimestamp = moment.utc(end).add(MAX_BACK_TO_BACK_DIFFERENCE).toISOString(); + const fromTimestamp = end; + + const filters = { + resourceId, + memberId, + start: { + [Op.and]: [ + {[Op.gte]: fromTimestamp}, + {[Op.lte]: toTimestamp} + ] + } + }; + + const nextReservation = await db.bookingReservation.findOne({where: filters}); + if (!nextReservation) { + return reservation; + } else { + return getLastReservationInBlock(nextReservation); + } + +}; + const writeBookingReservation = (bookingReservation) => { const { reservationId, memberId, officeId, resourceId, start, end, timezone, canceled, hourlyRate } = bookingReservation; const bookingReservationForDB = { @@ -369,5 +395,6 @@ module.exports = { getFirstNextBooking, getFirstPreviousBooking, getFirstReservationInBlock, + getLastReservationInBlock, bulkWriteReservationsWithChangesTracking, };