Skip to content

Commit 4b304a7

Browse files
committed
fixed feedback
1 parent 23164f6 commit 4b304a7

File tree

2 files changed

+45
-70
lines changed

2 files changed

+45
-70
lines changed

lib/transmissions.js

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ module.exports = function(client) {
6060
* @returns {Promise}
6161
*/
6262
send: function(transmission, options, callback) {
63-
64-
const modifiedTransmission = _.cloneDeep(transmission);
65-
6663
// Handle optional options argument
6764
if (typeof options === 'function') {
6865
callback = options;
@@ -73,16 +70,11 @@ module.exports = function(client) {
7370
return Promise.reject(new Error('transmission object is required')).asCallback(callback);
7471
}
7572

76-
// format the payload if we are given an array of recipients
77-
if (_.has(modifiedTransmission, 'recipients') &&
78-
_.isArray(modifiedTransmission.recipients) &&
79-
!_.has(modifiedTransmission.recipients[0], 'list_id')) {
80-
formatPayload(modifiedTransmission);
81-
}
73+
transmission = formatPayload(transmission);
8274

8375
const reqOpts = {
8476
uri: api,
85-
json: modifiedTransmission,
77+
json: transmission,
8678
qs: options
8779
};
8880

@@ -92,7 +84,14 @@ module.exports = function(client) {
9284

9385
};
9486

95-
function formatPayload(transmission) {
87+
function formatPayload(originalTransmission) {
88+
const transmission = _.cloneDeep(originalTransmission);
89+
90+
// don't format the payload if we are not given an array of recipients
91+
if (!_.isArray(transmission.recipients)) {
92+
return transmission;
93+
}
94+
9695
// format all the original recipients to be in the object format
9796
transmission.recipients = _.map(transmission.recipients, (recipient) => {
9897
recipient.address = addressToObject(recipient.address);
@@ -101,53 +100,53 @@ function formatPayload(transmission) {
101100
});
102101

103102
// add the CC headers
104-
if (_.has(transmission, 'cc') && _.isArray(transmission.cc)) {
105-
addCCHeader(transmission);
103+
if (_.isArray(transmission.cc)) {
104+
_.set(transmission, 'content.headers.CC', generateCCHeader(transmission));
106105
}
107106

108-
addListToRecipients(transmission, 'cc');
109-
addListToRecipients(transmission, 'bcc');
107+
const headerTo = generateHeaderTo(transmission.recipients);
108+
109+
transmission.recipients = addListToRecipients(transmission, 'cc', headerTo);
110+
transmission.recipients = addListToRecipients(transmission, 'bcc', headerTo);
111+
112+
delete transmission.cc;
113+
delete transmission.bcc;
110114

111115
return transmission;
112116
}
113117

114-
function addListToRecipients(transmission, listName) {
115-
if (_.has(transmission, listName) && _.isArray(transmission[listName])) {
116-
const headerTo = generateHeaderTo(transmission.recipients);
117-
const list = transmission[listName];
118-
119-
transmission.recipients = transmission.recipients.concat(_.map(list, (recipient) => {
120-
recipient.address = addressToObject(recipient.address);
118+
function addListToRecipients(transmission, listName, headerTo) {
119+
if (!_.isArray(transmission[listName])) {
120+
return transmission.recipients;
121+
}
121122

122-
recipient.address.header_to = headerTo;
123+
return transmission.recipients.concat(_.map(transmission[listName], (recipient) => {
124+
recipient.address = addressToObject(recipient.address);
123125

124-
// remove name from address - name is only put in the header for cc and not at all for bcc
125-
if (_.has(recipient.address, 'name')) {
126-
delete recipient.address.name;
127-
}
126+
recipient.address.header_to = headerTo;
128127

129-
return recipient;
130-
}));
128+
// remove name from address - name is only put in the header for cc and not at all for bcc
129+
if (_.has(recipient.address, 'name')) {
130+
delete recipient.address.name;
131+
}
131132

132-
delete transmission[listName];
133-
}
133+
return recipient;
134+
}));
134135
}
135136

136-
function addCCHeader(transmission) {
137-
// default the headers to a blank object
138-
transmission.content.headers = transmission.content.headers || {};
139-
140-
transmission.content.headers.CC = _.map(transmission.cc, (ccRecipient) => addressToString(ccRecipient.address)).join(', ');
137+
function generateCCHeader(transmission) {
138+
return _.map(transmission.cc, (ccRecipient) => addressToString(ccRecipient.address)).join(', ');
141139
}
142140

143141
function generateHeaderTo(recipients) {
142+
// if a recipient has a header_to then it is cc'd or bcc'd and we don't want it in the header_to value
144143
const originalRecipients = _.filter(recipients, (recipient) => !_.has(recipient.address, 'header_to'));
145144

146145
return _.map(originalRecipients, (recipient) => addressToString(recipient.address)).join(', ');
147146
}
148147

149148
function addressToString(address) {
150-
if (!_.isString(address)) {
149+
if (_.isPlainObject(address)) {
151150
if (_.has(address, 'name')) {
152151
address = `"${address.name}" <${address.email}>`;
153152
} else {
@@ -164,24 +163,15 @@ function addressToObject(address) {
164163
if (_.isString(address)) {
165164
addressObject = {};
166165

167-
if (isEmail(address)) {
168-
addressObject.email = address;
169-
} else {
170-
const matches = /"?(.[^"]*)?"?\s*<(.+)>/gi.exec(address);
166+
const matches = /"?(.[^"]*)?"?\s*<(.+)>/gi.exec(address);
171167

172-
if (matches) {
173-
addressObject.name = matches[1];
174-
addressObject.email = matches[2];
175-
} else {
176-
throw new Error(`Invalid address format: ${address}`);
177-
}
168+
if (matches) {
169+
addressObject.name = matches[1];
170+
addressObject.email = matches[2];
171+
} else {
172+
addressObject.email = address;
178173
}
179174
}
180175

181176
return addressObject;
182177
}
183-
184-
function isEmail(email) {
185-
const emailRegex = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
186-
return emailRegex.test(email);
187-
}

test/spec/transmissions.spec.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,9 @@ describe('Transmissions Library', function() {
201201

202202
it('should allow a list_id and template through', function() {
203203
var transmission = {
204-
recipients: [
205-
{
206-
list_id: 'my-list-id'
207-
}
208-
],
204+
recipients: {
205+
list_id: 'my-list-id'
206+
},
209207
content: {
210208
template_id: 'my-template-id'
211209
}
@@ -217,19 +215,6 @@ describe('Transmissions Library', function() {
217215
});
218216
});
219217

220-
it('should throw an error due to an invalid string format', function() {
221-
var transmission = {
222-
recipients: [
223-
{
224-
address: '"Bob" [email protected]>'
225-
}
226-
]
227-
};
228-
229-
expect(function() {
230-
transmissions.send(transmission);
231-
}).to.throw(Error);
232-
});
233218

234219
it('should convert cc to the correct recipients and headers', function() {
235220
return transmissions.send(ccTransmission)

0 commit comments

Comments
 (0)