From 1a21e91796916dbef5ac225be210e0dec6e02efe Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 9 Jan 2020 01:33:19 +0100 Subject: [PATCH] implement backend methods to update incident fees --- controllers/integration.js | 65 ++++++++++++++++++-- routes/index.js | 4 +- services/integration/bookingChangeCharges.js | 19 +++++- services/integration/doorLockCharges.js | 40 +++++++++++- 4 files changed, 120 insertions(+), 8 deletions(-) diff --git a/controllers/integration.js b/controllers/integration.js index e9c2d8b..2c538aa 100644 --- a/controllers/integration.js +++ b/controllers/integration.js @@ -2,15 +2,25 @@ const moment = require('moment-timezone'); -const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase, deleteMappingById, updateMappingById } = require('../services/officeRnD/resources'); +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 } = require('../services/integration/doorLockCharges'); -const { deleteBookingChangeIncidentsById } = require('../services/integration/bookingChangeCharges'); +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'); @@ -177,7 +187,7 @@ const addFees = (req, res) => { } }; -const deleteFees= (req, res) => { +const deleteFees = (req, res) => { const deleteData = req.body; const dateRange = deleteData.dateRange ? deleteData.dateRange : null; const incidents = deleteData.incidentsToDelete ? deleteData.incidentsToDelete : null; @@ -220,6 +230,50 @@ const deleteFees= (req, res) => { } }; +const updateFees = (req, res) => { + const updateData = req.body; + console.log(updateData); + 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) => { @@ -272,5 +326,6 @@ module.exports = { addFees, checkProcessingStatus, getPracticeSummaryReport, - deleteFees + deleteFees, + updateFees }; diff --git a/routes/index.js b/routes/index.js index bd3c65e..699bcde 100644 --- a/routes/index.js +++ b/routes/index.js @@ -13,7 +13,8 @@ const { addFees, checkProcessingStatus, getPracticeSummaryReport, - deleteFees + deleteFees, + updateFees } = require('../controllers/integration'); const { calculateDoorLockCharges } = require('../services/integration/doorLockCharges'); @@ -36,6 +37,7 @@ router.get('/officeRnD/membersList', fetchMembersList); router.post('/integration/addFees', addFees); router.delete('/integration/fees', deleteFees); +router.patch('/integration/fees', updateFees); router.get('/integration/processing', checkProcessingStatus); diff --git a/services/integration/bookingChangeCharges.js b/services/integration/bookingChangeCharges.js index 1f6f2ba..9d760bd 100644 --- a/services/integration/bookingChangeCharges.js +++ b/services/integration/bookingChangeCharges.js @@ -283,6 +283,22 @@ const deleteBookingChangeIncidentsById = (incidentIds) => { return db.bookingChangeIncident.update({deleted: true},{where: filters}); }; +const updateBookingChangeIncidentsById = (incidentFees) => { + const incidentIds = Object.keys(incidentFees); + + const asyncUpdateActions = []; + + incidentIds.forEach((incidentId) => { + const newFeeCharge = incidentFees[incidentId] ? parseFloat(incidentFees[incidentId]) : null; + + if (newFeeCharge || (newFeeCharge === 0)){ + asyncUpdateActions.push(db.bookingChangeIncident.update({chargeFee: newFeeCharge}, {where: {id: incidentId}})); + } + }); + + return Promise.all(asyncUpdateActions); +}; + module.exports = { getChargedCanceledReservations, getIncidentsFromChanges, @@ -290,5 +306,6 @@ module.exports = { getShorteningIncidentsForReservationId, getReservationsIncidentsForRemoval, deleteBookingChangeIncidents, - deleteBookingChangeIncidentsById + deleteBookingChangeIncidentsById, + updateBookingChangeIncidentsById }; diff --git a/services/integration/doorLockCharges.js b/services/integration/doorLockCharges.js index d331a37..a506b27 100644 --- a/services/integration/doorLockCharges.js +++ b/services/integration/doorLockCharges.js @@ -80,6 +80,26 @@ const deleteUnscheduledIncidentsById = (incidentIds) => { return db.unscheduledIncident.update({deleted: true},{where: filters}); }; +const updateUnscheduledIncidentsById = (incidentFees) => { + const incidentIds = Object.keys(incidentFees); + + const asyncUpdateActions = []; + + incidentIds.forEach((incidentId) => { + const newFeeCharge = incidentFees[incidentId] ? parseFloat(incidentFees[incidentId]) : null; + + if (newFeeCharge || (newFeeCharge === 0)){ + asyncUpdateActions.push(db.unscheduledIncident.update({ + totalChargeFee: newFeeCharge, + chargePrice: newFeeCharge, + timeIntervalsToCharge: 1 + }, {where: {id: incidentId}})); + } + }); + + return Promise.all(asyncUpdateActions); +}; + const insertUnlockedIncidents = (incidents) => { const asyncJobs = []; incidents.forEach((incident) => { @@ -124,6 +144,22 @@ const deleteUnlockedIncidentsById = (incidentIds) => { return db.unlockedIncident.update({deleted: true},{where: filters}); }; +const updateUnlockedIncidentsById = (incidentFees) => { + const incidentIds = Object.keys(incidentFees); + + const asyncUpdateActions = []; + + incidentIds.forEach((incidentId) => { + const newFeeCharge = incidentFees[incidentId] ? parseFloat(incidentFees[incidentId]) : null; + + if (newFeeCharge || (newFeeCharge === 0)){ + asyncUpdateActions.push(db.unlockedIncident.update({incidentLevelPrice: newFeeCharge}, {where: {id: incidentId}})); + } + }); + + return Promise.all(asyncUpdateActions); +}; + const setUnlockedIncidentsLevel = (incidents) => { return new Promise ((resolve, reject) => { const sortingFunction = (incidentA, incidentB) => { @@ -850,5 +886,7 @@ const calculateDoorLockCharges = () => { module.exports = { calculateDoorLockCharges, deleteUnlockedIncidentsById, - deleteUnscheduledIncidentsById + deleteUnscheduledIncidentsById, + updateUnlockedIncidentsById, + updateUnscheduledIncidentsById };