From 7ba1f99a951aef3ad5b8c889a6542e3932c06883 Mon Sep 17 00:00:00 2001 From: Ivan Loire <(none)> Date: Thu, 21 Nov 2019 19:15:36 +0100 Subject: [PATCH] Add recursive pagination request looping on the history endpoint --- lib/history.js | 65 +++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/lib/history.js b/lib/history.js index 19550ec..996704b 100644 --- a/lib/history.js +++ b/lib/history.js @@ -12,30 +12,45 @@ const request = require('superagent'); const endpoints = require('../util/endpoints'); -module.exports = (headers, account_id, start_date, end_date) => { - const endpoint = endpoints['history'](account_id); - return request - .get(`${endpoint}`) - .query({ - 'start-date': `${start_date}T07:00:00.000Z` - }) - .query({ - 'end-date': `${end_date}T07:00:00.000Z` - }) - .set(headers) - .send() - .then(res => { - const { - body: { - data: { - items - } - } - } = res; +const doRequest = (headers, endpoint, start_date, end_date, pageOffset) => { + return request + .get(`${endpoint}`) + .query({ + 'start-date': `${start_date}T07:00:00.000Z` + }) + .query({ + 'end-date': `${end_date}T07:00:00.000Z` + }) + .query({ + 'page-offset': pageOffset + }) + .set(headers) + .send() + .then(res => { + const { + body: { + pagination, + data: { + items + } + } + } = res; + return { items, pagination }; + }) + .catch(err => { + return err.message; + }); +} - return items; - }) - .catch(err => { - return err.message; - }); +module.exports = async (headers, account_id, start_date, end_date) => { + const endpoint = endpoints['history'](account_id); + const items = []; + let currentPage = -1; + let keepGoing = true; + while (keepGoing) { // run through pagination + const r = await doRequest(headers, endpoint, start_date, end_date, ++currentPage); + items.push.apply(items, r.items); + keepGoing = currentPage < r.pagination['total-pages'] - 1; + } + return items; }