add controller and service to fetch all incidents

This commit is contained in:
Bilal Catic
2019-06-16 11:53:20 +02:00
parent aa0264f230
commit 2c8d6bd077
3 changed files with 126 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
'use strict';
const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase } = require('../services/officeRnD/resources');
const { getAllDoorLockIncidents } = require('../services/integration/reports');
const getKnownOfficeResourceMappings = (req, res) => {
const dataToFetch = [getMappingsFromDatabase(), fetchOffices(), fetchResources() ];
@@ -31,7 +32,29 @@ const addNewMapping = (req, res) => {
}
};
const getAllIncidents = (req, res) => {
getAllDoorLockIncidents()
.then((incidents) => {
res.send(incidents);
})
.catch((error) => {
console.log(error);
res.send([]);
});
};
const getUnlockedIncidents = (req, res) => {
};
const getUnscheduledIncidents = (req, res) => {
};
module.exports = {
getKnownOfficeResourceMappings,
addNewMapping,
getAllIncidents,
getUnscheduledIncidents,
getUnlockedIncidents,
};

View File

@@ -2,7 +2,7 @@
const { apiStatusCheck } = require('../controllers/apiStatusCheck');
const { uploadDoorLockData } = require('../controllers/doorLock');
const { getKnownOfficeResourceMappings, addNewMapping } = require('../controllers/integration');
const { getKnownOfficeResourceMappings, addNewMapping, getAllIncidents,getUnscheduledIncidents, getUnlockedIncidents } = require('../controllers/integration');
const { calculateDoorLockCharges } = require('../services/integration/doorLockCharges');
const express = require('express');
@@ -14,6 +14,10 @@ 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/unlockedIncidents', getUnlockedIncidents);
router.get('/integration/report/unscheduledIncidents', getUnscheduledIncidents);
// temporary route, manually trigger door lock charge calculations
router.get('/calculate', (req, res) => { calculateDoorLockCharges(); res.send();});

View File

@@ -0,0 +1,98 @@
'use strict';
const db = require('../../models/index');
const { incidentType } = require('../../constants/constants');
const { fetchAllMembers } = require('../officeRnD/members');
const getUnlockedIncidents = () => {
const attributes = ['id', 'memberId', 'resourceId', 'bookingStart', 'bookingEnd', 'incidentLevel', 'incidentLevelPrice'];
return db.unlockedIncident.findAll({
attributes,
sort: [
['bookingStart', 'ASC']
]
});
};
const getUnscheduledIncidents = () => {
const attributes = [
'id',
'memberId',
'resourceId',
'bookingStart',
'bookingEnd',
'doorLockEventTimestamp',
'doorLockEventType',
'timeIntervalsToCharge',
'chargePrice',
'totalChargeFee'
];
return db.unscheduledIncident.findAll({
attributes,
sort: [
['bookingStart', 'ASC']
]
});
};
const getAllDoorLockIncidents = () => {
return new Promise ((resolve, reject) => {
const dataFetchJobs = [fetchAllMembers(), getUnlockedIncidents(), getUnscheduledIncidents()];
Promise.all(dataFetchJobs)
.then((data) => {
const members = data[0];
const unlockedIncidents = data[1];
const unscheduledIncidents = data[2];
const membersMap = {};
members.forEach((member) => membersMap[member.memberId] = member);
const allIncidents = [];
unlockedIncidents.forEach((unlockedIncident) => {
allIncidents.push({
incidentId: unlockedIncident.id,
memberId: unlockedIncident.memberId,
memberName: membersMap[unlockedIncident.memberId].name,
resourceName: '',
officeName: '',
bookingStart: unlockedIncident.bookingStart,
bookingEnd: unlockedIncident.bookingEnd,
incidentType: incidentType.UNLOCKED_INCIDENT,
incidentLevel: unlockedIncident.incidentLevel,
incidentPrice: unlockedIncident.incidentLevelPrice,
});
});
unscheduledIncidents.forEach((unscheduledIncident) => {
allIncidents.push({
incidentId: unscheduledIncident.id,
memberId: unscheduledIncident.memberId,
memberName: membersMap[unscheduledIncident.memberId].name,
resourceName: '',
officeName: '',
bookingStart: unscheduledIncident.bookingStart,
bookingEnd: unscheduledIncident.bookingEnd,
incidentType: incidentType.UNSCHEDULED_INCIDENT,
timeIntervalsToCharge: unscheduledIncident.timeIntervalsToCharge,
chargePrice: unscheduledIncident.chargePrice,
totalChargeFee: unscheduledIncident.totalChargeFee,
});
});
resolve(allIncidents);
})
.catch((error) => reject(error));
});
};
module.exports = {
getUnlockedIncidents,
getUnscheduledIncidents,
getAllDoorLockIncidents,
};