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

@@ -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) => {