@docbrasil/api-systemmanager 1.0.81 → 1.0.82

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.
@@ -0,0 +1,144 @@
1
+ import _ from 'lodash';
2
+ import Boom from '@hapi/boom';
3
+ import Joi from 'joi';
4
+
5
+ /**
6
+ * Class for user datasource access, to be used with documents
7
+ * @class
8
+ */
9
+ class Datasource {
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
+
20
+ /**
21
+ * @author Augusto Pissarra <abernardo.br@gmail.com>
22
+ * @description Get the return data and check for errors
23
+ * @param {object} retData Response HTTP
24
+ * @return {*}
25
+ * @private
26
+ */
27
+ _returnData(retData, def = {}) {
28
+ if (retData.status !== 200) {
29
+ return Boom.badRequest(_.get(retData, 'message', 'No error message reported!'))
30
+ } else {
31
+ return _.get(retData, 'data', def);
32
+ }
33
+ }
34
+
35
+ /**
36
+ * @author CloudBrasil <abernardo.br@gmail.com>
37
+ * @description Set header with new session
38
+ * @param {string} session Session, token JWT
39
+ * @return {object} header with new session
40
+ * @private
41
+ */
42
+ _setHeader(session) {
43
+ return {
44
+ headers: {
45
+ authorization: session,
46
+ }
47
+ };
48
+ }
49
+
50
+ _cleanIdCard(idcard = '') {
51
+ return idcard.replace(/\D+/g,'');
52
+ }
53
+
54
+ /**
55
+ * @author CloudBrasil <abernardo.br@gmail.com>
56
+ * @description Method to get autocomplete data from a datasource
57
+ * @param {object} params Params to add notification token
58
+ * @param {string} params.orgId The user organization _id
59
+ * @param {array<object>} params.dataSources The document type data sources information
60
+ * @param {string} params.dataSources._id The document type data sources _id
61
+ * @param {array<object>} params.dataSources.fields The document type data sources list of fields
62
+ * @param {array<object>} params.documents The document list
63
+ * @param {string} params.documents._id The document _id
64
+ * @param {string} session Is token JWT of user NOT allow SU
65
+ * @returns {promise<array>} docs The returned documents field with autocomplete
66
+ * @returns {string} docs._id the _id of the document
67
+ * @returns {object} data.docTypeFieldsData the field values
68
+ * @public
69
+ * @example
70
+ *
71
+ * const API = require('@docbrasil/api-systemmanager');
72
+ * const api = new API();
73
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
74
+ * const params = {
75
+ * orgId: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
76
+ * dataSources: [{}],
77
+ * documents: [{}]
78
+ * };
79
+ * const retData = await api.user.datasource.autocomplete(params, session);
80
+ */
81
+ async autocomplete(params = {}, session) {
82
+ const self = this;
83
+
84
+ try {
85
+ Joi.assert(params, Joi.object().required(), 'Params to get task');
86
+ Joi.assert(params.orgId, Joi.string().required(), 'The organization _id is required');
87
+ Joi.assert(params.dataSources, Joi.object().required(), 'Datasources is required');
88
+ Joi.assert(params.documents, Joi.array().required(), ' Documents is required');
89
+
90
+ const {
91
+ documents = [],
92
+ dataSources = [],
93
+ orgId
94
+ } = params;
95
+ const aDocs = [];
96
+
97
+ for(const doc of documents) {
98
+ const newDoc = { guid: doc._id, dataSources: [] };
99
+ for(const dataSource of dataSources) {
100
+ const clonedDataSource = { _id: dataSource._id, fields: [] };
101
+ const dataSourceFields = dataSource?.fields || [];
102
+ for(const field of dataSourceFields) {
103
+ const newField = {
104
+ associatedFieldName: field.associatedFieldName,
105
+ type: field.type,
106
+ isPK: field.isPK,
107
+ name: field.name
108
+ };
109
+ if(field.isPK) {
110
+ newField.value = _.get(doc, `docTypeFieldsData.${field.associatedFieldName}`);
111
+ if(newField.type === 'Número Inteiro') {
112
+ newField.value = parseInt(newField.value, 10);
113
+ } else if(newField.type === 'Número Duplo') {
114
+ newField.value = parseFloat(newField.value);
115
+ } else if(newField.type === 'CPF') {
116
+ newField.value = self._cleanIdCard(newField.value);
117
+ } else if(newField.type === 'CNPJ') {
118
+ newField.value = self._cleanIdCard(newField.value);
119
+ }
120
+ }
121
+ if(_.get(newField, 'value.docTypeFieldsData.docTypeFieldsData')) {
122
+ delete newField.value.docTypeFieldsData.docTypeFieldsData;
123
+ }
124
+ clonedDataSource.fields.push(newField);
125
+ }
126
+ newDoc.dataSources.push(clonedDataSource);
127
+ }
128
+ aDocs.push(newDoc);
129
+ }
130
+
131
+ const url = `/organizations/${orgId}/documents/datasources/autocomplete`;
132
+ const dataParams = { docs: aDocs };
133
+ const apiCall = self._client
134
+ .post(url, dataParams, self._setHeader(session));
135
+
136
+ const retData = self._returnData(await apiCall);
137
+ return retData;
138
+ } catch (ex) {
139
+ throw ex;
140
+ }
141
+ }
142
+ }
143
+
144
+ export default Datasource;
package/api/user/index.js CHANGED
@@ -8,6 +8,7 @@ import Task from './task';
8
8
  import User from './user';
