Fix problems with door unlock charges

This commit is contained in:
Senad Uka
2019-07-01 09:31:13 +02:00
parent ffc8412d7c
commit 1d9d5ac684
5 changed files with 419 additions and 185 deletions

View File

@@ -5,7 +5,7 @@ const moment = require('moment-timezone');
const Op = require('sequelize').Op;
const { API } = require('../../helpers/api');
const { officeRnDAPIErrors } = require('../../constants/constants');
const { officeRnDAPIErrors, MAX_BACK_TO_BACK_DIFFERENCE } = require('../../constants/constants');
const fetchAllBookings = () => {
return new Promise((resolve, reject) => {
@@ -57,7 +57,6 @@ const getAllFinishedBookings = () => {
const getFirstNextBooking = (reservation) => {
return new Promise ((resolve, reject) => {
const { resourceId, start } = reservation;
const endOfTheDay = moment.utc(start).endOf('Day').toISOString();
const attributes = ['reservationId', 'memberId', 'resourceId', 'start', 'end', 'timezone'];
const filters = {
@@ -65,9 +64,6 @@ const getFirstNextBooking = (reservation) => {
start: {
[Op.gt]: start
},
end: {
[Op.lte]: endOfTheDay
},
resourceId,
};
const order = [['start', 'ASC']];
@@ -91,14 +87,10 @@ const getFirstNextBooking = (reservation) => {
const getFirstPreviousBooking = (reservation) => {
return new Promise ((resolve, reject) => {
const { resourceId, start } = reservation;
const startOfTheDay = moment.utc(start).startOf('Day').toISOString();
const attributes = ['reservationId', 'memberId', 'resourceId', 'start', 'end', 'timezone'];
const filters = {
canceled: false,
start: {
[Op.gte]: startOfTheDay
},
end: {
[Op.lte]: start
},
@@ -122,6 +114,36 @@ const getFirstPreviousBooking = (reservation) => {
});
};
const getFirstReservationInBlock = (reservation) => {
return new Promise ((resolve, reject) => {
const {resourceId, memberId, start} = reservation;
const fromTimestamp = moment.utc(start).subtract(MAX_BACK_TO_BACK_DIFFERENCE).toISOString();
const toTimestamp = reservation.end;
const filters = {
resourceId,
memberId,
end: {
[Op.and]: [
{[Op.gte]: fromTimestamp},
{[Op.lte]: toTimestamp}
]
}
};
db.bookingReservation.findOne({where: filters})
.then((previousReservation) => {
if (!previousReservation) {
resolve(reservation);
} else {
resolve(getFirstReservationInBlock(previousReservation));
}
})
.catch((error) => reject(error));
});
};
const writeBookingReservation = (bookingReservation) => {
return db.bookingReservation.findOrCreate({where: {...bookingReservation}, defaults: {...bookingReservation}});
};
@@ -132,4 +154,5 @@ module.exports = {
getAllFinishedBookings,
getFirstNextBooking,
getFirstPreviousBooking,
getFirstReservationInBlock
};