@docbrasil/api-systemmanager 1.0.106 → 1.0.108

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 (76) hide show
  1. package/.github/workflows/static.yml +43 -0
  2. package/api/external.js +41 -0
  3. package/api/user/process.js +44 -0
  4. package/api/user/task.js +1 -0
  5. package/api/user/task_available.js +1 -1
  6. package/dist/bundle.cjs +87 -1
  7. package/dist/bundle.mjs +1 -1
  8. package/doc/api.md +412 -1
  9. package/docs/Admin.html +336 -0
  10. package/docs/AdminDocuments.html +2235 -0
  11. package/docs/AdminForm.html +934 -0
  12. package/docs/AdminLists.html +895 -0
  13. package/docs/AdminMessage.html +1490 -0
  14. package/docs/AdminNotification.html +1636 -0
  15. package/docs/AdminPlugin.html +696 -0
  16. package/docs/AdminPolicy.html +451 -0
  17. package/docs/AdminProcesses.html +692 -0
  18. package/docs/AdminTask.html +740 -0
  19. package/docs/AdminUser.html +1203 -0
  20. package/docs/Datasource.html +660 -0
  21. package/docs/Dispatch.html +624 -0
  22. package/docs/Documents.html +4875 -0
  23. package/docs/External.html +1203 -0
  24. package/docs/GeoLocation.html +503 -0
  25. package/docs/Login.html +1839 -0
  26. package/docs/Notification.html +625 -0
  27. package/docs/Organization.html +2767 -0
  28. package/docs/Process.html +1598 -0
  29. package/docs/Register.html +1604 -0
  30. package/docs/Session.html +482 -0
  31. package/docs/Task.html +1394 -0
  32. package/docs/TaskAvailable.html +893 -0
  33. package/docs/User.html +2002 -0
  34. package/docs/Users.html +569 -0
  35. package/docs/admin_doctypes.js.html +193 -0
  36. package/docs/admin_document.js.html +449 -0
  37. package/docs/admin_form.js.html +268 -0
  38. package/docs/admin_index.js.html +163 -0
  39. package/docs/admin_list.js.html +250 -0
  40. package/docs/admin_message.js.html +311 -0
  41. package/docs/admin_notification.js.html +350 -0
  42. package/docs/admin_organization.js.html +241 -0
  43. package/docs/admin_plugin.js.html +233 -0
  44. package/docs/admin_policy.js.html +195 -0
  45. package/docs/admin_processes.js.html +487 -0
  46. package/docs/admin_task.js.html +242 -0
  47. package/docs/admin_user.js.html +302 -0
  48. package/docs/dispatch.js.html +218 -0
  49. package/docs/external.js.html +333 -0
  50. package/docs/general_geoLocation.js.html +205 -0
  51. package/docs/general_index.js.html +140 -0
  52. package/docs/index.html +129 -0
  53. package/docs/login.js.html +384 -0
  54. package/docs/scripts/app.min.js +1 -0
  55. package/docs/scripts/linenumber.js +26 -0
  56. package/docs/scripts/search.js +39 -0
  57. package/docs/session.js.html +202 -0
  58. package/docs/styles/app.min.css +1 -0
  59. package/docs/styles/iframe.css +13 -0
  60. package/docs/styles/prettify-jsdoc.css +111 -0
  61. package/docs/styles/prettify-tomorrow.css +132 -0
  62. package/docs/styles/reset.css +44 -0
  63. package/docs/user_datasource.js.html +261 -0
  64. package/docs/user_document.js.html +847 -0
  65. package/docs/user_index.js.html +156 -0
  66. package/docs/user_notification.js.html +218 -0
  67. package/docs/user_organization.js.html +347 -0
  68. package/docs/user_process.js.html +352 -0
  69. package/docs/user_register.js.html +322 -0
  70. package/docs/user_task.js.html +319 -0
  71. package/docs/user_task_available.js.html +252 -0
  72. package/docs/user_user.js.html +404 -0
  73. package/docs/utils_promises.js.html +235 -0
  74. package/package.json +7 -4
  75. package/readme.md +7 -8
  76. package/doc.md +0 -653
