-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbHandler.js
More file actions
710 lines (658 loc) · 25.4 KB
/
dbHandler.js
File metadata and controls
710 lines (658 loc) · 25.4 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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
const fs = require('fs');
const dbFile = process.env.DB_FILEPATH;
const bcrypt = require("bcrypt");
const saltRounds = 10;
const api_freq = 1000 * 60 * 60; // retrieves data every hour
module.exports = new class DbHandler {
constructor() {
if(!fs.existsSync(dbFile)){
this.db = require("./db");
this.createTables();
}else{
this.db = require("./db");
}
this.base = require('airtable').base('appI6ReVlk9nCsFUB');
this.table = process.env.AIRTABLE_TABLE_NAME;
this.view = process.env.AIRTABLE_TABLE_VIEW;
this.getAirtableDataLoop();
}
/**
* Sets up the proper tables for the app's database.
*/
createTables(){
this.db.exec("CREATE TABLE 'users' ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT, `email` TEXT NOT NULL UNIQUE, `password` TEXT NOT NULL, `admin` INTEGER DEFAULT 0, `alumni` INTEGER DEFAULT 0, `month_access` INTEGER, `first_time` INTEGER DEFAULT 0, `done` INTEGER DEFAULT 0 );");
this.db.exec(`CREATE TABLE 'applicants' ('airtable_id' TEXT UNIQUE NOT NULL, 'asc_id' TEXT UNIQUE NOT NULL, 'month_applied' INTEGER, 'essay_1' TEXT, 'essay_2' TEXT, 'essay_3' TEXT, 'read_count' INTEGER DEFAULT 0, 'complete' INTEGER DEFAULT 0);`);
this.db.exec(`CREATE TABLE 'readScores' ('userId' INTEGER, 'asc_id' TEXT,'essay_score_1' INTEGER, 'essay_score_2' INTEGER, 'essay_score_3' INTEGER ,'essay_score' INTEGER, 'comment' TEXT);`);
}
/**
* Retrieves the Airtable data on a regular cycle.
*/
getAirtableDataLoop(){
this._getAirtableDataLoop();
setInterval(()=>{
this._getAirtableDataLoop()
}, api_freq);
}
/**
* Helper method for retrieving Airtable data.
*/
_getAirtableDataLoop(){
this.applicants = [];
this.getAirtableData()
.then(records=>this.populateApplicantDB(records)
.then(()=>this.keepOnlyLastMonthApplicants())
)
.catch(err=>console.log(err));
}
/**
* Return true if user's password successfully updates.
*/
changeUserPassword(user, oldPassword, newPassword){
const currentHash = user.password;
const self = this;
return new Promise((res, rej)=>{
bcrypt.compare(oldPassword, currentHash,(err, sameness)=>{
if(err) return rej(err);
if(!sameness) return res({error:"'Old Password' provided did not match original"});
bcrypt.hash(newPassword, saltRounds, function(err, hash){
if(err) return rej(err);
const sql ="UPDATE users SET password=$newPassword WHERE id = $userId";
self.db.run(sql, {
$userId: user.id,
$newPassword: hash,
}, function(err){
if(err) return rej(err);
res({});
});
})
})
})
}
/**
* Returns the applicants that have applied last month.
*/
getLastMonthApplicants(){
let lastMonth = new Date().getMonth();
if(lastMonth === 0){
lastMonth = 12;
}
return new Promise((res,rej)=>{
const sql = 'SELECT * FROM applicants WHERE month_applied = ?;';
this.db.all(sql, lastMonth, function(err, rows){
if (err) return rej(err);
res(rows);
})
})
}
/**
* Filters DB to keep only relevant applicants
*/
keepOnlyLastMonthApplicants(){
let lastMonth = new Date().getMonth();
lastMonth--;
if(lastMonth < 0){
lastMonth = 11;
}
return new Promise((res,rej)=>{
const sql = "DELETE FROM applicants WHERE month_applied <> ?;";
this.db.run(sql, lastMonth, err => {
if(err) return rej(err);
res();
})
})
}
/**
* Retrieves asc_id of next applicant for user to read.
*/
async getNextApplicant(user){
const userId = user.id;
const incompleteApplicant_asc_id = await this.findIncompleteApplicant(userId);
if(incompleteApplicant_asc_id){
return incompleteApplicant_asc_id;
}
const availApplicantASCID = await this.getAvailableApplicant(user);
return availApplicantASCID;
}
/**
* Returns true if the user is done with the readathon.
*/
checkUserDoneness(user){
let sql;
const userId = user.id;
const self = this;
if(user.alumni===1){
sql = `SELECT asc_id FROM applicants WHERE read_count<3 AND asc_id NOT IN (SELECT readScores.asc_id from users JOIN readScores ON users.id=readScores.userId WHERE users.alumni=1 GROUP BY readScores.asc_id)`;
return new Promise((res,rej)=>{
this.db.get(sql, function(err, row){
if (err) return rej(err);
const result = !row;
const doneness = result?1:0;
self.setUserDone(userId,doneness)
.then(()=>res(result))
.catch(err=>rej(err))
})
})
}else{
sql = `SELECT asc_id FROM applicants WHERE asc_id NOT IN (SELECT applicants.asc_id FROM applicants LEFT JOIN readScores ON applicants.asc_id=readScores.asc_id WHERE readScores.userId=? OR applicants.read_count > 2);`;
return new Promise((res,rej)=>{
this.db.get(sql, userId, function(err, row){
if (err) return rej(err);
const result = !row;
const doneness = result?1:0;
self.setUserDone(userId,doneness)
.then(()=>res(result))
.catch(err=>rej(err))
})
})
}
}
/**
* Returns a valid applicant for the given user to read.
*/
getAvailableApplicant(user){
const self = this;
let sql;
const userId = user.id;
if(user.alumni===1){
sql = `SELECT asc_id FROM applicants WHERE read_count<3 AND asc_id NOT IN (SELECT readScores.asc_id from users JOIN readScores ON users.id=readScores.userId WHERE users.alumni=1 GROUP BY readScores.asc_id)`;
return new Promise((res,rej)=>{
this.db.get(sql, function(err, row){
if (err) return rej(err);
if(!row){
return res(row);
}else{
const asc_id = row.asc_id;
self.holdApplicant(userId, asc_id)
.then(info=>res(info))
.catch(err=>rej(err))
}
})
})
}else{
sql = `SELECT asc_id FROM applicants WHERE asc_id NOT IN (SELECT applicants.asc_id FROM applicants LEFT JOIN readScores ON applicants.asc_id=readScores.asc_id WHERE readScores.userId=? OR applicants.read_count > 2);`;
return new Promise((res,rej)=>{
this.db.get(sql, userId, function(err, row){
if (err) return rej(err);
if(!row){
return res(row);
}else{
const asc_id = row.asc_id;
self.holdApplicant(userId, asc_id)
.then(info=>res(info))
.catch(err=>rej(err))
}
})
})
}
}
/**
* Reserves a spot in the DB for the user to score applicant.
*/
holdApplicant(userId,asc_id){
return new Promise((res,rej)=>{
const sql1 = 'INSERT INTO readScores (userID, asc_id) VALUES ($userId,$asc_id);'
const sql2 = 'UPDATE applicants SET read_count=(SELECT read_count FROM applicants WHERE asc_id=?)+1 WHERE asc_id=?;';
const data = {$userId:userId, $asc_id:asc_id};
this.db.run(sql1,data,(err)=>{
if (err) return rej(err);
this.db.run(sql2,[asc_id,asc_id],(err)=>{
if (err) return rej(err);
res(asc_id);
})
})
})
}
/**
* Returns the first unscored applicant reserved for the given user.
*/
findIncompleteApplicant(userId){
return new Promise((res,rej)=>{
const sql = 'SELECT asc_id FROM readScores WHERE userId = ? AND essay_score IS NULL;';
this.db.get(sql, userId, function(err, row){
if (err) return rej(err);
if(!row) return res(row);
res(row.asc_id); // undefined if not found
})
})
}
/**
* REturns the statistics of all applicants.
*/
getApplicantsStats(){
return new Promise((res,rej)=>{
const sql = "SELECT applicants.asc_id, applicants.read_count, applicants.complete, applicants.month_applied, sum(readScores.essay_score_1) AS essay_1_total, sum(readScores.essay_score_2) AS essay_2_total, sum(readScores.essay_score_3) AS essay_3_total, sum(readScores.essay_score) AS essay_total FROM applicants LEFT JOIN readScores ON readScores.asc_id=applicants.asc_id GROUP BY applicants.asc_id ORDER BY essay_total DESC;";
this.db.all(sql,(err,rows)=>{
if(err) return rej(err);
res(rows);
});
})
}
/**
* Returns applicant by their ASC ID.
*/
getApplicantByASCID(asc_id){
return new Promise((res,rej)=>{
const sql = "SELECT * FROM applicants WHERE asc_id=?;";
this.db.get(sql,asc_id,(err,row)=>{
if(err) return rej(err);
res(row);
});
})
}
/**
* Returns reserved applicants for the given ASC ID that have been scored.
*/
getCompleteReadScores(asc_id){
return new Promise((res,rej)=>{
const sql = "SELECT * FROM readScores WHERE asc_id=? AND essay_score NOT NULL;";
this.db.all(sql, asc_id, function(err,rows){
if(err) return rej(err);
res(rows);
});
});
}
/**
* Updates the local DB.
* If there are 3 reads, the airtable DB is also updated.
*/
async incrementApplicantReads(userId, asc_id, scores, comment){
const readScoresUpdated = await this.updateUserReadScore(userId, asc_id, scores, comment);
if(!readScoresUpdated) throw new Error("Could not update user: " + userId);
const completeReadScores = await this.getCompleteReadScores(asc_id);
if(completeReadScores.length < 3) return null;
const result = await this.completeRecord(asc_id, completeReadScores);
return result;
}
/**
* Updates a user's readScore in local DB.
*/
updateUserReadScore(userId, asc_id, scores, comment){
return new Promise((res,rej)=>{
const sql = "UPDATE readScores SET essay_score_1=$essay_score_1,essay_score_2=$essay_score_2,essay_score_3=$essay_score_3,essay_score=$essay_score, comment=$comment WHERE userId=$userId AND asc_id=$asc_id";
this.db.run(sql,{
$userId: userId,
$asc_id: asc_id,
$comment: comment,
$essay_score_1: scores.essay_score_1,
$essay_score_2: scores.essay_score_2,
$essay_score_3: scores.essay_score_3,
$essay_score: scores.essay_score_1+scores.essay_score_2+scores.essay_score_3
},function(err){
if(err) return rej(err);
res(this.changes > 0); // true if changes occured
})
});
}
/**
* Updates Airtable DB and completes applicant in local DB.
*/
async completeRecord(asc_id, completeReadScores){
const record = this.findAirtableRecordByASCID(asc_id);
if(!record) throw new Error("Could not find Airtable record: " + asc_id);
const fields = {
essay_score_1: 0,
essay_score_2: 0,
essay_score_3: 0
}
const keys = Object.keys(fields);
for(const readScore of completeReadScores){
for(const key of keys){
fields[key] += readScore[key];
}
}
fields.readathon_comments = completeReadScores.map(readScore=>readScore.comment).join(". ");
const api_result = record.updateFields(fields);
if(api_result.error){
console.log("Error updating Airtable: ", api_result.message);
throw api_result;
}
const applicantCompleted = await this.completeApplicant(asc_id);
if(applicantCompleted===0) throw new Error("Could not update applicant: " + asc_id);
return applicantCompleted;
}
/**
* Set's applicant's status to complete.
*/
completeApplicant(asc_id){
return new Promise((res,rej)=>{
const sql = "UPDATE applicants SET complete=1 WHERE asc_id=?";
this.db.run(sql,asc_id, function(err){
if(err) throw err;
res(this.changes > 0); // true if changes occured
})
});
}
/**
* Returns live airtable record.
*/
findAirtableRecordByASCID(asc_id){
return this.applicants.find(applicant=>applicant.fields["asc_id"]===asc_id);
}
/**
* Returns user with given userId.
*/
findUser(userId){
return new Promise((res,rej)=>{
const sql = 'SELECT * FROM users WHERE id = ? LIMIT 1;';
this.db.get(sql, userId, function(err, row){
if (err) return rej(err);
res(row);
})
})
}
/**
* Returns user by given email.
*/
findUserByEmail(email){
return new Promise((res,rej)=>{
const sql = 'SELECT * FROM users WHERE email = ? LIMIT 1;';
this.db.get(sql, email, function(err, row){
if (err) return rej(err);
res(row);
})
})
}
/**
* Returns all users from the DB.
* Intended for Admin purposes.
*/
getUsers(){
return new Promise((res,rej)=>{
const sql = 'SELECT id, name, email, admin, alumni, month_access FROM users;';
this.db.all(sql, function(err, rows){
if (err) return rej(err);
res(rows);
})
})
}
/**
* Adds a new user to the DB given a SQL parameter-like object.
*/
addNewUser({id, name, email, password, admin, alumni, month_access}){
const self = this;
return new Promise((res, rej)=>{
bcrypt.hash(password, saltRounds, function(err, hash){
if(err) return rej(err);
const sql ="INSERT INTO users (name, email, password, admin, alumni, month_access) VALUES ($name, $email, $password, $admin, $alumni, $month_access);";
self.db.run(sql, {
$id: id,
$name: name,
$email: email,
$password: hash,
$admin: admin,
$alumni: alumni,
$month_access: month_access
}, function(err){
if(err) return rej(err);
res(this.lastID);
});
})
})
}
/**
* Updates the user given an SQL parameter-like object.
*/
updateUser({id, name, email, password, admin, alumni, month_access}){
const self = this;
if(password){
return new Promise((res, rej)=>{
bcrypt.hash(password, saltRounds, function(err, hash){
if(err) return rej(err);
const sql ="UPDATE users SET name=$name, email=$email, password=$password, admin=$admin, alumni=$alumni, month_access=$month_access WHERE id = $id";
self.db.run(sql, {
$id: id,
$name: name,
$email: email,
$password: hash,
$admin: admin,
$alumni: alumni,
$month_access: month_access
}, function(err){
if(err) return rej(err);
self.findUser(id)
.then(res)
.catch(rej);
});
})
})
}else{
return new Promise((res, rej)=>{
const sql ="UPDATE users SET name = $name, email=$email, admin=$admin, alumni=$alumni, month_access=$month_access WHERE id = $id";
self.db.run(sql, {
$id: id,
$name: name,
$email: email,
$admin: admin,
$alumni: alumni,
$month_access: month_access
}, function(err){
if(err) return rej(err);
self.findUser(id)
.then(res)
.catch(rej);
});
})
}
}
/**
* Sets the given userId to having visited the website before.
*/
setUserVisited(userId, checkedState){
return new Promise((res, rej)=>{
const sql ="UPDATE users SET first_time = ? WHERE id = ?";
this.db.run(sql, [checkedState,userId], function(err){
if(err) return rej(err);
res();
});
})
}
/**
* Sets the given userId as having completed their Readathon.
*/
setUserDone(userId, doneness){
return new Promise((res, rej)=>{
const sql ="UPDATE users SET done = ? WHERE id = ?";
this.db.run(sql, [doneness,userId], function(err){
if(err) return rej(err);
res();
});
})
}
/**
* Returns total number of completed application reads.
*/
getCompletedApplicantCount(){
return new Promise((res, rej)=>{
let month = new Date().getMonth()-1;
month = month < 0 ? 11 : month;
const sql ="SELECT count(*) AS completed FROM readScores JOIN applicants ON applicants.asc_id=readScores.asc_id WHERE applicants.month_applied=? AND readScores.essay_score NOT NULL;";
this.db.get(sql, month, function(err, row){
if(err) return rej(err);
res(row.completed);
});
})
}
/**
* Returns the basic progress stats for the Readathon.
*/
getProgressStats(){
const self = this;
return new Promise((res, rej)=>{
const sql ="SELECT count(*) AS total FROM applicants;";
this.db.get(sql, function(err, row){
if(err) return rej(err);
self.getCompletedApplicantCount()
.then(completed=>{
row.completed = completed;
res(row);
})
.catch(err=>rej(err))
});
})
}
/**
* Returns the applicants that have been assigned to a user.
*/
getReadScoresWithUsers(){
return new Promise((res,rej)=>{
const sql = "SELECT users.name as 'username', asc_id, essay_score_1,essay_score_2,essay_score_3, essay_score, comment FROM readScores JOIN users ON users.id=readScores.userId;";
this.db.all(sql,(err,rows)=>{
if(err) return rej(err);
res(rows);
});
})
}
/**
* Returns average score for applicants.
*/
getAvgAppScore(){
return new Promise((res,rej)=>{
const sql = "SELECT avg(essay_score) as avg FROM readScores;";
this.db.get(sql,(err,row)=>{
if(err) return rej(err);
res(row.avg);
});
})
}
/**
* Returns the sample standard deviation for scored applicants.
*/
getSampleStdEssayScore(){
return new Promise((res,rej)=>{
const sql = "SELECT SUM((essay_score -(SELECT AVG(essay_score) FROM readScores)) * (essay_score -(SELECT AVG(essay_score) FROM readScores)))/(count(*)-1) AS sample_std, AVG(essay_score) AS avg FROM readScores;";
this.db.get(sql,(err,stats)=>{
if(err) return rej(err);
res(stats);
});
});
}
/**
* Returns statistics for all users for admin purposed.
*/
async getUserStats_admin(){
const stats = await this.getSampleStdEssayScore();
const avg = await this.getAvgAppScore();
return new Promise((res,rej)=>{
const sql = "SELECT users.id AS 'id',users.name AS 'username', count(*) AS count, AVG(essay_score_1) AS 'essay_score_1_avg', AVG(essay_score_2) AS 'essay_score_2_avg', AVG(essay_score_3) AS 'essay_score_3_avg', AVG(essay_score) AS 'essay_score_avg' FROM readScores JOIN users ON users.id = readScores.userId GROUP BY readScores.userId ORDER BY count(*) DESC;";
this.db.all(sql,(err,rows)=>{
if(err) return rej(err);
const output = {
userStats: rows,
avg: avg,
stats: stats
}
res(output);
});
});
}
/**
* Returns all applicant reservations for the given userId.
*/
getUserApplicants(userId){
return new Promise((res,rej)=>{
const sql="SELECT * FROM readScores WHERE userId=?;";
this.db.all(sql,userId,(err,rows)=>{
if(err) return rej(err);
res(rows);
})
});
}
/**
* Retrns all of the users and their readScores for the given asc_id.
*/
getApplicantUsers(asc_id){
return new Promise((res,rej)=>{
const sql="SELECT essay_score_1,essay_score_2,essay_score_3,comment,name FROM readScores JOIN users ON readScores.userId=users.id WHERE readScores.asc_id=?";
this.db.all(sql,asc_id,(err,rows)=>{
if(err) return rej(err);
res(rows);
})
});
}
/**
* Returns the scoring stats for the given userId.
*/
getUserScores(userId){
return new Promise((res,rej)=>{
const sql="SELECT count(*) AS count, AVG(essay_score_1) AS 'essay_score_1_avg', AVG(essay_score_2) AS 'essay_score_2_avg', AVG(essay_score_3) AS 'essay_score_3_avg', AVG(essay_score) AS 'essay_score_avg' FROM readScores WHERE userId=?;";
this.db.get(sql,userId,(err,row)=>{
if(err) return rej(err);
res(row);
})
});
}
/**
* Returns the statistics of the given userId.
*/
async getUserStats(userId){
const userApplicants = await this.getUserApplicants(userId);
const userScores = await this.getUserScores(userId);
return {
userApplicants: userApplicants,
userScores: userScores
}
}
/**
* Returns applicant by airtable ID.
*/
getApplicantByAirtableID(airtable_id){
return new Promise((res,rej)=>{
const sql = "SELECT * FROM applicants WHERE airtable_id=?;";
this.db.get(sql,airtable_id,(err,row)=>{
if(err) return rej(err);
res(row);
});
})
}
/**
* Takes "live" records and adds them to local DB, if not already there.
*/
async populateApplicantDB(apiRecords){
if(apiRecords.length===0) return;
this.applicants = apiRecords;
let sql = "INSERT INTO applicants (airtable_id, asc_id, month_applied, essay_1, essay_2, essay_3) VALUES";
// INSERTING MULTIPLE VALUES AT ONCE
const sqlParams = [];
const paramInject = [];
for(const applicant of this.applicants){
// don't want duplicates
const foundApplicant = await this.getApplicantByAirtableID(applicant.id);
if(foundApplicant) continue;
paramInject.push("(?,?,?,?,?,?)");
sqlParams.push(applicant.id);
sqlParams.push(applicant.fields["asc_id"]);
sqlParams.push(applicant.fields["month_received_int"]-1);
sqlParams.push(applicant.fields["essay_1"]);
sqlParams.push(applicant.fields["essay_2"]);
sqlParams.push(applicant.fields["essay_3"]);
}
if(paramInject.length===0) return;
sql += paramInject.join(",");
const confirmation = await new Promise((res,rej)=>{
this.db.run(sql, sqlParams, err => {
if(err) return rej(err);
res(true);
})
})
return confirmation;
}
/**
* Gets "live" records from Airtable DB.
*/
getAirtableData(){
const output = [];
return new Promise((res,rej)=>{
this.base(this.table).select({
view: this.view
}).eachPage(function page(records, fetchNextPage) {
records.forEach(function(record) {
output.push(record);
});
fetchNextPage();
}, function done(err) {
if (err) return rej(err);
res(output);
});
})
}
}