import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; import Knex from 'knex'; import asJson from '../lib/asJson'; export const PostIntegration: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkCreateIntegrationRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid PostIntegration request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const reqObject: api.CreateIntegrationRequest = req.body; const { id: userId } = req.user; console.log("User ", userId, ": Post Integration ", reqObject); await knex.transaction(async (trx) => { try { // Create the new integration. var integration: any = { name: reqObject.name, user: userId, type: reqObject.type, details: JSON.stringify(reqObject.details), secretDetails: JSON.stringify(reqObject.secretDetails), } const integrationId = (await trx('integrations') .insert(integration) .returning('id') // Needed for Postgres )[0]; // Respond to the request. const responseObject: api.CreateIntegrationResponse = { id: integrationId }; res.status(200).send(responseObject); } catch (e) { catchUnhandledErrors(e); trx.rollback(); } }) } export const GetIntegration: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkIntegrationDetailsRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid GetIntegration request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const { id: userId } = req.user; try { const integration = (await knex.select(['id', 'name', 'type', 'details']) .from('integrations') .where({ 'user': userId, 'id': req.params.id }))[0]; if (integration) { const response: api.IntegrationDetailsResponse = { name: integration.name, type: integration.type, details: asJson(integration.details), } await res.send(response); } else { await res.status(404).send({}); } } catch (e) { catchUnhandledErrors(e) } } export const ListIntegrations: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkIntegrationDetailsRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid ListIntegrations request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const { id: userId } = req.user; try { const integrations: api.ListIntegrationsResponse = ( await knex.select(['id', 'name', 'type', 'details']) .from('integrations') .where({ user: userId }) ).map((object: any) => { return { id: object.id, name: object.name, type: object.type, details: asJson(object.details), } }) await res.send(integrations); } catch (e) { catchUnhandledErrors(e) } } export const DeleteIntegration: 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(); } }) } export const PutIntegration: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkModifyIntegrationRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid PutIntegration request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const reqObject: api.ModifyIntegrationRequest = req.body; const { id: userId } = req.user; console.log("User ", userId, ": Put 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; } if ("secretDetails" in reqObject) { update["secretDetails"] = JSON.stringify(reqObject.details); } 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(); } }) }