|
| 1 | +import { |
| 2 | + IHttp, |
| 3 | + IModify, |
| 4 | + IPersistence, |
| 5 | + IRead, |
| 6 | +} from "@rocket.chat/apps-engine/definition/accessors"; |
| 7 | +import { |
| 8 | + ApiEndpoint, |
| 9 | + IApiEndpointInfo, |
| 10 | + IApiRequest, |
| 11 | + IApiResponse, |
| 12 | +} from "@rocket.chat/apps-engine/definition/api"; |
| 13 | +import { IDB } from "../core/db/db.types"; |
| 14 | +import { Neo4j } from "../core/db/neo4j"; |
| 15 | +import { EstablishRelationsEndpointRelations, EstablishRelationsEndpointRequestBody, EstablishRelationsEndpointResponseBody } from "./establishRelations.types"; |
| 16 | + |
| 17 | +namespace Helpers { |
| 18 | + async function establishRelation( |
| 19 | + db: IDB, |
| 20 | + sourceID: string, |
| 21 | + targetID: string, |
| 22 | + relation: string |
| 23 | + ) { |
| 24 | + const query = [ |
| 25 | + `MATCH (n { id: $sourceID })`, |
| 26 | + `MATCH (m { id: $targetID })`, |
| 27 | + `CREATE (n)-[:${relation}]->(m)\n`, |
| 28 | + ].join("\n"); |
| 29 | + try { |
| 30 | + await db.run(query, { sourceID, targetID }); |
| 31 | + } catch (e) { |
| 32 | + console.error(e); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + export async function establishRelations(db: IDB, relations: EstablishRelationsEndpointRelations[]) { |
| 37 | + const jobs: Promise<any>[] = []; |
| 38 | + for (const relation of relations) { |
| 39 | + const job = establishRelation( |
| 40 | + db, |
| 41 | + relation.source, |
| 42 | + relation.target, |
| 43 | + relation.relation |
| 44 | + ); |
| 45 | + jobs.push(job); |
| 46 | + } |
| 47 | + await Promise.all(jobs); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export class EstablishRelationsEndpoint extends ApiEndpoint { |
| 52 | + public path = "establishRelations"; |
| 53 | + |
| 54 | + makeBodies( |
| 55 | + content: any |
| 56 | + ): [EstablishRelationsEndpointRequestBody, EstablishRelationsEndpointResponseBody] { |
| 57 | + const requestBody = JSON.parse(content) as EstablishRelationsEndpointRequestBody; |
| 58 | + const responseBody: EstablishRelationsEndpointResponseBody = { |
| 59 | + status: 200, |
| 60 | + }; |
| 61 | + |
| 62 | + return [requestBody, responseBody]; |
| 63 | + } |
| 64 | + |
| 65 | + async commitProgress( |
| 66 | + db: IDB |
| 67 | + ): Promise<EstablishRelationsEndpointResponseBody["status"]> { |
| 68 | + try { |
| 69 | + try { |
| 70 | + await db.commitTransaction(); |
| 71 | + } catch (e) { |
| 72 | + console.error(e); |
| 73 | + await db.rollbackTransaction(); |
| 74 | + |
| 75 | + return 500; |
| 76 | + } |
| 77 | + } catch (e) { |
| 78 | + console.error(e); |
| 79 | + return 500; |
| 80 | + } |
| 81 | + |
| 82 | + return 200; |
| 83 | + } |
| 84 | + |
| 85 | + public async post( |
| 86 | + request: IApiRequest, |
| 87 | + endpoint: IApiEndpointInfo, |
| 88 | + read: IRead, |
| 89 | + modify: IModify, |
| 90 | + http: IHttp, |
| 91 | + persis: IPersistence |
| 92 | + ): Promise<IApiResponse> { |
| 93 | + let [{ relations }, responseBody] = this.makeBodies(request.content); |
| 94 | + |
| 95 | + const db = new Neo4j(http); |
| 96 | + await db.verifyConnectivity(); |
| 97 | + // ----------------------------------------------------------------------------------- |
| 98 | + await db.beginTransaction(); |
| 99 | + await Helpers.establishRelations(db, relations); |
| 100 | + responseBody.status = await this.commitProgress(db); |
| 101 | + // ----------------------------------------------------------------------------------- |
| 102 | + |
| 103 | + return this.success(JSON.stringify(responseBody)); |
| 104 | + } |
| 105 | +} |
0 commit comments