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

@@ -1,7 +1,7 @@
'use strict';
const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase } = require('../services/officeRnD/resources');
const { getAllDoorLockIncidents } = require('../services/integration/reports');
const { getAllIncidents } = require('../services/integration/reports');
const getKnownOfficeResourceMappings = (req, res) => {
const dataToFetch = [getMappingsFromDatabase(), fetchOffices(), fetchResources() ];
@@ -32,13 +32,13 @@ const addNewMapping = (req, res) => {
}
};
const getAllIncidents = (req, res) => {
const getAllIncidentsController = (req, res) => {
const dateRange = {
startDate: req.params.startDate,
endDate: req.params.endDate,
};
getAllDoorLockIncidents(dateRange)
getAllIncidents(dateRange)
.then((incidents) => {
res.send(incidents);
})
@@ -55,7 +55,7 @@ const getMemberIncidents = (req, res) => {
endDate: req.params.endDate,
};
getAllDoorLockIncidents(dateRange, memberId)
getAllIncidents(dateRange, memberId)
.then((incidents) => {
res.send(incidents);
})
@@ -65,19 +65,9 @@ const getMemberIncidents = (req, res) => {
});
};
const getUnlockedIncidents = (req, res) => {
};
const getUnscheduledIncidents = (req, res) => {
};
module.exports = {
getKnownOfficeResourceMappings,
addNewMapping,
getAllIncidents,
getUnscheduledIncidents,
getUnlockedIncidents,
getAllIncidentsController,
getMemberIncidents,
};

View File

@@ -2,7 +2,7 @@
const { apiStatusCheck } = require('../controllers/apiStatusCheck');
const { uploadDoorLockData } = require('../controllers/doorLock');
const { getKnownOfficeResourceMappings, addNewMapping, getAllIncidents, getMemberIncidents,getUnscheduledIncidents, getUnlockedIncidents } = require('../controllers/integration');
const { getKnownOfficeResourceMappings, addNewMapping, getAllIncidentsController, getMemberIncidents } = require('../controllers/integration');
const { fetchMembersList } = require('../controllers/officeRnD');
const { calculateDoorLockCharges } = require('../services/integration/doorLockCharges');
@@ -17,9 +17,7 @@ router.get('/integration/mappings', getKnownOfficeResourceMappings);
router.post('/integration/mappings', addNewMapping);
router.get('/integration/report/member/:memberId/:startDate/:endDate', getMemberIncidents);
router.get('/integration/report/allIncidents/:startDate/:endDate', getAllIncidents);
router.get('/integration/report/unlockedIncidents', getUnlockedIncidents);
router.get('/integration/report/unscheduledIncidents', getUnscheduledIncidents);
router.get('/integration/report/allIncidents/:startDate/:endDate', getAllIncidentsController);
router.get('/officeRnD/membersList', fetchMembersList);

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,
};