import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; import Knex from 'knex'; export const DeleteIntegrationEndpointHandler: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkDeleteIntegrationRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid DeleteIntegration request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const reqObject: api.DeleteIntegrationRequest = req.body; const { id: userId } = req.user; console.log("User ", userId, ": Delete Integration ", reqObject); await knex.transaction(async (trx) => { try { // Start retrieving the integration itself. const integrationId = await trx.select('id') .from('integrations') .where({ 'user': userId }) .where({ id: req.params.id }) .then((r: any) => (r && r[0]) ? r[0]['id'] : undefined) // Check that we found all objects we need. if (!integrationId) { const e: EndpointError = { internalMessage: 'Integration does not exist for DeleteIntegration request: ' + JSON.stringify(req.body), httpStatus: 404 }; throw e; } // Delete the integration. await trx('integrations') .where({ 'user': userId, 'id': integrationId }) .del(); // Respond to the request. res.status(200).send(); } catch (e) { catchUnhandledErrors(e); trx.rollback(); } }) }