Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Move the Resend domain update to queue (#3095)

Kiran Kd9b2eae9810a
diff --git a/apps/web/app/(ee)/api/cron/email-domains/update/route.ts b/apps/web/app/(ee)/api/cron/email-domains/update/route.ts
new file mode 100644
index 0000000000..2fb8577904
--- /dev/null
+++ b/apps/web/app/(ee)/api/cron/email-domains/update/route.ts
@@ -0,0 +1,73 @@
+import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { resend } from "@dub/email/resend/client";
+import { prisma } from "@dub/prisma";
+import { log } from "@dub/utils";
+import { z } from "zod";
+import { logAndRespond } from "../../utils";
+
+const schema = z.object({
+ domainId: z.string(),
+});
+
+export const dynamic = "force-dynamic";
+
+// POST /api/cron/email-domains/update
+// Update the Resend domain to enable click tracking
+export async function POST(req: Request) {
+ try {
+ const rawBody = await req.text();
+
+ await verifyQstashSignature({ req, rawBody });
+
+ const { domainId } = schema.parse(JSON.parse(rawBody));
+
+ if (!resend) {
+ return logAndRespond("Resend is not configured. Skipping update...");
+ }
+
+ const domainRecord = await prisma.emailDomain.findUnique({
+ where: {
+ id: domainId,
+ },
+ select: {
+ slug: true,
+ resendDomainId: true,
+ },
+ });
+
+ if (!domainRecord) {
+ return logAndRespond(`Domain ${domainId} not found. Skipping update...`);
+ }
+
+ if (!domainRecord.resendDomainId) {
+ return logAndRespond(
+ `Resend domain ID is not found for domain ${domainRecord.slug}. Skipping update...`,
+ );
+ }
+
+ const { error } = await resend.domains.update({
+ id: domainRecord.resendDomainId,
+ openTracking: true,
+ clickTracking: false,
+ tls: "enforced",
+ });
+
+ // This will be retried by QStash if it fails.
+ if (error) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: error.message,
+ });
+ }
+
+ return logAndRespond(`Domain ${domainRecord.slug} updated successfully.`);
+ } catch (error) {
+ await log({
+ message: `Resend email domain update failed with error ${JSON.stringify(error)}`,
+ type: "errors",
+ });
+
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/app/(ee)/api/email-domains/[domain]/route.ts b/apps/web/app/(ee)/api/email-domains/[domain]/route.ts
index 0ec7082bc2..070dc2ae4d 100644
--- a/apps/web/app/(ee)/api/email-domains/[domain]/route.ts
+++ b/apps/web/app/(ee)/api/email-domains/[domain]/route.ts
@@ -3,12 +3,14 @@ import { DubApiError } from "@/lib/api/errors";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { qstash } from "@/lib/cron";
import {
EmailDomainSchema,
updateEmailDomainBodySchema,
} from "@/lib/zod/schemas/email-domains";
import { resend } from "@dub/email/resend";
import { prisma } from "@dub/prisma";
+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -59,28 +61,25 @@ export const PATCH = withWorkspace(
waitUntil(
(async () => {
- // Verify an existing domain
- const { error: resendVerifyError } = await resend.domains.verify(
- resendDomain.id,
- );
-
- if (resendVerifyError) {
- console.error(
- `Resend domain verify failed - ${resendVerifyError.message}`,
- );
- }
-
- // Enable open tracking for the domain
- const { error: resendUpdateError } = await resend.domains.update({
- id: resendDomain.id,
- openTracking: true,
- clickTracking: false,
- tls: "opportunistic",
+ // Moving the updates to Qstash because updating the domain immediately after creation can fail.
+ const response = await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/email-domains/update`,
+ method: "POST",
+ delay: 1 * 60, // 1 minute delay
+ body: {
+ domainId: emailDomain.id,
+ },
});
- if (resendUpdateError) {
+ if (!response.messageId) {
console.error(
- `Resend domain update failed - ${resendUpdateError.message}`,
+ `Failed to queue email domain update for domain ${emailDomain.id}`,
+ response,
+ );
+ } else {
+ console.log(
+ `Queued email domain update for domain ${emailDomain.id}`,
+ response,
);
}
})(),
diff --git a/apps/web/app/(ee)/api/email-domains/route.ts b/apps/web/app/(ee)/api/email-domains/route.ts
index 3808a4c5a4..4b46d6b142 100644
--- a/apps/web/app/(ee)/api/email-domains/route.ts
+++ b/apps/web/app/(ee)/api/email-domains/route.ts
@@ -3,12 +3,14 @@ import { DubApiError } from "@/lib/api/errors";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { qstash } from "@/lib/cron";
import {
createEmailDomainBodySchema,
EmailDomainSchema,
} from "@/lib/zod/schemas/email-domains";
import { resend } from "@dub/email/resend";
import { prisma } from "@dub/prisma";
+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -88,28 +90,25 @@ export const POST = withWorkspace(
waitUntil(
(async () => {
- // Verify an existing domain
- const { error: resendVerifyError } = await resend.domains.verify(
- resendDomain.id,
- );
-
- if (resendVerifyError) {
- console.error(
- `Resend domain verify failed - ${resendVerifyError.message}`,
- );
- }
-
- // Enable open tracking for the domain
- const { error: resendUpdateError } = await resend.domains.update({
- id: resendDomain.id,
- openTracking: true,
- clickTracking: false,
- tls: "opportunistic",
+ // Moving the updates to Qstash because updating the domain immediately after creation can fail.
+ const response = await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/email-domains/update`,
+ method: "POST",
+ delay: 1 * 60, // 1 minute delay
+ body: {
+ domainId: emailDomain.id,
+ },
});
- if (resendUpdateError) {
+ if (!response.messageId) {
console.error(
- `Resend domain update failed - ${resendUpdateError.message}`,
+ `Failed to queue email domain update for domain ${emailDomain.id}`,
+ response,
+ );
+ } else {
+ console.log(
+ `Queued email domain update for domain ${emailDomain.id}`,
+ response,
);
}
})(),