Skip to content

Commit

Permalink
fix bug in updateBatchSubscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
diegodrip committed Nov 5, 2024
1 parent 872a464 commit f421792
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,16 @@ var batch = {
}]
}

client.updateBatchSubscribers(batch, function (errors, responses, bodies) {
// Do stuff
}
)
client.updateBatchSubscribers(batch, (errors, responses, bodies) => {
// Do stuff...
if (errors) {
console.error('Some requests failed:', errors);
} else {
console.log('All requests succeeded');
}
console.log('Responses:', responses);
console.log('Bodies:', bodies);
});
```

### Sending a batch of events
Expand Down
49 changes: 23 additions & 26 deletions lib/subscribers.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,26 @@ module.exports = {
* @param {callback} callback - Required. A callback
*/

// TODO: Return an optional Promise
updateBatchSubscribers(payload, callback) {
const subscribers = payload && payload.batches && payload.batches[0].subscribers || [];
updateBatchSubscribers: async function (payload, callback) {
const subscribers = (payload && payload.batches && payload.batches[0].subscribers) || [];
const batchSize = 1000;
const batches = [];
const errors = [];
const responses = [];
const bodies = [];
const headers = this.requestHeaders();
let done = 0;
let hasError = false;

// Break the payload into batch-sized chunks
for (let i = 0, j = subscribers.length; i < j; i += batchSize) {
batches.push(subscribers.slice(i, batchSize));
for (let i = 0; i < subscribers.length; i += batchSize) {
batches.push(subscribers.slice(i, i + batchSize));
}

batches.forEach((batch, batchIndex) => {
request.post(
{
// Map each batch to an axios request and store the results
await Promise.all(
batches.map((batch, batchIndex) =>
request({
method: 'post',
url: `${helpers.baseUrl}v2/${this.accountId}/subscribers/batches`,
headers,
responseType: 'json',
Expand All @@ -112,24 +112,21 @@ module.exports = {
subscribers: batch
}]
}
},
(error, response, body) => {
errors[batchIndex] = error;
})
.then((response) => {
responses[batchIndex] = response;
bodies[batchIndex] = body;
hasError = hasError || error;
done += 1;
bodies[batchIndex] = response.data;
errors[batchIndex] = null;
})
.catch((error) => {
errors[batchIndex] = error;
responses[batchIndex] = null;
bodies[batchIndex] = null;
hasError = true;
})
)
);

if (done === batches.length) {
// All batches complete; call back
callback(
hasError ? errors : null,
responses,
bodies
);
}
}
);
});
callback(hasError ? errors : null, responses, bodies);
}
};
26 changes: 13 additions & 13 deletions spec/lib/subscribers_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const request = require('axios');
const axios = require('axios');
const sinon = require('sinon');
const client = require('../../lib/index')({ token: 'abc123', accountId: 9999999 });

Expand Down Expand Up @@ -96,12 +96,11 @@ describe('Subscribers with callback', () => {
};

beforeEach(() => {
sinon.stub(request, 'post')
.yields(null, { statusCode: 201 }, {});
sinon.stub(axios, 'request').resolves({ status: 201, data: {} });
});

afterEach(() => {
request.post.restore();
axios.request.restore();
});

it('should post batches of subscribers and call request with post', (done) => {
Expand All @@ -113,7 +112,7 @@ describe('Subscribers with callback', () => {
expect(responses[0].statusCode).toBe(201);
expect(responses[1].statusCode).toBe(201);
expect(bodies).toEqual([{}, {}]);
expect(request.post.callCount).toBe(2);
expect(axios.request.callCount).toBe(2);
});
done();
});
Expand All @@ -127,13 +126,11 @@ describe('Subscribers with callback', () => {
};

beforeEach(() => {
sinon.stub(request, 'post')
.yields(null, { statusCode: 201 }, {});
spyOn(request, 'post').and.callThrough();
sinon.stub(axios, 'request').resolves({ status: 201, data: {} });
});

afterEach(() => {
request.post.restore();
axios.request.restore();
});

it('should set the correct request URL', (done) => {
Expand All @@ -143,9 +140,9 @@ describe('Subscribers with callback', () => {
expect(responses[0].statusCode).toBe(201);
expect(bodies).toEqual([{}]);
});
done();

expect(request.post).toHaveBeenCalledWith({

expect(axios.request.calledWith({
method: 'post',
url: 'https://api.getdrip.com/v2/9999999/subscribers/batches',
headers: client.requestHeaders(),
responseType: 'json',
Expand All @@ -154,7 +151,10 @@ describe('Subscribers with callback', () => {
subscribers: [undefined]
}]
}
}, jasmine.any(Function));
}));

done();

});
});

Expand Down

0 comments on commit f421792

Please sign in to comment.