1- const CRUD = require ( '@cocreate/crud-client' )
2- const socketClient = require ( '@cocreate/socket-client' )
3- let socket = new socketClient ( "ws" ) ;
4-
5- CRUD . setSocket ( socket ) ;
6-
7- var api = ( ( ) => {
8- return {
9- getOrg : async ( config , component ) => {
10-
11- socket . create ( {
12- namespace : config [ "organization_id" ] ,
13- room : null ,
14- host : config [ "host" ]
15- } )
16-
17- let org = await CRUD . send ( {
18- method : 'object.read' ,
19- array : "organizations" ,
20- key : config [ "key" ] ,
21- organization_id : config [ "organization_id" ] ,
22- object : {
23- _id : config [ "organization_id" ]
24- }
1+ class CoCreateApi {
2+ constructor ( crud ) {
3+ this . wsManager = crud . wsManager
4+ this . crud = crud
5+ this . init ( )
6+ }
257
26- } ) ;
8+ async getOrganization ( config , component ) {
279
28- if ( ! org || ! org . object && ! org . object [ 0 ] ) {
29- console . log ( component , " Error GET ORG in : " , e ) ;
30- return false ;
10+ let org = await CRUD . send ( {
11+ method : 'object.read' ,
12+ array : "organizations" ,
13+ key : config [ "key" ] ,
14+ organization_id : config [ "organization_id" ] ,
15+ object : {
16+ _id : config [ "organization_id" ]
3117 }
3218
33- return org . object [ 0 ] ;
34- } ,
35-
36- getOrgInRoutesbyHostname : async ( config , hostname ) => {
37- var socket_config = {
38- "config" : {
39- "key" : config [ "config" ] [ "key" ] ,
40- "organization_id" : config [ "config" ] [ "organization_id" ] ,
41- } ,
42- "prefix" : "ws" ,
43- "host" : "server.cocreate.app:8088"
44- } ;
45-
46- socket . create ( {
47- namespace : socket_config . config . organization_id ,
48- room : null ,
49- host : socket_config . host
50- } )
19+ } ) ;
5120
52- let data2 = await CRUD . send ( {
53- method : 'object.read' ,
54- array : "organizations" ,
55- object : {
56- $filter : {
57- query : [ {
58- key : 'host' ,
59- operator : "$in" ,
60- value : [ hostname ]
61- } ] ,
62- } ,
63- } ,
64- key : config [ "config" ] [ "key" ] ,
65- organization_id : config [ "config" ] [ "organization_id" ]
66- } ) ;
67-
68- var org = data2 . object [ 0 ]
21+ if ( ! org || ! org . object && ! org . object [ 0 ] ) {
22+ console . log ( component , " Error GET ORG in : " , e ) ;
23+ return false ;
24+ }
6925
70- var socket_config = {
71- "config" : {
72- "key" : org [ "key" ] ,
73- "organization_id" : org [ "_id" ] . toString ( ) ,
74- } ,
75- "prefix" : "ws" ,
76- "host" : "server.cocreate.app:8088"
77- }
26+ return org . object [ 0 ] ;
27+ }
7828
79- //other connection
80- socket . create ( {
81- namespace : socket_config . config . organization_id ,
82- room : null ,
83- host : socket_config . host
84- } )
29+ async getApiKey ( organization_id , name ) {
30+ this . organizations [ organization_id ] = this . getOrganization ( organization_id , name )
31+ this . organizations [ organization_id ] = await this . organizations [ organization_id ]
32+ return this . organizations [ organization_id ]
33+ }
8534
86- let myOrg = await CRUD . send ( {
87- method : 'object.read' ,
88- array : "organizations" ,
89- key : org [ "key" ] ,
90- organization_id : org [ "_id" ] ,
91- object : {
92- _id : org [ "_id" ]
93- }
94- } ) ;
95- let result = { 'row' : myOrg , 'socket_config' : socket_config } ;
96- return result ;
35+ async getOrganizationNew ( organization_id , name ) {
36+ let organization = await this . crud . send ( {
37+ method : 'object.read' ,
38+ database : organization_id ,
39+ array : 'organizations' ,
40+ object : [ { _id : organization_id } ] ,
41+ organization_id
42+ } )
43+
44+ if ( organization
45+ && organization . object
46+ && organization . object [ 0 ] ) {
47+ if ( organization . object [ 0 ] . apis && organization . object [ 0 ] . apis [ name ] ) {
48+ return organization . object [ 0 ] . apis && organization . object [ 0 ] . apis [ name ]
49+ } else
50+ return { [ this . name ] : false , error : 'An apikey could not be found' }
51+ } else {
52+ return { serverOrganization : false , error : 'An organization could not be found' }
9753 }
9854
9955 }
100- } ) ( ) ;
10156
102- module . exports = api ;
57+ }
58+
59+
60+ module . exports = CoCreateApi ;
0 commit comments