zocrmsdkmiblu 0.2.7 → 0.2.8

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.
@@ -1,349 +1,349 @@
1
-
2
- var OAuth = require('./OAuth');
3
- var path = require("path");
4
- var { MIB, LU, PROYECT_DOMAIN } = require('./config');
5
-
6
-
7
- var persistenceModule = path.dirname(__filename) + "/mysql/mysql_util";
8
- var iamurl = "accounts.zoho.com";
9
- var baseURL = "www.zohoapis.com";
10
- var version = "v4";
11
- var mysql_username = process.env.MYSQL_USER;
12
- var mysql_password = process.env.MYSQL_PASSWORD;
13
- var default_user_identifier = "zcrm_default_user";
14
-
15
- if (PROYECT_DOMAIN.includes(LU)) {
16
- var client_id = process.env.CLIENT_ID_CRM_LU;
17
- var client_secret = process.env.CLIENT_SECRET_CRM_LU;
18
- var redirect_url = process.env.REDIRECT_URL_LU;
19
- var user_identifier = process.env.USER_IDENTIFIER_CRM_LU;
20
- }
21
-
22
- if (PROYECT_DOMAIN.includes(MIB)) {
23
- var client_id = process.env.CLIENT_ID_CRM_MIB;
24
- var client_secret = process.env.CLIENT_SECRET_CRM_MIB;
25
- var redirect_url = process.env.REDIRECT_URL_MIB;
26
- var user_identifier = process.env.USER_IDENTIFIER_CRM_MIB;
27
- }
28
- var ZCRMRestClient = function () { };
29
-
30
- ZCRMRestClient.initialize = function (configJSON) {
31
-
32
-
33
- return new Promise(function (resolve, reject) {
34
-
35
- if (configJSON) {
36
-
37
- ZCRMRestClient.initializeWithValues(configJSON);
38
- resolve();
39
-
40
- }
41
-
42
- var PropertiesReader = require('properties-reader');
43
- var properties = PropertiesReader('resources/oauth_configuration.properties');
44
- var client_id = properties.get('zoho.crm.clientid');
45
- var client_secret = properties.get('zoho.crm.clientsecret');
46
- var redirect_url = properties.get('zoho.crm.redirecturl');
47
- var iam_url = properties.get('zoho.crm.iamurl') ? properties.get('zoho.crm.iamurl') : iamurl;
48
-
49
- var config_properties = PropertiesReader('resources/configuration.properties');
50
-
51
- persistenceModule = config_properties.get('crm.api.tokenmanagement') ? config_properties.get('crm.api.tokenmanagement') : persistenceModule;
52
-
53
- baseURL = config_properties.get('crm.api.url') ? config_properties.get('crm.api.url') : baseURL;
54
-
55
- mysql_username = config_properties.get('mysql.username') ? config_properties.get('mysql.username') : mysql_username;
56
-
57
- mysql_password = config_properties.get('mysql.password') ? config_properties.get('mysql.password') : mysql_password;
58
-
59
-
60
- if (config_properties.get('crm.api.user_identifier')) {
61
-
62
- ZCRMRestClient.setUserIdentifier(config_properties.get('crm.api.user_identifier'));
63
- }
64
- else {
65
-
66
- ZCRMRestClient.setUserIdentifier(default_user_identifier);
67
- }
68
-
69
- if (!client_id || !client_secret || !redirect_url) {
70
-
71
- throw new Error("Populate the oauth_configuration.properties file");
72
-
73
- }
74
-
75
- ZCRMRestClient.setClientId(client_id);
76
- ZCRMRestClient.setClientSecret(client_secret);
77
- ZCRMRestClient.setRedirectURL(redirect_url);
78
- ZCRMRestClient.setIAMUrl(iam_url);
79
-
80
-
81
- resolve();
82
-
83
- })
84
- }
85
-
86
- ZCRMRestClient.initializeWithValues = function (configJSON) {
87
-
88
- var mandatory_values = ['client_id', 'client_secret', 'redirect_url']
89
- mandatory_values.forEach(function (key) {
90
- if (!configJSON[key]) {
91
- throw new Error('Missing configuration for Zoho OAuth service: ' + key);
92
- }
93
- })
94
- var client_id = configJSON.client_id;
95
- var client_secret = configJSON.client_secret;
96
- var redirect_url = configJSON.redirect_url;
97
- var iam_url = configJSON.iamurl ? configJSON.iamurl : iamurl;
98
- var useridentifier = configJSON.user_identifier ? configJSON.user_identifier : default_user_identifier;
99
-
100
- mysql_username = configJSON.mysql_username ? configJSON.mysql_username : mysql_username;
101
- mysql_password = configJSON.mysql_password ? configJSON.mysql_password : mysql_password;
102
- baseURL = configJSON.base_url ? configJSON.base_url : baseURL;
103
- version = configJSON.version ? configJSON.version : version;
104
- persistenceModule = configJSON.tokenmanagement ? configJSON.tokenmanagement : persistenceModule;
105
-
106
- ZCRMRestClient.setClientId(client_id);
107
- ZCRMRestClient.setClientSecret(client_secret);
108
- ZCRMRestClient.setRedirectURL(redirect_url);
109
- ZCRMRestClient.setIAMUrl(iam_url);
110
- ZCRMRestClient.setUserIdentifier(useridentifier);
111
-
112
- }
113
-
114
- ZCRMRestClient.generateAuthTokens = function (user_identifier, grant_token) {
115
-
116
- return new Promise(function (resolve, reject) {
117
-
118
- if (!user_identifier) {
119
-
120
- user_identifier = ZCRMRestClient.getUserIdentifier();
121
- }
122
-
123
- var config = ZCRMRestClient.getConfig(grant_token);
124
- new OAuth(config, "generate_token");
125
- var api_url = OAuth.constructurl("generate_token");
126
-
127
- OAuth.generateTokens(api_url).then(function (response) {
128
-
129
- if (response.statusCode != 200) {
130
-
131
- throw new Error("Problem occured while generating access token from grant token. Response : " + JSON.stringify(response));
132
-
133
- }
134
-
135
- var persistence_module = require(persistenceModule);
136
- var resultObj = ZCRMRestClient.parseAndConstructObject(response);
137
- resultObj.user_identifier = user_identifier;
138
-
139
- if (resultObj.access_token) {
140
-
141
- persistence_module.saveOAuthTokens(resultObj).then(function (save_resp) {
142
-
143
- ZCRMRestClient.setUserIdentifier(user_identifier),
144
-
145
- resolve(resultObj)
146
-
147
- }
148
-
149
- );
150
- }
151
- else {
152
-
153
- throw new Error("Problem occured while generating access token and refresh token from grant token.Response : " + JSON.stringify(response));
154
- }
155
- })
156
- })
157
- }
158
-
159
-
160
- ZCRMRestClient.generateAuthTokenfromRefreshToken = function (user_identifier, refresh_token) {
161
-
162
- return new Promise(function (resolve, reject) {
163
-
164
- if (!user_identifier) {
165
-
166
- user_identifier = ZCRMRestClient.getUserIdentifier();
167
- }
168
-
169
- var config = ZCRMRestClient.getConfig_refresh(refresh_token);
170
- new OAuth(config, "refresh_access_token");
171
- var api_url = OAuth.constructurl("generate_token");
172
-
173
- OAuth.generateTokens(api_url).then(function (response) {
174
-
175
- if (response.statusCode != 200) {
176
-
177
- throw new Error("Problem occured while generating access token from refresh token . Response : " + JSON.stringify(response));
178
-
179
- }
180
- var persistence_module = require(persistenceModule);
181
- var resultObj = ZCRMRestClient.parseAndConstructObject(response);
182
-
183
- resultObj.user_identifier = user_identifier;
184
- resultObj.refresh_token = refresh_token;
185
-
186
- if (resultObj.access_token) {
187
-
188
- persistence_module.saveOAuthTokens(resultObj).then(function (save_response) {
189
-
190
- ZCRMRestClient.setUserIdentifier(user_identifier),
191
- resolve(resultObj)
192
-
193
- }
194
-
195
- );
196
- }
197
- else {
198
- throw new Error("Problem occured while generating access token from refresh token. Response : " + JSON.stringify(response));
199
-
200
- }
201
-
202
-
203
- })
204
- })
205
- };
206
-
207
-
208
- ZCRMRestClient.getConfig = function (grant_token) {
209
-
210
- var config = {};
211
-
212
- config.client_id = ZCRMRestClient.getClientId();
213
- config.client_secret = ZCRMRestClient.getClientSecret();
214
- config.code = grant_token;
215
- config.redirect_uri = ZCRMRestClient.getRedirectURL();
216
- config.grant_type = "authorization_code";
217
-
218
- return config;
219
-
220
- };
221
-
222
- ZCRMRestClient.getConfig_refresh = function (refresh_token) {
223
-
224
- var config = {};
225
-
226
- config.client_id = ZCRMRestClient.getClientId();
227
- config.client_secret = ZCRMRestClient.getClientSecret();
228
- config.refresh_token = refresh_token;
229
- config.grant_type = "refresh_token";
230
-
231
- return config;
232
-
233
- }
234
-
235
- ZCRMRestClient.setClientId = function (clientid) {
236
-
237
- client_id = clientid;
238
- }
239
-
240
- ZCRMRestClient.setClientSecret = function (clientsecret) {
241
-
242
- client_secret = clientsecret;
243
-
244
- }
245
-
246
- ZCRMRestClient.setRedirectURL = function (redirecturl) {
247
-
248
- redirect_url = redirecturl;
249
- }
250
-
251
- ZCRMRestClient.setUserIdentifier = function (useridentifier) {
252
-
253
- user_identifier = useridentifier;
254
-
255
- }
256
-
257
- ZCRMRestClient.setIAMUrl = function (iam_url) {
258
-
259
- iamurl = iam_url;
260
- }
261
-
262
- ZCRMRestClient.setBaseURL = function (baseurl) {
263
-
264
- baseURL = baseurl;
265
- }
266
-
267
- ZCRMRestClient.getClientId = function () {
268
-
269
- return client_id;
270
-
271
- }
272
-
273
- ZCRMRestClient.getClientSecret = function () {
274
-
275
- return client_secret;
276
-
277
- }
278
-
279
- ZCRMRestClient.getRedirectURL = function () {
280
-
281
- return redirect_url;
282
- }
283
-
284
- ZCRMRestClient.getUserIdentifier = function () {
285
-
286
- if (!user_identifier) {
287
-
288
- return default_user_identifier;
289
- }
290
- return user_identifier;
291
- }
292
-
293
- ZCRMRestClient.getPersistenceModule = function () {
294
-
295
- return persistenceModule;
296
- }
297
-
298
- ZCRMRestClient.getAPIURL = function () {
299
-
300
- return baseURL;
301
- }
302
-
303
- ZCRMRestClient.getVersion = function (allrecord) {
304
-
305
- return version;
306
- }
307
- ZCRMRestClient.getIAMUrl = function () {
308
-
309
- return iamurl;
310
- }
311
- ZCRMRestClient.getMySQLUserName = function () {
312
-
313
- return mysql_username;
314
- }
315
- ZCRMRestClient.getMYSQLPassword = function () {
316
-
317
- return mysql_password;
318
-
319
- }
320
- ZCRMRestClient.parseAndConstructObject = function (response) {
321
-
322
- var body = response["body"];
323
- body = JSON.parse(body);
324
-
325
- var date = new Date();
326
- var current_time = date.getTime();
327
-
328
- var resultObj = {};
329
-
330
- if (body.access_token) {
331
-
332
- resultObj.access_token = body.access_token;
333
-
334
- if (body.refresh_token) {
335
-
336
- resultObj.refresh_token = body.refresh_token;
337
- }
338
- if (!body.expires_in_sec) {
339
- body.expires_in = body.expires_in * 1000;
340
- }
341
- resultObj.expires_in = body.expires_in + current_time;
342
- }
343
- return resultObj;
344
-
345
- }
346
-
347
- ZCRMRestClient.API = require('./crmapi');
348
-
1
+
2
+ var OAuth = require('./OAuth');
3
+ var path = require("path");
4
+ var { MIB, LU, PROYECT_DOMAIN } = require('./config');
5
+
6
+
7
+ var persistenceModule = path.dirname(__filename) + "/mysql/mysql_util";
8
+ var iamurl = "accounts.zoho.com";
9
+ var baseURL = "www.zohoapis.com";
10
+ var version = "v4";
11
+ var mysql_username = process.env.MYSQL_USER;
12
+ var mysql_password = process.env.MYSQL_PASSWORD;
13
+ var default_user_identifier = "zcrm_default_user";
14
+
15
+ if (PROYECT_DOMAIN.includes(LU)) {
16
+ var client_id = process.env.CLIENT_ID_CRM_LU;
17
+ var client_secret = process.env.CLIENT_SECRET_CRM_LU;
18
+ var redirect_url = process.env.REDIRECT_URL_LU;
19
+ var user_identifier = process.env.USER_IDENTIFIER_CRM_LU;
20
+ }
21
+
22
+ if (PROYECT_DOMAIN.includes(MIB)) {
23
+ var client_id = process.env.CLIENT_ID_CRM_MIB;
24
+ var client_secret = process.env.CLIENT_SECRET_CRM_MIB;
25
+ var redirect_url = process.env.REDIRECT_URL_MIB;
26
+ var user_identifier = process.env.USER_IDENTIFIER_CRM_MIB;
27
+ }
28
+ var ZCRMRestClient = function () { };
29
+
30
+ ZCRMRestClient.initialize = function (configJSON) {
31
+
32
+
33
+ return new Promise(function (resolve, reject) {
34
+
35
+ if (configJSON) {
36
+
37
+ ZCRMRestClient.initializeWithValues(configJSON);
38
+ resolve();
39
+
40
+ }
41
+
42
+ var PropertiesReader = require('properties-reader');
43
+ var properties = PropertiesReader('resources/oauth_configuration.properties');
44
+ var client_id = properties.get('zoho.crm.clientid');
45
+ var client_secret = properties.get('zoho.crm.clientsecret');
46
+ var redirect_url = properties.get('zoho.crm.redirecturl');
47
+ var iam_url = properties.get('zoho.crm.iamurl') ? properties.get('zoho.crm.iamurl') : iamurl;
48
+
49
+ var config_properties = PropertiesReader('resources/configuration.properties');
50
+
51
+ persistenceModule = config_properties.get('crm.api.tokenmanagement') ? config_properties.get('crm.api.tokenmanagement') : persistenceModule;
52
+
53
+ baseURL = config_properties.get('crm.api.url') ? config_properties.get('crm.api.url') : baseURL;
54
+
55
+ mysql_username = config_properties.get('mysql.username') ? config_properties.get('mysql.username') : mysql_username;
56
+
57
+ mysql_password = config_properties.get('mysql.password') ? config_properties.get('mysql.password') : mysql_password;
58
+
59
+
60
+ if (config_properties.get('crm.api.user_identifier')) {
61
+
62
+ ZCRMRestClient.setUserIdentifier(config_properties.get('crm.api.user_identifier'));
63
+ }
64
+ else {
65
+
66
+ ZCRMRestClient.setUserIdentifier(default_user_identifier);
67
+ }
68
+
69
+ if (!client_id || !client_secret || !redirect_url) {
70
+
71
+ throw new Error("Populate the oauth_configuration.properties file");
72
+
73
+ }
74
+
75
+ ZCRMRestClient.setClientId(client_id);
76
+ ZCRMRestClient.setClientSecret(client_secret);
77
+ ZCRMRestClient.setRedirectURL(redirect_url);
78
+ ZCRMRestClient.setIAMUrl(iam_url);
79
+
80
+
81
+ resolve();
82
+
83
+ })
84
+ }
85
+
86
+ ZCRMRestClient.initializeWithValues = function (configJSON) {
87
+
88
+ var mandatory_values = ['client_id', 'client_secret', 'redirect_url']
89
+ mandatory_values.forEach(function (key) {
90
+ if (!configJSON[key]) {
91
+ throw new Error('Missing configuration for Zoho OAuth service: ' + key);
92
+ }
93
+ })
94
+ var client_id = configJSON.client_id;
95
+ var client_secret = configJSON.client_secret;
96
+ var redirect_url = configJSON.redirect_url;
97
+ var iam_url = configJSON.iamurl ? configJSON.iamurl : iamurl;
98
+ var useridentifier = configJSON.user_identifier ? configJSON.user_identifier : default_user_identifier;
99
+
100
+ mysql_username = configJSON.mysql_username ? configJSON.mysql_username : mysql_username;
101
+ mysql_password = configJSON.mysql_password ? configJSON.mysql_password : mysql_password;
102
+ baseURL = configJSON.base_url ? configJSON.base_url : baseURL;
103
+ version = configJSON.version ? configJSON.version : version;
104
+ persistenceModule = configJSON.tokenmanagement ? configJSON.tokenmanagement : persistenceModule;
105
+
106
+ ZCRMRestClient.setClientId(client_id);
107
+ ZCRMRestClient.setClientSecret(client_secret);
108
+ ZCRMRestClient.setRedirectURL(redirect_url);
109
+ ZCRMRestClient.setIAMUrl(iam_url);
110
+ ZCRMRestClient.setUserIdentifier(useridentifier);
111
+
112
+ }
113
+
114
+ ZCRMRestClient.generateAuthTokens = function (user_identifier, grant_token) {
115
+
116
+ return new Promise(function (resolve, reject) {
117
+
118
+ if (!user_identifier) {
119
+
120
+ user_identifier = ZCRMRestClient.getUserIdentifier();
121
+ }
122
+
123
+ var config = ZCRMRestClient.getConfig(grant_token);
124
+ new OAuth(config, "generate_token");
125
+ var api_url = OAuth.constructurl("generate_token");
126
+
127
+ OAuth.generateTokens(api_url).then(function (response) {
128
+
129
+ if (response.statusCode != 200) {
130
+
131
+ throw new Error("Problem occured while generating access token from grant token. Response : " + JSON.stringify(response));
132
+
133
+ }
134
+
135
+ var persistence_module = require(persistenceModule);
136
+ var resultObj = ZCRMRestClient.parseAndConstructObject(response);
137
+ resultObj.user_identifier = user_identifier;
138
+
139
+ if (resultObj.access_token) {
140
+
141
+ persistence_module.saveOAuthTokens(resultObj).then(function (save_resp) {
142
+
143
+ ZCRMRestClient.setUserIdentifier(user_identifier),
144
+
145
+ resolve(resultObj)
146
+
147
+ }
148
+
149
+ );
150
+ }
151
+ else {
152
+
153
+ throw new Error("Problem occured while generating access token and refresh token from grant token.Response : " + JSON.stringify(response));
154
+ }
155
+ })
156
+ })
157
+ }
158
+
159
+
160
+ ZCRMRestClient.generateAuthTokenfromRefreshToken = function (user_identifier, refresh_token) {
161
+
162
+ return new Promise(function (resolve, reject) {
163
+
164
+ if (!user_identifier) {
165
+
166
+ user_identifier = ZCRMRestClient.getUserIdentifier();
167
+ }
168
+
169
+ var config = ZCRMRestClient.getConfig_refresh(refresh_token);
170
+ new OAuth(config, "refresh_access_token");
171
+ var api_url = OAuth.constructurl("generate_token");
172
+
173
+ OAuth.generateTokens(api_url).then(function (response) {
174
+
175
+ if (response.statusCode != 200) {
176
+
177
+ throw new Error("Problem occured while generating access token from refresh token . Response : " + JSON.stringify(response));
178
+
179
+ }
180
+ var persistence_module = require(persistenceModule);
181
+ var resultObj = ZCRMRestClient.parseAndConstructObject(response);
182
+
183
+ resultObj.user_identifier = user_identifier;
184
+ resultObj.refresh_token = refresh_token;
185
+
186
+ if (resultObj.access_token) {
187
+
188
+ persistence_module.saveOAuthTokens(resultObj).then(function (save_response) {
189
+
190
+ ZCRMRestClient.setUserIdentifier(user_identifier),
191
+ resolve(resultObj)
192
+
193
+ }
194
+
195
+ );
196
+ }
197
+ else {
198
+ throw new Error("Problem occured while generating access token from refresh token. Response : " + JSON.stringify(response));
199
+
200
+ }
201
+
202
+
203
+ })
204
+ })
205
+ };
206
+
207
+
208
+ ZCRMRestClient.getConfig = function (grant_token) {
209
+
210
+ var config = {};
211
+
212
+ config.client_id = ZCRMRestClient.getClientId();
213
+ config.client_secret = ZCRMRestClient.getClientSecret();
214
+ config.code = grant_token;
215
+ config.redirect_uri = ZCRMRestClient.getRedirectURL();
216
+ config.grant_type = "authorization_code";
217
+
218
+ return config;
219
+
220
+ };
221
+
222
+ ZCRMRestClient.getConfig_refresh = function (refresh_token) {
223
+
224
+ var config = {};
225
+
226
+ config.client_id = ZCRMRestClient.getClientId();
227
+ config.client_secret = ZCRMRestClient.getClientSecret();
228
+ config.refresh_token = refresh_token;
229
+ config.grant_type = "refresh_token";
230
+
231
+ return config;
232
+
233
+ }
234
+
235
+ ZCRMRestClient.setClientId = function (clientid) {
236
+
237
+ client_id = clientid;
238
+ }
239
+
240
+ ZCRMRestClient.setClientSecret = function (clientsecret) {
241
+
242
+ client_secret = clientsecret;
243
+
244
+ }
245
+
246
+ ZCRMRestClient.setRedirectURL = function (redirecturl) {
247
+
248
+ redirect_url = redirecturl;
249
+ }
250
+
251
+ ZCRMRestClient.setUserIdentifier = function (useridentifier) {
252
+
253
+ user_identifier = useridentifier;
254
+
255
+ }
256
+
257
+ ZCRMRestClient.setIAMUrl = function (iam_url) {
258
+
259
+ iamurl = iam_url;
260
+ }
261
+
262
+ ZCRMRestClient.setBaseURL = function (baseurl) {
263
+
264
+ baseURL = baseurl;
265
+ }
266
+
267
+ ZCRMRestClient.getClientId = function () {
268
+
269
+ return client_id;
270
+
271
+ }
272
+
273
+ ZCRMRestClient.getClientSecret = function () {
274
+
275
+ return client_secret;
276
+
277
+ }
278
+
279
+ ZCRMRestClient.getRedirectURL = function () {
280
+
281
+ return redirect_url;
282
+ }
283
+
284
+ ZCRMRestClient.getUserIdentifier = function () {
285
+
286
+ if (!user_identifier) {
287
+
288
+ return default_user_identifier;
289
+ }
290
+ return user_identifier;
291
+ }
292
+
293
+ ZCRMRestClient.getPersistenceModule = function () {
294
+
295
+ return persistenceModule;
296
+ }
297
+
298
+ ZCRMRestClient.getAPIURL = function () {
299
+
300
+ return baseURL;
301
+ }
302
+
303
+ ZCRMRestClient.getVersion = function (allrecord) {
304
+
305
+ return version;
306
+ }
307
+ ZCRMRestClient.getIAMUrl = function () {
308
+
309
+ return iamurl;
310
+ }
311
+ ZCRMRestClient.getMySQLUserName = function () {
312
+
313
+ return mysql_username;
314
+ }
315
+ ZCRMRestClient.getMYSQLPassword = function () {
316
+
317
+ return mysql_password;
318
+
319
+ }
320
+ ZCRMRestClient.parseAndConstructObject = function (response) {
321
+
322
+ var body = response["body"];
323
+ body = JSON.parse(body);
324
+
325
+ var date = new Date();
326
+ var current_time = date.getTime();
327
+
328
+ var resultObj = {};
329
+
330
+ if (body.access_token) {
331
+
332
+ resultObj.access_token = body.access_token;
333
+
334
+ if (body.refresh_token) {
335
+
336
+ resultObj.refresh_token = body.refresh_token;
337
+ }
338
+ if (!body.expires_in_sec) {
339
+ body.expires_in = body.expires_in * 1000;
340
+ }
341
+ resultObj.expires_in = body.expires_in + current_time;
342
+ }
343
+ return resultObj;
344
+
345
+ }
346
+
347
+ ZCRMRestClient.API = require('./crmapi');
348
+
349
349
  module.exports = ZCRMRestClient;