diff --git a/examples/customers.js b/examples/customers.js index 718b23f..c9d58e6 100644 --- a/examples/customers.js +++ b/examples/customers.js @@ -7,9 +7,9 @@ const voucherify = voucherifyClient({ clientSecretKey: '3266b9f8-e246-4f79-bdf0-833929b1380c' }) -async function scrollCustomers() { - for await (const customer of voucherify.customers.scroll({limit: 5})) { - console.log("Customer", customer) +async function scrollCustomers () { + for await (const customer of voucherify.customers.scroll({ limit: 5 })) { + console.log('Customer', customer) } } diff --git a/package.json b/package.json index c0552da..72689d9 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,6 @@ }, "scripts": { "test": "mocha", - "lint": "standard" + "lint": "standard --fix" } } diff --git a/src/Customers.js b/src/Customers.js index 603e7bf..6bfd47c 100644 --- a/src/Customers.js +++ b/src/Customers.js @@ -28,7 +28,7 @@ module.exports = class Customers { async * scroll (params = {}) { let startingAfter = params.starting_after let endingBefore = params.ending_before - let direction = (startingAfter || !endingBefore) ? 'desc' : 'asc' + const direction = (startingAfter || !endingBefore) ? 'desc' : 'asc' let response = await this.client.get('/customers', Object.assign( {}, params, { @@ -39,7 +39,7 @@ module.exports = class Customers { while (true) { if (response.customers.length === 0) { - break; + break } for (const customer of response.customers) { // comparing ISOs @@ -51,7 +51,7 @@ module.exports = class Customers { yield customer } if (!response.has_more) { - break; + break } response = await this.client.get( diff --git a/test/customers-api.spec.js b/test/customers-api.spec.js index 83f28aa..c2d8795 100644 --- a/test/customers-api.spec.js +++ b/test/customers-api.spec.js @@ -82,25 +82,26 @@ describe('Customers API', function () { }) }) - describe('scroll', async function () { it('should scroll customers descending', async function () { var server = nock('https://api.voucherify.io', reqWithoutBody) .get('/v1/customers') .query({ filters: 'value', starting_after: '1970-01-01T00:00:00Z' }) - .reply(200, {has_more: true, customers: [{created_at: '2020-01-01T00:00:00Z'}, {created_at: '2020-01-02T00:00:00Z'}]}) + .reply(200, { has_more: true, customers: [{ created_at: '2020-01-01T00:00:00Z' }, { created_at: '2020-01-02T00:00:00Z' }] }) .get('/v1/customers') .query({ filters: 'value', starting_after: '2020-01-02T00:00:00Z' }) - .reply(200, {has_more: false, customers: [{created_at: '2020-01-03T00:00:00Z'}]}) - + .reply(200, { has_more: false, customers: [{ created_at: '2020-01-03T00:00:00Z' }] }) let callCount = 0 + const customers = [] for await (const customer of client.customers.scroll({ filters: 'value' })) { ++callCount + customers.push(customer) } expect(callCount).to.equal(3) + expect(customers[0]).to.eql({ created_at: '2020-01-01T00:00:00Z' }) await server.done() }) @@ -108,17 +109,20 @@ describe('Customers API', function () { var server = nock('https://api.voucherify.io', reqWithoutBody) .get('/v1/customers') .query({ filters: 'value', starting_after: '2019-12-31T23:59:00Z' }) - .reply(200, {has_more: true, customers: [{created_at: '2020-01-01T00:00:00Z'}, {created_at: '2020-01-02T00:00:00Z'}]}) + .reply(200, { has_more: true, customers: [{ created_at: '2020-01-01T00:00:00Z' }, { created_at: '2020-01-02T00:00:00Z' }] }) .get('/v1/customers') .query({ filters: 'value', starting_after: '2020-01-02T00:00:00Z' }) - .reply(200, {has_more: false, customers: [{created_at: '2020-01-03T00:00:00Z'}]}) + .reply(200, { has_more: false, customers: [{ created_at: '2020-01-03T00:00:00Z' }] }) let callCount = 0 + const customers = [] for await (const customer of client.customers.scroll({ starting_after: '2019-12-31T23:59:00Z', filters: 'value' })) { ++callCount + customers.push(customer) } expect(callCount).to.equal(3) + expect(customers[0]).to.eql({ created_at: '2020-01-01T00:00:00Z' }) await server.done() }) @@ -126,45 +130,53 @@ describe('Customers API', function () { var server = nock('https://api.voucherify.io', reqWithoutBody) .get('/v1/customers') .query({ filters: 'value', starting_after: '2019-12-31T23:59:00Z', ending_before: '2020-12-31T23:59:00Z' }) - .reply(200, {has_more: true, customers: [{created_at: '2020-01-01T00:00:00Z'}, {created_at: '2020-01-02T00:00:00Z'}]}) + .reply(200, { has_more: true, customers: [{ created_at: '2020-01-01T00:00:00Z' }, { created_at: '2020-01-02T00:00:00Z' }] }) .get('/v1/customers') - .query({ filters: 'value', starting_after: '2020-01-02T00:00:00Z', ending_before : '2020-12-31T23:59:00Z' }) - .reply(200, {has_more: false, customers: [{created_at: '2020-01-03T00:00:00Z'}]}) + .query({ filters: 'value', starting_after: '2020-01-02T00:00:00Z', ending_before: '2020-12-31T23:59:00Z' }) + .reply(200, { has_more: false, customers: [{ created_at: '2020-01-03T00:00:00Z' }] }) let callCount = 0 + const customers = [] for await (const customer of client.customers.scroll({ starting_after: '2019-12-31T23:59:00Z', - ending_before : '2020-12-31T23:59:00Z', + ending_before: '2020-12-31T23:59:00Z', filters: 'value' })) { ++callCount + customers.push(customer) } expect(callCount).to.equal(3) + expect(customers[0]).to.eql({ created_at: '2020-01-01T00:00:00Z' }) await server.done() }) it('should scroll customers ascending', async function () { - let now = new Date().toISOString() + const now = new Date().toISOString() var server = nock('https://api.voucherify.io', reqWithoutBody) .get('/v1/customers') .query({ ending_before: now, filters: 'value' }) - .reply(200, {has_more: true, customers: [ - {created_at: '2020-01-04T00:00:00Z'}, - {created_at: '2020-01-03T00:00:00Z'}, - {created_at: '2020-01-02T00:00:00Z'} - ]}) + .reply(200, { + has_more: true, + customers: [ + { created_at: '2020-01-04T00:00:00Z' }, + { created_at: '2020-01-03T00:00:00Z' }, + { created_at: '2020-01-02T00:00:00Z' } + ] + }) .get('/v1/customers') .query({ filters: 'value', ending_before: '2020-01-02T00:00:00Z' }) - .reply(200, {has_more: false, customers: [{created_at: '2020-01-01T00:00:00Z'}]}) - + .reply(200, { has_more: false, customers: [{ created_at: '2020-01-01T00:00:00Z' }] }) let callCount = 0 + const customers = [] for await (const customer of client.customers.scroll({ filters: 'value', ending_before: now })) { ++callCount + customers.push(customer) } expect(callCount).to.equal(4) + expect(customers[0]).to.eql({ created_at: '2020-01-04T00:00:00Z' }) await server.done() }) })