diff --git a/controllers/integration.js b/controllers/integration.js index be3ffed..47c0c0f 100644 --- a/controllers/integration.js +++ b/controllers/integration.js @@ -9,6 +9,8 @@ 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 { UI_TIMEZONE, DEFAULT_DATE_FORMAT, ALLOW_SENDING_FEES, integrationServiceErrors } = require('../constants/constants'); @@ -80,6 +82,8 @@ const updateMapping = (req, res) => { }; const getAllIncidentsController = (req, res) => { + console.log('get all incidents : '); + console.log(req.params); const dateRange = { startDate: req.params.startDate, endDate: req.params.endDate, @@ -175,6 +179,40 @@ const addFees = (req, res) => { } }; +const deleteFees= (req, res) => { + const deleteData = req.body; + console.log('Delete fees request : '); + console.log(req.body); + const dateRange = deleteData.dateRange ? deleteData.dateRange : null; + const incidents = deleteData.incidentsToDelete ? deleteData.incidentsToDelete : null; + + const unlockedIncidentIds = incidents.unlockedIncidentIds ? incidents.unlockedIncidentIds : []; + const unscheduledIncidentIds = incidents.unscheduledIncidentIds ? incidents.unscheduledIncidentIds : []; + const bookingChangeIncidentIds = incidents.bookingChangeIncidentIds ? incidents.bookingChangeIncidentIds : []; + + req.params.startDate = dateRange.startDate ? dateRange.startDate : null; + req.params.endDate = dateRange.endDate ? dateRange.endDate : null; + + if (Array.isArray(unlockedIncidentIds) && Array.isArray(unscheduledIncidentIds) && Array.isArray(bookingChangeIncidentIds)){ + const asyncDeleteActions = [ + deleteUnlockedIncidentsById(unlockedIncidentIds), + deleteUnscheduledIncidentsById(unscheduledIncidentIds), + deleteBookingChangeIncidentsById(bookingChangeIncidentIds) + ]; + + Promise.all(asyncDeleteActions) + .then(() => { + getAllIncidentsController(req, res); + }) + .catch((error) => { + console.log('Error deleting incidents : ', error); + res.status(500).send(); + }); + }else{ + getAllIncidentsController(req, res); + } +}; + const checkProcessingStatus = (req, res) => { checkIfProcessing() .then((processing) => { @@ -227,4 +265,5 @@ module.exports = { addFees, checkProcessingStatus, getPracticeSummaryReport, + deleteFees }; diff --git a/routes/index.js b/routes/index.js index c8d30fe..e35c18d 100644 --- a/routes/index.js +++ b/routes/index.js @@ -13,6 +13,7 @@ const { addFees, checkProcessingStatus, getPracticeSummaryReport, + deleteFees } = require('../controllers/integration'); const { calculateDoorLockCharges } = require('../services/integration/doorLockCharges'); @@ -34,6 +35,7 @@ router.get('/integration/report/allIncidents/:startDate/:endDate', getAllInciden router.get('/officeRnD/membersList', fetchMembersList); router.post('/integration/addFees', addFees); +router.delete('/integration/fees', deleteFees); router.get('/integration/processing', checkProcessingStatus); diff --git a/services/integration/bookingChangeCharges.js b/services/integration/bookingChangeCharges.js index 9412561..1197eb4 100644 --- a/services/integration/bookingChangeCharges.js +++ b/services/integration/bookingChangeCharges.js @@ -274,6 +274,16 @@ const deleteBookingChangeIncidents = (incidents) => { return Promise.all(asyncActions); }; +const deleteBookingChangeIncidentsById = (incidentIds) => { + console.log('Delete booking changes in DB :', incidentIds); + const filters = { + id: { + [Op.in]: incidentIds + } + }; + return db.bookingChangeIncident.update({deleted: true},{where: filters}); +}; + module.exports = { getChargedCanceledReservations, getIncidentsFromChanges, @@ -281,4 +291,5 @@ module.exports = { getShorteningIncidentsForReservationId, getReservationsIncidentsForRemoval, deleteBookingChangeIncidents, + deleteBookingChangeIncidentsById }; diff --git a/services/integration/doorLockCharges.js b/services/integration/doorLockCharges.js index 0a1d706..3460835 100644 --- a/services/integration/doorLockCharges.js +++ b/services/integration/doorLockCharges.js @@ -2,6 +2,7 @@ const moment = require('moment-timezone'); const db = require('../../models/index'); +const Op = require('sequelize').Op; const { doorLockEvents, @@ -69,6 +70,16 @@ const insertUnscheduledIncidents = (incidents) => { return Promise.all(asyncJobs); }; +const deleteUnscheduledIncidentsById = (incidentIds) => { + console.log('Delete unscheduled in DB :', incidentIds); + const filters = { + id: { + [Op.in]: incidentIds + } + }; + return db.unscheduledIncident.update({deleted: true},{where: filters}); +}; + const insertUnlockedIncidents = (incidents) => { const asyncJobs = []; incidents.forEach((incident) => { @@ -104,6 +115,16 @@ const insertUnlockedIncidents = (incidents) => { return Promise.all(asyncJobs); }; +const deleteUnlockedIncidentsById = (incidentIds) => { + console.log('Delete unlocked in DB :', incidentIds); + const filters = { + id: { + [Op.in]: incidentIds + } + }; + return db.unlockedIncident.update({deleted: true},{where: filters}); +}; + const setUnlockedIncidentsLevel = (incidents) => { return new Promise ((resolve, reject) => { const sortingFunction = (incidentA, incidentB) => { @@ -576,5 +597,7 @@ const calculateDoorLockCharges = () => { }; module.exports = { - calculateDoorLockCharges + calculateDoorLockCharges, + deleteUnlockedIncidentsById, + deleteUnscheduledIncidentsById };