@docbrasil/api-systemmanager 1.0.65 → 1.0.66

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/user/index.js CHANGED
@@ -7,6 +7,7 @@ import Process from './process';
7
7
  import Task from './task';
8
8
  import User from './user';
9
9
  import Register from './register';
10
+ import Notification from './notification';
10
11
 
11
12
  /**
12
13
  * @class API request, user permission level
@@ -29,6 +30,7 @@ class Users {
29
30
  self.task = new Task(options);
30
31
  self.user = self.profile = new User(options);
31
32
  self.register = new Register(options);
33
+ self.notification = new Notification(options);
32
34
  }
33
35
  }
34
36
 
@@ -0,0 +1,173 @@
1
+ import _ from 'lodash';
2
+ import Boom from '@hapi/boom';
3
+ import Joi from 'joi';
4
+ import Cypher from '../utils/cypher';
5
+
6
+ /**
7
+ * Class for user registration in a user
8
+ * @class
9
+ */
10
+ class Register {
11
+
12
+ constructor(options) {
13
+ Joi.assert(options, Joi.object().required());
14
+ Joi.assert(options.parent, Joi.object().required());
15
+
16
+ const self = this;
17
+ self.parent = options.parent;
18
+ self._client = self.parent.dispatch.getClient();
19
+ }
20
+
21
+ /**
22
+ * @author Augusto Pissarra <abernardo.br@gmail.com>
23
+ * @description Get the return data and check for errors
24
+ * @param {object} retData Response HTTP
25
+ * @return {*}
26
+ * @private
27
+ */
28
+ _returnData(retData, def = {}) {
29
+ if (retData.status !== 200) {
30
+ return Boom.badRequest(_.get(retData, 'message', 'No error message reported!'))
31
+ } else {
32
+ return _.get(retData, 'data', def);
33
+ }
34
+ }
35
+
36
+ /**
37
+ * @author CloudBrasil <abernardo.br@gmail.com>
38
+ * @description Set header with new session
39
+ * @param {string} session Session, token JWT
40
+ * @return {object} header with new session
41
+ * @private
42
+ */
43
+ _setHeader(session) {
44
+ return {
45
+ headers: {
46
+ authorization: session,
47
+ }
48
+ };
49
+ }
50
+
51
+
52
+ /**
53
+ * @author CloudBrasil <abernardo.br@gmail.com>
54
+ * @description Method to add a notification token
55
+ * @param {object} params Params to add notification token
56
+ * @param {string} params.token The token
57
+ * @param {object} params.type The token type
58
+ * @returns {promise<object>} data
59
+ * @returns {boolean} data._id the id of the added token
60
+ * @public
61
+ * @example
62
+ *
63
+ * const API = require('@docbrasil/api-systemmanager');
64
+ * const api = new API();
65
+ * const params = {
66
+ * token: 'V6OSBr4aEVoiE9H1b4xzLe+vqmXB+ShVNc/FvJGxnIz4tZv6jBJkk4aQzz2',
67
+ * type: 'FCM_WEB'
68
+ * };
69
+ * const retData = await api.user.notification.addToken(params);
70
+ */
71
+ async addToken(params = {}) {
72
+ const self = this;
73
+
74
+ try {
75
+ Joi.assert(params, Joi.object().required(), 'Params to get task');
76
+ Joi.assert(params.token, Joi.string().required(), 'Token is required');
77
+ Joi.assert(params.type, Joi.string().required(), ' The token type');
78
+
79
+ const apiCall = self._client
80
+ .put(`/users/notifications/token`, params);
81
+
82
+ const retData = self._returnData(await apiCall);
83
+ return retData;
84
+ } catch (ex) {
85
+ throw ex;
86
+ }
87
+ }
88
+
89
+ /**
90
+ * @author CloudBrasil <abernardo.br@gmail.com>
91
+ * @description Method to register a user
92
+ * @param {object} params Params to get task
93
+ * @param {string} params.registerId The registerId that comes with the registration page context
94
+ * @param {string} params.type=sign The type of the registration. By defailt,
95
+ * @param {boolean} params.login=false If we want to login the user directly after registering the user successfully. If you have a redirect, the best option is to login automatically.
96
+ * @param {object} params.emailInfo The information for the email validation
97
+ * @param {string} params.emailInfo.email The email validation information
98
+ * @param {string} params.emailInfo.code The 4 digit code to validate the email
99
+ * @param {object} params.registerData The registration data
100
+ * @param {string} params.registerData.name The name if the user
101
+ * @param {string} params.registerData.registerEmail The email of the user
102
+ * @param {string} params.registerData.phone The phone of the user
103
+ * @param {string} params.registerData.idcard The ID card of the user
104
+ * @param {string} params.registerData.registerPassword The user password in open text
105
+ * @param {string} params.registerData.emailValidationCode The code used to validate the email
106
+ * @param {string} params.registerData.phoneValidationCode The code used to validate the phone
107
+ * @param {string} params.registerData.language The defaulf navigator language (i.e.: navigator.language)
108
+ * @param {string} params.registerData.timezone The defaulf navigator timezone (i.e.: Intl.DateTimeFormat().resolvedOptions().timeZone)
109
+ * @returns {promise<object>} data
110
+ * @returns {boolean} data.success If the operation was successfully done (true|false)
111
+ * @returns {boolean} data.userAlreadyExists If the user already exists (true|false), if true, then the other information is not returned
112
+ * @returns {object} auth The full authentication data with session, if login is true.
113
+ * @returns {string} auth.redirectUrl The url to redirect.
114
+ * @public
115
+ * @example
116
+ *
117
+ * const API = require('@docbrasil/api-systemmanager');
118
+ * const api = new API();
119
+ * const params ={
120
+ * "registerId": 'U2FsdGVkX1+xEq+sV6OSBr4aEVoiE9H1b4xzLe+vqmXB+ShVNc/FvJGxnIz4tZv6jBJkk4aQzz24O5koH+rGmdl/DjqfyWfENe5NFuQ+6xXhuOSN24Z+Topo87+e+CrRO8ox...',
121
+ * "type": 'sign',
122
+ * "login": false,
123
+ * "emailInfo": {
124
+ * "code": "5974",
125
+ * "email": "cbtoto_1@mailinator.com"
126
+ * },
127
+ * "registerData": {
128
+ * "name": "Augusto Totlo",
129
+ * "registerEmail": "cbtoto_1@mailinator.com",
130
+ * "phone": "",
131
+ * "idcard": "",
132
+ * "dob": "1978-01-12T03:00:00.000Z",
133
+ * "registerPassword": "123456",
134
+ * "emailValidationCode": "5974",
135
+ * "phoneValidationCode": "",
136
+ * "language": "en-US",
137
+ * "timezone": "Europe/Dublin"
138
+ * }
139
+ * };
140
+ * const retData = await api.user.register.execute(params);
141
+ */
142
+ async execute(params = {}) {
143
+ const self = this;
144
+
145
+ try {
146
+ Joi.assert(params, Joi.object().required(), 'Params to get task');
147
+ Joi.assert(params.registerId, Joi.string().required(), ' RegisterId for registration');
148
+ Joi.assert(params.emailInfo, Joi.object().required(), ' The email info');
149
+ Joi.assert(params.registerData, Joi.object().required(), ' The registerData');
150
+
151
+ const {
152
+ type = 'sign',
153
+ registerId = '',
154
+ emailInfo = {},
155
+ registerData = {},
156
+ login = false
157
+ } = params;
158
+ const registerInfo = Cypher.get(registerId) || {};
159
+ const payload = { ...registerInfo, type, login, emailInfo, registerData };
160
+ const payloadInfo = { info: Cypher.set(payload) };
161
+ const apiCall = self._client
162
+ .put(`/users/register`, payloadInfo);
163
+
164
+ const { success = false, userAlreadyExists = false, auth } = self._returnData(await apiCall);
165
+ const retData = { success, userAlreadyExists, auth };
166
+ return retData;
167
+ } catch (ex) {
168
+ throw ex;
169
+ }
170
+ }
171
+ }
172
+
173
+ export default Register;
package/dist/bundle.cjs CHANGED
@@ -8675,7 +8675,7 @@ const cypher = new Cypher();
8675
8675
  * Class for user registration in a user
