refactor function for getting all bookings to allow more filters

This commit is contained in:
Bilal Catic
2019-08-09 16:31:59 +02:00
parent 44a2303877
commit 832c0f8eeb
2 changed files with 20 additions and 6 deletions

View File

@@ -48,9 +48,9 @@ const getActiveBookingsForMembersInDateRange = (dateRange, memberIds) => {
});
};
const getAllBookingsForYear = (year) => {
const startDate = moment.tz(year, 'YYYY', UI_TIMEZONE).startOf('year');
const endDate = moment.tz(year, 'YYYY', UI_TIMEZONE).endOf('year');
const getAllBookingsForMembersInDateRange = (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',
@@ -76,6 +76,12 @@ const getAllBookingsForYear = (year) => {
};
}
if (memberIds && Array.isArray(memberIds) && memberIds.length > 0){
filters.memberId = {
[Op.in]: memberIds
};
}
return db.bookingReservation.findAll({
attributes,
where: filters,
@@ -84,5 +90,5 @@ const getAllBookingsForYear = (year) => {
module.exports = {
getActiveBookingsForMembersInDateRange,
getAllBookingsForYear,
getAllBookingsForMembersInDateRange,
};

View File

@@ -10,7 +10,7 @@ const workbookCreator = require('excel4node');
const { checkBookingChanges } = require('./checkBookingChange');
const { incidentType, UI_TIMEZONE, DEFAULT_DATE_FORMAT, integrationServiceErrors } = require('../../constants/constants');
const { getAllBookingsForYear } = require('./bookings');
const { getAllBookingsForMembersInDateRange } = require('./bookings');
const { fetchAllMembers } = require('../officeRnD/members');
const { fetchOffices, fetchResources } = require('../officeRnD/resources');
const { getChargedCanceledReservations } = require('../integration/bookingChangeCharges');
@@ -320,7 +320,15 @@ const getAllIncidents = (dateRange, memberIds) => {
const getMemberPracticeSummaryReport = (year) => {
return new Promise((resolve, reject) => {
const asyncJobs = [checkBookingChanges(), getAllBookingsForYear(year), fetchAllMembers()];
const startDate = moment.tz(year, 'YYYY', UI_TIMEZONE).startOf('year').format(DEFAULT_DATE_FORMAT);
const endDate = moment.tz(year, 'YYYY', UI_TIMEZONE).endOf('year').format(DEFAULT_DATE_FORMAT);
const dateRange = {
startDate,
endDate,
};
const asyncJobs = [checkBookingChanges(), getAllBookingsForMembersInDateRange(dateRange), fetchAllMembers()];
Promise.all(asyncJobs)
.then((results) => {