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.

Trigger the renew domains workflow on charge.succeeded

Rafia3b09c50a71c
diff --git a/apps/web/app/(ee)/api/cron/domains/renewal-succeeded/route.ts b/apps/web/app/(ee)/api/cron/domains/renewal-succeeded/route.ts
index 423614605e..6370e30310 100644
--- a/apps/web/app/(ee)/api/cron/domains/renewal-succeeded/route.ts
+++ b/apps/web/app/(ee)/api/cron/domains/renewal-succeeded/route.ts
@@ -1,259 +1,31 @@
-import { parseRegisteredDomainSlugs } from "@/lib/api/domains/is-domain-registration-invoice";
-import { qstash } from "@/lib/cron";
import { withCron } from "@/lib/cron/with-cron";
-import { setRenewOption } from "@/lib/dynadot/set-renew-option";
-import { prisma } from "@/lib/prisma";
-import { sendBatchEmail } from "@dub/email";
-import DomainRenewed from "@dub/email/templates/domain-renewed";
-import {
- APP_DOMAIN_WITH_NGROK,
- chunk,
- log,
- pluralize,
- sleep,
-} from "@dub/utils";
-import { RegisteredDomain } from "@prisma/client";
-import { addDays, startOfDay } from "date-fns";
+import { dispatchWorkflows } from "@/lib/jobs/publish-workflows";
import * as z from "zod/v4";
import { logAndRespond } from "../../utils";
-export const maxDuration = 600;
-
export const dynamic = "force-dynamic";
const inputSchema = z.object({
invoiceId: z.string(),
- attempt: z.number().int().min(1).default(1),
});
-const BATCH_SIZE = 5;
-const MAX_ATTEMPTS = 3;
-const RETRY_DELAY_SECONDS = 60 * 60;
-
-// POST /api/cron/domains/renewal-succeeded - Renews domains for a given invoice
+// POST /api/cron/domains/renewal-succeeded - Deprecated, renewals run in /api/workflows/renew-domains.
+// Kept so that messages queued before the move still renew their domains. Safe to remove once none are left in QStash.
export const POST = withCron(async ({ rawBody }) => {
- const { invoiceId, attempt } = inputSchema.parse(JSON.parse(rawBody));
- const isLastAttempt = attempt >= MAX_ATTEMPTS;
+ const { invoiceId } = inputSchema.parse(JSON.parse(rawBody));
- const invoice = await prisma.invoice.findUnique({
- where: {
- id: invoiceId,
+ const { results } = await dispatchWorkflows({
+ name: "renew-domains-workflow",
+ payload: {
+ invoiceId,
},
- select: {
- id: true,
- status: true,
- type: true,
- paidAt: true,
- registeredDomains: true,
- workspaceId: true,
+ options: {
+ deduplicationId: `domain-renewal-${invoiceId}`,
+ label: invoiceId,
},
});
- if (!invoice) {
- return logAndRespond(`Invoice ${invoiceId} not found.`);
- }
-
- if (invoice.type !== "domainRenewal") {
- return logAndRespond(
- `Invoice ${invoiceId} is not a domain renewal invoice. Skipping...`,
- );
- }
-
- if (invoice.status !== "completed") {
- return logAndRespond(`Invoice ${invoiceId} is not completed. Skipping...`);
- }
-
- if (!invoice.paidAt) {
- return logAndRespond(`Invoice ${invoiceId} has no paidAt. Skipping...`);
- }
-
- const slugs = parseRegisteredDomainSlugs(invoice.registeredDomains);
-
- const domains = await prisma.registeredDomain.findMany({
- where: {
- slug: {
- in: slugs,
- },
- },
- orderBy: {
- expiresAt: "asc",
- },
- select: {
- id: true,
- slug: true,
- expiresAt: true,
- },
- });
-
- if (domains.length === 0) {
- return logAndRespond(
- `No domains found for invoice ${invoiceId}. Skipping...`,
- );
- }
-
- const todayStart = startOfDay(new Date());
- const alreadyAppliedThreshold = addDays(startOfDay(invoice.paidAt), 365);
-
- const domainsToRenew = domains.filter(
- (domain) => domain.expiresAt < alreadyAppliedThreshold,
- );
-
- const domainsFailed: typeof domains = [];
- const domainsRenewed: Pick<RegisteredDomain, "slug" | "expiresAt">[] = [];
-
- const succeeded: {
- domain: (typeof domains)[number];
- newExpiresAt: Date;
- }[] = [];
-
- for (const domainChunk of chunk(domainsToRenew, BATCH_SIZE)) {
- const results = await Promise.all(
- domainChunk.map(async (domain) => {
- const renewSucceeded = await setRenewOption({
- domain: domain.slug,
- autoRenew: true,
- logFailure: isLastAttempt,
- });
-
- return {
- domain,
- renewSucceeded,
- };
- }),
- );
-
- for (const { domain, renewSucceeded } of results) {
- if (!renewSucceeded) {
- domainsFailed.push(domain);
- continue;
- }
-
- // Extend expiration by 1 year from the later of this domain's expiry or today.
- // Remaining time is preserved when not yet expired; otherwise the new term
- // starts from today.
- const renewalBase =
- domain.expiresAt > todayStart ? domain.expiresAt : todayStart;
-
- succeeded.push({
- domain,
- newExpiresAt: addDays(renewalBase, 365),
- });
- }
-
- await sleep(500);
- }
-
- for (const updateChunk of chunk(succeeded, BATCH_SIZE)) {
- await Promise.all(
- updateChunk.map(async ({ domain, newExpiresAt }) => {
- await prisma.registeredDomain.update({
- where: {
- id: domain.id,
- },
- data: {
- expiresAt: newExpiresAt,
- autoRenewalDisabledAt: null,
- },
- });
-
- domainsRenewed.push({
- slug: domain.slug,
- expiresAt: newExpiresAt,
- });
- }),
- );
- }
-
- if (domainsFailed.length > 0) {
- console.error(
- "Failed to set auto-renew option on Dynadot for the following domains:",
- domainsFailed.map(({ slug }) => slug).join(", "),
- );
-
- // Dynadot failures are mostly short outages, so retry the invoice later instead of failing now.
- // The renewed domains are skipped on the next attempt, since their expiresAt is already extended.
- if (!isLastAttempt) {
- await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/renewal-succeeded`,
- delay: RETRY_DELAY_SECONDS,
- deduplicationId: `domain-renewal-${invoiceId}-attempt-${attempt + 1}`,
- body: {
- invoiceId,
- attempt: attempt + 1,
- },
- });
-
- return logAndRespond(
- `Failed to renew ${domainsFailed.length} domains for invoice ${invoiceId} (attempt ${attempt} of ${MAX_ATTEMPTS}). Retrying in ${RETRY_DELAY_SECONDS / 60} minutes.`,
- {
- logLevel: "warn",
- },
- );
- }
-
- await log({
- message: `Domain renewal partially failed for invoice ${invoiceId}. See the logs for more details.`,
- type: "errors",
- mention: true,
- });
-
- throw new Error(
- `Domain renewal partially failed for invoice ${invoiceId}.`,
- );
- }
-
- if (domainsRenewed.length === 0) {
- return logAndRespond(
- `Domain renewal for invoice ${invoiceId} already applied. Skipping...`,
- );
- }
-
- const workspace = await prisma.project.findUniqueOrThrow({
- where: {
- id: invoice.workspaceId,
- },
- include: {
- users: {
- where: {
- role: "owner",
- },
- select: {
- user: true,
- },
- },
- },
- });
-
- const workspaceOwners = workspace.users.filter(({ user }) => user.email);
-
- if (workspaceOwners.length === 0) {
- return logAndRespond(
- "No users found to send domain renewal success email.",
- );
- }
-
- // Email shows a single "active until" date; use the earliest new expiry.
- const earliestNewExpiresAt = domainsRenewed
- .map(({ expiresAt }) => expiresAt)
- .sort((a, b) => a.getTime() - b.getTime())[0];
-
- await sendBatchEmail(
- workspaceOwners.map(({ user }) => ({
- variant: "notifications",
- to: user.email!,
- subject: `Your ${pluralize("domain", domainsRenewed.length)} ${domainsRenewed.length === 1 ? "has" : "have"} been renewed`,
- react: DomainRenewed({
- email: user.email!,
- workspace: {
- slug: workspace.slug,
- },
- domains: domainsRenewed.map(({ slug }) => ({ slug })),
- expiresAt: earliestNewExpiresAt,
- }),
- })),
- );
-
return logAndRespond(
- `Renewed ${domainsRenewed.length}/${domains.length} domains. Domain renewal success email sent to ${workspaceOwners.length} users.`,
+ `Renew domains workflow ${results[0]?.status} for invoice ${invoiceId}.`,
);
});
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts
index 8024a13769..3bf47841aa 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts
@@ -4,6 +4,7 @@ import {
parseRegisteredDomainSlugs,
} from "@/lib/api/domains/is-domain-registration-invoice";
import { qstash } from "@/lib/cron";
+import { dispatchWorkflows } from "@/lib/jobs/publish-workflows";
import { prisma } from "@/lib/prisma";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { Invoice } from "@prisma/client";
@@ -136,17 +137,22 @@ async function processDomainRenewalInvoice({ invoice }: { invoice: Invoice }) {
return `No domains found for invoice ${invoice.id}, skipping...`;
}
- const qstashResponse = await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/renewal-succeeded`,
- deduplicationId: `domain-renewal-${invoice.id}`,
- body: {
+ const { results } = await dispatchWorkflows({
+ name: "renew-domains-workflow",
+ payload: {
invoiceId: invoice.id,
},
+ options: {
+ deduplicationId: `domain-renewal-${invoice.id}`,
+ label: invoice.id,
+ },
});
- if (qstashResponse.messageId) {
- return `Message sent to Qstash with id ${qstashResponse.messageId}`;
+ const [result] = results;
+
+ if (result?.status === "published") {
+ return `Renew domains workflow triggered for invoice ${invoice.id}`;
} else {
- return `Error sending message to Qstash: ${JSON.stringify(qstashResponse)}`;
+ return `Error triggering renew domains workflow for invoice ${invoice.id}: ${result?.lastError}`;
}
}