add controller and service to fetch all incidents
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase } = require('../services/officeRnD/resources');
|
const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase } = require('../services/officeRnD/resources');
|
||||||
|
const { getAllDoorLockIncidents } = require('../services/integration/reports');
|
||||||
|
|
||||||
const getKnownOfficeResourceMappings = (req, res) => {
|
const getKnownOfficeResourceMappings = (req, res) => {
|
||||||
const dataToFetch = [getMappingsFromDatabase(), fetchOffices(), fetchResources() ];
|
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 = {
|
module.exports = {
|
||||||
getKnownOfficeResourceMappings,
|
getKnownOfficeResourceMappings,
|
||||||
addNewMapping,
|
addNewMapping,
|
||||||
|
getAllIncidents,
|
||||||
|
getUnscheduledIncidents,
|
||||||
|
getUnlockedIncidents,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const { apiStatusCheck } = require('../controllers/apiStatusCheck');
|
const { apiStatusCheck } = require('../controllers/apiStatusCheck');
|
||||||
const { uploadDoorLockData } = require('../controllers/doorLock');
|
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 { calculateDoorLockCharges } = require('../services/integration/doorLockCharges');
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
@@ -14,6 +14,10 @@ router.post('/doorLock/upload', uploadDoorLockData);
|
|||||||
router.get('/integration/mappings', getKnownOfficeResourceMappings);
|
router.get('/integration/mappings', getKnownOfficeResourceMappings);
|
||||||
router.post('/integration/mappings', addNewMapping);
|
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
|
// temporary route, manually trigger door lock charge calculations
|
||||||
router.get('/calculate', (req, res) => { calculateDoorLockCharges(); res.send();});
|
router.get('/calculate', (req, res) => { calculateDoorLockCharges(); res.send();});
|
||||||
|
|
||||||
|
|||||||
98
services/integration/reports.js
Normal file
98
services/integration/reports.js
Normal 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,
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user