@docbrasil/api-systemmanager 1.0.89 → 1.0.91

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 (57) hide show
  1. package/api/admin/doctypes.js +76 -76
  2. package/api/admin/document.js +332 -332
  3. package/api/admin/form.js +151 -151
  4. package/api/admin/index.js +46 -46
  5. package/api/admin/list.js +133 -133
  6. package/api/admin/message.js +194 -194
  7. package/api/admin/notification.js +233 -233
  8. package/api/admin/organization.js +124 -124
  9. package/api/admin/plugin.js +116 -116
  10. package/api/admin/policy.js +78 -78
  11. package/api/admin/processes.js +370 -370
  12. package/api/admin/task.js +125 -125
  13. package/api/admin/user.js +185 -185
  14. package/api/dispatch.js +101 -101
  15. package/api/general/geoLocation.js +88 -88
  16. package/api/general/index.js +22 -22
  17. package/api/login.js +267 -267
  18. package/api/session.js +85 -85
  19. package/api/user/datasource.js +144 -144
  20. package/api/user/document.js +730 -730
  21. package/api/user/index.js +39 -39
  22. package/api/user/notification.js +101 -101
  23. package/api/user/organization.js +230 -230
  24. package/api/user/process.js +191 -191
  25. package/api/user/register.js +205 -205
  26. package/api/user/task.js +201 -201
  27. package/api/user/task_available.js +135 -135
  28. package/api/user/user.js +287 -287
  29. package/api/utils/cypher.js +37 -37
  30. package/api/utils/promises.js +118 -118
  31. package/bundleRollup.js +158 -158
  32. package/dist/bundle.cjs +4875 -4875
  33. package/dist/bundle.mjs +1 -1
  34. package/doc/api.md +674 -336
  35. package/doc.md +653 -653
  36. package/helper/boom.js +487 -487
  37. package/helper/cryptojs.js +6067 -6067
  38. package/index.js +85 -85
  39. package/package-lock.json +4635 -4635
  40. package/package.json +68 -68
  41. package/readme.md +25 -25
  42. package/tests/admin/document.spec.js +45 -45
  43. package/tests/admin/form.spec.js +74 -74
  44. package/tests/admin/list.spec.js +86 -86
  45. package/tests/admin/message.js +92 -92
  46. package/tests/admin/notification.spec.js +174 -174
  47. package/tests/admin/pluginspec..js +71 -71
  48. package/tests/admin/policy.spec.js +71 -71
  49. package/tests/admin/processes.spec.js +119 -119
  50. package/tests/admin/users.spec.js +127 -127
  51. package/tests/documents.spec.js +164 -164
  52. package/tests/login.spec.js +91 -91
  53. package/tests/session.spec..js +58 -58
  54. package/tests/user/documents.js +164 -164
  55. package/tests/user/organization.js +122 -122
  56. package/tests/user/process.js +71 -71
  57. package/tests/user/user.js +88 -88
