From 94f0bcc748bcf19c617a766b6df263509e74cb97 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 28 Aug 2019 09:30:19 +0200 Subject: [PATCH] add delete mapping route, controller and implementation --- controllers/integration.js | 21 +++++++++++++++++++-- routes/index.js | 2 ++ services/officeRnD/resources.js | 5 +++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/controllers/integration.js b/controllers/integration.js index 6b4c282..0ba8882 100644 --- a/controllers/integration.js +++ b/controllers/integration.js @@ -2,7 +2,7 @@ const moment = require('moment-timezone'); -const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase } = require('../services/officeRnD/resources'); +const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase, deleteMappingById } = require('../services/officeRnD/resources'); const { getAllIncidents, getMemberPracticeSummaryReport } = require('../services/integration/reports'); const { getMembersFeesForDateRange } = require('../services/integration/invoiceIntegration'); const { deleteFeesFromORD, addFeesToORD } = require('../services/officeRnD/fees'); @@ -13,7 +13,7 @@ const { checkIfProcessing } = require('../services/integration/processingStatus' const { UI_TIMEZONE } = require('../constants/constants'); const getKnownOfficeResourceMappings = (req, res) => { - const dataToFetch = [getMappingsFromDatabase(), fetchOffices(), fetchResources() ]; + const dataToFetch = [getMappingsFromDatabase(), fetchOffices(), fetchResources()]; Promise.all(dataToFetch) .then(result => { @@ -41,6 +41,22 @@ const addNewMapping = (req, res) => { } }; +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(); + }); + } +}; + const getAllIncidentsController = (req, res) => { const dateRange = { startDate: req.params.startDate, @@ -170,6 +186,7 @@ const getPracticeSummaryReport = (req, res) => { module.exports = { getKnownOfficeResourceMappings, addNewMapping, + deleteMapping, getAllIncidentsController, getMemberIncidents, addFees, diff --git a/routes/index.js b/routes/index.js index 27add39..439fb2e 100644 --- a/routes/index.js +++ b/routes/index.js @@ -6,6 +6,7 @@ const { fetchMembersList } = require('../controllers/officeRnD'); const { getKnownOfficeResourceMappings, addNewMapping, + deleteMapping, getAllIncidentsController, getMemberIncidents, addFees, @@ -23,6 +24,7 @@ router.get('/', apiStatusCheck); router.post('/doorLock/upload', uploadDoorLockData); router.get('/integration/mappings', getKnownOfficeResourceMappings); router.post('/integration/mappings', addNewMapping); +router.delete('/integration/mappings/:mappingId', deleteMapping); router.get('/integration/report/member/:memberId/:startDate/:endDate', getMemberIncidents); router.get('/integration/report/allIncidents/:startDate/:endDate', getAllIncidentsController); diff --git a/services/officeRnD/resources.js b/services/officeRnD/resources.js index 37d73b7..4c39ab7 100644 --- a/services/officeRnD/resources.js +++ b/services/officeRnD/resources.js @@ -73,6 +73,10 @@ const getMappingsFromDatabase = () => { return db.officeResourceMapping.findAll(); }; +const deleteMappingById = (id) => { + return db.officeResourceMapping.destroy({where: {id}}); +}; + const saveNewMappingToDatabase = (mapping) => { return db.officeResourceMapping.findOrCreate({where: {...mapping}, defaults: {...mapping}}); }; @@ -83,4 +87,5 @@ module.exports = { fetchResources, getResourceMappings, saveNewMappingToDatabase, + deleteMappingById, };