import * as api from '../../client/src/api/api'; import { EndpointError, EndpointHandler, handleErrorsInEndpoint } from './types'; import Knex from 'knex'; import asJson from '../lib/asJson'; import { createIntegration, deleteIntegration, getIntegration, listIntegrations, modifyIntegration } from '../db/Integration'; import { IntegrationDataWithId } from '../../client/src/api/api'; export const PostIntegration: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkPostIntegrationRequest(req.body)) { const e: EndpointError = { name: "EndpointError", message: 'Invalid PostIntegration request', httpStatus: 400 }; throw e; } const reqObject: api.PostIntegrationRequest = req.body; const { id: userId } = req.user; console.log("User ", userId, ": Post Integration ", reqObject); try { let id = await createIntegration(userId, reqObject, knex); const responseObject: api.PostIntegrationResponse = { id: id }; res.status(200).send(responseObject); } catch (e) { handleErrorsInEndpoint(e); } } export const GetIntegration: EndpointHandler = async (req: any, res: any, knex: Knex) => { try { let integration = await getIntegration(req.user.id, req.params.id, knex); res.status(200).send(integration); } catch (e) { handleErrorsInEndpoint(e) } } export const ListIntegrations: EndpointHandler = async (req: any, res: any, knex: Knex) => { const { id: userId } = req.user; console.log("List integrations"); try { const integrations: IntegrationDataWithId[] = await listIntegrations(req.user.id, knex); console.log("Found integrations:", integrations); await res.status(200).send(integrations); } catch (e) { handleErrorsInEndpoint(e) } } export const DeleteIntegration: EndpointHandler = async (req: any, res: any, knex: Knex) => { const { id: userId } = req.user; console.log("User ", userId, ": Delete Integration ", req.params.id); try { await deleteIntegration(userId, req.params.id, knex); res.status(200).send(); } catch (e) { handleErrorsInEndpoint(e); } } export const PutIntegration: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkPutIntegrationRequest(req.body)) { const e: EndpointError = { name: "EndpointError", message: 'Invalid PutIntegration request', httpStatus: 400 }; throw e; } const reqObject: api.PutIntegrationRequest = req.body; const { id: userId } = req.user; console.log("User ", userId, ": Put Integration ", reqObject); try { await modifyIntegration(userId, req.params.id, reqObject, knex); res.status(200).send(); } catch (e) { handleErrorsInEndpoint(e); } } export const PatchIntegration: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkPatchIntegrationRequest(req.body)) { const e: EndpointError = { name: "EndpointError", message: 'Invalid PatchIntegration request', httpStatus: 400 }; throw e; } const reqObject: api.PatchIntegrationRequest = req.body; const { id: userId } = req.user; console.log("User ", userId, ": Patch Integration ", reqObject); try { await modifyIntegration(userId, req.params.id, reqObject, knex); res.status(200).send(); } catch (e) { handleErrorsInEndpoint(e); } } export const integrationEndpoints: [string, string, boolean, EndpointHandler][] = [ [api.PostIntegrationEndpoint, 'post', true, PostIntegration], [api.GetIntegrationEndpoint, 'get', true, GetIntegration], [api.PutIntegrationEndpoint, 'put', true, PutIntegration], [api.PatchIntegrationEndpoint, 'patch', true, PatchIntegration], [api.DeleteIntegrationEndpoint, 'delete', true, DeleteIntegration], [api.ListIntegrationsEndpoint, 'get', true, ListIntegrations], ];