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

feat: Added support of replyToList in the library, #339: #1303

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't look like async is supported in node 6/8 which we still support

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, will look into it and do the required changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shwetha-manvinkurke - I removed the use of async in the test case as you suggested

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": "[email protected]"
},
{
"name": "Support Test Team",
"email": "[email protected]"
}
];
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": "[email protected]"
};
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": "[email protected]"
},
{
"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": "[email protected]"
};
data["replyToList"] = [
{
"name": "Test Team",
"email": "[email protected]"
},
{
"name": "Support Test Team",
"email": "[email protected]"
}
];
return expect(function() {
sgMail.send(data, false, {});
}).to.throw(Error);
});
});