terminusdb 12.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/Contributing.md +36 -0
  2. package/LICENSE +201 -0
  3. package/README.md +175 -0
  4. package/RELEASE_NOTES.md +462 -0
  5. package/dist/index.html +22 -0
  6. package/dist/terminusdb-client.min.js +3 -0
  7. package/dist/terminusdb-client.min.js.LICENSE.txt +188 -0
  8. package/dist/terminusdb-client.min.js.map +1 -0
  9. package/dist/typescript/index.d.ts +14 -0
  10. package/dist/typescript/lib/accessControl.d.ts +554 -0
  11. package/dist/typescript/lib/axiosInstance.d.ts +2 -0
  12. package/dist/typescript/lib/connectionConfig.d.ts +381 -0
  13. package/dist/typescript/lib/const.d.ts +54 -0
  14. package/dist/typescript/lib/dispatchRequest.d.ts +17 -0
  15. package/dist/typescript/lib/errorMessage.d.ts +25 -0
  16. package/dist/typescript/lib/query/woqlBuilder.d.ts +75 -0
  17. package/dist/typescript/lib/query/woqlCore.d.ts +341 -0
  18. package/dist/typescript/lib/query/woqlDoc.d.ts +63 -0
  19. package/dist/typescript/lib/query/woqlLibrary.d.ts +718 -0
  20. package/dist/typescript/lib/query/woqlPrinter.d.ts +71 -0
  21. package/dist/typescript/lib/query/woqlQuery.d.ts +833 -0
  22. package/dist/typescript/lib/typedef.d.ts +624 -0
  23. package/dist/typescript/lib/utils.d.ts +199 -0
  24. package/dist/typescript/lib/valueHash.d.ts +146 -0
  25. package/dist/typescript/lib/viewer/chartConfig.d.ts +62 -0
  26. package/dist/typescript/lib/viewer/chooserConfig.d.ts +38 -0
  27. package/dist/typescript/lib/viewer/documentFrame.d.ts +44 -0
  28. package/dist/typescript/lib/viewer/frameConfig.d.ts +74 -0
  29. package/dist/typescript/lib/viewer/frameRule.d.ts +145 -0
  30. package/dist/typescript/lib/viewer/graphConfig.d.ts +73 -0
  31. package/dist/typescript/lib/viewer/objectFrame.d.ts +212 -0
  32. package/dist/typescript/lib/viewer/streamConfig.d.ts +23 -0
  33. package/dist/typescript/lib/viewer/tableConfig.d.ts +66 -0
  34. package/dist/typescript/lib/viewer/terminusRule.d.ts +75 -0
  35. package/dist/typescript/lib/viewer/viewConfig.d.ts +47 -0
  36. package/dist/typescript/lib/viewer/woqlChart.d.ts +1 -0
  37. package/dist/typescript/lib/viewer/woqlChooser.d.ts +56 -0
  38. package/dist/typescript/lib/viewer/woqlGraph.d.ts +26 -0
  39. package/dist/typescript/lib/viewer/woqlPaging.d.ts +1 -0
  40. package/dist/typescript/lib/viewer/woqlResult.d.ts +128 -0
  41. package/dist/typescript/lib/viewer/woqlRule.d.ts +96 -0
  42. package/dist/typescript/lib/viewer/woqlStream.d.ts +31 -0
  43. package/dist/typescript/lib/viewer/woqlTable.d.ts +102 -0
  44. package/dist/typescript/lib/viewer/woqlView.d.ts +49 -0
  45. package/dist/typescript/lib/woql.d.ts +1267 -0
  46. package/dist/typescript/lib/woqlClient.d.ts +1216 -0
  47. package/index.js +28 -0
  48. package/lib/.eslintrc +1 -0
  49. package/lib/accessControl.js +988 -0
  50. package/lib/axiosInstance.js +5 -0
  51. package/lib/connectionConfig.js +765 -0
  52. package/lib/const.js +59 -0
  53. package/lib/dispatchRequest.js +236 -0
  54. package/lib/errorMessage.js +110 -0
  55. package/lib/query/woqlBuilder.js +234 -0
  56. package/lib/query/woqlCore.js +934 -0
  57. package/lib/query/woqlDoc.js +177 -0
  58. package/lib/query/woqlLibrary.js +1015 -0
  59. package/lib/query/woqlPrinter.js +476 -0
  60. package/lib/query/woqlQuery.js +1865 -0
  61. package/lib/typedef.js +248 -0
  62. package/lib/utils.js +817 -0
  63. package/lib/valueHash.js_old +581 -0
  64. package/lib/viewer/chartConfig.js +411 -0
  65. package/lib/viewer/chooserConfig.js +234 -0
  66. package/lib/viewer/documentFrame.js +206 -0
  67. package/lib/viewer/frameConfig.js +469 -0
  68. package/lib/viewer/frameRule.js +519 -0
  69. package/lib/viewer/graphConfig.js +345 -0
  70. package/lib/viewer/objectFrame.js +1550 -0
  71. package/lib/viewer/streamConfig.js +82 -0
  72. package/lib/viewer/tableConfig.js +310 -0
  73. package/lib/viewer/terminusRule.js +196 -0
  74. package/lib/viewer/viewConfig.js +219 -0
  75. package/lib/viewer/woqlChart.js +17 -0
  76. package/lib/viewer/woqlChooser.js +171 -0
  77. package/lib/viewer/woqlGraph.js +295 -0
  78. package/lib/viewer/woqlPaging.js +148 -0
  79. package/lib/viewer/woqlResult.js +258 -0
  80. package/lib/viewer/woqlRule.js +312 -0
  81. package/lib/viewer/woqlStream.js +27 -0
  82. package/lib/viewer/woqlTable.js +332 -0
  83. package/lib/viewer/woqlView.js +107 -0
  84. package/lib/woql.js +1693 -0
  85. package/lib/woqlClient.js +2091 -0
  86. package/package.json +110 -0
@@ -0,0 +1,988 @@
1
+ /* eslint-disable max-len */
2
+ /* eslint-disable no-underscore-dangle */
3
+ const DispatchRequest = require('./dispatchRequest');
4
+ const ErrorMessage = require('./errorMessage');
5
+ const CONST = require('./const');
6
+ const UTILS = require('./utils');
7
+ // eslint-disable-next-line no-unused-vars
8
+ const typedef = require('./typedef');
9
+ /**
10
+ * @license Apache Version 2
11
+ * @module AccessControl
12
+ * @constructor AccessControl
13
+ * @description The AccessControl is a driver to work with
14
+ * TerminusDB and TerminusX access control api
15
+ * for the credential you can use the JWT token, the API token or
16
+ * the basic authentication with username and password
17
+ * @example
18
+ * //connect with the API token
19
+ * //(to request a token create an account in https://terminusdb.com/)
20
+ * const accessContol = new AccessControl("https://servername.com",
21
+ * {organization:"my_team_name",
22
+ * token:"dGVybWludXNkYjovLy9kYXRhL2tleXNfYXB........"})
23
+ * accessControl.getOrgUsers().then(result=>{
24
+ * console.log(result)
25
+ * })
26
+ *
27
+ * //connect with the jwt token this type of connection is only for the dashboard
28
+ * //or for application integrate with our login workflow
29
+ * const accessContol = new AccessControl("https://servername.com",
30
+ * {organization:"my_team_name",
31
+ * jwt:"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkpXUjBIOXYyeTFORUd........"})
32
+ * accessControl.getOrgUsers().then(result=>{
33
+ * console.log(result)
34
+ * })
35
+ *
36
+ * //if the jwt is expired you can change it with
37
+ * accessControl.setJwtToken("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkpXUjBIOXYy
38
+ * eTFORUd.......")
39
+ *
40
+ * //connect with the base authentication this type of connection is only for the local installation
41
+ * const accessContol = new AccessControl("http://127.0.0.1:6363",
42
+ * {organization:"my_team_name", user:"admin"
43
+ * key:"mykey"})
44
+ * accessControl.getOrgUsers().then(result=>{
45
+ * console.log(result)
46
+ * })
47
+ *
48
+ */
49
+
50
+ function AccessControl(cloudAPIUrl, params) {
51
+ this.baseURL = this.getAPIUrl(cloudAPIUrl);
52
+ if (!params) return;
53
+ if (params.jwt) {
54
+ this.setJwtToken(params.jwt);
55
+ } else if (params.token) {
56
+ this.setApiToken(params.token);
57
+ } else if (params.key) {
58
+ this.setApiKey(params.key);
59
+ this.user = params.user;
60
+ }
61
+ this.defaultOrganization = this.getDefaultOrganization(params);
62
+ }
63
+
64
+ /**
65
+ * Get a organization from parameters.
66
+ * @param {object} params - The parameters
67
+ * @return {string|undefined} - organization
68
+ */
69
+ AccessControl.prototype.getDefaultOrganization = function (params) {
70
+ if (params && params.organization && typeof params.organization === 'string') {
71
+ return params.organization;
72
+ }
73
+ return undefined;
74
+ };
75
+ /**
76
+ * Sets the Jwt token for the object
77
+ * @param {string} jwt - The jwt api token to use
78
+ */
79
+ AccessControl.prototype.setJwtToken = function (jwt) {
80
+ if (!jwt) {
81
+ throw new Error('TerminusX Access token required');
82
+ }
83
+
84
+ this.apiKey = jwt;
85
+ this.apiType = 'jwt';
86
+ };
87
+
88
+ /**
89
+ * Sets the API token for the object, to request a token create an account in https://terminusdb.com/
90
+ * @param {string} atokenpi - The API token to use to connect with TerminusX
91
+ */
92
+ AccessControl.prototype.setApiToken = function (token) {
93
+ if (!token) {
94
+ throw new Error('TerminusX Access token required');
95
+ }
96
+
97
+ this.apiKey = token;
98
+ this.apiType = 'apikey';
99
+ };
100
+
101
+ /**
102
+ * Sets the API token for the object, to request a token create an account in https://terminusdb.com/
103
+ * @param {string} atokenpi - The API token to use to connect with TerminusX
104
+ */
105
+ AccessControl.prototype.setApiKey = function (key) {
106
+ if (!key) {
107
+ throw new Error('TerminusDB bacis authentication key required');
108
+ }
109
+
110
+ this.apiKey = key;
111
+ this.apiType = 'basic';
112
+ };
113
+
114
+ /**
115
+ * Get a API url from cloudAPIUrl
116
+ * @param {string} cloudAPIUrl - The base url for cloud
117
+ * @return {string} apiUrl
118
+ */
119
+ AccessControl.prototype.getAPIUrl = function (cloudAPIUrl) {
120
+ if (!cloudAPIUrl || typeof cloudAPIUrl !== 'string') {
121
+ throw new Error('TerminusX api url required!');
122
+ }
123
+ if (cloudAPIUrl.lastIndexOf('/') !== cloudAPIUrl.length - 1) {
124
+ // eslint-disable-next-line no-param-reassign
125
+ cloudAPIUrl += '/'; // always append slash to ensure regularity
126
+ }
127
+ return `${cloudAPIUrl}api`;
128
+ };
129
+
130
+ AccessControl.prototype.dispatch = function (requestUrl, action, payload) {
131
+ if (!requestUrl) {
132
+ return Promise.reject(
133
+ new Error(
134
+ ErrorMessage.getInvalidParameterMessage(
135
+ action,
136
+ 'Invalid request URL',
137
+ ),
138
+ ),
139
+ );
140
+ }
141
+ return DispatchRequest(
142
+ requestUrl,
143
+ action,
144
+ payload,
145
+ { type: this.apiType, key: this.apiKey, user: this.user },
146
+ null,
147
+ this.customHeaders(),
148
+ );
149
+ };
150
+
151
+ /**
152
+ * add extra headers to your request
153
+ * @param {object} customHeaders
154
+ * @returns {object}
155
+ */
156
+
157
+ // eslint-disable-next-line consistent-return
158
+ AccessControl.prototype.customHeaders = function (customHeaders) {
159
+ if (customHeaders) this._customHeaders = customHeaders;
160
+ else return this._customHeaders;
161
+ };
162
+
163
+ /**
164
+ * -- TerminusDB API ---
165
+ * Get an organization from the TerminusDB API.
166
+ * @param {string} organization - The organization
167
+ * @return {object} - organization
168
+ */
169
+ AccessControl.prototype.getOrganization = function (org) {
170
+ return this.dispatch(`${this.baseURL}/organizations/${org}`, CONST.GET);
171
+ };
172
+
173
+ /**
174
+ * -- TerminusDB API ---
175
+ * This end point works in basic authentication, admin user
176
+ * Get list of organizations
177
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
178
+ */
179
+
180
+ AccessControl.prototype.getAllOrganizations = function () {
181
+ return this.dispatch(`${this.baseURL}/organizations`, CONST.GET);
182
+ };
183
+
184
+ /**
185
+ * -- TerminusDB API ---
186
+ * This end point works in basic authentication, admin user
187
+ * Create an organization
188
+ * @param {string} orgName - The organization name to create
189
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
190
+ * @example
191
+ * accessControl.createOrganization("my_org_name").then(result=>{
192
+ * console.log(result)
193
+ * })
194
+ */
195
+ AccessControl.prototype.createOrganization = function (orgName) {
196
+ // maybe we have to review this
197
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(orgName)}`, CONST.POST, {});
198
+ };
199
+
200
+ /**
201
+ * -- TerminusDB API ---
202
+ * Delete an Organization
203
+ * @param {string} orgName - The organization name to delete
204
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
205
+ * @example
206
+ * accessControl.deleteOrganization("my_org_name").then(result=>{
207
+ * console.log(result)
208
+ * })
209
+ */
210
+ AccessControl.prototype.deleteOrganization = function (orgName) {
211
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(orgName)}`, CONST.DELETE);
212
+ };
213
+
214
+ /**
215
+ * --TerminusDB API ---
216
+ * basic authentication, admin user.
217
+ * Create a new role in the system database.
218
+ * @param {string} [name] - The role name.
219
+ * @param {typedef.RolesActions} [actions] - A list of actions
220
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
221
+ * @example
222
+ * accessControl.createRole("Reader",[ACTIONS.INSTANCE_READ_ACCESS]).then(result=>{
223
+ * console.log(result)
224
+ * })
225
+ *
226
+ */
227
+ AccessControl.prototype.createRole = function (name, actions) {
228
+ const payload = { name, action: actions };
229
+ return this.dispatch(`${this.baseURL}/roles`, CONST.POST, payload);
230
+ };
231
+
232
+ /**
233
+ * -- TerminusdDB API ---
234
+ * basic Authentication, admin user.
235
+ * Delete role in the system database, (this api is enabled only in the local installation)
236
+ * @param {string} [name] - The role name.
237
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
238
+ * @example
239
+ * accessControl.deleteRole("Reader").then(result=>{
240
+ * console.log(result)
241
+ * })
242
+ *
243
+ */
244
+ AccessControl.prototype.deleteRole = function (name) {
245
+ return this.dispatch(`${this.baseURL}/roles/${UTILS.encodeURISegment(name)}`, CONST.DELETE);
246
+ };
247
+
248
+ /**
249
+ * -- TerminusdDB API ---
250
+ * basic Authentication, admin user.
251
+ * Return the list of all the users (this api is enabled only in the local installation)
252
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
253
+ * @example
254
+ * accessControl.getAllUsers().then(result=>{
255
+ * console.log(result)
256
+ * })
257
+ *
258
+ */
259
+
260
+ AccessControl.prototype.getAllUsers = function () {
261
+ return this.dispatch(`${this.baseURL}/users`, CONST.GET);
262
+ };
263
+
264
+ /**
265
+ * -- TerminusdDB API ---
266
+ * basic Authentication, admin user.
267
+ * Add the user into the system database
268
+ * @param {string} name - the user name
269
+ * @param {string} [password] - you need the password for basic authentication
270
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
271
+ * @example
272
+ * accessControl.deleteUser(userId).then(result=>{
273
+ * console.log(result)
274
+ * })
275
+ *
276
+ */
277
+
278
+ AccessControl.prototype.createUser = function (name, password) {
279
+ const payload = { name, password };
280
+ return this.dispatch(`${this.baseURL}/users`, CONST.POST, payload);
281
+ };
282
+
283
+ /**
284
+ * -- TerminusdDB API ---
285
+ * basic Authentication, admin user.
286
+ * Remove the user from the system database.
287
+ * @param {string} userId - the document user id
288
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
289
+ * @example
290
+ * accessControl.deleteUser(userId).then(result=>{
291
+ * console.log(result)
292
+ * })
293
+ *
294
+ */
295
+
296
+ AccessControl.prototype.deleteUser = function (userId) {
297
+ return this.dispatch(`${this.baseURL}/users/${UTILS.encodeURISegment(userId)}`, CONST.DELETE);
298
+ };
299
+
300
+ /**
301
+ * -- TerminusdDB API ---
302
+ * Grant/Revoke Capability
303
+ * @param {string} userName - the document user id
304
+ * @param {string} resourceName - the name of a (database or team)
305
+ * @param {array} rolesArr - the roles name list
306
+ * @param {typedef.CapabilityCommand} operation - grant/revoke operation
307
+ * @param {typedef.ScopeType} [scopeType] - the resource type (database or organization)
308
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
309
+ * @example
310
+ * //we add an user to an organization and manage users' access
311
+ * //the user myUser can access the Organization and all the database under the organization with "reader" Role
312
+ * client.manageCapability(myUser,myteam,[reader],"grant","organization").then(result=>{
313
+ * consol.log(result)
314
+ * })
315
+ *
316
+ * //the user myUser can access the database db__001 under the organization myteam
317
+ * //with "writer" Role
318
+ * client.manageCapability(myUser,myteam/db__001,[writer],"grant","database").then(result=>{
319
+ * consol.log(result)
320
+ * })
321
+ */
322
+
323
+ AccessControl.prototype.manageCapability = function (userName, resourceName, rolesArr, operation, scopeType) {
324
+ const payload = {
325
+ operation,
326
+ user: userName,
327
+ roles: rolesArr,
328
+ scope: resourceName,
329
+ scope_type: scopeType,
330
+ };
331
+ return this.dispatch(`${this.baseURL}/capabilities`, CONST.POST, payload);
332
+ };
333
+
334
+ /**
335
+ * --TerminusX and TerminusDB API ---
336
+ * Get all the system database roles types.
337
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
338
+ */
339
+ AccessControl.prototype.getAccessRoles = function () {
340
+ return this.dispatch(`${this.baseURL}/roles`, CONST.GET);
341
+ };
342
+
343
+ /**
344
+ * -- TerminusX and TerminusDB API --
345
+ * Get all the organization's users and roles,
346
+ * @param {string} [orgName] - The organization name.
347
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
348
+ * @example
349
+ * accessControl.getOrgUsers().then(result=>{
350
+ * console.log(result)
351
+ * })
352
+ *
353
+ * //this function will return an array of capabilities with users and roles
354
+ * //-- TerminusX -- response array example
355
+ * //[{capability: "Capability/3ea26e1d698821c570afe9cb4fe81a3......"
356
+ * // email: {@type: "xsd:string", @value: "user@terminusdb.com"}
357
+ * // picture: {@type: "xsd:string",…}
358
+ * // role: "Role/dataReader"
359
+ * // scope: "Organization/my_org_name"
360
+ * // user: "User/auth0%7C613f5dnndjdjkTTT"}]
361
+ * //
362
+ * //
363
+ * // -- Local Installation -- response array example
364
+ * //[{ "@id":"User/auth0%7C615462f8ab33f4006a6bee0c",
365
+ * // "capability": [{
366
+ * // "@id":"Capability/c52af34b71f6f8916ac0115ecb5fe0e31248ead8b1e3d100852015...",
367
+ * // "@type":"Capability",
368
+ * // "role": [{
369
+ * // "@id":"Role/admin",
370
+ * // "@type":"Role",
371
+ * // "action": ["instance_read_access"],
372
+ * // "name":"Admin Role"
373
+ * // }],
374
+ * // "scope":"Organization/@team"}]]
375
+ */
376
+
377
+ AccessControl.prototype.getOrgUsers = function (orgName) {
378
+ if (!orgName && !this.defaultOrganization) {
379
+ return Promise.reject(
380
+ new Error(
381
+ ErrorMessage.getInvalidParameterMessage(
382
+ 'GET',
383
+ 'Please provide a organization name',
384
+ ),
385
+ ),
386
+ );
387
+ }
388
+ const org = orgName || this.defaultOrganization;
389
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/users`, CONST.GET);
390
+ };
391
+
392
+ /**
393
+ * -- TerminusX and TerminusDB API --
394
+ * Get the user roles for a given organization or the default organization,
395
+ * @param {string} [userName] - The organization name.
396
+ * @param {string} [orgName] - The organization name.
397
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
398
+ * @example
399
+ * accessControl.getTeamUserRole("myUser").then(result=>{
400
+ * console.log(result)
401
+ * })
402
+ *
403
+ * //response object example
404
+ * {
405
+ * "@id": "User/myUser",
406
+ * "capability": [
407
+ * {
408
+ * "@id":"Capability/server_access",
409
+ * "@type":"Capability",
410
+ * "role": [{
411
+ * "@id":"Role/reader",
412
+ * "@type":"Role",
413
+ * "action": [
414
+ * "instance_read_access",
415
+ * ],
416
+ * "name":"reader"
417
+ * }],
418
+ * "scope":"Organization/myteam"
419
+ * }
420
+ * ],
421
+ * "name": "myUser"
422
+ *}
423
+ */
424
+
425
+ AccessControl.prototype.getTeamUserRoles = function (userName, orgName) {
426
+ if (!orgName && !this.defaultOrganization) {
427
+ return Promise.reject(
428
+ new Error(
429
+ ErrorMessage.getInvalidParameterMessage(
430
+ 'GET',
431
+ 'Please provide a organization name',
432
+ ),
433
+ ),
434
+ );
435
+ }
436
+ const org = orgName || this.defaultOrganization;
437
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/users/${UTILS.encodeURISegment(userName)}`, CONST.GET);
438
+ };
439
+
440
+ /**
441
+ * -- TerminusX API ---
442
+ * Check if the organization exists. it is a Head call .
443
+ * IMPORTANT This does not work with the API-TOKEN.
444
+ * @param {string} orgName - The organization name to check if exists.
445
+ * @return {Promise} A promise that returns the call status object, 200: if the organization
446
+ * exists and 404: if the organization does not exist
447
+ */
448
+ AccessControl.prototype.ifOrganizationExists = function (orgName) {
449
+ if (!orgName) {
450
+ return Promise.reject(
451
+ new Error(
452
+ ErrorMessage.getInvalidParameterMessage(
453
+ 'HEAD',
454
+ 'Please provide a organization name',
455
+ ),
456
+ ),
457
+ );
458
+ }
459
+
460
+ return this.dispatch(`${this.baseURL}/private/organizations/${UTILS.encodeURISegment(orgName)}`, CONST.HEAD);
461
+ };
462
+
463
+ /**
464
+ * -- TerminusX API ---
465
+ * IMPORTANT This does not work with the API-TOKEN.
466
+ * Create an organization
467
+ * @param {string} orgName - The organization name to create
468
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
469
+ * @example
470
+ * accessControl.createOrganization("my_org_name").then(result=>{
471
+ * console.log(result)
472
+ * })
473
+ */
474
+ AccessControl.prototype.createOrganizationRemote = function (orgName) {
475
+ const payload = { organization: orgName };
476
+ return this.dispatch(`${this.baseURL}/private/organizations`, CONST.POST, payload);
477
+ };
478
+
479
+ /**
480
+ * -- TerminusX API ---
481
+ * Get the pending invitations list.
482
+ * @param {string} [orgName] - The organization name.
483
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
484
+ * @example
485
+ * const invitationList = accessControl.getPendingOrgInvites().then(result=>{
486
+ * console.log(invitationList)
487
+ *
488
+ * })
489
+ * //this will return an array of invitations object like this
490
+ * //[{@id: "Organization/my_team_name/invitations/Invitation/7ad0c9eb82b6175bcda9c0dfc2ac51161ef5ba
491
+ * cb0988d992c4bce82b3fa5d25"
492
+ * // @type: "Invitation"
493
+ * // creation_date: "2021-10-22T11:13:28.762Z"
494
+ * // email_to: "new_user@terminusdb.com"
495
+ * // invited_by: "User/auth0%7C6162f8ab33567406a6bee0c"
496
+ * // role: "Role/dataReader"
497
+ * // status: "needs_invite"}]
498
+ *
499
+ */
500
+ AccessControl.prototype.getPendingOrgInvites = function (orgName) {
501
+ if (!orgName && !this.defaultOrganization) {
502
+ return Promise.reject(
503
+ new Error(
504
+ ErrorMessage.getInvalidParameterMessage(
505
+ 'GET',
506
+ 'Please provide a organization name',
507
+ ),
508
+ ),
509
+ );
510
+ }
511
+ const org = orgName || this.defaultOrganization;
512
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/invites`, CONST.GET);
513
+ };
514
+
515
+ /**
516
+ * -- TerminusX API ---
517
+ * Send a new invitation
518
+ * @param {string} userEmail - The email of user.
519
+ * @param {string} role - The role for user. (the document @id role like Role/collaborator)
520
+ * @param {string} [note] - The note to send with the invitation.
521
+ * @param {string} [orgName] - The organization name.
522
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
523
+ * @example
524
+ * accessControl.sendOrgInvite("new_user@terminusdb.com","Role/admin",
525
+ * "please join myteam").then(result=>{
526
+ * console.log(result)
527
+ * })
528
+ */
529
+ // eslint-disable-next-line default-param-last
530
+ AccessControl.prototype.sendOrgInvite = function (userEmail, role, note = '', orgName) {
531
+ let errorMessage;
532
+ if (!orgName && !this.defaultOrganization) {
533
+ errorMessage = 'Please provide a organization name';
534
+ } else if (!userEmail) {
535
+ errorMessage = 'Please provide a user email';
536
+ } else if (!role) {
537
+ errorMessage = 'Please provide a role';
538
+ }
539
+
540
+ if (errorMessage) {
541
+ return Promise.reject(
542
+ new Error(
543
+ ErrorMessage.getInvalidParameterMessage(
544
+ 'POST',
545
+ errorMessage,
546
+ ),
547
+ ),
548
+ );
549
+ }
550
+ const org = orgName || this.defaultOrganization;
551
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/invites`, CONST.POST, {
552
+ email_to: userEmail,
553
+ role,
554
+ note,
555
+ });
556
+ };
557
+
558
+ /**
559
+ * -- TerminusX API ---
560
+ * Get the invitation info
561
+ * @param {string} inviteId - The invite id to retrieve.
562
+ * @param {string} [orgName] - The organization name.
563
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
564
+ * @example
565
+ * const fullInviteId="Organization/my_team_name/invitations/Invitation/7ad0c9eb82b6175bcda9c0dfc
566
+ * 2ac51161ef5ba7cb0988d992c4bce82b3fa5d25"
567
+ * accessControl.getOrgInvite(fullInviteId).then(result=>{
568
+ * console.log(result)
569
+ * })
570
+ */
571
+ AccessControl.prototype.getOrgInvite = function (inviteId, orgName) {
572
+ let errorMessage;
573
+ if (!orgName && !this.defaultOrganization) {
574
+ errorMessage = 'Please provide a organization name';
575
+ } else if (!inviteId) {
576
+ errorMessage = 'Please provide a invite id';
577
+ }
578
+
579
+ if (errorMessage) {
580
+ return Promise.reject(
581
+ new Error(
582
+ ErrorMessage.getInvalidParameterMessage(
583
+ 'POST',
584
+ errorMessage,
585
+ ),
586
+ ),
587
+ );
588
+ }
589
+ const org = orgName || this.defaultOrganization;
590
+ const inviteHash = UTILS.removeDocType(inviteId);
591
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/invites/${inviteHash}`, CONST.GET);
592
+ };
593
+
594
+ /**
595
+ * -- TerminusX API ---
596
+ * Delete an invitation
597
+ * @param {string} inviteId - The invite id to delete.
598
+ * @param {string} [orgName] - The organization name.
599
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
600
+ * @example
601
+ * const fullInviteId="Organization/my_team_name/invitations/Invitation/7ad0c9eb82b6175bcda9
602
+ * c0dfc2ac51161ef5ba7cb0988d992c4bce82b3fa5d25"
603
+ * accessControl.deleteOrgInvite(fullInviteId).then(result=>{
604
+ * console.log(result)
605
+ * })
606
+ */
607
+ AccessControl.prototype.deleteOrgInvite = function (inviteId, orgName) {
608
+ let errorMessage;
609
+ if (!orgName && !this.defaultOrganization) {
610
+ errorMessage = 'Please provide a organization name';
611
+ } else if (!inviteId) {
612
+ errorMessage = 'Please provide a invite id';
613
+ }
614
+
615
+ if (errorMessage) {
616
+ return Promise.reject(
617
+ new Error(
618
+ ErrorMessage.getInvalidParameterMessage(
619
+ 'POST',
620
+ errorMessage,
621
+ ),
622
+ ),
623
+ );
624
+ }
625
+ const org = orgName || this.defaultOrganization;
626
+ const inviteHash = UTILS.removeDocType(inviteId);
627
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/invites/${inviteHash}`, CONST.DELETE);
628
+ };
629
+
630
+ /**
631
+ * -- TerminusX API ---
632
+ * Accept /Reject invitation. if the invitation has been accepted we add the current user
633
+ * to the organization.
634
+ *
635
+ * the only user that can accept this invitation is the user registered with the invitation email,
636
+ * we indentify the user with the jwt token
637
+ * @param {string} inviteId - The invite id to updated.
638
+ * @param {boolean} accepted - The status of the invitation.
639
+ * @param {string} [orgName] - The organization name.
640
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
641
+ * @example
642
+ * const fullInviteId="Organization/my_team_name/invitations/Invitation/7ad0c9eb82b6175bcda9
643
+ * c0dfc2ac51161ef5ba7cb0988d992c4bce82b3fa5d25"
644
+ * accessControl.updateOrgInviteStatus(fullInviteId,true).then(result=>{
645
+ * console.log(result)
646
+ * })
647
+ */
648
+ AccessControl.prototype.updateOrgInviteStatus = function (inviteId, accepted, orgName) {
649
+ let errorMessage;
650
+ if (!orgName && !this.defaultOrganization) {
651
+ errorMessage = 'Please provide a organization name';
652
+ } else if (!inviteId) {
653
+ errorMessage = 'Please provide a invite id';
654
+ } else if (typeof accepted === 'undefined') {
655
+ errorMessage = 'Please provide a accepted status';
656
+ }
657
+
658
+ if (errorMessage) {
659
+ return Promise.reject(
660
+ new Error(
661
+ ErrorMessage.getInvalidParameterMessage(
662
+ 'PUT',
663
+ errorMessage,
664
+ ),
665
+ ),
666
+ );
667
+ }
668
+ const org = orgName || this.defaultOrganization;
669
+ const inviteHash = UTILS.removeDocType(inviteId);
670
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/invites/${inviteHash}`, CONST.PUT, {
671
+ accepted,
672
+ });
673
+ };
674
+
675
+ /**
676
+ * -- TerminusX API ---
677
+ * Get the user role for a given organization or the default organization
678
+ * The user is identified by the jwt or the access token
679
+ * @param {string} [orgName] - The organization name.
680
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
681
+ * @example
682
+ * accessControl.getTeamUserRole().then(result=>{
683
+ * console.log(result)
684
+ * })
685
+ *
686
+ * //response object example
687
+ * {"userRole":"Role/admin"}
688
+ */
689
+
690
+ AccessControl.prototype.getTeamUserRole = function (orgName) {
691
+ if (!orgName && !this.defaultOrganization) {
692
+ return Promise.reject(
693
+ new Error(
694
+ ErrorMessage.getInvalidParameterMessage(
695
+ 'GET',
696
+ 'Please provide a organization name',
697
+ ),
698
+ ),
699
+ );
700
+ }
701
+ const org = orgName || this.defaultOrganization;
702
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/role`, CONST.GET);
703
+ };
704
+
705
+ /**
706
+ * -- TerminusX API --
707
+ * Remove an user from an organization, only an admin user can remove an user from an organization
708
+ * @param {string} userId - The id of the user to be removed. (this is the document user's @id)
709
+ * @param {string} [orgName] - The organization name in which the user is to be removed.
710
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
711
+ * @example
712
+ * accessControl.removeUserFromOrg("User/auth0%7C613f5dnndjdjkTTT","my_org_name").then(result=>{
713
+ * console.log(result)
714
+ * })
715
+ *
716
+ */
717
+ AccessControl.prototype.removeUserFromOrg = function (userId, orgName) {
718
+ let errorMessage;
719
+ if (!orgName && !this.defaultOrganization) {
720
+ errorMessage = 'Please provide a organization name';
721
+ } else if (!userId) {
722
+ errorMessage = 'Please provide a userId';
723
+ }
724
+
725
+ if (errorMessage) {
726
+ return Promise.reject(
727
+ new Error(
728
+ ErrorMessage.getInvalidParameterMessage(
729
+ 'DELETE',
730
+ errorMessage,
731
+ ),
732
+ ),
733
+ );
734
+ }
735
+ const org = orgName || this.defaultOrganization;
736
+ const user = UTILS.removeDocType(userId);
737
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/users/${user}`, CONST.DELETE);
738
+ };
739
+
740
+ /**
741
+ * -- TerminusX API --
742
+ * Get the user's role for every databases under the organization
743
+ * @param {string} userId - The user's id.
744
+ * @param {string} [orgName] - The organization name.
745
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
746
+ * @example
747
+ * accessControl.getDatabaseRolesOfUser('User/auth0%7C61790e366377Yu6596a').then(result=>{
748
+ * console.log(result)
749
+ * })
750
+ *
751
+ * //this is a capabilities list of databases and roles
752
+ * //[ {capability: "Capability/b395e8523d509dec6b33aefc9baed3b2e2bfadbd4c79d4ff9b20dce2b14e2edc"
753
+ * //if there is an id we have a user specific capabality for this database
754
+ * // name: {@type: "xsd:string", @value: "profiles_test"}
755
+ * // role: "Role/dataUpdater"
756
+ * // scope: "UserDatabase/7ebdfae5a02bc7e8f6d79sjjjsa4e179b1df9d4576a3b1d2e5ff3b4859"
757
+ * // user: "User/auth0%7C61790e11a3966d006906596a"},
758
+ *
759
+ * //{ capability: null
760
+ * // if the capability id is null the user level of access for this database is the
761
+ * same of the team
762
+ * //name: {@type: "xsd:string", @value: "Collab002"}
763
+ * //role: "Role/dataReader"
764
+ * // scope: "UserDatabase/acfcc2db02b83792sssb15239ccdf586fc5b176846ffe4878b1aea6a36c8f"
765
+ * //user: "User/auth0%7C61790e11a3966d006906596a"}]
766
+ */
767
+ AccessControl.prototype.getDatabaseRolesOfUser = function (userId, orgName) {
768
+ let errorMessage;
769
+ if (!orgName && !this.defaultOrganization) {
770
+ errorMessage = 'Please provide a organization name';
771
+ } else if (!userId) {
772
+ errorMessage = 'Please provide a user id';
773
+ }
774
+
775
+ if (errorMessage) {
776
+ return Promise.reject(
777
+ new Error(
778
+ ErrorMessage.getInvalidParameterMessage(
779
+ 'GET',
780
+ errorMessage,
781
+ ),
782
+ ),
783
+ );
784
+ }
785
+ const org = orgName || this.defaultOrganization;
786
+ const user = UTILS.removeDocType(userId);
787
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/users/${user}/databases`, CONST.GET);
788
+ };
789
+
790
+ /**
791
+ * -- TerminusX API --
792
+ * Create a user's a role for a resource (organization/database)
793
+ * @param {string} userId - The user's id.
794
+ * @param {string} scope - The resource name/id.
795
+ * @param {string} role - The user role to be assigned.
796
+ * @param {string} [orgName] - The organization name.
797
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
798
+ * @example
799
+ * const dbId = "UserDatabase/acfcc2db02b83792sssb15239ccdf586fc5b176846ffe4878b1aea6a36c8f"
800
+ * accessControl.assignUserRole('User/auth0%7C61790e11a3966d006906596a',dbId,
801
+ * "Role/collaborator").then(result=>{
802
+ * console.log(result)
803
+ *
804
+ * })
805
+ */
806
+ AccessControl.prototype.createUserRole = function (userId, scope, role, orgName) {
807
+ let errorMessage;
808
+ if (!orgName && !this.defaultOrganization) {
809
+ errorMessage = 'Please provide a organization name';
810
+ } else if (!userId) {
811
+ errorMessage = 'Please provide a user id';
812
+ } else if (!scope) {
813
+ errorMessage = 'Please provide a scope';
814
+ } else if (!role) {
815
+ errorMessage = 'Please provide a role';
816
+ }
817
+
818
+ if (errorMessage) {
819
+ return Promise.reject(
820
+ new Error(
821
+ ErrorMessage.getInvalidParameterMessage(
822
+ 'POST',
823
+ errorMessage,
824
+ ),
825
+ ),
826
+ );
827
+ }
828
+ const org = orgName || this.defaultOrganization;
829
+ const user = UTILS.removeDocType(userId);
830
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/users/${user}/capabilities`, CONST.POST, {
831
+ scope,
832
+ role,
833
+ });
834
+ };
835
+
836
+ /**
837
+ * -- TerminusX API --
838
+ * Update user's a role for a resource (organization/database), (this api works only in terminusX)
839
+ * @param {string} userId - The user's id.
840
+ * @param {string} capabilityId - The capability id.
841
+ * @param {string} scope - The resource name/id.
842
+ * @param {string} role - The user role to be updated.
843
+ * @param {string} [orgName] - The organization name.
844
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
845
+ * @example
846
+ * const dbId = "UserDatabase/acfcc2db02b83792sssb15239ccdf586fc5b176846ffe4878b1aea6a36c8f"
847
+ * const capId= "Capability/b395e8523d509dec6b33aefc9baed3b2e2bfadbd4c79d4ff9b20dce2b14e2edc"
848
+ * accessControl.updateUserRole('User/auth0%7C61790e11a3966d006906596a',capId,dbId,
849
+ * "Role/dataUpdater").then(result=>{
850
+ * console.log(result)
851
+ *
852
+ * })
853
+ */
854
+ AccessControl.prototype.updateUserRole = function (userId, capabilityId, scope, role, orgName) {
855
+ let errorMessage;
856
+ if (!orgName && !this.defaultOrganization) {
857
+ errorMessage = 'Please provide a organization name';
858
+ } else if (!userId) {
859
+ errorMessage = 'Please provide a user id';
860
+ } else if (!capabilityId) {
861
+ errorMessage = 'Please provide a capabilityId';
862
+ } else if (!scope) {
863
+ errorMessage = 'Please provide a scope';
864
+ } else if (!role) {
865
+ errorMessage = 'Please provide a role';
866
+ }
867
+
868
+ if (errorMessage) {
869
+ return Promise.reject(
870
+ new Error(
871
+ ErrorMessage.getInvalidParameterMessage(
872
+ 'PUT',
873
+ errorMessage,
874
+ ),
875
+ ),
876
+ );
877
+ }
878
+ const org = orgName || this.defaultOrganization;
879
+ const user = UTILS.removeDocType(userId);
880
+ const capHash = UTILS.removeDocType(capabilityId);
881
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/users/${user}/capabilities/${capHash}`, CONST.PUT, {
882
+ scope,
883
+ role,
884
+ });
885
+ };
886
+
887
+ /**
888
+ * -- TerminusX API --
889
+ * Get all the access request list for a specify organization
890
+ * @param {string} [orgName] - The organization name.
891
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
892
+ * @example
893
+ * accessControl.accessRequestsList().then(result=>{
894
+ * console.log(result)
895
+ * })
896
+ *
897
+ */
898
+ AccessControl.prototype.accessRequestsList = function (orgName) {
899
+ if (!orgName && !this.defaultOrganization) {
900
+ return Promise.reject(
901
+ new Error(
902
+ ErrorMessage.getInvalidParameterMessage(
903
+ 'GET',
904
+ 'Please provide a organization name',
905
+ ),
906
+ ),
907
+ );
908
+ }
909
+ const org = orgName || this.defaultOrganization;
910
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/access_requests`, CONST.GET);
911
+ };
912
+
913
+ /**
914
+ * -- TerminusX API --
915
+ * Get all the access request list for a specify organization
916
+ * @param {string} [email] - the user email.
917
+ * @param {string} [affiliation] - the user affiliation, company, university etc..
918
+ * @param {string} [note] - the message for the team admin
919
+ * @param {string} [orgName] - The organization name.
920
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
921
+ * @example
922
+ * accessControl.sendAccessRequest("myemail@terminusdb.com",
923
+ * "my_company",
924
+ * "please add me to your team"
925
+ * ).then(result=>{
926
+ * console.log(result)
927
+ * })
928
+ *
929
+ */
930
+ AccessControl.prototype.sendAccessRequest = function (email, affiliation, note, orgName) {
931
+ if (!orgName && !this.defaultOrganization) {
932
+ return Promise.reject(
933
+ new Error(
934
+ ErrorMessage.getInvalidParameterMessage(
935
+ 'POST',
936
+ 'Please provide a organization name',
937
+ ),
938
+ ),
939
+ );
940
+ }
941
+ const payload = { email, affiliation, note };
942
+ const org = orgName || this.defaultOrganization;
943
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/access_requests`, CONST.POST, payload);
944
+ };
945
+
946
+ /**
947
+ * -- TerminusX API --
948
+ * Delete an access request to join your team, only an admin user can delete it
949
+ * @param {string} [orgName] - The organization name.
950
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
951
+ * @example
952
+ * accessControl.deleteAccessRequest("djjdshhsuuwewueueuiHYHYYW.......").then(result=>{
953
+ * console.log(result)
954
+ * })
955
+ *
956
+ */
957
+ AccessControl.prototype.deleteAccessRequest = function (acceId, orgName) {
958
+ if (!orgName && !this.defaultOrganization) {
959
+ return Promise.reject(
960
+ new Error(
961
+ ErrorMessage.getInvalidParameterMessage(
962
+ 'POST',
963
+ 'Please provide a organization name',
964
+ ),
965
+ ),
966
+ );
967
+ }
968
+ const org = orgName || this.defaultOrganization;
969
+ return this.dispatch(`${this.baseURL}/organizations/${UTILS.encodeURISegment(org)}/access_requests/${acceId}`, CONST.DELETE);
970
+ };
971
+
972
+ /**
973
+ * -- TerminusX API --
974
+ * Get the userinfo teams ownership and subscription
975
+ * @param {string} [orgName] - The organization name.
976
+ * @return {Promise} A promise that returns the call response object, or an Error if rejected.
977
+ * @example
978
+ * accessControl.getUserInfo().then(result=>{
979
+ * console.log(result)
980
+ * })
981
+ *
982
+ */
983
+ AccessControl.prototype.getUserInfo = function (userName) {
984
+ const userNameUrl = userName || 'info';
985
+ return this.dispatch(`${this.baseURL}/users/${UTILS.encodeURISegment(userNameUrl)}`, CONST.GET);
986
+ };
987
+
988
+ module.exports = AccessControl;