You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

126 lines
4.1 KiB

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 id = parseInt(req.params.id);
let integration = await getIntegration(req.user.id, 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;
let id = parseInt(req.params.id);
console.log("User ", userId, ": Delete Integration ", id);
try {
await deleteIntegration(userId, 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;
let id = parseInt(req.params.id);
console.log("User ", userId, ": Put Integration ", reqObject);
try {
await modifyIntegration(userId, 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);
let id = parseInt(req.params.id);
try {
await modifyIntegration(userId, 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],
];