89 lines
2.0 KiB
JavaScript
89 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const Op = require('sequelize').Op;
|
|
const db = require('../../models/index');
|
|
const moment = require('moment-timezone');
|
|
|
|
const { DEFAULT_DATE_FORMAT, UI_TIMEZONE } = require('../../constants/constants');
|
|
|
|
const getActiveBookingsForMembersInDateRange = (dateRange, memberIds) => {
|
|
const startDate = moment.tz(dateRange.startDate, DEFAULT_DATE_FORMAT, UI_TIMEZONE).startOf('day');
|
|
const endDate = moment.tz(dateRange.endDate, DEFAULT_DATE_FORMAT, UI_TIMEZONE).endOf('day');
|
|
|
|
const attributes = [
|
|
'id',
|
|
'reservationId',
|
|
'memberId',
|
|
'officeId',
|
|
'resourceId',
|
|
'start',
|
|
'end',
|
|
'timezone',
|
|
'canceled',
|
|
'hourlyRate'
|
|
];
|
|
|
|
const filters = {
|
|
canceled: false,
|
|
};
|
|
|
|
if (startDate && endDate) {
|
|
filters.start = {
|
|
[Op.gte]: startDate.toISOString()
|
|
};
|
|
filters.end = {
|
|
[Op.lte]: endDate.toISOString(),
|
|
};
|
|
}
|
|
|
|
if (memberIds.length > 0){
|
|
filters.memberId = {
|
|
[Op.in]: memberIds
|
|
};
|
|
}
|
|
|
|
return db.bookingReservation.findAll({
|
|
attributes,
|
|
where: filters,
|
|
});
|
|
};
|
|
|
|
const getAllBookingsForYear = (year) => {
|
|
const startDate = moment.tz(year, 'YYYY', UI_TIMEZONE).startOf('year');
|
|
const endDate = moment.tz(year, 'YYYY', UI_TIMEZONE).endOf('year');
|
|
|
|
const attributes = [
|
|
'id',
|
|
'reservationId',
|
|
'memberId',
|
|
'officeId',
|
|
'resourceId',
|
|
'start',
|
|
'end',
|
|
'timezone',
|
|
'canceled',
|
|
'hourlyRate'
|
|
];
|
|
|
|
const filters = {};
|
|
|
|
if (startDate && endDate) {
|
|
filters.start = {
|
|
[Op.gte]: startDate.toISOString()
|
|
};
|
|
filters.end = {
|
|
[Op.lte]: endDate.toISOString(),
|
|
};
|
|
}
|
|
|
|
return db.bookingReservation.findAll({
|
|
attributes,
|
|
where: filters,
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
getActiveBookingsForMembersInDateRange,
|
|
getAllBookingsForYear,
|
|
};
|