Add click and open tracking

This commit is contained in:
KMKoushik
2024-04-10 08:35:03 +10:00
parent dab3d7ad25
commit ffad4050de
7 changed files with 156 additions and 47 deletions

View File

@@ -7,7 +7,11 @@ import {
teamProcedure,
} from "~/server/api/trpc";
import { db } from "~/server/db";
import { createDomain, getDomain } from "~/server/service/domain-service";
import {
createDomain,
getDomain,
updateDomain,
} from "~/server/service/domain-service";
export const domainRouter = createTRPCRouter({
createDomain: teamProcedure
@@ -21,6 +25,9 @@ export const domainRouter = createTRPCRouter({
where: {
teamId: ctx.team.id,
},
orderBy: {
createdAt: "desc",
},
});
return domains;
@@ -31,4 +38,19 @@ export const domainRouter = createTRPCRouter({
.query(async ({ ctx, input }) => {
return getDomain(input.id);
}),
updateDomain: teamProcedure
.input(
z.object({
id: z.number(),
clickTracking: z.boolean().optional(),
openTracking: z.boolean().optional(),
})
)
.mutation(async ({ ctx, input }) => {
return updateDomain(input.id, {
clickTracking: input.clickTracking,
openTracking: input.openTracking,
});
}),
});

View File

@@ -78,6 +78,12 @@ async function setupSESConfiguration() {
topicArn,
[...GENERAL_EVENTS, "OPEN"]
);
await setWebhookConfiguration(APP_SETTINGS.SES_CONFIGURATION_FULL, topicArn, [
...GENERAL_EVENTS,
"CLICK",
"OPEN",
]);
}
async function setWebhookConfiguration(

View File

@@ -67,3 +67,13 @@ export async function getDomain(id: number) {
return domain;
}
export async function updateDomain(
id: number,
data: { clickTracking?: boolean; openTracking?: boolean }
) {
return db.domain.update({
where: { id },
data,
});
}

View File

@@ -1,6 +1,7 @@
import { EmailContent } from "~/types";
import { db } from "../db";
import { sendEmailThroughSes } from "../ses";
import { APP_SETTINGS } from "~/utils/constants";
export async function sendEmail(
emailContent: EmailContent & { teamId: number }
@@ -30,6 +31,10 @@ export async function sendEmail(
text,
html,
region: domain.region,
configurationSetName: getConfigurationSetName(
domain.clickTracking,
domain.openTracking
),
});
if (messageId) {
@@ -47,3 +52,20 @@ export async function sendEmail(
});
}
}
function getConfigurationSetName(
clickTracking: boolean,
openTracking: boolean
) {
if (clickTracking && openTracking) {
return APP_SETTINGS.SES_CONFIGURATION_FULL;
}
if (clickTracking) {
return APP_SETTINGS.SES_CONFIGURATION_CLICK_TRACKING;
}
if (openTracking) {
return APP_SETTINGS.SES_CONFIGURATION_OPEN_TRACKING;
}
return APP_SETTINGS.SES_CONFIGURATION_GENERAL;
}

View File

@@ -97,8 +97,10 @@ export async function sendEmailThroughSes({
text,
html,
region = "us-east-1",
configurationSetName,
}: EmailContent & {
region?: string;
configurationSetName: string;
}) {
const sesClient = getSesClient(region);
const command = new SendEmailCommand({
@@ -128,6 +130,7 @@ export async function sendEmailThroughSes({
},
},
},
ConfigurationSetName: configurationSetName,
});
try {