@@ -0,0 +1,847 @@
1
+
2
+
3
+ <!DOCTYPE html>
4
+ <html lang="en">
5
+
6
+ <head>
7
+
8
+ <meta charset="utf-8">
9
+ <meta name="viewport" content="width=device-width, initial-scale=1">
10
+ <title> user/document.js</title>
11
+
12
+ <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
13
+ <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
14
+ <script src="./build/entry.js"></script>
15
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
16
+ <!--[if lt IE 9]>
17
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
18
+ <![endif]-->
19
+ <link href="https://fonts.googleapis.com/css?family=Roboto:100,400,700|Inconsolata,700" rel="stylesheet">
20
+ <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
21
+ <link type="text/css" rel="stylesheet" href="https://jmblog.github.io/color-themes-for-google-code-prettify/themes/tomorrow-night.min.css">
22
+ <link type="text/css" rel="stylesheet" href="styles/app.min.css">
23
+ <link type="text/css" rel="stylesheet" href="styles/iframe.css">
24
+ <link type="text/css" rel="stylesheet" href="">
25
+ <script async defer src="https://buttons.github.io/buttons.js"></script>
26
+
27
+
28
+ </head>
29
+
30
+
31
+
32
+ <body class="layout small-header">
33
+ <div id="stickyNavbarOverlay"></div>
34
+
35
+
36
+ <div class="top-nav">
37
+ <div class="inner">
38
+ <a id="hamburger" role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
39
+ <span aria-hidden="true"></span>
40
+ <span aria-hidden="true"></span>
41
+ <span aria-hidden="true"></span>
42
+ </a>
43
+ <div class="logo">
44
+
45
+
46
+ </div>
47
+ <div class="menu">
48
+
49
+ <div class="navigation">
50
+ <a
51
+ href="index.html"
52
+ class="link"
53
+ >
54
+ Documentation
55
+ </a>
56
+
57
+
58
+
59
+ </div>
60
+ </div>
61
+ </div>
62
+ </div>
63
+ <div id="main">
64
+ <div
65
+ class="sidebar "
66
+ id="sidebarNav"
67
+ >
68
+
69
+ <nav>
70
+
71
+ <h2><a href="index.html">Documentation</a></h2><div class="category"><h3>Classes</h3><ul><li><a href="Admin.html">Admin</a></li><li><a href="AdminDocuments.html">AdminDocuments</a></li><li><a href="AdminForm.html">AdminForm</a></li><li><a href="AdminLists.html">AdminLists</a></li><li><a href="AdminMessage.html">AdminMessage</a></li><li><a href="AdminNotification.html">AdminNotification</a></li><li><a href="AdminPlugin.html">AdminPlugin</a></li><li><a href="AdminPolicy.html">AdminPolicy</a></li><li><a href="AdminProcesses.html">AdminProcesses</a></li><li><a href="AdminTask.html">AdminTask</a></li><li><a href="AdminUser.html">AdminUser</a></li><li><a href="Datasource.html">Datasource</a></li><li><a href="Dispatch.html">Dispatch</a></li><li><a href="Documents.html">Documents</a></li><li><a href="External.html">External</a></li><li><a href="GeoLocation.html">GeoLocation</a></li><li><a href="Login.html">Login</a></li><li><a href="Notification.html">Notification</a></li><li><a href="Organization.html">Organization</a></li><li><a href="Process.html">Process</a></li><li><a href="Register.html">Register</a></li><li><a href="Session.html">Session</a></li><li><a href="Task.html">Task</a></li><li><a href="TaskAvailable.html">TaskAvailable</a></li><li><a href="User.html">User</a></li><li><a href="Users.html">Users</a></li></ul></div>
72
+
73
+ </nav>
74
+ </div>
75
+ <div class="core" id="main-content-wrapper">
76
+ <div class="content">
77
+ <header class="page-title">
78
+ <p>Source</p>
79
+ <h1>user/document.js</h1>
80
+ </header>
81
+
82
+
83
+
84
+
85
+
86
+ <section>
87
+ <article>
88
+ <pre class="prettyprint source linenums"><code>import _ from 'lodash';
89
+ import Boom from '@hapi/boom';
90
+ import Joi from 'joi';
91
+ import Moment from 'moment';
92
+
93
+ /**
94
+ * Class for documents, permission user
95
+ * @class
96
+ */
97
+ class Documents {
98
+
99
+ constructor(options) {
100
+ Joi.assert(options, Joi.object().required());
101
+ Joi.assert(options.parent, Joi.object().required());
102
+
103
+ const self = this;
104
+ self.parent = options.parent;
105
+ self._client = self.parent.dispatch.getClient();
106
+ }
107
+
108
+ /**
109
+ * @author Augusto Pissarra &lt;abernardo.br@gmail.com>
110
+ * @description Get the return data and check for errors
111
+ * @param {object} retData Response HTTP
112
+ * @return {*}
113
+ * @private
114
+ */
115
+ _returnData(retData, def = {}) {
116
+ if (retData.status !== 200) {
117
+ throw Boom.badRequest(_.get(retData, 'message', 'No error message reported!'))
118
+ } else {
119
+ return _.get(retData, 'data', def);
120
+ }
121
+ }
122
+
123
+ /**
124
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
125
+ * @description Set header with new session
126
+ * @param {string} session Session, token JWT
127
+ * @return {object} header with new session
128
+ * @private
129
+ */
130
+ _setHeader(session) {
131
+ return {
132
+ headers: {
133
+ authorization: session,
134
+ }
135
+ };
136
+ }
137
+
138
+ /**
139
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
140
+ * @description Create document with informed data
141
+ * @param {object} params Object for add new document
142
+ * @param {string} params.orgname Organization name
143
+ * @param {string} params.areaId Doc area id (_id database)
144
+ * @param {string} params.docId Document id (_id database)
145
+ * @param {string} [params.documentDate=new\ Date()] Date of document
146
+ * @param {string} params.filename File name
147
+ * @param {string} params.type Mimetype of the document (image/png)
148
+ * @param {string} params.name Doument name
149
+ * @param {string} [params.content=''] Content of document
150
+ * @param {string} [params.description=''] Description of document
151
+ * @param {string} [params.category=''] Category of document
152
+ * @param {array} [params.tags=[]] Tags of document
153
+ * @param {string} params.docTypeId Document type id (_id database)
154
+ * @param {boolean} [params.hasPhisicalStorage=false] Has Phisical Storage
155
+ * @param {string} [params.boxId=''] Box ID
156
+ * @param {string} [params.storageStatus=''] Storage status
157
+ * @param {boolean} [params.ocrDocumentBackend=false] Ocr document backend
158
+ * @param {number} params.bytes Size document in bytes
159
+ * @param {object} [params.docAreaPermission={}] Permission to doc area
160
+ * @param {object} [params.docTypeFieldsData={}] Fields data "extraField'
161
+ * @param {string} params.signedUrl SIgned URL
162
+ * @param {string} [params.urlType='S3'] URL type
163
+ * @param {string} [params.addType='S3_SIGNED'] Add type
164
+ * @return {{documentDate: *, docId: *, docAreaPermission: *, document: *, docTypeFieldsData: *, description: *, storageStatus: *, type: *, content: *, tags: *, addType: *, urlType: *, areaId: *, orgname: *, docTypeId: *, bytes: *, name: *, category: *, hasPhisicalStorage: *, signedUrl: *, ocrDocumentBackend: *, boxId: *}}
165
+ * @private
166
+ */
167
+ _formatDocument(params) {
168
+ try {
169
+ return {
170
+ orgname: _.get(params, 'orgname'),
171
+ areaId: _.get(params, 'areaId'),
172
+ docId: _.get(params, 'docId'),
173
+ documentDate: _.get(params, 'documentDate', Moment().format()),
174
+ document: _.get(params, 'document'),
175
+ type: _.get(params, 'type'),
176
+ name: _.get(params, 'name'),
177
+ content: _.get(params, 'content', ''),
178
+ description: _.get(params, 'description', ''),
179
+ category: _.get(params, 'category', ''),
180
+ tags: _.get(params, 'tags', []),
181
+ docTypeId: _.get(params, 'docTypeId'),
182
+ hasPhisicalStorage: _.get(params, 'hasPhisicalStorage', false),
183
+ boxId: _.get(params, 'boxId', ''),
184
+ storageStatus: _.get(params, 'storageStatus', ''),
185
+ ocrDocumentBackend: _.get(params, 'ocrDocumentBackend', false),
186
+ bytes: _.get(params, 'bytes'),
187
+ docAreaPermission: _.get(params, 'docAreaPermission', {}),
188
+ docTypeFields: _.get(params, 'docTypeFields', []), // {"extraId": userId},
189
+ docTypeFieldsData: _.get(params, 'docTypeFieldsData', {}), // {"extraId": userId},
190
+ signedUrl: _.get(params, 'signedUrl', ''),
191
+ urlType: _.get(params, 'urlType', 'S3'),
192
+ addType: _.get(params, 'addType', 'S3_SIGNED'),
193
+ };
194
+ } catch (ex) {
195
+ throw ex;
196
+ }
197
+ }
198
+
199
+ /**
200
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
201
+ * @description Transform objtect query in query string
202
+ * @param {object} searchQuery object wicth query
203
+ * @return {string} Retutn string query
204
+ * @private
205
+ */
206
+ _queryReducer(searchQuery) {
207
+ return Object.keys(searchQuery).reduce((query, key, idx, sourceList) => {
208
+ idx++;
209
+
210
+ const data = key === 'ix'
211
+ ? JSON.stringify(searchQuery[key])
212
+ : searchQuery[key];
213
+
214
+ const querySearch = sourceList.length === idx
215
+ ? `${key}=${data}`
216
+ : `${key}=${data}&amp;`;
217
+
218
+ query += querySearch;
219
+ return query;
220
+ }, '');
221
+ }
222
+
223
+ /**
224
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
225
+ * @description Create new document
226
+ * @param {object} params Object for add new document
227
+ * @param {string} params.orgname Organization name
228
+ * @param {string} params.areaId Doc area id (_id database)
229
+ * @param {string} params.docId Document id (_id database)
230
+ * @param {string} [params.documentDate=new Date()] Date of document
231
+ * @param {string} params.document The path to the file. If S3, the key to S3, gotten after getting a signed URL
232
+ * @param {string} params.filename File name
233
+ * @param {string} params.type Mimetype of the document (image/png)
234
+ * @param {string} params.name Document name
235
+ * @param {string} [params.content=''] Content of document
236
+ * @param {string} [params.description=''] Description of document
237
+ * @param {string} [params.category=''] Category of document
238
+ * @param {array} [params.tags=[]] Tags of document
239
+ * @param {string} params.docTypeId Document type id (_id database)
240
+ * @param {boolean} [params.hasPhisicalStorage=false] Has Phisical Storage
241
+ * @param {string} [params.boxId=''] Box ID
242
+ * @param {string} [params.storageStatus=''] Storage status
243
+ * @param {boolean} [params.ocrDocumentBackend=false] Ocr document backend
244
+ * @param {number} params.bytes Size document in bytes
245
+ * @param {object} [params.docAreaPermission={}] Permission to doc area
246
+ * @param {object} [params.docTypeFieldsData={}] Fields data "extraField'
247
+ * @param {string} params.signedUrl SIgned URL
248
+ * @param {string} [params.urlType='S3'] URL type
249
+ * @param {string} [params.addType='S3_SIGNED'] Add type
250
+ * @param {string} params.orgId Organization id (_id database)
251
+ * @param {string} session Session, token JWT
252
+ * @return {Promise}
253
+ * @public
254
+ * @async
255
+ * @example
256
+ *
257
+ * const API = require('@docbrasil/api-systemmanager');
258
+ * const api = new API();
259
+ * const params = {
260
+ * orgname: 'cloundbrasil',
261
+ * areaId: '5edf9f8ee896b817e45b8dac',
262
+ * docId: '5edf86fbe896b817e45b8da6',
263
+ * fileName: 'foto',
264
+ * type: 'image/png',
265
+ * name: 'Fotografia',
266
+ * docTypeId = '5edf9f8ee896b817e45b8dac',
267
+ * bytes: 12345,
268
+ * signedUrl: 'https://s3.amazonaws.com...'
269
+ * docTypeFieldsData: {extraUser: '12349f8ee896b817e45b8dac'},
270
+ * orgId: '5df7f19618430c89a41a19d2',
271
+ * };
272
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
273
+ * await api.user.document.add(params, session);
274
+ */
275
+ async add(params, session) {
276
+ const self = this;
277
+
278
+ try {
279
+ Joi.assert(params, Joi.object().required().error(new Error('params is required')));
280
+ Joi.assert(params.orgname, Joi.string().required().error(new Error('orgname is required')));
281
+ Joi.assert(params.areaId, Joi.string().required().error(new Error('areaId is required')));
282
+ Joi.assert(params.docId, Joi.string().required().error(new Error('docId is required')));
283
+ Joi.assert(params.type, Joi.string().required().error(new Error('type is required')));
284
+ Joi.assert(params.name, Joi.string().required().error(new Error('name is required')));
285
+ Joi.assert(params.docTypeId, Joi.string().required().error(new Error('docTypeId is required')));
286
+ Joi.assert(params.bytes, Joi.number().required().error(new Error('bytes is required')));
287
+ Joi.assert(params.orgId, Joi.string().required().error(new Error('orgId is required')));
288
+ Joi.assert(session, Joi.string().required().error(new Error('session is required')));
289
+
290
+ // Get fields required, and set data default to create document
291
+ const payloadToSend = self._formatDocument(params);
292
+
293
+ Joi.assert(payloadToSend.documentDate, Joi.string().allow('').error(new Error('documentData is required after formatting payload')));
294
+ Joi.assert(payloadToSend.content, Joi.string().allow('').error(new Error('content is required after formatting payload')));
295
+ Joi.assert(payloadToSend.description, Joi.string().allow('').error(new Error('description is required after formatting payload')));
296
+ Joi.assert(payloadToSend.category, Joi.string().allow('').error(new Error('catetory is required after formatting payload')));
297
+ Joi.assert(payloadToSend.tags, Joi.array().error(new Error('tags is required after formatting payload')));
298
+ Joi.assert(payloadToSend.hasPhisicalStorage, Joi.boolean().error(new Error('hasPhisicalStorage is required after formatting payload')));
299
+ Joi.assert(payloadToSend.boxId, Joi.string().allow('').error(new Error('boxId is required after formatting payload')));
300
+ Joi.assert(payloadToSend.storageStatus, Joi.string().allow('').error(new Error('storageStatus is required after formatting payload')));
301
+ Joi.assert(payloadToSend.ocrDocumentBackend, Joi.boolean().error(new Error('ocrDocumentBackend is required after formatting payload')));
302
+ Joi.assert(payloadToSend.docAreaPermission, Joi.object().allow({}).error(new Error('docAreaPermission is required after formatting payload')));
303
+ Joi.assert(payloadToSend.docTypeFieldsData, Joi.object().allow({}).error(new Error('docTypeFieldsData is required after formatting payload')));
304
+ Joi.assert(payloadToSend.urlType, Joi.string().allow('').error(new Error('urlType is required after formatting payload')));
305
+ Joi.assert(payloadToSend.addType, Joi.string().allow('').error(new Error('addType is required after formatting payload')));
306
+
307
+ const {areaId, orgId} = params;
308
+ const apiCall = self._client
309
+ .put(`/organizations/${orgId}/areas/${areaId}/documents`, payloadToSend, self._setHeader(session));
310
+
311
+ return self._returnData(await apiCall);
312
+ } catch (ex) {
313
+ throw ex;
314
+ }
315
+ }
316
+
317
+ /**
318
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
319
+ * @param {object} params Object with params
320
+ * @param {string} params.index Field to search
321
+ * @param {string} params.txtToSearch Text to search
322
+ * @param {string} [params.compare=*] Filter to search (=, ~, *, =*, *=, *?)
323
+ * @param {string} params.docId Document id for serach
324
+ * @param {string} params.docAreaId Doc area id
325
+ * @param {string} params.tag Tag of the document
326
+ * @param {string} [params.projection=""] Projection to return fields
327
+ * @param {string} [params.sort="Mais+recentes"] Sort data
328
+ * @param {string} params.orgId Organization id (_id database)
329
+ * @param {string} params.pagination Set pagination
330
+ * @param {number} [params.pagination.page=1] Page
331
+ * @param {number} [params.pagination.perPage=100] perPage Itens per page
332
+ * @param {string} session Session, token JWT
333
+ * @return {Promise}
334
+ * @public
335
+ * @async
336
+ * @example
337
+ *
338
+ * const API = require('@docbrasil/api-systemmanager');
339
+ * const api = new API();
340
+ * const params - {
341
+ * index: 'extraCity',
342
+ * txtToSearch: 'São',
343
+ * docId: '5df7f19618430c89a41a19d2',
344
+ * docAreaId: '5edd11c46b6ce9729c2c297c',
345
+ * tag: 'Nome da cidade',
346
+ * orgId: '1234d01dc4af3941d42f8c5c'
347
+ * };
348
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
349
+ * await api.user.document.findByIdAndRemove(params, session);
350
+ */
351
+ async find(params, session) {
352
+ const self = this;
353
+
354
+ try {
355
+ Joi.assert(params, Joi.object().required());
356
+ Joi.assert(params.index, Joi.string().required());
357
+ Joi.assert(params.txtToSearch, Joi.string().allow(null));
358
+ Joi.assert(params.compare, Joi.string().allow(null));
359
+ Joi.assert(params.docId, Joi.string().required());
360
+ Joi.assert(params.docAreaId, Joi.string().required());
361
+ Joi.assert(params.tag, Joi.string().required());
362
+ Joi.assert(params.projection, Joi.string());
363
+ Joi.assert(params.orgId, Joi.string().required());
364
+ Joi.assert(params.sort, Joi.string());
365
+
366
+ if (_.hasIn(params, 'pagination')) {
367
+ Joi.assert(params.pagination, Joi.object());
368
+ Joi.assert(params.pagination.page, Joi.number());
369
+ Joi.assert(params.pagination.perPage, Joi.number());
370
+ }
371
+
372
+ Joi.assert(session, Joi.string().required());
373
+
374
+ const orgId = _.get(params, 'orgId');
375
+ const index = _.get(params, 'index');
376
+ const txtToSearch = _.get(params, 'txtToSearch', null);
377
+ const compare = _.get(params, 'compare', '*');
378
+ const tag = _.get(params, 'tag');
379
+ const defaultSearch = {
380
+ p: 100, // Per page
381
+ i: 1, // Initial page
382
+ s: 'Mais+recentes', // Sort to search
383
+ ai: '', // Doc Area Id lists emprego_net
384
+ di: '', // Document Type Id
385
+ m: 'w', // Default
386
+ pj: '' // Projection
387
+ };
388
+
389
+ // Mount query to search autocomplete
390
+ defaultSearch.p = _.get(params, 'pagination.perPage', 100);
391
+ defaultSearch.i = _.get(params, 'pagination.page', 1);
392
+ defaultSearch.s = _.get(params, 'sort', 'Mais+recentes');
393
+ defaultSearch.ai = _.get(params, 'docAreaId');
394
+ defaultSearch.di = _.get(params, 'docId');
395
+ defaultSearch.pj = `_id,${_.get(params, 'projection', '')}`;
396
+
397
+ if (!_.isNull(txtToSearch)) {
398
+ defaultSearch.ix = {ix: [[index, txtToSearch, compare, 'string', tag]]};
399
+ }
400
+
401
+ const query = self._queryReducer(defaultSearch);
402
+ const apiCall = self._client.get(`/organizations/${orgId}/documents/search?${query}`, self._setHeader(session));
403
+ return self._returnData(await apiCall);
404
+ } catch (ex) {
405
+ throw ex;
406
+ }
407
+ }
408
+
409
+ /**
410
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
411
+ * @description Remove document by id
412
+ * @param {object} params Params to remove document
413
+ * @param {string} params.docId Document Id (_id database)
414
+ * @param {string} params.orgId Organizarion id (_id database)
415
+ * @param {string} session Session, token JWT
416
+ * @return {Promise&lt;object>} data The returned data
417
+ * @return {number} data.removed The quantity of removed documents
418
+ * @return {array&lt;object>} data.errors Array of errors
419
+ * @return {string} data.errors.id Id of the document that had an error
420
+ * @return {string} data.errors.code Error code
421
+ * @return {string} data.errors.message Error message
422
+ * @public
423
+ * @async
424
+ * @example
425
+ *
426
+ * const API = require('@docbrasil/api-systemmanager');
427
+ * const api = new API();
428
+ * const params - {
429
+ * docId: '5dadd01dc4af3941d42f8c5c',
430
+ * orgIdId: '5df7f19618430c89a41a19d2',
431
+ * };
432
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
433
+ * await api.user.document.findByIdAndRemove(params, session);
434
+ */
435
+ async findByIdAndRemove(params, session) {
436
+ const self = this;
437
+
438
+ try {
439
+ Joi.assert(params, Joi.object().required());
440
+ Joi.assert(params.docId, Joi.string().required());
441
+ Joi.assert(params.orgId, Joi.string().required());
442
+ Joi.assert(session, Joi.string().required());
443
+
444
+ const {docId, orgId} = params;
445
+ const payloadToSend = {documents: [{_id: docId}]};
446
+ const apiCall = self._client.post(`/organizations/${orgId}/documents/remove`, payloadToSend, self._setHeader(session));
447
+ return self._returnData(await apiCall);
448
+ } catch (ex) {
449
+ throw ex;
450
+ }
451
+ }
452
+
453
+ /**
454
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
455
+ * @description Remove documents
456
+ * @param {object} params Params to remove document
457
+ * @param {array&lt;string>} params.documents An array ids of documents (_id database)
458
+ * @param {array&lt;string>} params.documents._id The document id (_id database)
459
+ * @param {string} params.orgId Organizarion id (_id database)
460
+ * @param {string} session Session, token JWT
461
+ * @return {Promise&lt;object>} data The returned data
462
+ * @return {number} data.removed The quantity of removed documents
463
+ * @return {array&lt;object>} data.errors Array of errors
464
+ * @return {string} data.errors.id Id of the document that had an error
465
+ * @return {string} data.errors.code Error code
466
+ * @return {string} data.errors.message Error message
467
+ * @public
468
+ * @async
469
+ * @example
470
+ *
471
+ * const API = require('@docbrasil/api-systemmanager');
472
+ * const api = new API();
473
+ * const params - {
474
+ * documents: [{ _id: '5dadd01dc4af3941d42f8c5c' }],
475
+ * orgId: '5df7f19618430c89a41a19d2',
476
+ * };
477
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
478
+ * await api.user.document.findByIdsAndRemove(params, session);
479
+ */
480
+ async findByIdsAndRemove(params, session) {
481
+ const self = this;
482
+
483
+ try {
484
+ Joi.assert(params, Joi.object().required());
485
+ Joi.assert(params.documents, Joi.array().required());
486
+ Joi.assert(params.orgId, Joi.string().required());
487
+ Joi.assert(session, Joi.string().required());
488
+
489
+ const {documents, orgId} = params;
490
+ const payloadToSend = { documents };
491
+ const apiCall = self._client.post(`/organizations/${orgId}/documents/remove`, payloadToSend, self._setHeader(session));
492
+ return self._returnData(await apiCall);
493
+ } catch (ex) {
494
+ throw ex;
495
+ }
496
+ }
497
+
498
+ /**
499
+ *
500
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
501
+ * @description Request signed url url to put or get
502
+ * @param {object} params Params to request signed url
503
+ * @param {string} params.methodType Method type HTTP get or put
504
+ * @param {string} params.docId Document id
505
+ * @param {string} params.fileName File name
506
+ * @param {string} params.docAreaId docAreaId of the document
507
+ * @param {string} params.type mimeType image/png image/jpg others
508
+ * @param {string} params.document Name document to request if method type is get
509
+ * @param {string} params.orgId Organization id (_id database)
510
+ * @param {string} session Session, token JWT
511
+ * @return {Promise&lt;object>} doc Returned document data with the signed url
512
+ * @return {string} doc.docId Document id
513
+ * @return {string} doc.name The name of the document, which is the fileName
514
+ * @return {string} doc.areaId docAreaId of the document
515
+ * @return {string} doc.type the document mimi type
516
+ * @return {string} doc.signedUrl the signed URL to upload
517
+ * @public
518
+ * @async
519
+ * @example
520
+ *
521
+ * const API = require('@docbrasil/api-systemmanager');
522
+ * const api = new API();
523
+ * const params - {
524
+ * methodType: 'put',
525
+ * docId: '5dadd01dc4af3941d42f8c5c',
526
+ * docAreaId: '5df7f19618430c89a41a19d2',
527
+ * fileName: 'Foto',
528
+ * type: 'image/png'
529
+ * orgId: '5df7f19618430c89a41a19f8'
530
+ * };
531
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
532
+ * // each doc: { docId, name, areaId, type, signedUrl }
533
+ * const { docs } = await api.user.document.signedUrl(params, session);
534
+ *
535
+ * @example
536
+ *
537
+ * const API = require('@docbrasil/api-systemmanager');
538
+ * const api = new API();
539
+ * const params - {
540
+ * methodType: 'get',
541
+ * document: 'pinkandthebrain/5df7f19618430c89a41a19d2/5dadd01dc4af3941d42f8c5c/9dadd01dc4af3941d42f6dd4.pdf',
542
+ * };
543
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
544
+ * const base64Data = await api.user.document.signedUrl(params, session);
545
+ */
546
+ async signedUrl(params, session) {
547
+ const self = this;
548
+
549
+ try {
550
+ Joi.assert(params, Joi.object().required());
551
+ Joi.assert(params.methodType, Joi.string().required());
552
+ Joi.assert(params.orgId, Joi.string().required());
553
+ Joi.assert(session, Joi.string().required());
554
+
555
+ const {methodType} = params;
556
+
557
+ if (methodType === 'put') {
558
+ Joi.assert(params, Joi.object().required());
559
+ Joi.assert(params.docId, Joi.string().required());
560
+ Joi.assert(params.docAreaId, Joi.string().required());
561
+ Joi.assert(params.fileName, Joi.string().required());
562
+ Joi.assert(params.type, Joi.string().required());
563
+ } else {
564
+ Joi.assert(params.document, Joi.string().required());
565
+ }
566
+
567
+ const {orgId} = params;
568
+ let payloadToSend;
569
+
570
+ if (params.methodType === 'put') {
571
+ const {docId, fileName: name, docAreaId: areaId, type} = params;
572
+ payloadToSend = {docs: [{docId, name, areaId, type}]};
573
+ } else {
574
+ const {document} = params;
575
+ payloadToSend = {docs: [{document}]};
576
+ }
577
+
578
+ const apiCall = self._client
579
+ .post(`/organizations/${orgId}/documents/getDocumentSignedUrl/${methodType}`, payloadToSend, self._setHeader(session));
580
+
581
+ return self._returnData(await apiCall);
582
+ } catch (ex) {
583
+ throw ex;
584
+ }
585
+ }
586
+
587
+ /**
588
+ *
589
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
590
+ * @description Request signed url url to put or get
591
+ * @param {object} params Params to request signed url
592
+ * @param {array} params.docs the list of documents to get the signed urls
593
+ * @param {string} params.docs.docId Document id
594
+ * @param {string} params.docs.name File name
595
+ * @param {string} params.docs.areaId docAreaId of the document
596
+ * @param {string} params.docs.type mimeType image/png image/jpg others
597
+ * @param {string} params.docs.document Name document to request if method type is get
598
+ * @param {string} params.methodType Method type HTTP get or put
599
+ * @param {string} params.orgId Organization id (_id database)
600
+ * @param {string} session Session, token JWT
601
+ * @return {Promise&lt;object>} doc Returned document data with the signed url
602
+ * @return {string} doc.docId Document id
603
+ * @return {string} doc.name The name of the document, which is the fileName
604
+ * @return {string} doc.areaId docAreaId of the document
605
+ * @return {string} doc.type the document mimi type
606
+ * @return {string} doc.signedUrl the signed URL to upload
607
+ * @public
608
+ * @async
609
+ * @example
610
+ *
611
+ * const API = require('@docbrasil/api-systemmanager');
612
+ * const api = new API();
613
+ * const params - {
614
+ * methodType: 'put',
615
+ * orgId: '5df7f19618430c89a41a19f8'
616
+ * docs: [
617
+ * {
618
+ * docId: '5dadd01dc4af3941d42f8c5c',
619
+ * areaId: '5df7f19618430c89a41a19d2',
620
+ * name: 'Foto.png',
621
+ * type: 'image/png'
622
+ * }
623
+ * ]
624
+ * };
625
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
626
+ * // each doc: { docId, name, areaId, type, signedUrl }
627
+ * const { docs } = await api.user.document.signedUrls(params, session);
628
+ *
629
+ * @example
630
+ *
631
+ * const API = require('@docbrasil/api-systemmanager');
632
+ * const api = new API();
633
+ * const params - {
634
+ * methodType: 'get',
635
+ * docs: [
636
+ * { document: 'pinkandthebrain/5df7f19618430c89a41a19d2/5dadd01dc4af3941d42f8c5c/9dadd01dc4af3941d42f6dd4.pdf' }
637
+ * ],
638
+ * };
639
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
640
+ * const base64Data = await api.user.document.signedUrls(params, session);
641
+ */
642
+ async signedUrls(params, session) {
643
+ const self = this;
644
+
645
+ try {
646
+ Joi.assert(params, Joi.object().required());
647
+ Joi.assert(params.docs, Joi.array().required());
648
+ Joi.assert(params.orgId, Joi.string().required());
649
+ Joi.assert(session, Joi.string().required());
650
+
651
+ const {orgId, methodType = 'put', docs = []} = params;
652
+ const apiCall = self._client
653
+ .post(`/organizations/${orgId}/documents/getDocumentSignedUrl/${methodType}`, { docs }, self._setHeader(session));
654
+
655
+ return self._returnData(await apiCall);
656
+ } catch (ex) {
657
+ throw ex;
658
+ }
659
+ }
660
+
661
+ /**
662
+ * Uploads the file
663
+ * @param {object} params Params to upload document
664
+ * @param {buffer} params.content The content of the file (Buffer)
665
+ * @param {string} params.signedUrl The signed URL
666
+ * @param {string} params.type The file mime type
667
+ * @param {string} params.onUploadProgress A callback for the upload progress. It will return a progressEvent.
668
+ * @return {Promise&lt;boolean>} True if success
669
+ *
670
+ * @public
671
+ * @async
672
+ * @example
673
+ *
674
+ * const FS = require('fs');
675
+ * const Path = require('path');
676
+ * const API = require('@docbrasil/api-systemmanager');
677
+ * const api = new API();
678
+ * const params - {
679
+ * content: FS.readFileSync(Path.join(__dirname, '.mypdf.pdf')),
680
+ * signedUrl: 'https://signedurl.com/token...',
681
+ * type: 'application/pdf'
682
+ * };
683
+ * const retData = await api.user.document.uploadSignedDocument(params);
684
+ *
685
+ * onUploadProgress return the progressEvent
686
+ * - lengthComputable: A Boolean that indicates whether or not the total number of bytes is known.
687
+ * - loaded: The number of bytes of the file that have been uploaded.
688
+ * - total: The total number of bytes in the file.
689
+ */
690
+ async uploadSignedDocument(params= {}) {
691
+ const { content, signedUrl, type } = params;
692
+ Joi.assert(params, Joi.object().required());
693
+ Joi.assert(params.content, Joi.required());
694
+ Joi.assert(params.signedUrl, Joi.string().required());
695
+ Joi.assert(params.type, Joi.string().required());
696
+
697
+ const self = this;
698
+ const reqOpts = {
699
+ headers: {
700
+ 'Content-Type': type
701
+ },
702
+ maxContentLength: Infinity,
703
+ maxBodyLength: Infinity
704
+ };
705
+
706
+ const onUploadProgress = params.onUploadProgress;
707
+
708
+ if(onUploadProgress) {
709
+ reqOpts.onUploadProgress = onUploadProgress;
710
+ }
711
+
712
+ const apiCall = self._client
713
+ .put(signedUrl, content, reqOpts);
714
+ self._returnData(await apiCall);
715
+ return true;
716
+ }
717
+
718
+ /**
719
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
720
+ * Checks if a document can be added and it does not repeat its primary key
721
+ * @param params
722
+ * @param params.orgId {string} the organization id
723
+ * @param params.docTypeId {string} the id of the doc type
724
+ * @param params.docs {array&lt;object>} an array of documents
725
+ * @param params.docs.id {string} an unique id representing the document
726
+ * @param params.docs.docTypeFields {object} thje docTypeFields of the document
727
+ * @param params.docs.docTypeFieldsData {object} thje docTypeFieldsData of the document
728
+ * @param session
729
+ * @return {Promise&lt;array>} Return the array of the documents that are repeated. If not document is repeaded, then if returns an empty array.
730
+ * @return id {array&lt;string>} the id of the repeated document
731
+ * @public
732
+ * @async
733
+ * @example
734
+ *
735
+ * const API = require('@docbrasil/api-systemmanager');
736
+ * const api = new API();
737
+ * const docTypeFields = [...]; // the doc type fields array
738
+ * const docTypeFieldsData = {...}; // the data of this fields
739
+ * const params - {
740
+ * docs: [{ id: '5dadd01dc4af3941d42f8c5c', docTypeFields, docTypeFieldsData }],
741
+ * orgId: '5df7f19618430c89a41a19d2',
742
+ * docTypeId: '5df7f19618430c89a41a19d5',
743
+ * };
744
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
745
+ * const retDocs = await api.user.document.checkPrimaryKeys(params, session);
746
+ *
747
+ */
748
+ async checkPrimaryKeys(params, session) {
749
+ Joi.assert(params, Joi.object().required().label('params'));
750
+ Joi.assert(params.orgId, Joi.string().required().label('orgId'));
751
+ Joi.assert(params.docTypeId, Joi.string().required().label('docTypeId'));
752
+ Joi.assert(params.docs, Joi.array().required().label('docs'));
753
+ Joi.assert(session, Joi.string().required().label('session'));
754
+
755
+ const { docs = [], orgId = '', docTypeId = '' } = params;
756
+
757
+ if(docs.length === 0) return;
758
+
759
+ try {
760
+ const self = this;
761
+ const payloadToSend = {
762
+ docs,
763
+ docTypeId
764
+ };
765
+ const apiCall = self._client.post(`/organizations/${orgId}/documents/can/add`, payloadToSend, self._setHeader(session));
766
+ return self._returnData(await apiCall);
767
+ } catch (ex) {
768
+ throw ex;
769
+ }
770
+ }
771
+
772
+ /**
773
+ * @author CloudBrasil &lt;abernardo.br@gmail.com>
774
+ * @description Method to search documents for
775
+ * @param {object} params Params to search the documents
776
+ * @param {object} params.query Search documents query
777
+ * @param {object} params.orgId Organization id (_id database)
778
+ * @param {string} session Session, token JWT
779
+ * @returns {promise} returned data from the search
780
+ * @returns {number} count the count of items searched
781
+ * @returns {array&lt;object>} items the items returned from search
782
+ * @returns {number} took the number of documents taken
783
+ * @returns {number} totalCount the total count of all documents
784
+ * @public
785
+ * @example
786
+ *
787
+ * const API = require('@docbrasil/api-systemmanager');
788
+ * const api = new API();
789
+ * const params = {
790
+ * query: {p: 20, i: 1, s: 'Mais recentes', as: '', m: 'w', ai: '57e6a3bd6be6b45210833fae'},
791
+ * orgId: '55e4a3bd6be6b45210833fae',
792
+ * };
793
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
794
+ * const retSearch = await api.user.document.searchDocuments(params, session);
795
+ */
796
+ async searchDocuments(params, session) {
797
+ const self = this;
798
+
799
+ try {
800
+ Joi.assert(params, Joi.object().required(), 'Params to search the documents');
801
+ Joi.assert(params.query, Joi.object().required(), 'The query for the search documents');
802
+ Joi.assert(params.orgId, Joi.string().required(), 'Organization id (_id database)');
803
+ Joi.assert(session, Joi.string().required(), 'Session token JWT');
804
+
805
+ const {query, orgId} = params;
806
+ const queryString = encodeURIComponent(JSON.stringify(query));
807
+ const apiCall = self._client.get(`/organizations/${orgId}/documents/search?${queryString}`, self._setHeader(session));
808
+
809
+ return self._returnData(await apiCall);
810
+ } catch (ex) {
811
+ throw ex;
812
+ }
813
+ }
814
+
815
+ }
816
+
817
+ export default Documents;
818
+ </code></pre>
819
+ </article>
820
+ </section>
821
+
822
+
823
+
824
+
825
+ </div>
826
+
827
+ <footer class="footer">
828
+ <div class="content has-text-centered">
829
+ <p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a></p>
830
+ <p class="sidebar-created-by">
831
+ <a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
832
+ <a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
833
+ </p>
834
+ </div>
835
+ </footer>
836
+
837
+ </div>
838
+ <div id="side-nav" class="side-nav">
839
+ </div>
840
+ </div>
841
+ <script src="scripts/app.min.js"></script>
842
+ <script>PR.prettyPrint();</script>
843
+ <script src="scripts/linenumber.js"> </script>
844
+
845
+
846
+ </body>
847
+ </html>