forked from Trifoia/zcrm-nodejs-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZCRMRestClient.js
325 lines (269 loc) · 10.5 KB
/
ZCRMRestClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
'use strict';
const PropertiesReader = require('properties-reader');
const OAuth = require('./OAuth');
const STATIC_CONFIG_LOCATIONS = [
'resources/oauth_configuration.properties',
'resources/configuration.properties'
];
const DEFAULTS = {
user_identifier: 'zcrm_default_user',
mysql_module: './mysql/mysql_util',
iamurl: 'accounts.zoho.com',
base_url: 'www.zohoapis.com',
mysql_username: 'root',
mysql_password: ''
};
const LOCAL_MODULE = './local/local_util';
/**
* Static configuration data used to create requests to the ZCRM endpoints and manage access tokens
*/
const data = {
client_id: null,
client_secret: null,
redirect_url: null,
user_identifier: null,
refresh_token: null,
mysql_module: null,
iamurl: null,
base_url: null,
mysql_username: null,
mysql_password: null,
version: 'v2' // The version is always v2
};
const ZCRMRestClient = function() {};
/**
* Globally initializes the Zoho CRM Client. Configurations will be used from the following order, where higher items
* supersede lower items:
* 1. Provided configJSON argument
* 2. Static configuration files in the `resources` directory
* 3. Environment Variables
* 4. Pre-defined defaults (when available)
*
* @param {object} configJSON Configuration object
* @return {Promise} Promise will resolve immediately
*/
ZCRMRestClient.initialize = function(configJSON) {
// 1. Check to see if the config json has anything good
if (typeof configJSON === 'object') {
for (const dataKey in data) {
if (configJSON[dataKey]) data[dataKey] = configJSON[dataKey];
}
}
// 2. Check for the configuration resources
const config = PropertiesReader('');
STATIC_CONFIG_LOCATIONS.forEach((location) => {
try {
config.append(location);
} catch (e) {
// If an error occurs it's probably because the file doesn't exist. That's okay
}
});
data.iamurl = data.iamurl || config.get('zoho.crm.iamurl') || null;
data.client_id = data.client_id || config.get('zoho.crm.client_id') || config.get('zoho.crm.clientid') || null;
data.client_secret = data.client_secret || config.get('zoho.crm.client_secret') || config.get('zoho.crm.clientsecret') || null;
data.redirect_url = data.redirect_url || config.get('zoho.crm.redirect_url') || config.get('zoho.crm.redirecturl') || null;
data.refresh_token = data.refresh_token || config.get('zoho.crm.refresh_token') || config.get('zoho.crm.refreshtoken') || null;
data.base_url = data.base_url || config.get('crm.api.base_url') || config.get('crm.api.url') || null;
data.user_identifier = data.user_identifier || config.get('crm.api.user_identifier') || null;
data.mysql_module = data.mysql_module || config.get('crm.api.mysql_module') || config.get('crm.api.tokenmanagement') || null;
data.mysql_username = data.mysql_username || config.get('mysql.username') || null;
data.mysql_password = data.mysql_password || config.get('mysql.password') || null;
// 3. Check for environment variables
data.iamurl = data.iamurl || process.env.ZCRM__iamurl || null;
data.client_id = data.client_id || process.env.ZCRM__client_id || null;
data.client_secret = data.client_secret || process.env.ZCRM__client_secret || null;
data.redirect_url = data.redirect_url || process.env.ZCRM__redirect_url || null;
data.refresh_token = data.refresh_token || process.env.ZCRM__refresh_token || null;
data.base_url = data.base_url || process.env.ZCRM__base_url || null;
data.user_identifier = data.user_identifier || process.env.ZCRM__user_identifier || null;
data.mysql_module = data.mysql_module || process.env.ZCRM__mysql_module || null;
data.mysql_username = data.mysql_username || process.env.ZCRM__mysql_username || null;
data.mysql_password = data.mysql_password || process.env.ZCRM__mysql_password || null;
// 4. Apply applicable defaults
for (const defaultKey in DEFAULTS) {
if (data[defaultKey] === null) data[defaultKey] = DEFAULTS[defaultKey];
};
// Make sure everything has been given a value
const missingValues = [];
for (const key in data) {
// Skip the refresh token - it is optional
if (key === 'refresh_token') continue;
if (data[key] === null) missingValues.push(key);
}
if (missingValues.length > 0) {
throw new Error(`The following configuration values were not given values: ${missingValues.join(', ')}`);
}
// Check for the local storage option
if (data.mysql_module === 'local') data.mysql_module = LOCAL_MODULE;
return Promise.resolve(ZCRMRestClient);
};
/**
* Helper method resets the client by setting all data values to null
*
* @return {Promise} Promise will resolve immediately with a reference to the rest client
*/
ZCRMRestClient.reset = function() {
for (const key in data) {
// Ignore "version"
if (key === 'version') continue;
data[key] = null;
}
return Promise.resolve(ZCRMRestClient);
};
/**
* Depreciated. This method wraps `ZCRMRestClient.initialize` for backwards compatibility
*
* @param {object} configJSON Configuration object
* @return {Promise} Promise will resolve immediately
*/
ZCRMRestClient.initializeWithValues = function(configJSON) {
return ZCRMRestClient.initialize(configJSON);
};
ZCRMRestClient.generateAuthTokens = function(user_identifier, grant_token) {
return new Promise(function(resolve, reject) {
if (!user_identifier) {
user_identifier = ZCRMRestClient.getUserIdentifier();
}
const config = ZCRMRestClient.getConfig(grant_token);
new OAuth(config, 'generate_token');
const api_url = OAuth.constructurl('generate_token');
OAuth.generateTokens(api_url).then(function(response) {
if (response.statusCode!=200) {
throw new Error('Problem occurred while generating access token from grant token. Response : '+JSON.stringify(response));
}
const mysql_util = require(ZCRMRestClient.getMySQLModule());
const resultObj = ZCRMRestClient.parseAndConstructObject(response);
resultObj.user_identifier = user_identifier;
if (resultObj.access_token) {
mysql_util.saveOAuthTokens(resultObj).then(function(save_resp) {
ZCRMRestClient.setUserIdentifier(user_identifier),
resolve(resultObj);
}
);
} else {
throw new Error('Problem occurred while generating access token and refresh token from grant token.Response : '+JSON.stringify(response));
}
});
});
};
ZCRMRestClient.generateAuthTokenfromRefreshToken = function(user_identifier, refresh_token) {
return new Promise(function(resolve, reject) {
if (!user_identifier) {
user_identifier = ZCRMRestClient.getUserIdentifier();
}
const config = ZCRMRestClient.getConfig_refresh(refresh_token);
new OAuth(config, 'refresh_access_token');
const api_url = OAuth.constructurl('generate_token');
OAuth.generateTokens(api_url).then(function(response) {
if (response.statusCode!=200) {
throw new Error('Problem occurred while generating access token from refresh token . Response : '+JSON.stringify(response));
}
const mysql_util = require(ZCRMRestClient.getMySQLModule());
const resultObj = ZCRMRestClient.parseAndConstructObject(response);
resultObj.user_identifier = user_identifier;
resultObj.refresh_token = refresh_token;
if (resultObj.access_token) {
mysql_util.saveOAuthTokens(resultObj).then(function(save_response) {
ZCRMRestClient.setUserIdentifier(user_identifier),
resolve(resultObj);
}
);
} else {
throw new Error('Problem occurred while generating access token from refresh token. Response : '+JSON.stringify(response));
}
});
});
};
ZCRMRestClient.getConfig = function(grant_token) {
const config = {};
config.client_id = ZCRMRestClient.getClientId();
config.client_secret = ZCRMRestClient.getClientSecret();
config.code = grant_token;
config.redirect_uri = ZCRMRestClient.getRedirectURL();
config.grant_type = 'authorization_code';
return config;
};
ZCRMRestClient.getConfig_refresh = function(refresh_token) {
const config = {};
config.client_id = ZCRMRestClient.getClientId();
config.client_secret = ZCRMRestClient.getClientSecret();
config.refresh_token = refresh_token;
config.grant_type = 'refresh_token';
return config;
};
ZCRMRestClient.setRefreshToken = function(refreshToken) {
data.refresh_token = refreshToken;
};
ZCRMRestClient.setClientId = function(clientid) {
data.client_id = clientid;
};
ZCRMRestClient.setClientSecret = function(clientsecret) {
data.client_secret = clientsecret;
};
ZCRMRestClient.setRedirectURL = function(redirecturl) {
data.redirect_url = redirecturl;
};
ZCRMRestClient.setUserIdentifier = function(useridentifier) {
data.user_identifier = useridentifier;
};
ZCRMRestClient.setIAMUrl = function(iam_url) {
data.iamurl = iam_url;
};
ZCRMRestClient.setBaseURL = function(baseurl) {
data.base_url = baseurl;
};
ZCRMRestClient.getRefreshToken = function() {
return data.refresh_token;
};
ZCRMRestClient.getClientId = function() {
return data.client_id;
};
ZCRMRestClient.getClientSecret = function() {
return data.client_secret;
};
ZCRMRestClient.getRedirectURL = function() {
return data.redirect_url;
};
ZCRMRestClient.getUserIdentifier = function() {
return data.user_identifier;
};
ZCRMRestClient.getMySQLModule = function() {
return data.mysql_module;
};
ZCRMRestClient.getAPIURL = function() {
return data.base_url;
};
ZCRMRestClient.getVersion = function() {
return data.version;
};
ZCRMRestClient.getIAMUrl = function() {
return data.iamurl;
};
ZCRMRestClient.getMySQLUserName = function() {
return data.mysql_username;
};
ZCRMRestClient.getMYSQLPassword = function() {
return data.mysql_password;
};
ZCRMRestClient.parseAndConstructObject = function(response) {
let body = response['body'];
body = JSON.parse(body);
const date = new Date();
const current_time = date.getTime();
const resultObj = {};
if (body.access_token) {
resultObj.access_token = body.access_token;
if (body.refresh_token) {
resultObj.refresh_token = body.refresh_token;
}
// Race condition:
// It is possible for the system to send a request with an API key a few milliseconds away from expiration, meaning
// the token will expire on the way to the API and will be rejected. We fix this by subtracting 60 seconds from
// the expiry time
const oneMinuteInMs = 1000 * 60;
resultObj.expires_in = body.expires_in + current_time - oneMinuteInMs;
}
return resultObj;
};
ZCRMRestClient.API = require('./crmapi');
module.exports = ZCRMRestClient;