diff --git a/controllers/integration.js b/controllers/integration.js index d882457..8830a94 100644 --- a/controllers/integration.js +++ b/controllers/integration.js @@ -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');