Skip to content

Commit 71b2a5a

Browse files
authored
fix bug in updateBatchSubscribers (#49)
1 parent 872a464 commit 71b2a5a

3 files changed

Lines changed: 45 additions & 43 deletions

File tree

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,15 @@ var batch = {
245245
}]
246246
}
247247

248-
client.updateBatchSubscribers(batch, function (errors, responses, bodies) {
249-
// Do stuff
250-
}
251-
)
248+
client.updateBatchSubscribers(batch, (errors, responses, bodies) => {
249+
if (errors) {
250+
console.error('Some requests failed:', errors);
251+
} else {
252+
console.log('All requests succeeded');
253+
}
254+
console.log('Responses:', responses);
255+
console.log('Bodies:', bodies);
256+
});
252257
```
253258

254259
### Sending a batch of events

lib/subscribers.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,26 @@ module.exports = {
8484
* @param {callback} callback - Required. A callback
8585
*/
8686

87-
// TODO: Return an optional Promise
88-
updateBatchSubscribers(payload, callback) {
89-
const subscribers = payload && payload.batches && payload.batches[0].subscribers || [];
87+
updateBatchSubscribers: async function (payload, callback) {
88+
const subscribers = (payload && payload.batches && payload.batches[0].subscribers) || [];
9089
const batchSize = 1000;
9190
const batches = [];
9291
const errors = [];
9392
const responses = [];
9493
const bodies = [];
9594
const headers = this.requestHeaders();
96-
let done = 0;
9795
let hasError = false;
9896

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

104-
batches.forEach((batch, batchIndex) => {
105-
request.post(
106-
{
102+
// Map each batch to an axios request and store the results
103+
await Promise.all(
104+
batches.map((batch, batchIndex) =>
105+
request({
106+
method: 'post',
107107
url: `${helpers.baseUrl}v2/${this.accountId}/subscribers/batches`,
108108
headers,
109109
responseType: 'json',
@@ -112,24 +112,21 @@ module.exports = {
112112
subscribers: batch
113113
}]
114114
}
115-
},
116-
(error, response, body) => {
117-
errors[batchIndex] = error;
115+
})
116+
.then((response) => {
118117
responses[batchIndex] = response;
119-
bodies[batchIndex] = body;
120-
hasError = hasError || error;
121-
done += 1;
118+
bodies[batchIndex] = response.data;
119+
errors[batchIndex] = null;
120+
})
121+
.catch((error) => {
122+
errors[batchIndex] = error;
123+
responses[batchIndex] = null;
124+
bodies[batchIndex] = null;
125+
hasError = true;
126+
})
127+
)
128+
);
122129

123-
if (done === batches.length) {
124-
// All batches complete; call back
125-
callback(
126-
hasError ? errors : null,
127-
responses,
128-
bodies
129-
);
130-
}
131-
}
132-
);
133-
});
130+
callback(hasError ? errors : null, responses, bodies);
134131
}
135132
};

spec/lib/subscribers_spec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const request = require('axios');
1+
const axios = require('axios');
22
const sinon = require('sinon');
33
const client = require('../../lib/index')({ token: 'abc123', accountId: 9999999 });
44

@@ -96,12 +96,11 @@ describe('Subscribers with callback', () => {
9696
};
9797

9898
beforeEach(() => {
99-
sinon.stub(request, 'post')
100-
.yields(null, { statusCode: 201 }, {});
99+
sinon.stub(axios, 'request').resolves({ status: 201, data: {} });
101100
});
102101

103102
afterEach(() => {
104-
request.post.restore();
103+
axios.request.restore();
105104
});
106105

107106
it('should post batches of subscribers and call request with post', (done) => {
@@ -113,7 +112,7 @@ describe('Subscribers with callback', () => {
113112
expect(responses[0].statusCode).toBe(201);
114113
expect(responses[1].statusCode).toBe(201);
115114
expect(bodies).toEqual([{}, {}]);
116-
expect(request.post.callCount).toBe(2);
115+
expect(axios.request.callCount).toBe(2);
117116
});
118117
done();
119118
});
@@ -127,13 +126,11 @@ describe('Subscribers with callback', () => {
127126
};
128127

129128
beforeEach(() => {
130-
sinon.stub(request, 'post')
131-
.yields(null, { statusCode: 201 }, {});
132-
spyOn(request, 'post').and.callThrough();
129+
sinon.stub(axios, 'request').resolves({ status: 201, data: {} });
133130
});
134131

135132
afterEach(() => {
136-
request.post.restore();
133+
axios.request.restore();
137134
});
138135

139136
it('should set the correct request URL', (done) => {
@@ -143,9 +140,9 @@ describe('Subscribers with callback', () => {
143140
expect(responses[0].statusCode).toBe(201);
144141
expect(bodies).toEqual([{}]);
145142
});
146-
done();
147-
148-
expect(request.post).toHaveBeenCalledWith({
143+
144+
expect(axios.request.calledWith({
145+
method: 'post',
149146
url: 'https://api.getdrip.com/v2/9999999/subscribers/batches',
150147
headers: client.requestHeaders(),
151148
responseType: 'json',
@@ -154,7 +151,10 @@ describe('Subscribers with callback', () => {
154151
subscribers: [undefined]
155152
}]
156153
}
157-
}, jasmine.any(Function));
154+
}));
155+
156+
done();
157+
158158
});
159159
});
160160

0 commit comments

Comments
 (0)