Skip to content

Commit 1af6adf

Browse files
committed
Merge pull request #146 from SparkPost/145-single-suppression
Fixes #145
2 parents e6a2d10 + 7349971 commit 1af6adf

File tree

4 files changed

+44
-72
lines changed

4 files changed

+44
-72
lines changed

examples/suppressionList/upsert.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@
33
var key = 'YOURAPIKEY'
44
, SparkPost = require('sparkpost')
55
, client = new SparkPost(key)
6-
, recipient = {
7-
8-
, transactional: false
9-
, non_transactional: true
10-
, description: 'Test description'
11-
};
6+
, recipients = [
7+
{
8+
9+
, transactional: false
10+
, non_transactional: true
11+
, description: 'Test description 1'
12+
},
13+
{
14+
15+
, transactional: true
16+
, non_transactional: true
17+
, description: 'Test description 2'
18+
},
19+
{
20+
21+
, transactional: true
22+
, non_transactional: false
23+
, description: 'Test description 3'
24+
}
25+
];
1226

13-
client.suppressionList.upsert(recipient, function(err, res) {
27+
client.suppressionList.upsert(recipients, function(err, res) {
1428
if (err) {
1529
console.log(err);
1630
} else {

examples/suppressionList/upsert_bulk.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

lib/suppressionList.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,29 @@ module.exports = function(client) {
4545
};
4646
client['delete'](options, callback);
4747
},
48-
upsert: function(recipient, callback) {
49-
if(typeof recipient === 'function') {
50-
callback = recipient;
51-
recipient = null;
48+
upsert: function(recipients, callback) {
49+
var options;
50+
51+
if(typeof recipients === 'function') {
52+
callback = recipients;
53+
recipients = null;
5254
}
5355

54-
if(!recipient) {
56+
if(!recipients) {
5557
callback(new Error('recipient is required'));
5658
return;
5759
}
5860

59-
var options = {
60-
uri: api + '/' + recipient.email
61-
, json: recipient
62-
};
63-
64-
// Check for bulk upsert
65-
if(recipient.constructor === Array) {
66-
options.uri = api;
67-
options.json = { recipients: toApiFormat(recipient) };
68-
} else if(!recipient.email) {
69-
callback(new Error('email is required in the recipient object'));
70-
return;
61+
if(!Array.isArray(recipients)) {
62+
recipients = [recipients];
7163
}
7264

65+
recipients = toApiFormat(recipients);
66+
options = {
67+
uri: api,
68+
json: { recipients: recipients }
69+
};
70+
7371
client.put(options, callback);
7472
}
7573
};

test/spec/suppressionList.spec.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,15 @@ describe('Suppression List Library', function() {
7979
});
8080

8181
describe('upsert Method', function() {
82-
it('should call client put method with the appropriate uri', function(done) {
83-
suppressionList.upsert({email: '[email protected]'}, function(err, data) {
84-
expect(client.put.firstCall.args[0].uri).to.equal('suppression-list/[email protected]');
85-
done();
86-
});
87-
});
88-
89-
it('should accept an array of recipients for bulk upsert', function(done) {
82+
it('should accept an array of recipients', function(done) {
9083
var recipients = [
91-
{email: '[email protected]'}
92-
, {email: '[email protected]'}
84+
{ email: '[email protected]' },
85+
{ email: '[email protected]' }
9386
];
9487

9588
suppressionList.upsert(recipients, function(err, data) {
9689
expect(client.put.firstCall.args[0].uri).to.equal('suppression-list');
90+
expect(client.put.firstCall.args[0].json.recipients).to.deep.equal(recipients);
9791
done();
9892
});
9993
});
@@ -114,10 +108,10 @@ describe('Suppression List Library', function() {
114108
});
115109
});
116110

117-
it('should throw an error if email is missing on recipient object', function(done) {
118-
suppressionList.upsert({name: 'test'}, function(err, data) {
119-
expect(err.message).to.equal('email is required in the recipient object');
120-
expect(client.put).not.to.have.been.called;
111+
it('should upsert a single recipient as an array', function(done) {
112+
var recipient = { email: 'test1@test.com' };
113+
suppressionList.upsert(recipient, function(err, data) {
114+
expect(client.put.firstCall.args[0].json.recipients).to.deep.equal([recipient]);
121115
done();
122116
});
123117
});

0 commit comments

Comments
 (0)