import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; import Knex from 'knex'; export const ModifyIntegrationEndpointHandler: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkModifyIntegrationRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid ModifyIntegration request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const reqObject: api.ModifyIntegrationRequest = req.body; const { id: userId } = req.user; console.log("User ", userId, ": Modify Integration ", reqObject); await knex.transaction(async (trx) => { try { // Start retrieving the integration. const integrationId = await trx.select('id') .from('integrations') .where({ 'user': userId }) .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 ModifyIntegration request: ' + JSON.stringify(req.body), httpStatus: 404 }; throw e; } // Modify the integration. var update: any = {}; if ("name" in reqObject) { update["name"] = reqObject.name; } if ("details" in reqObject) { update["details"] = JSON.stringify(reqObject.details); } if ("type" in reqObject) { update["type"] = reqObject.type; } await trx('integrations') .where({ 'user': userId, 'id': req.params.id }) .update(update) // Respond to the request. res.status(200).send(); } catch (e) { catchUnhandledErrors(e); trx.rollback(); } }) }