-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquickbase_codepage_hero.js
More file actions
616 lines (493 loc) · 20.7 KB
/
quickbase_codepage_hero.js
File metadata and controls
616 lines (493 loc) · 20.7 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//CLIENT
//The client object represents the Quickbase realm you will be interacting with.
//PARAMETERS
//User Token: your QB User token (string)
//Realm: Your unique realm domain, [your realm name].quickbase.com (string)
//numberOfAttempts: How many times you wish to try again if Quickbase returns a 429 code (too many requests) (integer)
//timeout: How long you want to wait in milliseconds between attempts if Quickbase returns a 429 code (too many requests) (integer)
class client {
constructor(userToken=null, realm=null, numberOfAttempts=0, timeout=0, loggedIn=true) {
this.userToken = userToken
this.realm = realm
this.numberOfAttempts = numberOfAttempts
this.timeout = timeout
this.loggedIn = loggedIn
if(realm === null){
// get the domain from the url
this.realm = window.location.hostname.split(".")[0]+'.quickbase.com'
}
}//end constructor
//METHODS:
//QUERY:
//search a table using Quickbase query argument
//PARAMETERS:
//table: the table id (string)
//query: the Quickbase query, example: "3.GT.0" (all records where field #3 is greater than 0)
//select array: an array of the fields you want returned (array of integers)
//RETURNS:
// an array of the found records
async query(table, query, selectArray, additionalParams = null) {
const body = {
"from": table,
"select": selectArray,
"where": `${query}`
};
if (additionalParams !== null) {
//add additional parameters to the body
for (const [key, value] of Object.entries(additionalParams)) {
body[key] = value
}
}
let response = {};
response.status = 429
let attemptCounter = 0
while(response.status === 429 && attemptCounter<=this.numberOfAttempts) {
response = await fetch('https://api.quickbase.com/v1/records/query', {
method: 'POST',
headers: await this.getHeaders(table),
body: JSON.stringify(body)
})
console.log("response: ", response)
if (response.status === 429) {
console.log("Too Many Request, Trying Again")
await new Promise(resolve => setTimeout(resolve, this.timeout))
attemptCounter++
}
}
if(response.status===200) {
let data = await response.json()
return await data['data']
}else{
console.log("Error querying records. Quickbase error code: " + response.status + ". Error message: \""+response.statusText +"\"")
console.log("response: ", response.json())
return response
}
}
//MULTIQUERY
//get multiple records at once, with values matching an array of values
//PARAMETERS:
//Table: the table id (string)
//search field: the field id of the field you are querying (integer)
//queryArray: an array of values you are looking for (array of strings or numbers)
//selectArray: an array of the fields you want returned (array of integers)
//RETURNS:
// an array of the found records
async multiquery(table, searchfield, queryArray, selectArray) {
let array = []
let arrayArray = []
let datareturn = []
//Divide the queryArray into an array of arrays with length 100
//(Quickbase only allows 100 queries per API call)
for(let i = 0; i<queryArray.length; i++){
array.push(queryArray[i])
if(i!==0&&(i+1)%100 === 0){
arrayArray.push(array)
array = []
}
else if (i === queryArray.length-1){
arrayArray.push(array)
}
}
//Loop through the array of query arrays and query Quickbase 100 queries at a time
//add found records to arrayArray
for (let i in arrayArray){
let queryString = queryStringBuilder(searchfield, "EX", "OR", arrayArray[i])
const body = {
"from": table,
"select": selectArray,
"where": `${queryString}`
};
let response = {};
response.status = 429
let attemptCounter = 0
while(response.status === 429 && attemptCounter<=this.numberOfAttempts) {
response = await fetch('https://api.quickbase.com/v1/records/query', {
method: 'POST',
headers: await this.getHeaders(table),
body: JSON.stringify(body)
})
if (response.status === 429) {
console.log("Too Many Request, Trying Again")
await new Promise(resolve => setTimeout(resolve, this.timeout))
attemptCounter++
}
}
if(response.status === 200){
let data = await response.json()
datareturn.push(data['data'])
}else{
console.log("Error querying records. Quickbase error code: " + response.status + ". Error message: \""+response.statusText +"\"")
console.log("response: ", response.json())
return response
}
}
return datareturn;
}
//POST
//creates or updates records depending on whether key field (usually field ID #3) is present
//if key id is given and exists in the table, that record will be updated
//if key id is not given, a record will be created
//if key id is given but does not exist in the table, a record will be created with the given key id
//PARAMETERS
//table_id: the table id (string)
//record_array: an array of record objects (each containing field objects)
//RETURNS
//a dictionary of arrays of the RID's that have been created, unchanged and edited
async post(table_id, record_array, fieldsToReturn = null) {
const data = {
to: table_id,
data: record_array
};
//if fieldsToReturn is not null, add it to the body
if (fieldsToReturn !== null) {
data['fieldsToReturn'] = fieldsToReturn
}
let response = {};
response.status = 429
let attemptCounter = 0
while (response.status === 429 && attemptCounter <= this.numberOfAttempts) {
response = await fetch('https://api.quickbase.com/v1/records', {
method: 'POST',
headers: await this.getHeaders(table_id),
body: JSON.stringify(data)
})
if (response.status === 429) {
console.log("Too Many Request, Trying Again")
await new Promise(resolve => setTimeout(resolve, this.timeout))
attemptCounter++
}
}
if (response.status === 200) {
let data = await response.json()
return {
createdRecordIds: data.metadata.createdRecordIds,
unchangedRecordIds: data.metadata.unchangedRecordIds,
updatedRecordIds: data.metadata.updatedRecordIds,
createdRecords: data.data
}
} else {
let data = await response.json()
console.log("Error creating records. Quickbase error code: " + response.status + ". Error message: \"" + response.statusText + "\"")
console.log("response: ", data)
if ('metadata' in data) {
if ('lineErrors' in data['metadata']) {
let dict = data['metadata']['lineErrors']
for ( const [key] of Object.entries(dict)) {
console.log("Line errors from posting Record #", key, ":")
for (let i = 0; i < data['metadata']['lineErrors'][key].length; i++) {
console.log(data['metadata']['lineErrors'][key][i])
}
}
return data
}
}
return data
}
}
//DELETE:
//Delete ALL records that satisfy Quickbase query
//PARAMETERS:
//table: the table id (string)
//query: the Quickbase query, example: "3.EX.499" (all records where field #3 equals 499)
//RETURNS:
// The number of records deleted
async delete(table_id, query){
const body = {
"from": table_id,
"where": `${query}`
};
let response = {};
response.status = 429
let attemptCounter = 0
while(response["status"] === 429 && attemptCounter<=this.numberOfAttempts) {
response = await fetch('https://api.quickbase.com/v1/records', {
method: 'DELETE',
headers: await this.getHeaders(table_id),
body: JSON.stringify(body)
})
if (response.status === 429) {
console.log("Too Many Request, Trying Again")
await new Promise(resolve => setTimeout(resolve, this.timeout))
attemptCounter++
}
}
if(response.status===200){
let data = await response.json()
return await data['numberDeleted']
}else{
console.log("Error deleting records. Quickbase error code: " + response.status + ". Error message: \""+response.statusText +"\"")
console.log("response: ", response.json())
return response
}
}
//MULTIDELETE
//delete multiple records at once with an array of values as input
//PARAMETERS:
//Table: the table id (string)
//search field: the field id of the field you are querying (integer)
//deleteArray: an array of values you are looking for (array of strings or numbers)
//RETURNS:
// the number of deleted records
async multidelete(table_id, searchfield, deleteArray) {
let array = []
let arrayArray = []
let number_deleted = 0
//Divide the deleteArray into an array of arrays with length 100
//(Quickbase only allows 100 queries per API call)
for(let i = 0; i<deleteArray.length; i++){
array.push(deleteArray[i])
if(i!==0&&(i+1)%100 === 0){
arrayArray.push(array)
array = []
}
else if (i === deleteArray.length-1){
arrayArray.push(array)
}
}
//Loop through the array of delete arrays and delete Quickbase records 100 at a time
//add number of return records to number_deleted
for (let i in arrayArray){
let queryString = queryStringBuilder(searchfield, "EX", "OR", arrayArray[i])
const body = {
"from": table_id,
"where": `${queryString}`
};
let response = {};
response.status = 429
let attemptCounter = 0
while(response.status === 429 && attemptCounter<=this.numberOfAttempts) {
response = await fetch('https://api.quickbase.com/v1/records', {
method: 'DELETE',
headers: await this.getHeaders(table_id),
body: JSON.stringify(body)
})
if (response.status === 429) {
console.log("Too Many Request, Trying Again")
await new Promise(resolve => setTimeout(resolve, this.timeout))
attemptCounter++
}
}
if(response.status === 200){
let data = await response.json()
console.log("multidelete response: ", data)
number_deleted+=data['numberDeleted']
}else{
console.log("Error querying records. Quickbase error code: " + response.status + ". Error message: \""+response.statusText +"\"")
console.log("response: ", response.json())
return response
}
}
return number_deleted
}
//GETCHOICES
//get all the options of a multiple choice field
//PARAMETERS:
//table_id(str)
//FID (int)
async getchoices(table_id, field_id){
let response = {};
response.status = 429
let attemptCounter = 0
let fetch_url = "https://api.quickbase.com/v1/fields/"+field_id.toString()+"?tableId="+table_id+"&includeFieldPerms=false"
while(response["status"] === 429 && attemptCounter<=this.numberOfAttempts) {
response = await fetch(fetch_url, {
method: 'GET',
headers: await this.getHeaders(table_id),
})
if (response.status === 429) {
console.log("Too Many Request, Trying Again")
await new Promise(resolve => setTimeout(resolve, this.timeout))
attemptCounter++
}
}
if(response.status===200){
let data = await response.json()
return await data['properties']['choices']
}else{
//give error to user
console.log("Error querying for field choices. Quickbase error code: " + response.status + ". Error message: \""+response.statusText +"\"")
console.log("response: ", response.json())
return response
}
}
async getfields(table_id){
let response = {};
response.status = 429
let attemptCounter = 0
let fetch_url = "https://api.quickbase.com/v1/fields/?tableId="+table_id+"&includeFieldPerms=false"
while(response["status"] === 429 && attemptCounter<=this.numberOfAttempts) {
response = await fetch(fetch_url, {
method: 'GET',
headers: await this.getHeaders(table_id),
})
if (response.status === 429) {
console.log("Too Many Request, Trying Again")
await new Promise(resolve => setTimeout(resolve, this.timeout))
attemptCounter++
}
}
if(response.status===200){
let data = await response.json()
return await data['properties']['choices']
}else{
//give error to user
console.log("Error querying for field choices. Quickbase error code: " + response.status + ". Error message: \""+response.statusText +"\"")
console.log("response: ", response.json())
return response
}
}
//GET AUTHORIZATION
async getAuthorization(table_id){
if (this.userToken !== null){
return 'QB-USER-TOKEN ' + this.userToken
}else{
var headers = {
'QB-Realm-Hostname': this.realm,
'Content-Type': 'application/json'
}
let response = await fetch(`https://api.quickbase.com/v1/auth/temporary/${table_id}`, {
method: 'GET',
headers: headers,
credentials: 'include'
})
let result = await response.json();
console.log("TEMPORARY TOKEN RESPONSE: ", result)
if ('temporaryAuthorization' in result) {
return 'QB-TEMP-TOKEN ' + result.temporaryAuthorization
}
else {
console.log("error getting temporary token: ", result);
}
}
}
async getUserInfo(){
let temp_token = await this.getAuthorization()
let url = `https://matthewclark-634.quickbase.com//db/main?a=API_GetUserInfo&ticket=${temp_token}`
let response = await fetch(url)
console.log("response: ", response)
let data = await response.text()
console.log("data: ", data)
return parseXmlToJsonObject(data)
}
async getHeaders(table_id){
console.log("getting headers...")
console.log("this realm: ", this.realm)
let headers = null
if(this.loggedIn){
headers = {
'QB-Realm-Hostname': this.realm,
'Authorization': await this.getAuthorization(table_id),
'Content-Type': 'application/json'
}}else{
headers = {
'QB-Realm-Hostname': this.realm,
'Authorization': null,
'Content-Type': 'application/json'
}
}
console.log("headers: ", headers)
return headers
}
}//end class
//helper function to build query strings
function queryStringBuilder(searchfield, argument, operator, valueArray){
let queries = []
for(let i = 0; i < valueArray.length; i++){
if(typeof valueArray[i] === "string" || valueArray[i] instanceof String){
let query = "{" + searchfield + "." + argument + ".\'" + valueArray[i] + "\'}"
queries.push(query)
}else{
let query = "{" + searchfield + "." + argument + "." + valueArray[i] + "}"
queries.push(query)
}
}
return queries.join(operator)
}
function dynamicallyLoadScript(url) {
var script = document.createElement("script"); // create a script DOM node
script.src = url; // set its src to the provided URL
document.head.appendChild(script); // add it to the end of the head section of the page (could change 'head' to 'body' to add it to the end of the body section instead)
}
dynamicallyLoadScript("https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js")
async function create_pdf_b64(opt){
const element = document.querySelector('body');
const worker = html2pdf().set(opt).from(element);
var pdf_object
await worker.outputPdf('datauristring').then(function (pdfAsString) {
pdf_object = pdfAsString
});
return pdf_object.split('base64,')[1]
}
//function that takes a url and returns a base64 string
async function url_to_b64(url) {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
resolve(reader.result);
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
});
}
//function that takes in a number and returns a currency format with 2 decimal places
function formatCurrency(num) {
return "$" + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}
//function to change date to mm-dd-yyyy
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [month, day, year].join('-');
}
function parseXmlToJsonObject(xmlString) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
// Initialize an empty JSON object to store the extracted values
const result = {};
// Helper function to safely extract text content from an element
const extractTextContent = (element, tagName) => {
const elements = element.getElementsByTagName(tagName);
return elements.length > 0 ? elements[0].textContent : null;
};
// Extract values from the XML document
const action = extractTextContent(xmlDoc, "action");
const errcode = extractTextContent(xmlDoc, "errcode");
const errtext = extractTextContent(xmlDoc, "errtext");
const userElement = xmlDoc.getElementsByTagName("user")[0];
// Add values to the JSON object if they exist
if (action) result.action = action;
if (errcode) result.errcode = errcode;
if (errtext) result.errtext = errtext;
if (userElement) {
const userId = userElement.getAttribute("id");
const firstName = extractTextContent(userElement, "firstName");
const lastName = extractTextContent(userElement, "lastName");
const login = extractTextContent(userElement, "login");
const email = extractTextContent(userElement, "email");
const screenName = extractTextContent(userElement, "screenName");
const isVerified = extractTextContent(userElement, "isVerified");
const externalAuth = extractTextContent(userElement, "externalAuth");
// Add user information to the JSON object if it exists
result.user = {};
if (userId) result.user.id = userId;
if (firstName) result.user.firstName = firstName;
if (lastName) result.user.lastName = lastName;
if (login) result.user.login = login;
if (email) result.user.email = email;
if (screenName) result.user.screenName = screenName;
if (isVerified) result.user.isVerified = isVerified;
if (externalAuth) result.user.externalAuth = externalAuth;
}
return result;
}