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.
135 lines
4.7 KiB
135 lines
4.7 KiB
import * as api from '../../client/src/api/api'; |
|
import Knex from 'knex'; |
|
import asJson from '../lib/asJson'; |
|
import { DBError, DBErrorKind } from '../endpoints/types'; |
|
import { IntegrationDataWithId, IntegrationDataWithSecret, PartialIntegrationData } from '../../client/src/api/api'; |
|
|
|
export async function createIntegration(userId: number, integration: api.IntegrationDataWithSecret, knex: Knex): Promise<number> { |
|
return await knex.transaction(async (trx) => { |
|
try { |
|
// Create the new integration. |
|
var integration: any = { |
|
name: integration.name, |
|
user: userId, |
|
type: integration.type, |
|
details: JSON.stringify(integration.details), |
|
secretDetails: JSON.stringify(integration.secretDetails), |
|
} |
|
const integrationId = (await trx('integrations') |
|
.insert(integration) |
|
.returning('id') // Needed for Postgres |
|
)[0]; |
|
|
|
return integrationId; |
|
} catch (e) { |
|
trx.rollback(); |
|
throw e; |
|
} |
|
}) |
|
} |
|
|
|
export async function getIntegration(userId: number, id: number, knex: Knex): Promise<api.IntegrationData> { |
|
const integration = (await knex.select(['id', 'name', 'type', 'details']) |
|
.from('integrations') |
|
.where({ 'user': userId, 'id': id }))[0]; |
|
|
|
if (integration) { |
|
const r: api.IntegrationData = { |
|
mbApi_typename: "integrationData", |
|
name: integration.name, |
|
type: integration.type, |
|
details: asJson(integration.details), |
|
} |
|
return r; |
|
} else { |
|
let e: DBError = { |
|
name: "DBError", |
|
kind: DBErrorKind.ResourceNotFound, |
|
message: "Resource not found." |
|
} |
|
throw e; |
|
} |
|
} |
|
|
|
export async function listIntegrations(userId: number, knex: Knex): Promise<api.IntegrationDataWithId[]> { |
|
const integrations: api.IntegrationDataWithId[] = ( |
|
await knex.select(['id', 'name', 'type', 'details']) |
|
.from('integrations') |
|
.where({ user: userId }) |
|
).map((object: any) => { |
|
return { |
|
mbApi_typename: "integrationData", |
|
id: object.id, |
|
name: object.name, |
|
type: object.type, |
|
details: asJson(object.details), |
|
} |
|
}) |
|
|
|
return integrations; |
|
} |
|
|
|
export async function deleteIntegration(userId: number, id: number, knex: Knex) { |
|
await knex.transaction(async (trx) => { |
|
try { |
|
// Start retrieving the integration itself. |
|
const integrationId = await trx.select('id') |
|
.from('integrations') |
|
.where({ 'user': userId }) |
|
.where({ id: id }) |
|
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined) |
|
|
|
// Check that we found all objects we need. |
|
if (!integrationId) { |
|
const e: DBError = { |
|
name: "DBError", |
|
kind: DBErrorKind.ResourceNotFound, |
|
message: "Resource not found." |
|
}; |
|
throw e; |
|
} |
|
|
|
// Delete the integration. |
|
await trx('integrations') |
|
.where({ 'user': userId, 'id': integrationId }) |
|
.del(); |
|
|
|
} catch (e) { |
|
trx.rollback(); |
|
} |
|
}) |
|
} |
|
|
|
export async function modifyIntegration(userId: number, id: number, integration: PartialIntegrationData, knex: Knex): Promise<void> { |
|
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: DBError = { |
|
name: "DBError", |
|
kind: DBErrorKind.ResourceNotFound, |
|
message: "Resource not found", |
|
}; |
|
throw e; |
|
} |
|
|
|
// Modify the integration. |
|
var update: any = {}; |
|
if ("name" in integration) { update["name"] = integration.name; } |
|
if ("details" in integration) { update["details"] = JSON.stringify(integration.details); } |
|
if ("type" in integration) { update["type"] = integration.type; } |
|
if ("secretDetails" in integration) { update["secretDetails"] = JSON.stringify(integration.details); } |
|
await trx('integrations') |
|
.where({ 'user': userId, 'id': id }) |
|
.update(update) |
|
} catch (e) { |
|
trx.rollback(); |
|
} |
|
}) |
|
} |