Skip to content

Commit

Permalink
Added support of replyToList in the library, sendgrid#339:
Browse files Browse the repository at this point in the history
- Use the replyToList header to set multiple emailIds in the reply-to section of an email
- Fixed old test cases that were failing as SendGrid changed the acceptable response code to 202
  • Loading branch information
Subinoy Ghosh committed Sep 23, 2021
1 parent 85b2c70 commit 1cb6525
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 9 deletions.
8 changes: 8 additions & 0 deletions packages/helpers/classes/mail.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ export interface MailData {
dynamicTemplateData?: { [key: string]: any },

hideWarnings?: boolean,

replyToList?: EmailJSON | EmailJSON[],
}

export type MailDataRequired = MailData & (
Expand All @@ -179,6 +181,7 @@ export interface MailJSON {
batch_id?: string;
template_id?: string;
ip_pool_name?: string;
reply_to_list?: EmailJSON[];
}

export default class Mail {
Expand Down Expand Up @@ -353,4 +356,9 @@ export default class Mail {
* Create a Mail instance from given data
*/
static create(data: MailData[]): Mail[];

/**
* Set reply_to_list header from given data
*/
setReplyToList(replyToList: EmailJSON[]): void;
}
21 changes: 19 additions & 2 deletions packages/helpers/classes/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Mail {
templateId, personalizations, attachments, ipPoolName, batchId,
sections, headers, categories, category, customArgs, asm, mailSettings,
trackingSettings, substitutions, substitutionWrappers, dynamicTemplateData, isMultiple,
hideWarnings,
hideWarnings, replyToList,
} = data;

//Set data
Expand All @@ -90,6 +90,7 @@ class Mail {
this.setMailSettings(mailSettings);
this.setTrackingSettings(trackingSettings);
this.setHideWarnings(hideWarnings);
this.setReplyToList(replyToList);

if (this.isDynamic) {
this.setDynamicTemplateData(dynamicTemplateData);
Expand Down Expand Up @@ -504,7 +505,7 @@ class Mail {
from, replyTo, sendAt, subject, content, templateId,
personalizations, attachments, ipPoolName, batchId, asm,
sections, headers, categories, customArgs, mailSettings,
trackingSettings,
trackingSettings, replyToList,
} = this;

//Initialize with mandatory values
Expand Down Expand Up @@ -560,6 +561,9 @@ class Mail {
if (typeof ipPoolName !== 'undefined') {
json.ipPoolName = ipPoolName;
}
if(typeof replyToList !== 'undefined') {
json.replyToList = replyToList;
}

//Return as snake cased object
return toSnakeCase(json, ['substitutions', 'dynamicTemplateData', 'customArgs', 'headers', 'sections']);
Expand Down Expand Up @@ -667,6 +671,19 @@ class Mail {
value,
[this._checkUndefined, this._createCheckThatThrows(Array.isArray, 'Array expected for`' + propertyName + '`')]);
}

/**
* Set the replyToList from email body
*/
setReplyToList(replyToList) {
if (this._doArrayCheck('replyToList', replyToList) && replyToList.length) {
if (!replyToList.every(replyTo => replyTo && typeof replyTo.email === 'string')) {
throw new Error('Expected each replyTo to contain a `email` string');
}
// this.replyToList = EmailAddress.create(replyToList);
this.replyToList = replyToList;
}
}
}

//Export class
Expand Down
79 changes: 72 additions & 7 deletions packages/mail/src/mail.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ before(() => {
* Default mock header
*/
beforeEach(() => {
sgClient.setDefaultHeader('X-Mock', 200);
sgClient.setDefaultHeader('X-Mock', 202);
});

/**
Expand All @@ -38,12 +38,12 @@ describe('sgMail.send()', () => {
return expect(sgMail.send()).to.eventually.be.rejectedWith(Error);
});

it('should send a basic email', () => {
sgClient.setDefaultHeader('X-Mock', 201);
it('should send a basic email', async () => {
sgClient.setDefaultHeader('X-Mock', 202);
return sgMail
.send(data)
.then(([response, body]) => {
expect(response.statusCode).to.equal(201);
expect(response.statusCode).to.equal(202);
});
});

Expand All @@ -53,19 +53,84 @@ describe('sgMail.send()', () => {
}).to.throw(Error);
});

it('should include custom headers to the request', () => {
sgClient.setDefaultHeader('X-Mock', 201);
it('should include custom headers to the request', async () => {
sgClient.setDefaultHeader('X-Mock', 202);
const clientSpy = sinon.spy(sgClient, "request")
return sgMail
.send(Object.assign(data, { headers: { customHeader: "Custom Header Content" } }))
.then(([response, body]) => {
expect(response.statusCode).to.equal(201);
expect(response.statusCode).to.equal(202);
expect(clientSpy).to.have.been.calledWith(sinon.match({
url: "/v3/mail/send",
method: "POST",
headers: { customHeader: "Custom Header Content" }
}));
});
});

it('should send email with correct replyToList format', async () => {
sgClient.setDefaultHeader('X-Mock', 202);
data["replyToList"] = [
{
"name": "Test Team",
"email": "test@example.org"
},
{
"name": "Support Test Team",
"email": "support.test@example.org"
}
];
return sgMail
.send(data)
.then(([response, body]) => {
expect(response.statusCode).to.equal(202);
});
});

it('should throw error with wrong replyToList format', async () => {
sgClient.setDefaultHeader('X-Mock', 202);
data["replyToList"] = {
"name": "Support Test Team",
"email": "support.test@example.org"
};
return expect(function() {
sgMail.send(data, false, {});
}).to.throw(Error);
});

it('should throw error if any record in replyToList is without email', async () => {
data["replyToList"] = [
{
"name": "Test Team",
"email": "test@example.org"
},
{
"name": "Support Test Team"
}
];
return expect(function() {
sgMail.send(data, false, {});
}).to.throw(Error);
});

it('should throw error if both replyTo and replyToList are mentioned', async () => {
data["replyTo"] = {
"name": "Manual Tester",
"email": "manual.test@example.org"
};
data["replyToList"] = [
{
"name": "Test Team",
"email": "test@example.org"
},
{
"name": "Support Test Team",
"email": "support.test@example.org"
}
];
return expect(function() {
sgMail.send(data, false, {});
}).to.throw(Error);
});
});

0 comments on commit 1cb6525

Please sign in to comment.