@docbrasil/api-systemmanager 1.1.43 → 1.1.45

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.
package/api/dispatch.js CHANGED
@@ -6,15 +6,72 @@ import Axios from 'axios';
6
6
  * @class Api dispatch manager
7
7
  */
8
8
  class Dispatch {
9
-
10
9
  constructor(options) {
11
-
12
10
  Joi.assert(options, Joi.object().required());
13
11
  Joi.assert(options.parent, Joi.object().required());
14
12
 
15
13
  const self = this;
16
14
  self.parent = options.parent;
15
+ self._cache = options.cache;
17
16
  self._client = Axios.create({baseURL: self.parent.options.uri, withCredentials: true});
17
+
18
+ // Add request interceptor for offline handling
19
+ self._client.interceptors.request.use(
20
+ async (config) => {
21
+ if (self._isOnline()) {
22
+ return config;
23
+ }
24
+
25
+ const cachedData = await self._getCache(config.url, {
26
+ method: config.method,
27
+ data: config.data,
28
+ params: config.params,
29
+ headers: config.headers
30
+ });
31
+
32
+ if (cachedData) {
33
+ // Cancel the actual request and return cached data
34
+ const dummyResponse = {
35
+ status: 200,
36
+ data: cachedData,
37
+ headers: {},
38
+ config,
39
+ cached: true
40
+ };
41
+
42
+ // Throwing a special error that includes our cached response
43
+ throw {
44
+ __CACHE_HIT__: true,
45
+ response: dummyResponse
46
+ };
47
+ }
48
+
49
+ throw new Error('Network error: No internet connection and no cached data available');
50
+ },
51
+ error => Promise.reject(error)
52
+ );
53
+
54
+ // Add response interceptor to handle cache hits and cache successful responses
55
+ self._client.interceptors.response.use(
56
+ async response => {
57
+ // Cache successful responses when online
58
+ if (response.status === 200 && self._isOnline()) {
59
+ await self._setCache(response.config.url, {
60
+ method: response.config.method,
61
+ data: response.config.data,
62
+ params: response.config.params,
63
+ headers: response.config.headers
64
+ }, response.data);
65
+ }
66
+ return response;
67
+ },
68
+ error => {
69
+ if (error.__CACHE_HIT__) {
70
+ return error.response;
71
+ }
72
+ return Promise.reject(error);
73
+ }
74
+ );
18
75
  }
19
76
 
20
77
  /**
@@ -47,6 +104,47 @@ class Dispatch {
47
104
  };
48
105
  }
49
106
 
107
+ /**
108
+ * @description Check if the browser/client is currently online
109
+ * @return {boolean} True if online, false if offline
110
+ * @private
111
+ */
112
+ _isOnline() {
113
+ return typeof navigator !== 'undefined' && navigator.onLine && this._cache;
114
+ }
115
+
116
+ /**
117
+ * @description Get cached data for a specific request
118
+ * @param {string} url The request URL
119
+ * @param {object} options Request options including method, data, params, and headers
120
+ * @return {Promise<object|null>} Cached data or null if no cache exists
121
+ * @private
122
+ */
123
+ async _getCache(url, options) {
124
+ try {
125
+ return await this._cache.getCache(url, options);
126
+ } catch (error) {
127
+ console.warn('Cache retrieval failed:', error);
128
+ return null;
129
+ }
130
+ }
131
+
132
+ /**
133
+ * @description Set data in cache for a specific request
134
+ * @param {string} url The request URL
135
+ * @param {object} options Request options including method, data, params, and headers
136
+ * @param {object} data The data to cache
137
+ * @return {Promise<void>}
138
+ * @private
139
+ */
140
+ async _setCache(url, options, data) {
141
+ try {
142
+ await this._cache.setCache(url, options, data);
143
+ } catch (error) {
144
+ console.warn('Cache storage failed:', error);
145
+ }
146
+ }
147
+
50
148
  /**
51
149
  * Get the URL context
52
150
  * @param url {string} Full url
@@ -72,8 +170,8 @@ class Dispatch {
72
170
 
73
171
  const self = this;
74
172
  const header = session ? self._setHeader(session) : {};
75
- const apiCall = self._client.get(url, header);
76
- return self._returnData(await apiCall);
173
+ const apiCall = await self._client.get(url, header);
174
+ return self._returnData(apiCall);
77
175
  }
78
176
 
79
177
  /**
@@ -439,7 +439,7 @@ class Process {
439
439
  }
440
440
  }
441
441
 
442
- /**
442
+ /**
443
443
  * @author Myndware <augusto.pissarra@myndware.com>
444
444
  * @description Get DocType properties of process
445
445
  * @param {object} params Params to get document DocType
@@ -476,6 +476,83 @@ class Process {
476
476
  throw ex;
477
477
  }
478
478
  }
479
+
480
+ /**
481
+ * @author Myndware <augusto.pissarra@myndware.com>
482
+ * @description Get Org Groups
483
+ * @param {object} params Params to get Org Groups
484
+ * @param {string} params.orgId Organization id (_id database);
485
+ * @param {string} session Session, token JWT
486
+ * @return {Promise}
487
+ * @public
488
+ * @async
489
+ * @example
490
+ *
491
+ * const API = require('@docbrasil/api-systemmanager');
492
+ * const api = new API();
493
+ * const params = {
494
+ * orgId: '5edd11c46b6ce9729c2c297c',
495
+ * }
496
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
497
+ * await api.user.process.getOrgGroups(params, session);
498
+ */
499
+ async getOrgGroups(params, session) {
500
+ const self = this;
501
+
502
+ try {
503
+ Joi.assert(params, Joi.object().required());
504
+ Joi.assert(params.orgId, Joi.string().required());
505
+ Joi.assert(session, Joi.string().required());
506
+
507
+ const {orgId} = params;
508
+ const apiCall = self._client.get(`/organizations/${orgId}/groups`, self._setHeader(session));
509
+ return self._returnData(await apiCall);
510
+ } catch (ex) {
511
+ throw ex;
512
+ }
513
+ }
514
+
515
+ /**
516
+ * @author Myndware <augusto.pissarra@myndware.com>
517
+ * @description Get Org Users
518
+ * @param {object} params Params to get Org Users
519
+ * @param {string} params.orgId Organization id (_id database);
520
+ * @param {array} params.userIds UserIds
521
+ * @param {string} session Session, token JWT
522
+ * @return {Promise}
523
+ * @public
524
+ * @async
525
+ * @example
526
+ *
527
+ * const API = require('@docbrasil/api-systemmanager');
528
+ * const api = new API();
529
+ * const params = {
530
+ * orgId: '5edd11c46b6ce9729c2c297c',
531
+ * userIds: []
532
+ * }
533
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
534
+ * await api.user.process.getOrgUsers(params, session);
535
+ */
536
+ async getOrgUsers(params, session) {
537
+ const self = this;
538
+
539
+ try {
540
+ Joi.assert(params, Joi.object().required());
541
+ Joi.assert(params.orgId, Joi.string().required());
542
+ Joi.assert(params.userIds, Joi.array().required());
543
+ Joi.assert(session, Joi.string().required());
544
+
545
+ const {orgId, userIds} = params;
546
+ let queryString = '';
547
+ if(!_.isEmpty(userIds)) {
548
+ queryString = `?userIds=${JSON.stringify(userIds)}&{"sort":{"name":1}}`;
549
+ }
550
+ const apiCall = self._client.get(`/admin/organizations/${orgId}/orgusers${queryString}`, self._setHeader(session));
551
+ return self._returnData(await apiCall);
552
+ } catch (ex) {
553
+ throw ex;
554
+ }
555
+ }
479
556
  }
480
557
 
481
558
  export default Process;
package/dist/bundle.cjs CHANGED
@@ -18,15 +18,72 @@ var Moment__default = /*#__PURE__*/_interopDefaultLegacy(Moment);
18
18
  * @class Api dispatch manager
19
19
  */
20
20
  class Dispatch {
21
-
22
21
  constructor(options) {
23
-
24
22
  Joi__default["default"].assert(options, Joi__default["default"].object().required());
25
23
  Joi__default["default"].assert(options.parent, Joi__default["default"].object().required());
26
24
 
27
25
  const self = this;
28
26
  self.parent = options.parent;
29
- self._client = Axios__default["default"].create({baseURL: self.parent.options.uri});
27
+ self._cache = options.cache;
28
+ self._client = Axios__default["default"].create({baseURL: self.parent.options.uri, withCredentials: true});
29
+
30
+ // Add request interceptor for offline handling
31
+ self._client.interceptors.request.use(
32
+ async (config) => {
33
+ if (self._isOnline()) {
34
+ return config;
35
+ }
36
+
37
+ const cachedData = await self._getCache(config.url, {
38
+ method: config.method,
39
+ data: config.data,
40
+ params: config.params,
41
+ headers: config.headers
42
+ });
43
+
44
+ if (cachedData) {
45
+ // Cancel the actual request and return cached data
46
+ const dummyResponse = {
47
+ status: 200,
48
+ data: cachedData,
49
+ headers: {},
50
+ config,
51
+ cached: true
52
+ };
53
+
54
+ // Throwing a special error that includes our cached response
55
+ throw {
56
+ __CACHE_HIT__: true,
57
+ response: dummyResponse
58
+ };
59
+ }
60
+
61
+ throw new Error('Network error: No internet connection and no cached data available');
62
+ },
63
+ error => Promise.reject(error)
64
+ );
65
+
66
+ // Add response interceptor to handle cache hits and cache successful responses
67
+ self._client.interceptors.response.use(
68
+ async response => {
69
+ // Cache successful responses when online
70
+ if (response.status === 200 && self._isOnline()) {
71
+ await self._setCache(response.config.url, {
72
+ method: response.config.method,
73
+ data: response.config.data,
74
+ params: response.config.params,
75
+ headers: response.config.headers
76
+ }, response.data);
77
+ }
78
+ return response;
79
+ },
80
+ error => {
81
+ if (error.__CACHE_HIT__) {
82
+ return error.response;
83
+ }
84
+ return Promise.reject(error);
85
+ }
86
+ );
30
87
  }
31
88
 
32
89
  /**
@@ -59,6 +116,47 @@ class Dispatch {
59
116
  };
60
117
  }
61
118
 
119
+ /**
120
+ * @description Check if the browser/client is currently online
121
+ * @return {boolean} True if online, false if offline
122
+ * @private
123
+ */
124
+ _isOnline() {
125
+ return typeof navigator !== 'undefined' && navigator.onLine && this._cache;
126
+ }
127
+
128
+ /**
129
+ * @description Get cached data for a specific request
130
+ * @param {string} url The request URL
131
+ * @param {object} options Request options including method, data, params, and headers
132
+ * @return {Promise<object|null>} Cached data or null if no cache exists
133
+ * @private
134
+ */
135
+ async _getCache(url, options) {
136
+ try {
137
+ return await this._cache.getCache(url, options);
138
+ } catch (error) {
139
+ console.warn('Cache retrieval failed:', error);
140
+ return null;
141
+ }
142
+ }
143
+
144
+ /**
145
+ * @description Set data in cache for a specific request
146
+ * @param {string} url The request URL
147
+ * @param {object} options Request options including method, data, params, and headers
148
+ * @param {object} data The data to cache
149
+ * @return {Promise<void>}
150
+ * @private
151
+ */
152
+ async _setCache(url, options, data) {
153
+ try {
154
+ await this._cache.setCache(url, options, data);
155
+ } catch (error) {
156
+ console.warn('Cache storage failed:', error);
157
+ }
158
+ }
159
+
62
160
  /**
63
161
  * Get the URL context
64
162
  * @param url {string} Full url
@@ -84,8 +182,8 @@ class Dispatch {
84
182
 
85
183
  const self = this;
86
184
  const header = session ? self._setHeader(session) : {};
87
- const apiCall = self._client.get(url, header);
88
- return self._returnData(await apiCall);
185
+ const apiCall = await self._client.get(url, header);
186
+ return self._returnData(apiCall);
89
187
  }
90
188
 
91
189
  /**
@@ -2247,7 +2345,7 @@ class Process {
2247
2345
  }
2248
2346
  }
2249
2347
 
2250
- /**
2348
+ /**
2251
2349
  * @author Myndware <augusto.pissarra@myndware.com>
2252
2350
  * @description Get DocType properties of process
2253
2351
  * @param {object} params Params to get document DocType
@@ -2284,6 +2382,83 @@ class Process {
2284
2382
  throw ex;
2285
2383
  }
2286
2384
  }
2385
+
2386
+ /**
2387
+ * @author Myndware <augusto.pissarra@myndware.com>
2388
+ * @description Get Org Groups
2389
+ * @param {object} params Params to get Org Groups
2390
+ * @param {string} params.orgId Organization id (_id database);
2391
+ * @param {string} session Session, token JWT
2392
+ * @return {Promise}
2393
+ * @public
2394
+ * @async
2395
+ * @example
2396
+ *
2397
+ * const API = require('@docbrasil/api-systemmanager');
2398
+ * const api = new API();
2399
+ * const params = {
2400
+ * orgId: '5edd11c46b6ce9729c2c297c',
2401
+ * }
2402
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
2403
+ * await api.user.process.getOrgGroups(params, session);
2404
+ */
2405
+ async getOrgGroups(params, session) {
2406
+ const self = this;
2407
+
2408
+ try {
2409
+ Joi__default["default"].assert(params, Joi__default["default"].object().required());
2410
+ Joi__default["default"].assert(params.orgId, Joi__default["default"].string().required());
2411
+ Joi__default["default"].assert(session, Joi__default["default"].string().required());
2412
+
2413
+ const {orgId} = params;
2414
+ const apiCall = self._client.get(`/organizations/${orgId}/groups`, self._setHeader(session));
2415
+ return self._returnData(await apiCall);
2416
+ } catch (ex) {
2417
+ throw ex;
2418
+ }
2419
+ }
2420
+
2421
+ /**
2422
+ * @author Myndware <augusto.pissarra@myndware.com>
2423
+ * @description Get Org Users
2424
+ * @param {object} params Params to get Org Users
2425
+ * @param {string} params.orgId Organization id (_id database);
2426
+ * @param {array} params.userIds UserIds
2427
+ * @param {string} session Session, token JWT
2428
+ * @return {Promise}
2429
+ * @public
2430
+ * @async
2431
+ * @example
2432
+ *
2433
+ * const API = require('@docbrasil/api-systemmanager');
2434
+ * const api = new API();
2435
+ * const params = {
2436
+ * orgId: '5edd11c46b6ce9729c2c297c',
2437
+ * userIds: []
2438
+ * }
2439
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
2440
+ * await api.user.process.getOrgUsers(params, session);
2441
+ */
2442
+ async getOrgUsers(params, session) {
2443
+ const self = this;
2444
+
2445
+ try {
2446
+ Joi__default["default"].assert(params, Joi__default["default"].object().required());
2447
+ Joi__default["default"].assert(params.orgId, Joi__default["default"].string().required());
2448
+ Joi__default["default"].assert(params.userIds, Joi__default["default"].array().required());
2449
+ Joi__default["default"].assert(session, Joi__default["default"].string().required());
2450
+
2451
+ const {orgId, userIds} = params;
2452
+ let queryString = '';
2453
+ if(!___default["default"].isEmpty(userIds)) {
2454
+ queryString = `?userIds=${JSON.stringify(userIds)}&{"sort":{"name":1}}`;
2455
+ }
2456
+ const apiCall = self._client.get(`/admin/organizations/${orgId}/orgusers${queryString}`, self._setHeader(session));
2457
+ return self._returnData(await apiCall);
2458
+ } catch (ex) {
2459
+ throw ex;
2460
+ }
2461
+ }
2287
2462
  }
2288
2463
 
2289
2464
  /**