fix fetching incidents for specific date in backend

This commit is contained in:
Bilal Catic
2019-06-18 10:32:17 +02:00
parent 1371ab0580
commit 16a62b35de
4 changed files with 54 additions and 9 deletions

View File

@@ -56,6 +56,7 @@ const integrationServiceErrors = {
FAILED_TO_SAVE_BOOKINGS: 'Failed to save booking reservations',
FAILED_TO_SAVE_DOOR_LOCK_ENTRIES: 'Failed to save door lock entries',
FAILED_TO_SAVE_DATA_GENERIC: 'Failed to save data',
INVALID_DATE_RANGE: 'Dates in date range are invalid',
};
const incidentType = {
@@ -64,6 +65,10 @@ const incidentType = {
UNSCHEDULED_INCIDENT: 3,
};
const UI_TIMEZONE = process.env.UI_TIMEZONE || 'America/Los_Angeles';
const DEFAULT_DATE_FORMAT = 'YYYY-MM-DD';
module.exports = {
VALID_CSV_HEADERS,
USER_ENTRY_EVENT,
@@ -75,4 +80,6 @@ module.exports = {
unlockedIncidentLevelsPrices,
integrationServiceErrors,
incidentType,
UI_TIMEZONE,
DEFAULT_DATE_FORMAT,
};

View File

@@ -33,7 +33,12 @@ const addNewMapping = (req, res) => {
};
const getAllIncidents = (req, res) => {
getAllDoorLockIncidents()
const dateRange = {
startDate: req.params.startDate,
endDate: req.params.endDate,
};
getAllDoorLockIncidents(dateRange)
.then((incidents) => {
res.send(incidents);
})

View File

@@ -14,7 +14,7 @@ router.post('/doorLock/upload', uploadDoorLockData);
router.get('/integration/mappings', getKnownOfficeResourceMappings);
router.post('/integration/mappings', addNewMapping);
router.get('/integration/report/allIncidents', getAllIncidents);
router.get('/integration/report/allIncidents/:startDate/:endDate', getAllIncidents);
router.get('/integration/report/unlockedIncidents', getUnlockedIncidents);
router.get('/integration/report/unscheduledIncidents', getUnscheduledIncidents);

View File

@@ -3,23 +3,35 @@
const moment = require('moment-timezone');
const db = require('../../models/index');
const { incidentType } = require('../../constants/constants');
const Op = require('sequelize').Op;
const { incidentType, UI_TIMEZONE, DEFAULT_DATE_FORMAT, integrationServiceErrors } = require('../../constants/constants');
const { fetchAllMembers } = require('../officeRnD/members');
const { fetchOffices, fetchResources } = require('../officeRnD/resources');
const getUnlockedIncidents = () => {
const getUnlockedIncidents = (startDate, endDate) => {
const attributes = ['id', 'memberId', 'resourceId', 'bookingStart', 'bookingEnd', 'incidentLevel', 'incidentLevelPrice'];
const filters = (startDate && endDate) ? {
bookingStart: {
[Op.and]: {
[Op.gte]: startDate.utc().toISOString(),
[Op.lte]: endDate.utc().toISOString(),
}
},
} : null;
return db.unlockedIncident.findAll({
attributes,
where: filters,
sort: [
['bookingStart', 'ASC']
]
});
};
const getUnscheduledIncidents = () => {
const getUnscheduledIncidents = (startDate, endDate) => {
const attributes = [
'id',
'memberId',
@@ -33,8 +45,18 @@ const getUnscheduledIncidents = () => {
'totalChargeFee'
];
const filters = (startDate && endDate) ? {
bookingStart: {
[Op.and]: {
[Op.gte]: startDate.utc().toISOString(),
[Op.lte]: endDate.utc().toISOString(),
}
},
} : null;
return db.unscheduledIncident.findAll({
attributes,
where: filters,
sort: [
['bookingStart', 'ASC']
]
@@ -42,13 +64,24 @@ const getUnscheduledIncidents = () => {
};
const formatTime = (timestamp) => {
const timezone = process.env.UI_TIMEZONE || 'America/Los_Angeles';
return moment.tz(timestamp, timezone).format('MM/DD/YYYY hh:mm a');
return moment.tz(timestamp, UI_TIMEZONE).format('MM/DD/YYYY hh:mm a');
};
const getAllDoorLockIncidents = () => {
const getAllDoorLockIncidents = (dateRange) => {
return new Promise ((resolve, reject) => {
const dataFetchJobs = [fetchAllMembers(), fetchOffices(), fetchResources(), getUnlockedIncidents(), getUnscheduledIncidents()];
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);
if (!startDate.isValid() || !endDate.isValid() || endDate.isBefore(startDate)){
reject(integrationServiceErrors.INVALID_DATE_RANGE);
return;
}
}
const dataFetchJobs = [fetchAllMembers(), fetchOffices(), fetchResources(), getUnlockedIncidents(startDate, endDate), getUnscheduledIncidents(startDate, endDate)];
Promise.all(dataFetchJobs)
.then((data) => {