221 lines
7.6 KiB
JavaScript
221 lines
7.6 KiB
JavaScript
'use strict';
|
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase, deleteMappingById, updateMappingById } = 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 { reformatMembershipsName } = require('../services/officeRnD/memberships');
|
|
const { checkBookingChanges } = require('../services/integration/checkBookingChange');
|
|
const { checkIfProcessing } = require('../services/integration/processingStatus');
|
|
|
|
const { UI_TIMEZONE } = require('../constants/constants');
|
|
|
|
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 => {
|
|
console.log('Error with fetching mappings : ', 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 deleteMapping = (req, res) => {
|
|
const mappingId = req.params.mappingId;
|
|
|
|
if (mappingId && parseInt(mappingId)){
|
|
deleteMappingById(mappingId)
|
|
.then(() => {
|
|
getKnownOfficeResourceMappings(req, res);
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error deleting mapping with id = ', mappingId);
|
|
console.log(error);
|
|
res.status(500).send();
|
|
});
|
|
}else{
|
|
getKnownOfficeResourceMappings(req, res);
|
|
}
|
|
};
|
|
|
|
const updateMapping = (req, res) => {
|
|
const mappingId = req.params.mappingId;
|
|
const mapping = req.body && req.body.mapping ? req.body.mapping : null;
|
|
|
|
if (mappingId && parseInt(mappingId)){
|
|
updateMappingById(mappingId, mapping)
|
|
.then(() => {
|
|
getKnownOfficeResourceMappings(req, res);
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error updating mapping with id = ', mappingId);
|
|
console.log(error);
|
|
res.status(500).send();
|
|
});
|
|
}else{
|
|
getKnownOfficeResourceMappings(req, res);
|
|
}
|
|
};
|
|
|
|
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((invoicedFees) => {
|
|
// TODO: Change this to parallel execution
|
|
getMembersFeesForDateRange(dateRange, memberIds)
|
|
.then(({allFees, memberships}) => {
|
|
addFeesToORD(allFees, invoicedFees)
|
|
.then((numberOfInsertedFees) => {
|
|
reformatMembershipsName(memberships)
|
|
.then(() => {
|
|
res.send({
|
|
status: 'ok',
|
|
numberOfInsertedFees
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error reformatting memberships name');
|
|
console.log(error);
|
|
res.status(500).send(error);
|
|
})
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error adding fees');
|
|
res.status(500).send(error);
|
|
})
|
|
})
|
|
.catch((error) => {
|
|
console.log(error);
|
|
res.status(500).send(error);
|
|
})
|
|
})
|
|
.catch((error) => {
|
|
console.log(error);
|
|
res.status(500).send(error);
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error with updating booking reservations');
|
|
res.status(500).send(error);
|
|
})
|
|
}else{
|
|
console.log('Date range is missing to send fees to ORD');
|
|
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) => {
|
|
const year = req.params.year;
|
|
|
|
const currentYear = moment.tz(UI_TIMEZONE).year();
|
|
const parsedYear = parseInt(year);
|
|
|
|
if (!parsedYear || isNaN(parsedYear)){
|
|
res.status(400).send('Year is not a number');
|
|
return;
|
|
}
|
|
|
|
if (parsedYear > currentYear){
|
|
res.status(400).send('Selected year cannot be greater than current year');
|
|
return;
|
|
}
|
|
|
|
getMemberPracticeSummaryReport(parsedYear)
|
|
.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,
|
|
deleteMapping,
|
|
updateMapping,
|
|
getAllIncidentsController,
|
|
getMemberIncidents,
|
|
addFees,
|
|
checkProcessingStatus,
|
|
getPracticeSummaryReport,
|
|
};
|