diff --git a/controllers/integration.js b/controllers/integration.js index 6d56b8c..ddaf1cf 100644 --- a/controllers/integration.js +++ b/controllers/integration.js @@ -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, }; diff --git a/routes/index.js b/routes/index.js index 6e007a2..c1b82eb 100644 --- a/routes/index.js +++ b/routes/index.js @@ -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();}); diff --git a/services/integration/reports.js b/services/integration/reports.js new file mode 100644 index 0000000..ba2200a --- /dev/null +++ b/services/integration/reports.js @@ -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, +};