Disable sending fees for current and future months

This commit is contained in:
Senad Uka
2019-09-06 09:34:58 +02:00
parent cf0f96eb83
commit a8a7683f2d
2 changed files with 78 additions and 45 deletions

View File

@@ -10,7 +10,7 @@ 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 { UI_TIMEZONE, DEFAULT_DATE_FORMAT } = require('../constants/constants');
const getKnownOfficeResourceMappings = (req, res) => {
const dataToFetch = [getMappingsFromDatabase(), fetchOffices(), fetchResources()];
@@ -117,48 +117,61 @@ const addFees = (req, res) => {
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);
})
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');
}
}else{
console.log('Date range is missing to send fees to ORD');
res.status(400).send('Date range is missing');