@docbrasil/api-systemmanager 1.0.96 → 1.0.97
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/admin/doctypes.js +76 -76
- package/api/admin/document.js +332 -332
- package/api/admin/form.js +151 -151
- package/api/admin/index.js +46 -46
- package/api/admin/list.js +133 -133
- package/api/admin/message.js +194 -194
- package/api/admin/notification.js +233 -233
- package/api/admin/organization.js +124 -124
- package/api/admin/plugin.js +116 -116
- package/api/admin/policy.js +78 -78
- package/api/admin/processes.js +370 -370
- package/api/admin/task.js +125 -125
- package/api/admin/user.js +185 -185
- package/api/dispatch.js +101 -101
- package/api/general/geoLocation.js +88 -88
- package/api/general/index.js +23 -23
- package/api/login.js +267 -267
- package/api/session.js +85 -85
- package/api/user/datasource.js +144 -144
- package/api/user/document.js +730 -730
- package/api/user/index.js +39 -39
- package/api/user/notification.js +101 -101
- package/api/user/organization.js +230 -230
- package/api/user/process.js +191 -191
- package/api/user/register.js +205 -205
- package/api/user/task.js +202 -201
- package/api/user/task_available.js +135 -135
- package/api/user/user.js +287 -287
- package/api/utils/cypher.js +37 -37
- package/api/utils/promises.js +118 -118
- package/bundleRollup.js +158 -158
- package/dist/bundle.cjs +4876 -4875
- package/dist/bundle.mjs +1 -1
- package/doc/api.md +2453 -2453
- package/doc.md +653 -653
- package/helper/boom.js +487 -487
- package/helper/cryptojs.js +6067 -6067
- package/index.js +85 -85
- package/package-lock.json +4635 -4635
- package/package.json +68 -68
- package/readme.md +25 -25
- package/tests/admin/document.spec.js +45 -45
- package/tests/admin/form.spec.js +74 -74
- package/tests/admin/list.spec.js +86 -86
- package/tests/admin/message.js +92 -92
- package/tests/admin/notification.spec.js +174 -174
- package/tests/admin/pluginspec..js +71 -71
- package/tests/admin/policy.spec.js +71 -71
- package/tests/admin/processes.spec.js +119 -119
- package/tests/admin/users.spec.js +127 -127
- package/tests/documents.spec.js +164 -164
- package/tests/login.spec.js +91 -91
- package/tests/session.spec..js +58 -58
- package/tests/user/documents.js +164 -164
- package/tests/user/organization.js +122 -122
- package/tests/user/process.js +71 -71
- package/tests/user/task_available.js +75 -75
- package/tests/user/user.js +88 -88
package/api/admin/task.js
CHANGED
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import Boom from '@hapi/boom';
|
|
3
|
-
import Joi from 'joi';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Admin Class for task, permission admin
|
|
7
|
-
* @class
|
|
8
|
-
*/
|
|
9
|
-
class AdminTask {
|
|
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
|
-
/**
|
|
51
|
-
* @author CloudBrasil <abernardo.br@gmail.com>
|
|
52
|
-
* @description Get filter to search tasks
|
|
53
|
-
* @param {string} filter Filter type
|
|
54
|
-
* @return {*}
|
|
55
|
-
* @private
|
|
56
|
-
*/
|
|
57
|
-
_taskFilters(filter) {
|
|
58
|
-
const taskFilters = {
|
|
59
|
-
CLEAN: 1,
|
|
60
|
-
EXECUTED: 2,
|
|
61
|
-
PENDING: 3,
|
|
62
|
-
LATE: 4,
|
|
63
|
-
NOT_DONE: 5,
|
|
64
|
-
DONE: 6
|
|
65
|
-
};
|
|
66
|
-
return _.get(taskFilters, filter, 'NOT_DONE');
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @author CloudBrasil <abernardo.br@gmail.com>
|
|
71
|
-
* @description Get task by user Id
|
|
72
|
-
* @param {object} params Params to get task
|
|
73
|
-
* @param {string} params.userId User id (_id database)
|
|
74
|
-
* @param {string} [params.filter=NOT_DONE] Filter type CLEAN | EXECUTED | PENDING | LATE | NOT_DONE | DONE
|
|
75
|
-
|
|
76
|
-
* @param {object} params.project Project to return
|
|
77
|
-
* @param {boolean} params.project.returnProcessProperties Return process properties
|
|
78
|
-
* @param {boolean} params.project.returnInitParams Return init params
|
|
79
|
-
|
|
80
|
-
* @param {string} params.userId User id (_id database)
|
|
81
|
-
* @param {boolean} [params.includeOwner=false] Include owner true | false
|
|
82
|
-
* @param {string} session Session, token JWT
|
|
83
|
-
* @public
|
|
84
|
-
* @async
|
|
85
|
-
* @example
|
|
86
|
-
*
|
|
87
|
-
* const API = require('@docbrasil/api-systemmanager');
|
|
88
|
-
* const api = new API();
|
|
89
|
-
* const params = {
|
|
90
|
-
* userId: '55e4a3bd6be6b45210833fae',
|
|
91
|
-
* };
|
|
92
|
-
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
93
|
-
* await api.admin.task.find(params, session);
|
|
94
|
-
*/
|
|
95
|
-
async find(params, session) {
|
|
96
|
-
const self = this;
|
|
97
|
-
|
|
98
|
-
try {
|
|
99
|
-
Joi.assert(params, Joi.object().required());
|
|
100
|
-
Joi.assert(params.userId, Joi.string().required());
|
|
101
|
-
Joi.assert(params.filter, Joi.string());
|
|
102
|
-
Joi.assert(params.project, Joi.object());
|
|
103
|
-
Joi.assert(params.includeOwner, Joi.boolean());
|
|
104
|
-
Joi.assert(session, Joi.string().required());
|
|
105
|
-
|
|
106
|
-
const filterType = _.get(params, 'filter', 'NOT_DONE') || 'NOT_DONE';
|
|
107
|
-
const includeOwner = _.get(params, 'includeOwner', false) || false;
|
|
108
|
-
const {userId} = params;
|
|
109
|
-
const filter = self._taskFilters(filterType);
|
|
110
|
-
const { returnProcessProperties, returnInitParams } = params?.project ?? {};
|
|
111
|
-
|
|
112
|
-
let queryString = `taskFilter=${filter}&includeOwner=${includeOwner}`;
|
|
113
|
-
|
|
114
|
-
if (returnProcessProperties) queryString = `${queryString}&returnProcessProperties=${returnProcessProperties}`;
|
|
115
|
-
if (returnInitParams) queryString = `${queryString}&returnInitParams=${returnInitParams}`;
|
|
116
|
-
|
|
117
|
-
const apiCall = self._client.get(`/admin/users/${userId}/tasks?${queryString}`, self._setHeader(session));
|
|
118
|
-
return self._returnData(await apiCall);
|
|
119
|
-
} catch (ex) {
|
|
120
|
-
throw ex;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export default AdminTask;
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import Boom from '@hapi/boom';
|
|
3
|
+
import Joi from 'joi';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Admin Class for task, permission admin
|
|
7
|
+
* @class
|
|
8
|
+
*/
|
|
9
|
+
class AdminTask {
|
|
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
|
+
/**
|
|
51
|
+
* @author CloudBrasil <abernardo.br@gmail.com>
|
|
52
|
+
* @description Get filter to search tasks
|
|
53
|
+
* @param {string} filter Filter type
|
|
54
|
+
* @return {*}
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
_taskFilters(filter) {
|
|
58
|
+
const taskFilters = {
|
|
59
|
+
CLEAN: 1,
|
|
60
|
+
EXECUTED: 2,
|
|
61
|
+
PENDING: 3,
|
|
62
|
+
LATE: 4,
|
|
63
|
+
NOT_DONE: 5,
|
|
64
|
+
DONE: 6
|
|
65
|
+
};
|
|
66
|
+
return _.get(taskFilters, filter, 'NOT_DONE');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @author CloudBrasil <abernardo.br@gmail.com>
|
|
71
|
+
* @description Get task by user Id
|
|
72
|
+
* @param {object} params Params to get task
|
|
73
|
+
* @param {string} params.userId User id (_id database)
|
|
74
|
+
* @param {string} [params.filter=NOT_DONE] Filter type CLEAN | EXECUTED | PENDING | LATE | NOT_DONE | DONE
|
|
75
|
+
|
|
76
|
+
* @param {object} params.project Project to return
|
|
77
|
+
* @param {boolean} params.project.returnProcessProperties Return process properties
|
|
78
|
+
* @param {boolean} params.project.returnInitParams Return init params
|
|
79
|
+
|
|
80
|
+
* @param {string} params.userId User id (_id database)
|
|
81
|
+
* @param {boolean} [params.includeOwner=false] Include owner true | false
|
|
82
|
+
* @param {string} session Session, token JWT
|
|
83
|
+
* @public
|
|
84
|
+
* @async
|
|
85
|
+
* @example
|
|
86
|
+
*
|
|
87
|
+
* const API = require('@docbrasil/api-systemmanager');
|
|
88
|
+
* const api = new API();
|
|
89
|
+
* const params = {
|
|
90
|
+
* userId: '55e4a3bd6be6b45210833fae',
|
|
91
|
+
* };
|
|
92
|
+
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
93
|
+
* await api.admin.task.find(params, session);
|
|
94
|
+
*/
|
|
95
|
+
async find(params, session) {
|
|
96
|
+
const self = this;
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
Joi.assert(params, Joi.object().required());
|
|
100
|
+
Joi.assert(params.userId, Joi.string().required());
|
|
101
|
+
Joi.assert(params.filter, Joi.string());
|
|
102
|
+
Joi.assert(params.project, Joi.object());
|
|
103
|
+
Joi.assert(params.includeOwner, Joi.boolean());
|
|
104
|
+
Joi.assert(session, Joi.string().required());
|
|
105
|
+
|
|
106
|
+
const filterType = _.get(params, 'filter', 'NOT_DONE') || 'NOT_DONE';
|
|
107
|
+
const includeOwner = _.get(params, 'includeOwner', false) || false;
|
|
108
|
+
const {userId} = params;
|
|
109
|
+
const filter = self._taskFilters(filterType);
|
|
110
|
+
const { returnProcessProperties, returnInitParams } = params?.project ?? {};
|
|
111
|
+
|
|
112
|
+
let queryString = `taskFilter=${filter}&includeOwner=${includeOwner}`;
|
|
113
|
+
|
|
114
|
+
if (returnProcessProperties) queryString = `${queryString}&returnProcessProperties=${returnProcessProperties}`;
|
|
115
|
+
if (returnInitParams) queryString = `${queryString}&returnInitParams=${returnInitParams}`;
|
|
116
|
+
|
|
117
|
+
const apiCall = self._client.get(`/admin/users/${userId}/tasks?${queryString}`, self._setHeader(session));
|
|
118
|
+
return self._returnData(await apiCall);
|
|
119
|
+
} catch (ex) {
|
|
120
|
+
throw ex;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export default AdminTask;
|
package/api/admin/user.js
CHANGED
|
@@ -1,185 +1,185 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
import Boom from '@hapi/boom';
|
|
3
|
-
import Joi from 'joi';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Admin Class for user, permission admin
|
|
7
|
-
* @class
|
|
8
|
-
*/
|
|
9
|
-
class AdminUser {
|
|
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 CloudBrasil <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
|
-
/**
|
|
51
|
-
* @author CloudBrasil <abernardo.br@gmail.com>
|
|
52
|
-
* @description Request profile by userId
|
|
53
|
-
* @param {string} userId User identifier (_id database)
|
|
54
|
-
* @param {string} session Is token JWT
|
|
55
|
-
* @return {Promise}
|
|
56
|
-
* @public
|
|
57
|
-
* @async
|
|
58
|
-
* @example
|
|
59
|
-
*
|
|
60
|
-
* const API = require('@docbrasil/api-systemmanager');
|
|
61
|
-
* const api = new API();
|
|
62
|
-
* const userId = '55e4a3bd6be6b45210833fae';
|
|
63
|
-
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
64
|
-
* await api.admin.user.findById(userId, session);
|
|
65
|
-
*/
|
|
66
|
-
async findById(userId, session) {
|
|
67
|
-
const self = this;
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
|
|
71
|
-
Joi.assert(userId, Joi.string().required());
|
|
72
|
-
Joi.assert(session, Joi.string().required());
|
|
73
|
-
|
|
74
|
-
const apiCall = self.client.get(`/admin/users/${userId}`, self._setHeader(session));
|
|
75
|
-
return self._returnData(await apiCall);
|
|
76
|
-
} catch (ex) {
|
|
77
|
-
throw ex;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @author CloudBrasil <abernardo.br@gmail.com>
|
|
83
|
-
* @description Update password by userId
|
|
84
|
-
* @param {object} params Params to update password
|
|
85
|
-
* @param {string} params.userId Id of the user
|
|
86
|
-
* @param {string} params.oldPassword Old password
|
|
87
|
-
* @param {string} params.newPassword New password
|
|
88
|
-
* @param {string} session Is token JWT
|
|
89
|
-
* @return {Promise<unknown>}
|
|
90
|
-
* @public
|
|
91
|
-
* @async
|
|
92
|
-
* @example
|
|
93
|
-
*
|
|
94
|
-
* const API = require('@docbrasil/api-systemmanager');
|
|
95
|
-
* const api = new API();
|
|
96
|
-
* const params = {
|
|
97
|
-
* userId: '55e4a3bd6be6b45210833fae',
|
|
98
|
-
* oldPassword: '123456',
|
|
99
|
-
* newPassword: '123456789'
|
|
100
|
-
* };
|
|
101
|
-
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
102
|
-
* await api.admin.user.findByIdAndUpdatePassword(params, session);
|
|
103
|
-
*/
|
|
104
|
-
async findByIdAndUpdatePassword(params, session) {
|
|
105
|
-
const self = this;
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
Joi.assert(params, Joi.object().required());
|
|
109
|
-
Joi.assert(params.userId, Joi.string().required());
|
|
110
|
-
Joi.assert(params.oldPassword, Joi.string().required());
|
|
111
|
-
Joi.assert(params.newPassword, Joi.string().required());
|
|
112
|
-
Joi.assert(session, Joi.string().required());
|
|
113
|
-
|
|
114
|
-
const {userId, ...payload} = params;
|
|
115
|
-
const apiCall = self.client.put(`/admin/users/${userId}/password`, payload, self._setHeader(session));
|
|
116
|
-
return self._returnData(await apiCall);
|
|
117
|
-
} catch (ex) {
|
|
118
|
-
throw ex;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* @author Thiago Anselmo <thiagoo.anselmoo@gmail.com>
|
|
124
|
-
* @description Check if email is unique
|
|
125
|
-
* @param {string} email Check if email is unique
|
|
126
|
-
* @param {string} session Is token JWT
|
|
127
|
-
* @public
|
|
128
|
-
* @async
|
|
129
|
-
* @example
|
|
130
|
-
*
|
|
131
|
-
* const API = require('@docbrasil/api-systemmanager');
|
|
132
|
-
* const api = new API();
|
|
133
|
-
* const email = 'ana.silva@gmail.com';
|
|
134
|
-
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
135
|
-
* await api.admin.user.emailExist(email, session);
|
|
136
|
-
*/
|
|
137
|
-
async emailExist(email, session) {
|
|
138
|
-
const self = this;
|
|
139
|
-
|
|
140
|
-
try {
|
|
141
|
-
Joi.assert(email, Joi.string().email().required());
|
|
142
|
-
Joi.assert(session, Joi.string().required());
|
|
143
|
-
|
|
144
|
-
const payload = {email};
|
|
145
|
-
const apiCall = self.client.post(`/admin/users/email/exist`, payload, self._setHeader(session));
|
|
146
|
-
return self._returnData(await apiCall);
|
|
147
|
-
} catch (ex) {
|
|
148
|
-
throw ex;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* @description update userData by userSMId
|
|
154
|
-
* @param {string} userId - User SM Id
|
|
155
|
-
* @param {object} payload - Payload to update
|
|
156
|
-
* @param {string} session - Is token JWT
|
|
157
|
-
* @returns {Promise<*>}
|
|
158
|
-
* @async
|
|
159
|
-
* @public
|
|
160
|
-
* @example
|
|
161
|
-
*
|
|
162
|
-
* const userId = '55e4a3bd6be6b45210833fae';
|
|
163
|
-
* const payload = {
|
|
164
|
-
* name: 'Maria joaquina',
|
|
165
|
-
* email: 'maria@gmail.com'
|
|
166
|
-
* };
|
|
167
|
-
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
168
|
-
*/
|
|
169
|
-
async findByIdAndUpdate(userId, payload, session) {
|
|
170
|
-
const self = this;
|
|
171
|
-
|
|
172
|
-
try {
|
|
173
|
-
Joi.assert(userId, Joi.string().required(), 'User id of SM');
|
|
174
|
-
Joi.assert(payload, Joi.object().required(), 'Payload to update');
|
|
175
|
-
Joi.assert(session, Joi.string().required(), 'Session user admin');
|
|
176
|
-
|
|
177
|
-
const apiCall = self.client.put(`/admin/users/${userId}`, payload, self._setHeader(session));
|
|
178
|
-
return self._returnData(await apiCall);
|
|
179
|
-
} catch (ex) {
|
|
180
|
-
throw ex;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export default AdminUser;
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import Boom from '@hapi/boom';
|
|
3
|
+
import Joi from 'joi';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Admin Class for user, permission admin
|
|
7
|
+
* @class
|
|
8
|
+
*/
|
|
9
|
+
class AdminUser {
|
|
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 CloudBrasil <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
|
+
/**
|
|
51
|
+
* @author CloudBrasil <abernardo.br@gmail.com>
|
|
52
|
+
* @description Request profile by userId
|
|
53
|
+
* @param {string} userId User identifier (_id database)
|
|
54
|
+
* @param {string} session Is token JWT
|
|
55
|
+
* @return {Promise}
|
|
56
|
+
* @public
|
|
57
|
+
* @async
|
|
58
|
+
* @example
|
|
59
|
+
*
|
|
60
|
+
* const API = require('@docbrasil/api-systemmanager');
|
|
61
|
+
* const api = new API();
|
|
62
|
+
* const userId = '55e4a3bd6be6b45210833fae';
|
|
63
|
+
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
64
|
+
* await api.admin.user.findById(userId, session);
|
|
65
|
+
*/
|
|
66
|
+
async findById(userId, session) {
|
|
67
|
+
const self = this;
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
|
|
71
|
+
Joi.assert(userId, Joi.string().required());
|
|
72
|
+
Joi.assert(session, Joi.string().required());
|
|
73
|
+
|
|
74
|
+
const apiCall = self.client.get(`/admin/users/${userId}`, self._setHeader(session));
|
|
75
|
+
return self._returnData(await apiCall);
|
|
76
|
+
} catch (ex) {
|
|
77
|
+
throw ex;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @author CloudBrasil <abernardo.br@gmail.com>
|
|
83
|
+
* @description Update password by userId
|
|
84
|
+
* @param {object} params Params to update password
|
|
85
|
+
* @param {string} params.userId Id of the user
|
|
86
|
+
* @param {string} params.oldPassword Old password
|
|
87
|
+
* @param {string} params.newPassword New password
|
|
88
|
+
* @param {string} session Is token JWT
|
|
89
|
+
* @return {Promise<unknown>}
|
|
90
|
+
* @public
|
|
91
|
+
* @async
|
|
92
|
+
* @example
|
|
93
|
+
*
|
|
94
|
+
* const API = require('@docbrasil/api-systemmanager');
|
|
95
|
+
* const api = new API();
|
|
96
|
+
* const params = {
|
|
97
|
+
* userId: '55e4a3bd6be6b45210833fae',
|
|
98
|
+
* oldPassword: '123456',
|
|
99
|
+
* newPassword: '123456789'
|
|
100
|
+
* };
|
|
101
|
+
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
102
|
+
* await api.admin.user.findByIdAndUpdatePassword(params, session);
|
|
103
|
+
*/
|
|
104
|
+
async findByIdAndUpdatePassword(params, session) {
|
|
105
|
+
const self = this;
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
Joi.assert(params, Joi.object().required());
|
|
109
|
+
Joi.assert(params.userId, Joi.string().required());
|
|
110
|
+
Joi.assert(params.oldPassword, Joi.string().required());
|
|
111
|
+
Joi.assert(params.newPassword, Joi.string().required());
|
|
112
|
+
Joi.assert(session, Joi.string().required());
|
|
113
|
+
|
|
114
|
+
const {userId, ...payload} = params;
|
|
115
|
+
const apiCall = self.client.put(`/admin/users/${userId}/password`, payload, self._setHeader(session));
|
|
116
|
+
return self._returnData(await apiCall);
|
|
117
|
+
} catch (ex) {
|
|
118
|
+
throw ex;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @author Thiago Anselmo <thiagoo.anselmoo@gmail.com>
|
|
124
|
+
* @description Check if email is unique
|
|
125
|
+
* @param {string} email Check if email is unique
|
|
126
|
+
* @param {string} session Is token JWT
|
|
127
|
+
* @public
|
|
128
|
+
* @async
|
|
129
|
+
* @example
|
|
130
|
+
*
|
|
131
|
+
* const API = require('@docbrasil/api-systemmanager');
|
|
132
|
+
* const api = new API();
|
|
133
|
+
* const email = 'ana.silva@gmail.com';
|
|
134
|
+
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
135
|
+
* await api.admin.user.emailExist(email, session);
|
|
136
|
+
*/
|
|
137
|
+
async emailExist(email, session) {
|
|
138
|
+
const self = this;
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
Joi.assert(email, Joi.string().email().required());
|
|
142
|
+
Joi.assert(session, Joi.string().required());
|
|
143
|
+
|
|
144
|
+
const payload = {email};
|
|
145
|
+
const apiCall = self.client.post(`/admin/users/email/exist`, payload, self._setHeader(session));
|
|
146
|
+
return self._returnData(await apiCall);
|
|
147
|
+
} catch (ex) {
|
|
148
|
+
throw ex;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @description update userData by userSMId
|
|
154
|
+
* @param {string} userId - User SM Id
|
|
155
|
+
* @param {object} payload - Payload to update
|
|
156
|
+
* @param {string} session - Is token JWT
|
|
157
|
+
* @returns {Promise<*>}
|
|
158
|
+
* @async
|
|
159
|
+
* @public
|
|
160
|
+
* @example
|
|
161
|
+
*
|
|
162
|
+
* const userId = '55e4a3bd6be6b45210833fae';
|
|
163
|
+
* const payload = {
|
|
164
|
+
* name: 'Maria joaquina',
|
|
165
|
+
* email: 'maria@gmail.com'
|
|
166
|
+
* };
|
|
167
|
+
* const session = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
|
|
168
|
+
*/
|
|
169
|
+
async findByIdAndUpdate(userId, payload, session) {
|
|
170
|
+
const self = this;
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
Joi.assert(userId, Joi.string().required(), 'User id of SM');
|
|
174
|
+
Joi.assert(payload, Joi.object().required(), 'Payload to update');
|
|
175
|
+
Joi.assert(session, Joi.string().required(), 'Session user admin');
|
|
176
|
+
|
|
177
|
+
const apiCall = self.client.put(`/admin/users/${userId}`, payload, self._setHeader(session));
|
|
178
|
+
return self._returnData(await apiCall);
|
|
179
|
+
} catch (ex) {
|
|
180
|
+
throw ex;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export default AdminUser;
|