1- import {
2- IHttp ,
3- IHttpResponse ,
4- } from "@rocket.chat/apps-engine/definition/accessors" ;
1+ import { IHttp } from "@rocket.chat/apps-engine/definition/accessors" ;
52import { IDB } from "./db.types" ;
63
4+ export type Neo4jResponse = {
5+ transactionUrl ?: string ;
6+ results : {
7+ columns : string [ ] ;
8+ data : {
9+ row : Record < string , any > [ ] ;
10+ meta : {
11+ id : number ;
12+ elementId : string ;
13+ type : string ;
14+ deleted : boolean ;
15+ } [ ] ;
16+ } [ ] ;
17+ } [ ] ;
18+ errors : any [ ] ;
19+ lastBookmarks : string [ ] ;
20+ } ;
21+
722export class Neo4j implements IDB {
823 private http : IHttp ;
924 private readonly baseUrl : string ;
@@ -18,34 +33,44 @@ export class Neo4j implements IDB {
1833 // password: string
1934 ) {
2035 this . http = http ;
21- // this.baseUrl = baseUrl;
22- // this.username = username;
23- // this.password = password;
36+ // this.baseUrl = "http://neo4j:7474";
37+ // this.username = "neo4j";
38+ // this.password = "strongpasswordsafe123";
39+ this . baseUrl = "http://3.89.86.217:7474" ;
40+ this . username = "neo4j" ;
41+ this . password = "errors-fourths-seeds" ;
2442 }
2543
2644 private async sendRequest (
2745 endpoint : string ,
2846 method : string ,
2947 data ?: any
30- ) : Promise < IHttpResponse > {
48+ ) : Promise < Neo4jResponse | null > {
3149 const url = `${ this . baseUrl } ${ endpoint } ` ;
3250 const headers = {
3351 "Content-Type" : "application/json" ,
3452 Authorization : `Basic ${ Buffer . from (
3553 `${ this . username } :${ this . password } `
3654 ) . toString ( "base64" ) } `,
3755 } ;
38- return this . http . post ( url , {
56+
57+ const res = await this . http . post ( url , {
3958 headers,
40- data : data ? JSON . stringify ( data ) : undefined ,
59+ data : data ,
4160 } ) ;
61+ if ( ! res || ! [ 200 , 201 ] . includes ( res . statusCode ) || ! res . content ) {
62+ console . log ( res ) ;
63+ return null ;
64+ }
65+
66+ return JSON . parse ( res . content ) ;
4267 }
4368
4469 async verifyConnectivity ( ) : Promise < void > {
4570 const response = await this . sendRequest ( "/db/neo4j/tx/commit" , "POST" , {
4671 statements : [ ] ,
4772 } ) ;
48- if ( response . statusCode !== 200 ) {
73+ if ( ! response ) {
4974 throw new Error ( "Failed to connect to Neo4j" ) ;
5075 }
5176 }
@@ -63,10 +88,10 @@ export class Neo4j implements IDB {
6388 const response = await this . sendRequest ( "/db/neo4j/tx" , "POST" , {
6489 statements : [ ] ,
6590 } ) ;
66- if ( response . statusCode !== 201 ) {
67- throw new Error ( `Failed to begin transaction: ${ response . content } ` ) ;
91+ if ( ! response ) {
92+ throw new Error ( `Failed to begin transaction` ) ;
6893 }
69- this . transactionUrl = response . headers ?. [ "location" ] ;
94+ this . transactionUrl = response . transactionUrl ;
7095 }
7196
7297 async commitTransaction ( ) : Promise < void > {
@@ -77,10 +102,8 @@ export class Neo4j implements IDB {
77102 `${ this . transactionUrl } /commit` ,
78103 "POST"
79104 ) ;
80- if ( response . statusCode !== 200 ) {
81- throw new Error (
82- `Failed to commit transaction: ${ response . content } `
83- ) ;
105+ if ( ! response ) {
106+ throw new Error ( "Failed to commit transaction" ) ;
84107 }
85108 this . transactionUrl = undefined ;
86109 }
@@ -90,15 +113,16 @@ export class Neo4j implements IDB {
90113 throw new Error ( "No transaction to rollback" ) ;
91114 }
92115 const response = await this . sendRequest ( this . transactionUrl , "DELETE" ) ;
93- if ( response . statusCode !== 200 ) {
94- throw new Error (
95- `Failed to rollback transaction: ${ response . content } `
96- ) ;
116+ if ( ! response ) {
117+ throw new Error ( "Failed to rollback transaction" ) ;
97118 }
98119 this . transactionUrl = undefined ;
99120 }
100121
101- async run ( query : string , params ?: any ) : Promise < any > {
122+ async run (
123+ query : string ,
124+ params ?: any
125+ ) : Promise < Record < string , any > [ ] | null > {
102126 const data = {
103127 statements : [
104128 {
@@ -108,7 +132,7 @@ export class Neo4j implements IDB {
108132 ] ,
109133 } ;
110134
111- let response ;
135+ let response : Neo4jResponse | null = null ;
112136 if ( this . transactionUrl ) {
113137 response = await this . sendRequest (
114138 this . transactionUrl ,
@@ -123,10 +147,23 @@ export class Neo4j implements IDB {
123147 ) ;
124148 }
125149
126- if ( response . statusCode !== 200 ) {
127- throw new Error ( `Failed to run query: ${ response . content } ` ) ;
150+ if ( ! response ) {
151+ throw new Error ( "Failed to run query" ) ;
152+ }
153+
154+ if ( response . errors . length ) {
155+ return null ;
156+ }
157+
158+ const nodes : Record < string , any > [ ] = [ ] ;
159+ for ( const result of response . results ) {
160+ for ( const data of result . data ) {
161+ for ( const row of data . row ) {
162+ nodes . push ( row ) ;
163+ }
164+ }
128165 }
129166
130- return response . data ;
167+ return nodes ;
131168 }
132169}
0 commit comments