Files
old-crm-integration/controllers/integration.js

234 lines
8.5 KiB
JavaScript
Raw Normal View History

2019-06-14 17:41:09 +02:00
'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');
2019-07-25 06:53:32 +02:00
const { getMembersFeesForDateRange } = require('../services/integration/invoiceIntegration');
const { deleteFeesFromORD, addFeesToORD } = require('../services/officeRnD/fees');
2019-08-27 09:43:37 +02:00
const { reformatMembershipsName } = require('../services/officeRnD/memberships');
2019-07-25 06:53:32 +02:00
const { checkBookingChanges } = require('../services/integration/checkBookingChange');
2019-07-25 15:36:49 +02:00
const { checkIfProcessing } = require('../services/integration/processingStatus');
2019-06-14 17:41:09 +02:00
const { UI_TIMEZONE, DEFAULT_DATE_FORMAT } = require('../constants/constants');
2019-06-14 17:41:09 +02:00
const getKnownOfficeResourceMappings = (req, res) => {
const dataToFetch = [getMappingsFromDatabase(), fetchOffices(), fetchResources()];
2019-06-14 17:41:09 +02:00
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);
2019-06-14 17:41:09 +02:00
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);
}
};
2019-07-08 20:37:14 +02:00
const getAllIncidentsController = (req, res) => {
2019-06-19 11:23:58 +02:00
const dateRange = {
startDate: req.params.startDate,
endDate: req.params.endDate,
};
2019-07-25 06:53:32 +02:00
getAllIncidents(dateRange, [])
2019-06-19 11:23:58 +02:00
.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,
};
2019-07-25 06:53:32 +02:00
getAllIncidents(dateRange, [memberId])
.then((incidents) => {
res.send(incidents);
})
.catch((error) => {
console.log(error);
res.send([]);
});
};
2019-07-25 06:53:32 +02:00
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();
if ((startMomentYear < currentYear) || (startMomentYear === currentYear && startMomentMonth < currentMonth)) {
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('Selected month/year pair is current month or in the future');
res.status(400).send('Selected month/year pair is current month or in the future');
}
2019-07-25 06:53:32 +02:00
}else{
2019-08-30 18:43:02 +02:00
console.log('Date range is missing to send fees to ORD');
2019-07-25 06:53:32 +02:00
res.status(400).send('Date range is missing');
}
};
2019-07-25 15:36:49 +02:00
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);
});
};
2019-06-14 17:41:09 +02:00
module.exports = {
getKnownOfficeResourceMappings,
addNewMapping,
deleteMapping,
updateMapping,
2019-07-08 20:37:14 +02:00
getAllIncidentsController,
2019-06-19 11:23:58 +02:00
getMemberIncidents,
2019-07-25 06:53:32 +02:00
addFees,
2019-07-25 15:36:49 +02:00
checkProcessingStatus,
getPracticeSummaryReport,
2019-06-14 17:41:09 +02:00
};