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.
 
 
 
 

43 lines
1.4 KiB

import * as api from '../../client/src/api';
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types';
import Knex from 'knex';
export const CreateIntegrationEndpointHandler: EndpointHandler = async (req: any, res: any, knex: Knex) => {
if (!api.checkCreateIntegrationRequest(req)) {
const e: EndpointError = {
internalMessage: 'Invalid CreateIntegration request: ' + JSON.stringify(req.body),
httpStatus: 400
};
throw e;
}
const reqObject: api.CreateIntegrationRequest = req.body;
const { id: userId } = req.user;
console.log("User ", userId, ": Create 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),
}
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();
}
})
}