'use strict'; const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase } = require('../services/officeRnD/resources'); const { getAllIncidents, getMemberPracticeSummaryReport } = require('../services/integration/reports'); const { getMembersFeesForDateRange } = require('../services/integration/invoiceIntegration'); const { deleteFeesFromORD, addFeesToORD } = require('../services/officeRnD/fees'); const { checkBookingChanges } = require('../services/integration/checkBookingChange'); const { checkIfProcessing } = require('../services/integration/processingStatus'); 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([]); }); }; const addFees = (req, res) => { const memberIds = req.body && req.body.memberIds ? req.body.memberIds : []; const dateRange = req.body && req.body.dateRange ? req.body.dateRange : {startDate: null, endDate: null}; const { startDate, endDate } = dateRange; if (startDate && endDate && Array.isArray(memberIds)){ checkBookingChanges() .then(() => { deleteFeesFromORD(dateRange, memberIds) .then(() => { // TODO: Change this to parallel execution getMembersFeesForDateRange(dateRange, memberIds) .then((allFees) => { addFeesToORD(allFees) .then((numberOfInsertedFees) => { res.send({ status: 'ok', numberOfInsertedFees }); }) .catch((error) => { console.log('Error adding fees'); res.status(500).send(error); }) }) .catch((error) => { console.log(error); res.status(500).send(error); }) }) .catch((error) => { res.status(500).send(error); }); }) .catch((error) => { console.log('Error with updating booking reservations'); res.status(500).send(error); }) }else{ res.status(400).send('Date range is missing'); } }; const checkProcessingStatus = (req, res) => { checkIfProcessing() .then((processing) => { res.send({ processing }) }) .catch((error) => { console.log('Error checking if processing '); console.log(error); res.status(500).send(error); }); }; const getPracticeSummaryReport = (req, res) => { getMemberPracticeSummaryReport() .then((result) => { const pathToDownloadFile = `${__dirname}/../${result.reportPath}`; res.download(pathToDownloadFile); }) .catch((error) => { console.log('Error - Member Practice Summary Report'); console.log(error); res.status(500).send(error); }); }; module.exports = { getKnownOfficeResourceMappings, addNewMapping, getAllIncidentsController, getMemberIncidents, addFees, checkProcessingStatus, getPracticeSummaryReport, };