-
Notifications
You must be signed in to change notification settings - Fork 0
/
reportdemo.js
258 lines (243 loc) · 6.89 KB
/
reportdemo.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
/**
* @fileoverview Simple demonstration of the use of the InContact Reporting API
* @author Joey Whelan <[email protected]>
*/
'use strict';
'use esversion 6';
/*jshint esversion: 6 */
const fetch = require('node-fetch');
const btoa = require('btoa');
const atob = require('atob');
const fs = require('fs');
const util = require('util');
const writeFile = util.promisify(fs.writeFile);
const app = 'yourApp';
const vendor = 'yourVendor';
const reportId = 'yourId';
const bu = 'yourBu';
const username = 'yourName';
const password = 'yourPwd';
const outfile = 'report.csv';
/** @desc Class providing an object wrapper for REST calls to the InContact Reporting API. */
class CustomReport {
/**
* @param {string} app InContact app name for API access
* @param {string} vendor InContact vendor name for API access
* @param {string} bu InContact business unit
* @param {string} username InContact username
* @param {string} password InContact password
*/
constructor(app, vendor, bu, username, password) {
this.authCode = btoa(app + '@' + vendor + ':' + bu);
this.username = username;
this.password = password;
}
/**
* Uses a base64-encoded key to make an request for a password-grant API token. Will propagate exceptions.
* @return {Promise} Promise object representing the result of fetching an API token
*/
getToken() {
console.log('getToken()');
const url = 'https://api.incontact.com/InContactAuthorizationServer/Token';
const body = {
'grant_type' : 'password',
'username' : this.username,
'password' : this.password
};
return fetch(url, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type' : 'application/json',
'Authorization' : 'basic ' + this.authCode
},
cache: 'no-store',
mode: 'cors'
})
.then(response => {
if (response.ok) {
return response.json();
}
else {
const msg = 'response status: ' + response.status;
throw new Error(msg);
}
})
.then(json => {
if (json && json.access_token && json.resource_server_base_uri) {
return json;
}
else {
const msg = 'missing token and/or uri';
throw new Error(msg);
}
})
.catch(err => {
console.error('getToken() - ' + err.message);
throw err;
});
}
/**
* Kicks off custom reporting job
* @param {string} reportID custom report ID
* @param {string} reportURL base URL for the reporting API
* @param {string} token InContact API token
* @return {Promise} Promise object representing the jobId of the reporting job
*/
startReportJob(reportId, reportURL, token) {
const url = reportURL + reportId;
console.log('startReportJob() - url: ' + url);
const body = {
'fileType': 'CSV',
'includeHeaders': 'true',
'appendDate': 'true',
'deleteAfter': '7',
'overwrite': 'true'
};
return fetch(url, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type' : 'application/json',
'Authorization' : 'bearer ' + token
},
cache: 'no-store',
mode: 'cors'
})
.then(response => {
if (response.ok) {
return response.json();
}
else {
const msg = 'response status: ' + response.status;
throw new Error(msg);
}
})
.then(json => {
return json.jobId;
})
.catch(err => {
console.error('startReportJob() - ' + err.message);
throw err;
});
}
/**
* Checks on the status of a reporting job. Loops for a user-defined number of retries waiting for the job to complete.
* By default, waits up to 10 minutes for a job to complete.
* @param {string} jobId id of the reporting job
* @param {string} reportURL base URL for the reporting API
* @param {string} token InContact API token
* @param {integer} numTries number of times to retry while waiting for a job to finish
* @return {Promise} Promise object with URL of the report file location
*/
getFileURL(jobId, reportURL, token, numTries=10) {
console.log('getFileURL() - jobId: ' + jobId + ' numTries: ' + numTries);
const that = this;
const url = reportURL + jobId;
return fetch(url, {
method: 'GET',
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : 'bearer ' + token
},
cache: 'no-store',
mode: 'cors'
})
.then(response => {
if (response.ok) {
return response.json();
}
else {
const msg = 'response status: ' + response.status;
throw new Error(msg);
}
})
.then(json => {
if (json.jobResult.resultFileURL) {
return json.jobResult.resultFileURL;
}
else {
if (numTries > 0) { //loop (recursive) up to the numTries parameter
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(that.getFileURL(jobId, reportURL, token, numTries-1));
}, 60000); //retry once per minute
});
}
else {
throw new Error('Maximum retries reached');
}
}
})
.catch(err => {
console.error('getFileURL() - ' + err.message);
throw err;
});
}
/**
* Pulls down the report data. Fetchs the base64-encoded data of the report.
* @param {string} URL InContact API file fetch url
* @param {string} token InContact API token
* @return {Promise} Promise object with base64 data
*/
downloadReport(url, token) {
console.log('downLoadReport() - url: ' + url);
return fetch(url, {
method: 'GET',
headers: {'Authorization' : 'bearer ' + token},
cache: 'no-store',
mode: 'cors'
})
.then(response => {
if (response.ok) {
return response.json();
}
else {
const msg = 'response status: ' + response.status;
throw new Error(msg);
}
})
.then(json => {
return json.files.file;
})
.catch(err => {
console.error('downloadReport() - ' + err.message);
throw err;
});
}
/**
* Main procedure. Chains promises to get an API token, start a reporting job, check status/get file URL of the resulting job,
* download the base64-encoded string of the job, convert that string to binary and write it to a local file.
* @param {string} reportId ID of the custom report template to be executed
* @param {string} target name of filename where report data is to be written
* @return none
*/
getReport(reportId, target) {
console.log('getReport() - reportId: ' + reportId);
let token, reportURL;
const version = 'v13.0';
return this.getToken()
.then(data => {
token = data.access_token;
reportURL = `${data.resource_server_base_uri}services/${version}/report-jobs/`;
return this.startReportJob(reportId, reportURL, token);
})
.then(jobId => {
return this.getFileURL(jobId, reportURL, token);
})
.then(url => {
return this.downloadReport(url, token);
})
.then(file => {
return writeFile(target, atob(file));
})
.then(() => {
console.log('Job Complete');
})
.catch(err => {
console.error('loadReport() - ' + err.message);
});
}
}
const report = new CustomReport(app, vendor, bu, username, password);
report.getReport(reportId, outfile);