'use strict'; const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase } = require('../services/officeRnD/resources'); const { getAllIncidents } = require('../services/integration/reports'); const getKnownOfficeResourceMappings = (req, res) => { const dataToFetch = [getMappingsFromDatabase(), fetchOffices(), fetchResources() ]; Promise.all(dataToFetch) .then(result => { res.send({ existingMappings: result[0], offices: result[1], resources: result[2], }); }) .catch(error => { res.status(500).send(); }); }; const addNewMapping = (req, res) => { const newMapping = req.body && req.body.mapping ? req.body.mapping : null; if (newMapping && newMapping.officeSlug && newMapping.resourceSlug && newMapping.officeId && newMapping.resourceId){ saveNewMappingToDatabase(newMapping) .then(() => { res.send(newMapping); }) .catch(error => { res.status(500).send(error); }); } }; const getAllIncidentsController = (req, res) => { const dateRange = { startDate: req.params.startDate, endDate: req.params.endDate, }; getAllIncidents(dateRange) .then((incidents) => { res.send(incidents); }) .catch((error) => { console.log(error); res.send([]); }); }; const getMemberIncidents = (req, res) => { const memberId = req.params.memberId; const dateRange = { startDate: req.params.startDate, endDate: req.params.endDate, }; getAllIncidents(dateRange, memberId) .then((incidents) => { res.send(incidents); }) .catch((error) => { console.log(error); res.send([]); }); }; module.exports = { getKnownOfficeResourceMappings, addNewMapping, getAllIncidentsController, getMemberIncidents, };