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: Pass customData asm parameters to SendGrid #5403

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions providers/sendgrid/src/lib/sendgrid.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const mockNovuMessage = {
{ mime: 'text/plain', file: Buffer.from('dGVzdA=='), name: 'test.txt' },
],
id: 'message_id',
customData: {
asm: {
groupId: 12345,
groupsToDisplay: [12345],
},
},
};

test('should trigger sendgrid correctly', async () => {
Expand Down Expand Up @@ -71,6 +77,10 @@ test('should trigger sendgrid correctly', async () => {
},
],
templateId: undefined,
asm: {
groupId: 12345,
groupsToDisplay: [12345],
},
});
});

Expand Down
15 changes: 15 additions & 0 deletions providers/sendgrid/src/lib/sendgrid.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
import { MailDataRequired, MailService } from '@sendgrid/mail';

type AttachmentJSON = MailDataRequired['attachments'][0];
type ASMConfig = {
groupId: number;
groupsToDisplay: number[];
};
Comment on lines +16 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
type ASMConfig = {
groupId: number;
groupsToDisplay: number[];
};
type ASMConfig = {
groupId: number | string;
groupsToDisplay: Array<number | string>;
};


export class SendgridEmailProvider implements IEmailProvider {
id = 'sendgrid';
Expand Down Expand Up @@ -70,12 +74,22 @@ export class SendgridEmailProvider implements IEmailProvider {
private createMailData(options: IEmailOptions) {
const dynamicTemplateData = options.customData?.dynamicTemplateData;
const templateId = options.customData?.templateId as unknown as string;
const asm = options.customData?.asm as ASMConfig;
if (asm?.groupId) {
asm.groupId = parseInt(asm.groupId as any); // Sendgrid expects groupId to be a number
}
if (asm?.groupsToDisplay) {
asm.groupsToDisplay = asm.groupsToDisplay.map((group) =>
parseInt(group as any)
); // Sendgrid expects groupsToDisplay to be an array of numbers
}
Comment on lines +77 to +85
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: if we would like to allow passing the string or number for both fields, please create a mapper function that will create a new object with appropriate value types like:

  private mapAsmOptions(asm: ASMConfig): MailDataRequired['asm'] {
    return {
      groupId: parseInt(`${asm.groupId}`),
      groupsToDisplay: asm.groupsToDisplay.map((group) => parseInt(`${group}`)),
    };
  }
...

const asm = this.mapAsmOptions(options.customData?.asm);

/*
* deleted below values from customData to avoid passing them
* in customArgs because customArgs has max limit of 10,000 bytes
*/
delete options.customData?.dynamicTemplateData;
delete options.customData?.templateId;
delete options.customData?.asm;

const attachments = options.attachments?.map(
(attachment: IAttachmentOptions) => {
Expand Down Expand Up @@ -128,6 +142,7 @@ export class SendgridEmailProvider implements IEmailProvider {
},
],
templateId: templateId,
asm: asm,
};

if (options.replyTo) {
Expand Down