Skip to content

Commit

Permalink
fix(mails): Deactivate link tracking for login
Browse files Browse the repository at this point in the history
  • Loading branch information
Baboo7 committed Mar 24, 2023
1 parent da8d1c4 commit 3f52ef3
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions packages/server/src/modules/notifications/services/mails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mailClient from "@sendgrid/mail";
import mailClient, { MailDataRequired } from "@sendgrid/mail";
import invariant from "tiny-invariant";
import { logger } from "../../../logger";

Expand All @@ -22,11 +22,23 @@ interface TemplateData {
};
}

interface TemplateConfig {
templateId: string;
asmGroupId?: number;
trackingSettings?: MailDataRequired["trackingSettings"];
}

const TEMPLATE_NAME_TO_CONFIG: {
[k in TemplateName]: { templateId: string; asmGroupId?: number };
[k in TemplateName]: TemplateConfig;
} = {
"login-magic-link": {
templateId: "d-a64abce41def4af0915688059ed632ac",
trackingSettings: {
clickTracking: {
enable: false,
enableText: false,
},
},
},
};

Expand All @@ -37,42 +49,39 @@ export async function sendMail<T extends TemplateName>(
templateName: T,
templateData: TemplateData[T]
) {
const config = TEMPLATE_NAME_TO_CONFIG[templateName];
const templateConfig = TEMPLATE_NAME_TO_CONFIG[templateName];

if (!config) {
if (!templateConfig) {
throw new Error(`Could not find config for email ${templateName}`);
}

await doSendEmail({
to,
templateConfig,
templateData,
templateId: config.templateId,
asmGroupId: config.asmGroupId,
});
}

async function doSendEmail({
to,
templateId,
templateConfig,
templateData,
asmGroupId,
}: {
to: string;
templateId: string;
templateConfig: TemplateConfig;
templateData: TemplateData[TemplateName];
asmGroupId?: number;
}) {
try {
await mailClient.send({
from: {
email: MAIL_SENDER,
name: MAIL_NAME,
},
templateId,
...(asmGroupId
templateId: templateConfig.templateId,
...(templateConfig.asmGroupId
? {
asm: {
groupId: asmGroupId,
groupId: templateConfig.asmGroupId,
},
}
: {}),
Expand All @@ -86,6 +95,7 @@ async function doSendEmail({
dynamicTemplateData: templateData as any,
},
],
...(templateConfig.trackingSettings || {}),
});
} catch (err) {
logger.error(err);
Expand Down

0 comments on commit 3f52ef3

Please sign in to comment.