Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added CC and BCC sugar #194

Merged
merged 6 commits into from
Dec 19, 2016
Merged

added CC and BCC sugar #194

merged 6 commits into from
Dec 19, 2016

Conversation

avigoldman
Copy link
Contributor

No description provided.

@jasonrhodes
Copy link
Contributor

Haven't looked at the code at all but just want to be sure that:
a) the sugar doesn't get in the way of someone who wants to just send a CC or BCC the "hard way"
b) we shouldn't get rid of the examples of how to do it the hard way, either, I don't think -- just also show the "sugar" way (this is my opinion)

side note: why do we not mention header_to anywhere in the docs?

@avigoldman
Copy link
Contributor Author

It definitely needs more documentation around it. I always look at the docs here.
https://developers.sparkpost.com/api/recipient-lists.html#header-address-attributes

@jasonrhodes
Copy link
Contributor

jasonrhodes commented Nov 26, 2016 via email

@jasonrhodes
Copy link
Contributor

While I'm taking a look at this code, @avrahamgoldman can you use rebase -i against origin/master and squash some of these commits together? Whatever you think makes sense to combine. Let me know if you need a hand.

const reqOpts = {
uri: api,
json: transmission,
json: modifiedTransmission,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be nice to move all of the checks into formatPayload, and also collapse the first _.has check and the _.isArray check because _.isArray will return false if transmission.recipients isn't defined.

Then you don't need the const here, you can do the clone in formatPayload, and do 2 individual checks with early returns, and comments about why for each (e.g. " // return as-is if recipients is not an array") which I think will be a little clearer.

}

function addListToRecipients(transmission, listName) {
if (_.has(transmission, listName) && _.isArray(transmission[listName])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the isArray check is sufficient here too.

// default the headers to a blank object
transmission.content.headers = transmission.content.headers || {};

transmission.content.headers.CC = _.map(transmission.cc, (ccRecipient) => addressToString(ccRecipient.address)).join(', ');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use _.set() here, like _.set(transmission, 'content.headers.CC', <value>)

}

addListToRecipients(transmission, 'cc');
addListToRecipients(transmission, 'bcc');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these add* methods (addCCHeader and addListToRecipients) I think I'd usually prefer to see them return values that get added to the modified object here, instead of modifying/mutating in place. That'd mean we'd probably need to do the delete separately, here, but that might be worth it? Do you have thoughts on mutation lol

}

function isEmail(email) {
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why are we pulling down this hell upon ourselves?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. We should let the API determine if it's a valid email address.

}

function generateHeaderTo(recipients) {
const originalRecipients = _.filter(recipients, (recipient) => !_.has(recipient.address, 'header_to'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this line do? I don't see any examples where header_to is ever defined so I forget why this scenario would arise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they do a weird mix of the full syntax and the shortcut - i.e. they set the header_to for one cc'd recipient and use the shortcut for another.

}

function addListToRecipients(transmission, listName, headerTo) {
if (!_.isArray(transmission[listName])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need that check for list_id ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, that check was incorrect. I just reviewed the docs.

@avigoldman avigoldman merged commit d745867 into master Dec 19, 2016
@avigoldman avigoldman deleted the cc-and-bcc branch December 19, 2016 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants