331 lines
12 KiB
JavaScript
331 lines
12 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 {
|
|
deleteUnlockedIncidentsById,
|
|
deleteUnscheduledIncidentsById,
|
|
updateUnscheduledIncidentsById,
|
|
updateUnlockedIncidentsById } = require('../services/integration/doorLockCharges');
|
|
const { deleteBookingChangeIncidentsById, updateBookingChangeIncidentsById } = require('../services/integration/bookingChangeCharges');
|
|
|
|
const { UI_TIMEZONE, DEFAULT_DATE_FORMAT, ALLOW_SENDING_FEES, integrationServiceErrors } = 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;
|
|
|
|
const startDateMoment = moment.tz(dateRange.startDate, DEFAULT_DATE_FORMAT, UI_TIMEZONE);
|
|
|
|
if (startDate && endDate && startDateMoment.isValid() && Array.isArray(memberIds)){
|
|
const currentMoment = moment.tz(UI_TIMEZONE);
|
|
const startMomentMonth = startDateMoment.month();
|
|
const startMomentYear = startDateMoment.year();
|
|
const currentMonth = currentMoment.month();
|
|
const currentYear = currentMoment.year();
|
|
|
|
const allowSendingFees = ALLOW_SENDING_FEES ||
|
|
((startMomentYear < currentYear) || (startMomentYear === currentYear && startMomentMonth < currentMonth));
|
|
|
|
if (allowSendingFees) {
|
|
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) => {
|
|
res.status(500).send(error);
|
|
})
|
|
})
|
|
.catch((error) => {
|
|
res.status(500).send(error);
|
|
})
|
|
})
|
|
.catch((error) => {
|
|
res.status(500).send(error);
|
|
})
|
|
})
|
|
.catch((error) => {
|
|
res.status(500).send(error);
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
res.status(500).send(error);
|
|
})
|
|
}else{
|
|
console.log(integrationServiceErrors.SENDING_FEES_DISABLED);
|
|
res.status(400).send(integrationServiceErrors.SENDING_FEES_DISABLED);
|
|
}
|
|
}else{
|
|
console.log(integrationServiceErrors.MONTH_MISSING);
|
|
res.status(400).send(integrationServiceErrors.MONTH_MISSING);
|
|
}
|
|
};
|
|
|
|
const deleteFees = (req, res) => {
|
|
const deleteData = req.body;
|
|
const dateRange = deleteData.dateRange ? deleteData.dateRange : null;
|
|
const incidents = deleteData.incidentsToDelete ? deleteData.incidentsToDelete : null;
|
|
const memberId = deleteData.memberId ? deleteData.memberId : null;
|
|
|
|
const unlockedIncidentIds = incidents.unlockedIncidentIds ? incidents.unlockedIncidentIds : [];
|
|
const unscheduledIncidentIds = incidents.unscheduledIncidentIds ? incidents.unscheduledIncidentIds : [];
|
|
const bookingChangeIncidentIds = incidents.bookingChangeIncidentIds ? incidents.bookingChangeIncidentIds : [];
|
|
|
|
req.params.startDate = dateRange.startDate ? dateRange.startDate : null;
|
|
req.params.endDate = dateRange.endDate ? dateRange.endDate : null;
|
|
|
|
if (Array.isArray(unlockedIncidentIds) && Array.isArray(unscheduledIncidentIds) && Array.isArray(bookingChangeIncidentIds)){
|
|
const asyncDeleteActions = [
|
|
deleteUnlockedIncidentsById(unlockedIncidentIds),
|
|
deleteUnscheduledIncidentsById(unscheduledIncidentIds),
|
|
deleteBookingChangeIncidentsById(bookingChangeIncidentIds)
|
|
];
|
|
|
|
Promise.all(asyncDeleteActions)
|
|
.then(() => {
|
|
if (memberId){
|
|
req.params.memberId = memberId;
|
|
getMemberIncidents(req, res);
|
|
}else{
|
|
getAllIncidentsController(req, res);
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error deleting incidents : ', error);
|
|
res.status(500).send();
|
|
});
|
|
}else{
|
|
if (memberId){
|
|
req.params.memberId = memberId;
|
|
getMemberIncidents(req, res);
|
|
}else{
|
|
getAllIncidentsController(req, res);
|
|
}
|
|
}
|
|
};
|
|
|
|
const updateFees = (req, res) => {
|
|
const updateData = req.body;
|
|
const dateRange = updateData.dateRange ? updateData.dateRange : null;
|
|
const incidents = updateData.updatedIncidentsData ? updateData.updatedIncidentsData : null;
|
|
const memberId = updateData.memberId ? updateData.memberId : null;
|
|
|
|
const unlockedIncidentFees = incidents.unlockedIncidentFees ? incidents.unlockedIncidentFees : {};
|
|
const unscheduledIncidentFees = incidents.unscheduledIncidentFees ? incidents.unscheduledIncidentFees : {};
|
|
const bookingChangeIncidentFees = incidents.bookingChangeIncidentFees ? incidents.bookingChangeIncidentFees : {};
|
|
|
|
req.params.startDate = dateRange.startDate ? dateRange.startDate : null;
|
|
req.params.endDate = dateRange.endDate ? dateRange.endDate : null;
|
|
|
|
if (unlockedIncidentFees && unscheduledIncidentFees && bookingChangeIncidentFees){
|
|
const asyncUpdateActions = [
|
|
updateUnlockedIncidentsById(unlockedIncidentFees),
|
|
updateUnscheduledIncidentsById(unscheduledIncidentFees),
|
|
updateBookingChangeIncidentsById(bookingChangeIncidentFees)
|
|
];
|
|
|
|
Promise.all(asyncUpdateActions)
|
|
.then(() => {
|
|
if (memberId){
|
|
req.params.memberId = memberId;
|
|
getMemberIncidents(req, res);
|
|
}else{
|
|
getAllIncidentsController(req, res);
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error updating incidents : ', error);
|
|
res.status(500).send();
|
|
});
|
|
}else{
|
|
if (memberId){
|
|
req.params.memberId = memberId;
|
|
getMemberIncidents(req, res);
|
|
}else{
|
|
getAllIncidentsController(req, res);
|
|
}
|
|
}
|
|
};
|
|
|
|
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,
|
|
deleteFees,
|
|
updateFees
|
|
};
|