8676
8676
  * @class
8677
8677
  */
8678
- class Register {
8678
+ class Register$1 {
8679
8679
 
8680
8680
  constructor(options) {
8681
8681
  Joi__default["default"].assert(options, Joi__default["default"].object().required());
@@ -8870,6 +8870,173 @@ class Register {
8870
8870
  }
8871
8871
  }
8872
8872
 
8873
+ /**
8874
+ * Class for user registration in a user
8875
+ * @class
8876
+ */
8877
+ class Register {
8878
+
8879
+ constructor(options) {
8880
+ Joi__default["default"].assert(options, Joi__default["default"].object().required());
8881
+ Joi__default["default"].assert(options.parent, Joi__default["default"].object().required());
8882
+
8883
+ const self = this;
8884
+ self.parent = options.parent;
8885
+ self._client = self.parent.dispatch.getClient();
8886
+ }
8887
+
8888
+ /**
8889
+ * @author Augusto Pissarra <abernardo.br@gmail.com>
8890
+ * @description Get the return data and check for errors
8891
+ * @param {object} retData Response HTTP
8892
+ * @return {*}
8893
+ * @private
8894
+ */
8895
+ _returnData(retData, def = {}) {
8896
+ if (retData.status !== 200) {
8897
+ return Boom__default["default"].badRequest(___default["default"].get(retData, 'message', 'No error message reported!'))
8898
+ } else {
8899
+ return ___default["default"].get(retData, 'data', def);
8900
+ }
8901
+ }
8902
+
8903
+ /**
8904
+ * @author CloudBrasil <abernardo.br@gmail.com>
8905
+ * @description Set header with new session
8906
+ * @param {string} session Session, token JWT
8907
+ * @return {object} header with new session
8908
+ * @private
8909
+ */
8910
+ _setHeader(session) {
8911
+ return {
8912
+ headers: {
8913
+ authorization: session,
8914
+ }
8915
+ };
8916
+ }
8917
+
8918
+
8919
+ /**
8920
+ * @author CloudBrasil <abernardo.br@gmail.com>
8921
+ * @description Method to add a notification token
8922
+ * @param {object} params Params to add notification token
8923
+ * @param {string} params.token The token
8924
+ * @param {object} params.type The token type
8925
+ * @returns {promise<object>} data
8926
+ * @returns {boolean} data._id the id of the added token
8927
+ * @public
8928
+ * @example
8929
+ *
8930
+ * const API = require('@docbrasil/api-systemmanager');
8931
+ * const api = new API();
8932
+ * const params = {
8933
+ * token: 'V6OSBr4aEVoiE9H1b4xzLe+vqmXB+ShVNc/FvJGxnIz4tZv6jBJkk4aQzz2',
8934
+ * type: 'FCM_WEB'
8935
+ * };
8936
+ * const retData = await api.user.notification.addToken(params);
8937
+ */
8938
+ async addToken(params = {}) {
8939
+ const self = this;
8940
+
8941
+ try {
8942
+ Joi__default["default"].assert(params, Joi__default["default"].object().required(), 'Params to get task');
8943
+ Joi__default["default"].assert(params.token, Joi__default["default"].string().required(), 'Token is required');
8944
+ Joi__default["default"].assert(params.type, Joi__default["default"].string().required(), ' The token type');
8945
+
8946
+ const apiCall = self._client
8947
+ .put(`/users/notifications/token`, params);
8948
+
8949
+ const retData = self._returnData(await apiCall);
8950
+ return retData;
8951
+ } catch (ex) {
8952
+ throw ex;
8953
+ }
8954
+ }
8955
+
8956
+ /**
8957
+ * @author CloudBrasil <abernardo.br@gmail.com>
8958
+ * @description Method to register a user
8959
+ * @param {object} params Params to get task
8960
+ * @param {string} params.registerId The registerId that comes with the registration page context
8961
+ * @param {string} params.type=sign The type of the registration. By defailt,
8962
+ * @param {boolean} params.login=false If we want to login the user directly after registering the user successfully. If you have a redirect, the best option is to login automatically.
8963
+ * @param {object} params.emailInfo The information for the email validation
8964
+ * @param {string} params.emailInfo.email The email validation information
8965
+ * @param {string} params.emailInfo.code The 4 digit code to validate the email
8966
+ * @param {object} params.registerData The registration data
8967
+ * @param {string} params.registerData.name The name if the user
8968
+ * @param {string} params.registerData.registerEmail The email of the user
8969
+ * @param {string} params.registerData.phone The phone of the user
8970
+ * @param {string} params.registerData.idcard The ID card of the user
8971
+ * @param {string} params.registerData.registerPassword The user password in open text
8972
+ * @param {string} params.registerData.emailValidationCode The code used to validate the email
8973
+ * @param {string} params.registerData.phoneValidationCode The code used to validate the phone
8974
+ * @param {string} params.registerData.language The defaulf navigator language (i.e.: navigator.language)
8975
+ * @param {string} params.registerData.timezone The defaulf navigator timezone (i.e.: Intl.DateTimeFormat().resolvedOptions().timeZone)
8976
+ * @returns {promise<object>} data
8977
+ * @returns {boolean} data.success If the operation was successfully done (true|false)
8978
+ * @returns {boolean} data.userAlreadyExists If the user already exists (true|false), if true, then the other information is not returned
8979
+ * @returns {object} auth The full authentication data with session, if login is true.
8980
+ * @returns {string} auth.redirectUrl The url to redirect.
8981
+ * @public
8982
+ * @example
8983
+ *
8984
+ * const API = require('@docbrasil/api-systemmanager');
8985
+ * const api = new API();
8986
+ * const params ={
8987
+ * "registerId": 'U2FsdGVkX1+xEq+sV6OSBr4aEVoiE9H1b4xzLe+vqmXB+ShVNc/FvJGxnIz4tZv6jBJkk4aQzz24O5koH+rGmdl/DjqfyWfENe5NFuQ+6xXhuOSN24Z+Topo87+e+CrRO8ox...',
8988
+ * "type": 'sign',
8989
+ * "login": false,
8990
+ * "emailInfo": {
8991
+ * "code": "5974",
8992
+ * "email": "cbtoto_1@mailinator.com"
8993
+ * },
8994
+ * "registerData": {
8995
+ * "name": "Augusto Totlo",
8996
+ * "registerEmail": "cbtoto_1@mailinator.com",
8997
+ * "phone": "",
8998
+ * "idcard": "",
8999
+ * "dob": "1978-01-12T03:00:00.000Z",
9000
+ * "registerPassword": "123456",
9001
+ * "emailValidationCode": "5974",
9002
+ * "phoneValidationCode": "",
9003
+ * "language": "en-US",
9004
+ * "timezone": "Europe/Dublin"
9005
+ * }
9006
+ * };
9007
+ * const retData = await api.user.register.execute(params);
9008
+ */
9009
+ async execute(params = {}) {
9010
+ const self = this;
9011
+
9012
+ try {
9013
+ Joi__default["default"].assert(params, Joi__default["default"].object().required(), 'Params to get task');
9014
+ Joi__default["default"].assert(params.registerId, Joi__default["default"].string().required(), ' RegisterId for registration');
9015
+ Joi__default["default"].assert(params.emailInfo, Joi__default["default"].object().required(), ' The email info');
9016
+ Joi__default["default"].assert(params.registerData, Joi__default["default"].object().required(), ' The registerData');
9017
+
9018
+ const {
9019
+ type = 'sign',
9020
+ registerId = '',
9021
+ emailInfo = {},
9022
+ registerData = {},
9023
+ login = false
9024
+ } = params;
9025
+ const registerInfo = cypher.get(registerId) || {};
9026
+ const payload = { ...registerInfo, type, login, emailInfo, registerData };
9027
+ const payloadInfo = { info: cypher.set(payload) };
9028
+ const apiCall = self._client
9029
+ .put(`/users/register`, payloadInfo);
9030
+
9031
+ const { success = false, userAlreadyExists = false, auth } = self._returnData(await apiCall);
9032
+ const retData = { success, userAlreadyExists, auth };
9033
+ return retData;
9034
+ } catch (ex) {
9035
+ throw ex;
9036
+ }
9037
+ }
9038
+ }
9039
+
8873
9040
  /**
8874
9041
  * @class API request, user permission level
8875
9042
  */
@@ -8890,7 +9057,8 @@ class Users {
8890
9057
  self.process = new Process(options);
8891
9058
  self.task = new Task(options);
8892
9059
  self.user = self.profile = new User(options);
8893
- self.register = new Register(options);
9060
+ self.register = new Register$1(options);
9061
+ self.notification = new Register(options);
8894
9062
  }
8895
9063
  }
8896
9064