@@ -1,370 +1,370 @@
1
- import _ from 'lodash';
2
- import Boom from '@hapi/boom';
3
- import Joi from 'joi';
4
-
5
- /**
6
- * Admin Class for processes, permission admin
7
- * @class
8
- */
9
- class AdminProcesses {
10
-
11
- constructor(options) {
12
- Joi.assert(options, Joi.object().required());
13
- Joi.assert(options.parent, Joi.object().required());
14
-
15
- const self = this;
16
- self.parent = options.parent;
17
- self._client = self.parent.dispatch.getClient();
18
-
19
- self._operatorsType = ['string', 'date', 'datetime', 'idcard', 'orgidcard', 'number', 'currency'];
20
- self._operatorOfString = ['=', '*', '*=', '=*', '*?', '~'];
21
-
22
- /**
23
- * @description Operator of string, format is: DD/MM/YYY -> Exemple: 22/10/2000
24
- * @type {string[]}
25
- * @private
26
- */
27
- self._operatorOfDate = ['=', '>', '>=', '<', '<=', '...'];
28
-
29
- /**
30
- * @description Operator of string, format is: DD/MM/YYY HH:mm -> Exemple: 22/10/2000 14:32
31
- * @type {string[]}
32
- * @private
33
- */
34
- self._operatorOfDateTime = ['=', '>', '>=', '<', '<=', '...'];
35
- self._operatorOfIdCard = ['=', '*', '*=', '=*'];
36
- self._operatorOfOrgICard = ['=', '*', '*=', '=*'];
37
- self._operatorOfNumber = ['=', '>', '>=', '<', '<='];
38
- self._operatorOfCurrency = ['=', '>', '>=', '<', '<='];
39
-
40
- self.allOperators = new Set([
41
- ...self._operatorOfString, ...self._operatorOfDate, ...self._operatorOfDateTime,
42
- ...self._operatorOfIdCard, ...self._operatorOfOrgICard, ...self._operatorOfNumber,
43
- ...self._operatorOfCurrency
44
- ]);
45
-
46
- self._status = ['FINISHED', 'NOT_FINISHED'];
47
-
48
- //
49
- // SCHEMAS
50
- //
51
- self._schemaOrgId = Joi.string().required().label('Organization id'),
52
-
53
- self._schemaProcessId = Joi.object({
54
- value: Joi.array().items(Joi.string()).min(1).label('Numbers of processId'),
55
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
56
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
57
- name: Joi.string().default('processId').label('Propertie name'),
58
- });
59
-
60
- self._schemaProtocol = Joi.object({
61
- value: Joi.string().required().label('Number of protocol'),
62
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
63
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
64
- });
65
-
66
- self._schemaHistoryBegin = Joi.object({
67
- value: Joi.string().required().label('History begin'),
68
- type: Joi.string().default('date').valid(...self._operatorsType).label('Operation type'),
69
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
70
- });
71
-
72
- self._schemaHistoryEnd = Joi.object({
73
- value: Joi.string().required().label('History end'),
74
- type: Joi.string().default('date').valid(...self._operatorsType).label('Operation type'),
75
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
76
- });
77
-
78
- self._schemaStatus = Joi.object({
79
- value: Joi.string().default('FINISHED').valid(self._status.toString()).label('Process Status'),
80
- type: Joi.string().default('string').valid('string').label('Operation type'),
81
- oper: Joi.string().default('=').valid('=').label('Type of condition'),
82
- });
83
-
84
- self._schemaOrgProcessId = Joi.object({
85
- value: Joi.string().required().label('Organization process id'),
86
- type: Joi.string().default('string').valid('string').label('Operation type'),
87
- oper: Joi.string().default('=').valid('=').label('Type of condition'),
88
- }).required().label('Organization process id');
89
-
90
- self._schemaFormData = Joi.array().items(
91
- Joi.object({
92
- value: Joi.string().required().label('Text to search'),
93
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
94
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
95
- name: Joi.string().required().label('Property name'),
96
- })
97
- );
98
-
99
- self._schemaAdvFormData = Joi.array().items(
100
- Joi.object({
101
- value: Joi.any().required().label('Value to search'),
102
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
103
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
104
- name: Joi.string().required().label('Property name'),
105
- })
106
- );
107
-
108
- self._schemaProcessProperties = Joi.array().items(
109
- Joi.object({
110
- value: Joi.any().required().label('Value to search'),
111
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
112
- oper: Joi.string().default('=').label('Type of condition'),
113
- name: Joi.string().required().label('Property name'),
114
- })
115
- );
116
-
117
- self._schemaInitParams = Joi.array().items(
118
- Joi.object({
119
- value: Joi.any().required().label('Value to search'),
120
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
121
- oper: Joi.string().default('=').label('Type of condition'),
122
- name: Joi.string().required().label('Property name'),
123
- })
124
- );
125
-
126
- self._schemaParticipants = Joi.object({
127
- value: Joi.array().items(
128
- Joi.string().label('Value to search')
129
- ),
130
- type: Joi.string().default('string').valid('string').label('Operation type'),
131
- oper: Joi.string().default('=').valid('=').label('Type of condition'),
132
- name: Joi.string().required().label('Process participant group'),
133
- });
134
-
135
- self._schemaProcessParticipantsGroup = Joi.object({
136
- value: Joi.string().label('Value to search'),
137
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
138
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
139
- name: Joi.string().required().label('Process participant group'),
140
- });
141
-
142
- self._schemaUserId = Joi.object({
143
- value: Joi.string().label('Value to search'),
144
- type: Joi.string().default('string').valid('string').label('Operation type'),
145
- oper: Joi.string().default('=').valid('=').label('Type of condition'),
146
- name: Joi.string().required().label('User id'),
147
- });
148
-
149
- self._schemaUserGroups = Joi.object({
150
- value: Joi.array().items(
151
- Joi.string().label('Value to search')
152
- ),
153
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
154
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
155
- name: Joi.string().required().label('User groups'),
156
- });
157
-
158
- self._schemaUserName = Joi.object({
159
- value: Joi.string().label('Value to search'),
160
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
161
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
162
- name: Joi.string().required().label('User name'),
163
- });
164
-
165
- self._schemaUserDepartment = Joi.object({
166
- value: Joi.string().label('Value to search'),
167
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
168
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
169
- name: Joi.string().required().label('User department'),
170
- });
171
-
172
- self._schemaUserSubDepartment = Joi.object({
173
- value: Joi.string().label('Value to search'),
174
- type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
175
- oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
176
- name: Joi.string().required().label('User sub department'),
177
- });
178
-
179
- self._schemaNp = Joi.number().default(0).label('Enable or disable pagination'); // 1 disable pagination, 0 enable pagination
180
-
181
- self._schemaPj = Joi.array().items(
182
- Joi.string().label('Value to projection')
183
- ).default([
184
- 'processId', 'orgId', 'protocol', 'historyBegin', 'historyEnd', 'status',
185
- 'orgProcessId', 'processProperties', 'userId', 'userName', 'initParams'
186
- ]);
187
-
188
- self._schemaSort = Joi.object().label('Sort data');
189
- self._schemaPerPage = Joi.number().default(20).label('Item per page');
190
- self._schemaPage = Joi.number().default(1).label('Start page in pagination');
191
- }
192
-
193
- /**
194
- * @author Augusto Pissarra <abernardo.br@gmail.com>
195
- * @description Get the return data and check for errors
196
- * @param {object} retData Response HTTP
197
- * @return {*}
198
- * @private
199
- */
200
- _returnData(retData, def = {}) {
201
- if (retData.status !== 200) {
202
- return Boom.badRequest(_.get(retData, 'message', 'No error message reported!'))
203
- } else {
204
- return _.get(retData, 'data', def);
205
- }
206
- }
207
-
208
- /**
209
- * @author CloudBrasil <abernardo.br@gmail.com>
210
- * @description Set header with new session
211
- * @param {string} session Session, token JWT
212
- * @return {object} header with new session
213
- * @private
214
- */
215
- _setHeader(session) {
216
- return {
217
- headers: {
218
- authorization: session,
219
- }
220
- };
221
- }
222
-
223
- /**
224
- * @author Thiago Anselmo <thiagoo.anselmoo@gmail.com>
225
- * @description Mount query string to send in URL
226
- * @param {object} params Params to mount query string
227
- * @private
228
- * @example
229
- *
230
- * const params = {name: 'Thiago', lastname: 'anselmo'};
231
- * self._mountQueryString(params);
232
- * // output name=Thiago&lastname=anselmo
233
- */
234
- async _mountQueryString(params) {
235
- try {
236
- const allowTypeForQueryString = ['string', 'boolean', 'number'];
237
-
238
- Joi.assert(params, Joi.object(), 'Params to mount query string');
239
-
240
- params = Object.keys(params).reduce((newParams, field) => {
241
- const fieldData = params[field];
242
- return _.isArray(fieldData) || _.isObject(fieldData)
243
- ? {...newParams, [field]: JSON.stringify(fieldData)}
244
- : {...newParams, [field]: fieldData}
245
- }, {});
246
-
247
- const queryString = Object.entries(params)
248
- .map(filter => filter.join('='))
249
- .join('&');
250
-
251
- return queryString;
252
- } catch (ex) {
253
- throw ex;
254
- }
255
- }
256
-
257
- /**
258
- * @see https://confluence.external-share.com/content/7450b014-52c6-4d9e-b30e-a062b57453b5/17104899/17694721/532545537
259
- * @author CloudBrasil <abernardo.br@gmail.com>
260
- * @description Advanced search of processes, check documentation, to verify all params, pass to method search
261
- * @return {Promise}
262
- * @public
263
- * @async
264
- * @example
265
- *
266
- * const API = require('@docbrasil/api-systemmanager');
267
- * const api = new API();
268
- * const params = {
269
- * orgId: '5edd11c46b6ce9729c2c297c',
270
- * ...
271
- * ...
272
- * ...
273
- * };
274
- * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
275
- * await api.admin.processes.search(params, session);
276
- */
277
- async search(params, session) {
278
- const self = this;
279
-
280
- try {
281
- const paramsAfterValidation = Joi.attempt(params,
282
- Joi.object({
283
- orgId: self._schemaOrgId,
284
- processId: self._schemaProcessId,
285
- protocol: self._schemaProtocol,
286
- historyBegin: self._schemaHistoryBegin,
287
- historyEnd: self._schemaHistoryEnd,
288
- status: self._schemaStatus,
289
- orgProcessId: self._schemaOrgProcessId,
290
- formData: self._schemaFormData,
291
- advFormData: self._schemaAdvFormData,
292
- processProperties: self._schemaProcessProperties,
293
- initParams: self._schemaInitParams,
294
- participants: self._schemaParticipants,
295
- processParticipantsGroup: self._schemaProcessParticipantsGroup,
296
- userId: self._schemaUserId,
297
- userGroups: self._schemaUserGroups,
298
- userName: self._schemaUserName,
299
- userDepartment: self._schemaUserDepartment,
300
- userSubDepartment: self._schemaUserSubDepartment,
301
- np: self._schemaNp,
302
- pj: self._schemaPj,
303
- s: self._schemaSort,
304
- p: self._schemaPerPage,
305
- i: self._schemaPage
306
- }).options({abortEarly: false, stripUnknown: true})
307
- )
308
-
309
- const setParams = {...paramsAfterValidation, pj: paramsAfterValidation.pj.toString()};
310
- const {orgId, ...payload} = setParams;
311
- const queryString = JSON.stringify(payload);
312
-
313
- const apiCall = self._client.get(`/admin/organizations/${orgId}/processes/search?query=${queryString}`, self._setHeader(session));
314
- return self._returnData(await apiCall);
315
- } catch (ex) {
316
- throw ex;
317
- }
318
- }
319
-
320
- /**
321
- * @author CloudBrasil <abernardo.br@gmail.com>
322
- * @description Advanced search of process in elastic search ussing system manager
323
- * @param {!object} params - Params to search document
324
- * @param {!string} params.orgProcessId - Document id (_id database) of the process
325
- * @param {!object} params.query - Query to search in elastic search
326
- * @param {!string} session Session, token JWT
327
- * @return {Promise}
328
- * @public
329
- * @async
330
- * @example
331
- *
332
- * const API = require('@docbrasil/api-systemmanager');
333
- * const api = new API();
334
- * const params = {
335
- * orgProcessId: '5edd11c46b6ce9729c2c297c',
336
- * query: {
337
- * "_source": "processData.properties.processProperties",
338
- * "query": {
339
- * "term": {
340
- * "initParams.email.keyword": {
341
- * "value": "clintes001@gmail.com"
342
- * }
343
- * }
344
- * }
345
- * }
346
- * }
347
- * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
348
- * await api.admin.processes.advancedSearch(params, session);
349
- */
350
- async advancedSearch(params, session) {
351
- const self = this;
352
-
353
- try {
354
- Joi.assert(params, Joi.object().required(), 'Params to search document');
355
- Joi.assert(params.orgProcessId, Joi.string().required(), 'Document id (_id database) of the process');
356
- Joi.assert(params.query, Joi.object().required(), 'eQuery, query to search document in elastic search');
357
- Joi.assert(session, Joi.string().required(), 'Session is token JWT');
358
-
359
- const {orgProcessId, query} = params;
360
- const payload = {orgProcessId, query};
361
-
362
- const apiCall = self._client.post(`/admin/processes/search`, payload, self._setHeader(session));
363
- return self._returnData(await apiCall);
364
- } catch (ex) {
365
- throw ex;
366
- }
367
- }
368
- }
369
-
370
- export default AdminProcesses;
1
+ import _ from 'lodash';
2
+ import Boom from '@hapi/boom';
3
+ import Joi from 'joi';
4
+
5
+ /**
6
+ * Admin Class for processes, permission admin
7
+ * @class
8
+ */
9
+ class AdminProcesses {
10
+
11
+ constructor(options) {
12
+ Joi.assert(options, Joi.object().required());
13
+ Joi.assert(options.parent, Joi.object().required());
14
+
15
+ const self = this;
16
+ self.parent = options.parent;
17
+ self._client = self.parent.dispatch.getClient();
18
+
19
+ self._operatorsType = ['string', 'date', 'datetime', 'idcard', 'orgidcard', 'number', 'currency'];
20
+ self._operatorOfString = ['=', '*', '*=', '=*', '*?', '~'];
21
+
22
+ /**
23
+ * @description Operator of string, format is: DD/MM/YYY -> Exemple: 22/10/2000
24
+ * @type {string[]}
25
+ * @private
26
+ */
27
+ self._operatorOfDate = ['=', '>', '>=', '<', '<=', '...'];
28
+
29
+ /**
30
+ * @description Operator of string, format is: DD/MM/YYY HH:mm -> Exemple: 22/10/2000 14:32
31
+ * @type {string[]}
32
+ * @private
33
+ */
34
+ self._operatorOfDateTime = ['=', '>', '>=', '<', '<=', '...'];
35
+ self._operatorOfIdCard = ['=', '*', '*=', '=*'];
36
+ self._operatorOfOrgICard = ['=', '*', '*=', '=*'];
37
+ self._operatorOfNumber = ['=', '>', '>=', '<', '<='];
38
+ self._operatorOfCurrency = ['=', '>', '>=', '<', '<='];
39
+
40
+ self.allOperators = new Set([
41
+ ...self._operatorOfString, ...self._operatorOfDate, ...self._operatorOfDateTime,
42
+ ...self._operatorOfIdCard, ...self._operatorOfOrgICard, ...self._operatorOfNumber,
43
+ ...self._operatorOfCurrency
44
+ ]);
45
+
46
+ self._status = ['FINISHED', 'NOT_FINISHED'];
47
+
48
+ //
49
+ // SCHEMAS
50
+ //
51
+ self._schemaOrgId = Joi.string().required().label('Organization id'),
52
+
53
+ self._schemaProcessId = Joi.object({
54
+ value: Joi.array().items(Joi.string()).min(1).label('Numbers of processId'),
55
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
56
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
57
+ name: Joi.string().default('processId').label('Propertie name'),
58
+ });
59
+
60
+ self._schemaProtocol = Joi.object({
61
+ value: Joi.string().required().label('Number of protocol'),
62
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
63
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
64
+ });
65
+
66
+ self._schemaHistoryBegin = Joi.object({
67
+ value: Joi.string().required().label('History begin'),
68
+ type: Joi.string().default('date').valid(...self._operatorsType).label('Operation type'),
69
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
70
+ });
71
+
72
+ self._schemaHistoryEnd = Joi.object({
73
+ value: Joi.string().required().label('History end'),
74
+ type: Joi.string().default('date').valid(...self._operatorsType).label('Operation type'),
75
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
76
+ });
77
+
78
+ self._schemaStatus = Joi.object({
79
+ value: Joi.string().default('FINISHED').valid(self._status.toString()).label('Process Status'),
80
+ type: Joi.string().default('string').valid('string').label('Operation type'),
81
+ oper: Joi.string().default('=').valid('=').label('Type of condition'),
82
+ });
83
+
84
+ self._schemaOrgProcessId = Joi.object({
85
+ value: Joi.string().required().label('Organization process id'),
86
+ type: Joi.string().default('string').valid('string').label('Operation type'),
87
+ oper: Joi.string().default('=').valid('=').label('Type of condition'),
88
+ }).required().label('Organization process id');
89
+
90
+ self._schemaFormData = Joi.array().items(
91
+ Joi.object({
92
+ value: Joi.string().required().label('Text to search'),
93
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
94
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
95
+ name: Joi.string().required().label('Property name'),
96
+ })
97
+ );
98
+
99
+ self._schemaAdvFormData = Joi.array().items(
100
+ Joi.object({
101
+ value: Joi.any().required().label('Value to search'),
102
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
103
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
104
+ name: Joi.string().required().label('Property name'),
105
+ })
106
+ );
107
+
108
+ self._schemaProcessProperties = Joi.array().items(
109
+ Joi.object({
110
+ value: Joi.any().required().label('Value to search'),
111
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
112
+ oper: Joi.string().default('=').label('Type of condition'),
113
+ name: Joi.string().required().label('Property name'),
114
+ })
115
+ );
116
+
117
+ self._schemaInitParams = Joi.array().items(
118
+ Joi.object({
119
+ value: Joi.any().required().label('Value to search'),
120
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
121
+ oper: Joi.string().default('=').label('Type of condition'),
122
+ name: Joi.string().required().label('Property name'),
123
+ })
124
+ );
125
+
126
+ self._schemaParticipants = Joi.object({
127
+ value: Joi.array().items(
128
+ Joi.string().label('Value to search')
129
+ ),
130
+ type: Joi.string().default('string').valid('string').label('Operation type'),
131
+ oper: Joi.string().default('=').valid('=').label('Type of condition'),
132
+ name: Joi.string().required().label('Process participant group'),
133
+ });
134
+
135
+ self._schemaProcessParticipantsGroup = Joi.object({
136
+ value: Joi.string().label('Value to search'),
137
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
138
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
139
+ name: Joi.string().required().label('Process participant group'),
140
+ });
141
+
142
+ self._schemaUserId = Joi.object({
143
+ value: Joi.string().label('Value to search'),
144
+ type: Joi.string().default('string').valid('string').label('Operation type'),
145
+ oper: Joi.string().default('=').valid('=').label('Type of condition'),
146
+ name: Joi.string().required().label('User id'),
147
+ });
148
+
149
+ self._schemaUserGroups = Joi.object({
150
+ value: Joi.array().items(
151
+ Joi.string().label('Value to search')
152
+ ),
153
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
154
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
155
+ name: Joi.string().required().label('User groups'),
156
+ });
157
+
158
+ self._schemaUserName = Joi.object({
159
+ value: Joi.string().label('Value to search'),
160
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
161
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
162
+ name: Joi.string().required().label('User name'),
163
+ });
164
+
165
+ self._schemaUserDepartment = Joi.object({
166
+ value: Joi.string().label('Value to search'),
167
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
168
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
169
+ name: Joi.string().required().label('User department'),
170
+ });
171
+
172
+ self._schemaUserSubDepartment = Joi.object({
173
+ value: Joi.string().label('Value to search'),
174
+ type: Joi.string().default('string').valid(...self._operatorsType).label('Operation type'),
175
+ oper: Joi.string().default('=').valid(...self.allOperators).label('Type of condition'),
176
+ name: Joi.string().required().label('User sub department'),
177
+ });
178
+
179
+ self._schemaNp = Joi.number().default(0).label('Enable or disable pagination'); // 1 disable pagination, 0 enable pagination
180
+
181
+ self._schemaPj = Joi.array().items(
182
+ Joi.string().label('Value to projection')
183
+ ).default([
184
+ 'processId', 'orgId', 'protocol', 'historyBegin', 'historyEnd', 'status',
185
+ 'orgProcessId', 'processProperties', 'userId', 'userName', 'initParams'
186
+ ]);
187
+
188
+ self._schemaSort = Joi.object().label('Sort data');
189
+ self._schemaPerPage = Joi.number().default(20).label('Item per page');
190
+ self._schemaPage = Joi.number().default(1).label('Start page in pagination');
191
+ }
192
+
193
+ /**
194
+ * @author Augusto Pissarra <abernardo.br@gmail.com>
195
+ * @description Get the return data and check for errors
196
+ * @param {object} retData Response HTTP
197
+ * @return {*}
198
+ * @private
199
+ */
200
+ _returnData(retData, def = {}) {
201
+ if (retData.status !== 200) {
202
+ return Boom.badRequest(_.get(retData, 'message', 'No error message reported!'))
203
+ } else {
204
+ return _.get(retData, 'data', def);
205
+ }
206
+ }
207
+
208
+ /**
209
+ * @author CloudBrasil <abernardo.br@gmail.com>
210
+ * @description Set header with new session
211
+ * @param {string} session Session, token JWT
212
+ * @return {object} header with new session
213
+ * @private
214
+ */
215
+ _setHeader(session) {
216
+ return {
217
+ headers: {
218
+ authorization: session,
219
+ }
220
+ };
221
+ }
222
+
223
+ /**
224
+ * @author Thiago Anselmo <thiagoo.anselmoo@gmail.com>
225
+ * @description Mount query string to send in URL
226
+ * @param {object} params Params to mount query string
227
+ * @private
228
+ * @example
229
+ *
230
+ * const params = {name: 'Thiago', lastname: 'anselmo'};
231
+ * self._mountQueryString(params);
232
+ * // output name=Thiago&lastname=anselmo
233
+ */
234
+ async _mountQueryString(params) {
235
+ try {
236
+ const allowTypeForQueryString = ['string', 'boolean', 'number'];
237
+
238
+ Joi.assert(params, Joi.object(), 'Params to mount query string');
239
+
240
+ params = Object.keys(params).reduce((newParams, field) => {
241
+ const fieldData = params[field];
242
+ return _.isArray(fieldData) || _.isObject(fieldData)
243
+ ? {...newParams, [field]: JSON.stringify(fieldData)}
244
+ : {...newParams, [field]: fieldData}
245
+ }, {});
246
+
247
+ const queryString = Object.entries(params)
248
+ .map(filter => filter.join('='))
249
+ .join('&');
250
+
251
+ return queryString;
252
+ } catch (ex) {
253
+ throw ex;
254
+ }
255
+ }
256
+
257
+ /**
258
+ * @see https://confluence.external-share.com/content/7450b014-52c6-4d9e-b30e-a062b57453b5/17104899/17694721/532545537
259
+ * @author CloudBrasil <abernardo.br@gmail.com>
260
+ * @description Advanced search of processes, check documentation, to verify all params, pass to method search
261
+ * @return {Promise}
262
+ * @public
263
+ * @async
264
+ * @example
265
+ *
266
+ * const API = require('@docbrasil/api-systemmanager');
267
+ * const api = new API();
268
+ * const params = {
269
+ * orgId: '5edd11c46b6ce9729c2c297c',
270
+ * ...
271
+ * ...
272
+ * ...
273
+ * };
274
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
275
+ * await api.admin.processes.search(params, session);
276
+ */
277
+ async search(params, session) {
278
+ const self = this;
279
+
280
+ try {
281
+ const paramsAfterValidation = Joi.attempt(params,
282
+ Joi.object({
283
+ orgId: self._schemaOrgId,
284
+ processId: self._schemaProcessId,
285
+ protocol: self._schemaProtocol,
286
+ historyBegin: self._schemaHistoryBegin,
287
+ historyEnd: self._schemaHistoryEnd,
288
+ status: self._schemaStatus,
289
+ orgProcessId: self._schemaOrgProcessId,
290
+ formData: self._schemaFormData,
291
+ advFormData: self._schemaAdvFormData,
292
+ processProperties: self._schemaProcessProperties,
293
+ initParams: self._schemaInitParams,
294
+ participants: self._schemaParticipants,
295
+ processParticipantsGroup: self._schemaProcessParticipantsGroup,
296
+ userId: self._schemaUserId,
297
+ userGroups: self._schemaUserGroups,
298
+ userName: self._schemaUserName,
299
+ userDepartment: self._schemaUserDepartment,
300
+ userSubDepartment: self._schemaUserSubDepartment,
301
+ np: self._schemaNp,
302
+ pj: self._schemaPj,
303
+ s: self._schemaSort,
304
+ p: self._schemaPerPage,
305
+ i: self._schemaPage
306
+ }).options({abortEarly: false, stripUnknown: true})
307
+ )
308
+
309
+ const setParams = {...paramsAfterValidation, pj: paramsAfterValidation.pj.toString()};
310
+ const {orgId, ...payload} = setParams;
311
+ const queryString = JSON.stringify(payload);
312
+
313
+ const apiCall = self._client.get(`/admin/organizations/${orgId}/processes/search?query=${queryString}`, self._setHeader(session));
314
+ return self._returnData(await apiCall);
315
+ } catch (ex) {
316
+ throw ex;
317
+ }
318
+ }
319
+
320
+ /**
321
+ * @author CloudBrasil <abernardo.br@gmail.com>
322
+ * @description Advanced search of process in elastic search ussing system manager
323
+ * @param {!object} params - Params to search document
324
+ * @param {!string} params.orgProcessId - Document id (_id database) of the process
325
+ * @param {!object} params.query - Query to search in elastic search
326
+ * @param {!string} session Session, token JWT
327
+ * @return {Promise}
328
+ * @public
329
+ * @async
330
+ * @example
331
+ *
332
+ * const API = require('@docbrasil/api-systemmanager');
333
+ * const api = new API();
334
+ * const params = {
335
+ * orgProcessId: '5edd11c46b6ce9729c2c297c',
336
+ * query: {
337
+ * "_source": "processData.properties.processProperties",
338
+ * "query": {
339
+ * "term": {
340
+ * "initParams.email.keyword": {
341
+ * "value": "clintes001@gmail.com"
342
+ * }
343
+ * }
344
+ * }
345
+ * }
346
+ * }
347
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
348
+ * await api.admin.processes.advancedSearch(params, session);
349
+ */
350
+ async advancedSearch(params, session) {
351
+ const self = this;
352
+
353
+ try {
354
+ Joi.assert(params, Joi.object().required(), 'Params to search document');
355
+ Joi.assert(params.orgProcessId, Joi.string().required(), 'Document id (_id database) of the process');
356
+ Joi.assert(params.query, Joi.object().required(), 'eQuery, query to search document in elastic search');
357
+ Joi.assert(session, Joi.string().required(), 'Session is token JWT');
358
+
359
+ const {orgProcessId, query} = params;
360
+ const payload = {orgProcessId, query};
361
+
362
+ const apiCall = self._client.post(`/admin/processes/search`, payload, self._setHeader(session));
363
+ return self._returnData(await apiCall);
364
+ } catch (ex) {
365
+ throw ex;
366
+ }
367
+ }
368
+ }
369
+
370
+ export default AdminProcesses;