Files
old-psihologija/controllers/integration.js

231 lines
8.2 KiB
JavaScript
Raw Normal View History

'use strict';
const moment = require('moment-timezone');
2019-08-28 12:44:47 +02:00
const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase, deleteMappingById, updateMappingById } = require('../services/officeRnD/resources');
const { getAllIncidents, getMemberPracticeSummaryReport } = require('../services/integration/reports');
2019-07-25 02:00:27 +02:00
const { getMembersFeesForDateRange } = require('../services/integration/invoiceIntegration');
const { deleteFeesFromORD, addFeesToORD } = require('../services/officeRnD/fees');
const { reformatMembershipsName } = require('../services/officeRnD/memberships');
2019-07-25 02:00:27 +02:00
const { checkBookingChanges } = require('../services/integration/checkBookingChange');
const { checkIfProcessing } = require('../services/integration/processingStatus');
2019-09-10 06:56:02 +02:00
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 => {
2019-08-28 12:44:47 +02:00
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();
});
2019-08-28 12:44:47 +02:00
}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,
};
2019-07-25 02:00:27 +02:00
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,
};
2019-07-25 02:00:27 +02:00
getAllIncidents(dateRange, [memberId])
.then((incidents) => {
res.send(incidents);
})
.catch((error) => {
console.log(error);
res.send([]);
});
};
const addFees = (req, res) => {
2019-07-18 03:45:39 +02:00
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();
2019-09-10 06:56:02 +02:00
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{
2019-09-10 06:56:02 +02:00
console.log(integrationServiceErrors.SENDING_FEES_DISABLED);
res.status(400).send(integrationServiceErrors.SENDING_FEES_DISABLED);
}
}else{
2019-09-10 06:56:02 +02:00
console.log(integrationServiceErrors.MONTH_MISSING);
res.status(400).send(integrationServiceErrors.MONTH_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,
2019-08-28 12:44:47 +02:00
updateMapping,
getAllIncidentsController,
getMemberIncidents,
addFees,
checkProcessingStatus,
getPracticeSummaryReport,
};