include booking change incidents in report; refactor

This commit is contained in:
Bilal Catic
2019-07-08 18:50:10 +02:00
parent 6bed9aecc0
commit c0d7418f3f
3 changed files with 61 additions and 24 deletions

View File

@@ -111,6 +111,45 @@ const getUnscheduledIncidents = (startDate, endDate, memberId) => {
});
};
const getBookingChangeIncidents = (startDate, endDate, memberId) => {
const attributes = [
'id',
'reservationId',
'memberId',
'resourceId',
'oldBookingStart',
'oldBookingEnd',
'newBookingStart',
'newBookingEnd',
'incidentType',
'chargeFee',
'createdAt'
];
const filters = {};
if (startDate && endDate) {
filters.createdAt = {
[Op.and]: {
[Op.gte]: startDate.toISOString(),
[Op.lte]: endDate.toISOString(),
}
}
}
if (memberId){
filters.memberId = memberId;
}
return db.bookingChangeIncident.findAll({
attributes,
where: filters,
sort: [
['createdAt', 'ASC']
]
});
};
const formatTime = (timestamp) => {
const momentObject = moment.tz(timestamp, UI_TIMEZONE);
if (momentObject.isValid()){
@@ -120,13 +159,13 @@ const formatTime = (timestamp) => {
}
};
const getAllDoorLockIncidents = (dateRange, memberId) => {
const getAllIncidents = (dateRange, memberId) => {
return new Promise ((resolve, reject) => {
let startDate, endDate;
if (dateRange.startDate && dateRange.endDate){
startDate = moment.tz(dateRange.startDate, DEFAULT_DATE_FORMAT, UI_TIMEZONE);
endDate = moment.tz(dateRange.endDate, DEFAULT_DATE_FORMAT, UI_TIMEZONE);
startDate = moment.tz(dateRange.startDate, DEFAULT_DATE_FORMAT, UI_TIMEZONE).startOf('day');
endDate = moment.tz(dateRange.endDate, DEFAULT_DATE_FORMAT, UI_TIMEZONE).endOf('day');
if (!startDate.isValid() || !endDate.isValid() || endDate.isBefore(startDate)){
reject(integrationServiceErrors.INVALID_DATE_RANGE);
@@ -134,7 +173,14 @@ const getAllDoorLockIncidents = (dateRange, memberId) => {
}
}
const dataFetchJobs = [fetchAllMembers(), fetchOffices(), fetchResources(), getUnlockedIncidents(startDate, endDate, memberId), getUnscheduledIncidents(startDate, endDate, memberId)];
const dataFetchJobs = [
fetchAllMembers(),
fetchOffices(),
fetchResources(),
getUnlockedIncidents(startDate, endDate, memberId),
getUnscheduledIncidents(startDate, endDate, memberId),
getBookingChangeIncidents(startDate, endDate, memberId)
];
Promise.all(dataFetchJobs)
.then((data) => {
@@ -143,6 +189,7 @@ const getAllDoorLockIncidents = (dateRange, memberId) => {
const resources = data[2];
const unlockedIncidents = data[3];
const unscheduledIncidents = data[4];
const bookingChangeIncidents = data[5];
const membersMap = {};
const officesMap = {};
@@ -200,6 +247,8 @@ const getAllDoorLockIncidents = (dateRange, memberId) => {
});
});
allIncidents.push(...bookingChangeIncidents);
resolve(allIncidents);
})
.catch((error) => reject(error));
@@ -209,5 +258,5 @@ const getAllDoorLockIncidents = (dateRange, memberId) => {
module.exports = {
getUnlockedIncidents,
getUnscheduledIncidents,
getAllDoorLockIncidents,
getAllIncidents,
};