9
9
  import Register from './register';
10
10
  import Notification from './notification';
11
+ import Datasource from './datasource';
11
12
 
12
13
  /**
13
14
  * @class API request, user permission level
@@ -25,6 +26,7 @@ class Users {
25
26
 
26
27
  const self = this;
27
28
  self.document = new Document(options);
29
+ self.datasource = new Datasource(options);
28
30
  self.organization = new Organization(options);
29
31
  self.process = new Process(options);
30
32
  self.task = new Task(options);
package/dist/bundle.cjs CHANGED
@@ -1207,7 +1207,7 @@ class Documents {
1207
1207
  * docTypeId: '5df7f19618430c89a41a19d5',
1208
1208
  * };
1209
1209
  * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
1210
- * const retDocs = await api.user.document.findByIdsAndRemove(params, session);
1210
+ * const retDocs = await api.user.document.checkPrimaryKeys(params, session);
1211
1211
  *
1212
1212
  */
1213
1213
  async checkPrimaryKeys(params, session) {
@@ -9109,6 +9109,145 @@ class Notification {
9109
9109
  }
9110
9110
  }
9111
9111
 
9112
+ /**
9113
+ * Class for user datasource access, to be used with documents
9114
+ * @class
9115
+ */
9116
+ class Datasource {
9117
+
9118
+ constructor(options) {
9119
+ Joi__default["default"].assert(options, Joi__default["default"].object().required());
9120
+ Joi__default["default"].assert(options.parent, Joi__default["default"].object().required());
9121
+
9122
+ const self = this;
9123
+ self.parent = options.parent;
9124
+ self._client = self.parent.dispatch.getClient();
9125
+ }
9126
+
9127
+ /**
9128
+ * @author Augusto Pissarra <abernardo.br@gmail.com>
9129
+ * @description Get the return data and check for errors
9130
+ * @param {object} retData Response HTTP
9131
+ * @return {*}
9132
+ * @private
9133
+ */
9134
+ _returnData(retData, def = {}) {
9135
+ if (retData.status !== 200) {
9136
+ return Boom__default["default"].badRequest(___default["default"].get(retData, 'message', 'No error message reported!'))
9137
+ } else {
9138
+ return ___default["default"].get(retData, 'data', def);
9139
+ }
9140
+ }
9141
+
9142
+ /**
9143
+ * @author CloudBrasil <abernardo.br@gmail.com>
9144
+ * @description Set header with new session
9145
+ * @param {string} session Session, token JWT
9146
+ * @return {object} header with new session
9147
+ * @private
9148
+ */
9149
+ _setHeader(session) {
9150
+ return {
9151
+ headers: {
9152
+ authorization: session,
9153
+ }
9154
+ };
9155
+ }
9156
+
9157
+ _cleanIdCard(idcard = '') {
9158
+ return idcard.replace(/\D+/g,'');
9159
+ }
9160
+
9161
+ /**
9162
+ * @author CloudBrasil <abernardo.br@gmail.com>
9163
+ * @description Method to get autocomplete data from a datasource
9164
+ * @param {object} params Params to add notification token
9165
+ * @param {string} params.orgId The user organization _id
9166
+ * @param {array<object>} params.dataSources The document type data sources information
9167
+ * @param {string} params.dataSources._id The document type data sources _id
9168
+ * @param {array<object>} params.dataSources.fields The document type data sources list of fields
9169
+ * @param {array<object>} params.documents The document list
9170
+ * @param {string} params.documents._id The document _id
9171
+ * @param {string} session Is token JWT of user NOT allow SU
9172
+ * @returns {promise<array>} docs The returned documents field with autocomplete
9173
+ * @returns {string} docs._id the _id of the document
9174
+ * @returns {object} data.docTypeFieldsData the field values
9175
+ * @public
9176
+ * @example
9177
+ *
9178
+ * const API = require('@docbrasil/api-systemmanager');
9179
+ * const api = new API();
9180
+ * const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
9181
+ * const params = {
9182
+ * orgId: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
9183
+ * dataSources: [{}],
9184
+ * documents: [{}]
9185
+ * };
9186
+ * const retData = await api.user.datasource.autocomplete(params, session);
9187
+ */
9188
+ async autocomplete(params = {}, session) {
9189
+ const self = this;
9190
+
9191
+ try {
9192
+ Joi__default["default"].assert(params, Joi__default["default"].object().required(), 'Params to get task');
9193
+ Joi__default["default"].assert(params.orgId, Joi__default["default"].string().required(), 'The organization _id is required');
9194
+ Joi__default["default"].assert(params.dataSources, Joi__default["default"].object().required(), 'Datasources is required');
9195
+ Joi__default["default"].assert(params.documents, Joi__default["default"].array().required(), ' Documents is required');
9196
+
9197
+ const {
9198
+ documents = [],
9199
+ dataSources = [],
9200
+ orgId
9201
+ } = params;
9202
+ const aDocs = [];
9203
+
9204
+ for(const doc of documents) {
9205
+ const newDoc = { guid: doc._id, dataSources: [] };
9206
+ for(const dataSource of dataSources) {
9207
+ const clonedDataSource = { _id: dataSource._id, fields: [] };
9208
+ const dataSourceFields = dataSource?.fields || [];
9209
+ for(const field of dataSourceFields) {
9210
+ const newField = {
9211
+ associatedFieldName: field.associatedFieldName,
9212
+ type: field.type,
9213
+ isPK: field.isPK,
9214
+ name: field.name
9215
+ };
9216
+ if(field.isPK) {
9217
+ newField.value = ___default["default"].get(doc, `docTypeFieldsData.${field.associatedFieldName}`);
9218
+ if(newField.type === 'Número Inteiro') {
9219
+ newField.value = parseInt(newField.value, 10);
9220
+ } else if(newField.type === 'Número Duplo') {
9221
+ newField.value = parseFloat(newField.value);
9222
+ } else if(newField.type === 'CPF') {
9223
+ newField.value = self._cleanIdCard(newField.value);
9224
+ } else if(newField.type === 'CNPJ') {
9225
+ newField.value = self._cleanIdCard(newField.value);
9226
+ }
9227
+ }
9228
+ if(___default["default"].get(newField, 'value.docTypeFieldsData.docTypeFieldsData')) {
9229
+ delete newField.value.docTypeFieldsData.docTypeFieldsData;
9230
+ }
9231
+ clonedDataSource.fields.push(newField);
9232
+ }
9233
+ newDoc.dataSources.push(clonedDataSource);
9234
+ }
9235
+ aDocs.push(newDoc);
9236
+ }
9237
+
9238
+ const url = `/organizations/${orgId}/documents/datasources/autocomplete`;
9239
+ const dataParams = { docs: aDocs };
9240
+ const apiCall = self._client
9241
+ .post(url, dataParams, self._setHeader(session));
9242
+
9243
+ const retData = self._returnData(await apiCall);
9244
+ return retData;
9245
+ } catch (ex) {
9246
+ throw ex;
9247
+ }
9248
+ }
9249
+ }
9250
+
9112
9251
  /**
9113
9252
  * @class API request, user permission level
9114
9253
  */
@@ -9125,6 +9264,7 @@ class Users {
9125
9264
 
9126
9265
  const self = this;
9127
9266
  self.document = new Documents(options);
9267
+ self.datasource = new Datasource(options);
9128
9268
  self.organization = new Organization$1(options);
9129
9269
  self.process = new Process(options);
9130
9270
  self.task = new Task(options);