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.

simplify deletedomain links

Steven Teyb7084bdac422
diff --git a/apps/web/app/api/cron/domains/delete/route.ts b/apps/web/app/api/cron/domains/delete/route.ts
index 8acf80e6eb..f75c969080 100644
--- a/apps/web/app/api/cron/domains/delete/route.ts
+++ b/apps/web/app/api/cron/domains/delete/route.ts
@@ -1,7 +1,11 @@
-import { deleteDomainAndLinks } from "@/lib/api/domains/delete-domain-links";
+import { queueDomainDeletion } from "@/lib/api/domains/queue";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { linkCache } from "@/lib/api/links/cache";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { storage } from "@/lib/storage";
+import { recordLink } from "@/lib/tinybird/record-link";
import { prisma } from "@dub/prisma";
+import { R2_URL } from "@dub/utils";
import { z } from "zod";
export const dynamic = "force-dynamic";
@@ -30,12 +34,97 @@ export async function POST(req: Request) {
return new Response(`Domain ${domain} not found. Skipping...`);
}
- await deleteDomainAndLinks({
- domain,
- workspaceId,
+ const links = await prisma.link.findMany({
+ where: {
+ domain,
+ },
+ include: {
+ tags: true,
+ },
+ take: 100, // TODO: We can adjust this number based on the performance
+ });
+
+ if (links.length === 0) {
+ return new Response("No more links to delete. Exiting...");
+ }
+
+ const response = await Promise.allSettled([
+ // Remove the link from Redis
+ linkCache.deleteMany(links),
+
+ // Record link in the Tinybird
+ recordLink(
+ links.map((link) => ({
+ link_id: link.id,
+ domain: link.domain,
+ key: link.key,
+ url: link.url,
+ tag_ids: link.tags.map((tag) => tag.id),
+ workspace_id: workspaceId,
+ program_id: link.programId ?? "",
+ created_at: link.createdAt,
+ deleted: true,
+ })),
+ ),
+
+ // Remove image from R2 storage if it exists
+ links
+ .filter((link) => link.image?.startsWith(`${R2_URL}/images/${link.id}`))
+ .map((link) => storage.delete(link.image!.replace(`${R2_URL}/`, ""))),
+
+ // Remove the link from MySQL
+ prisma.link.deleteMany({
+ where: {
+ id: { in: links.map((link) => link.id) },
+ },
+ }),
+ ]);
+
+ console.log(response);
+
+ response.forEach((promise) => {
+ if (promise.status === "rejected") {
+ console.error("deleteDomainAndLinks", {
+ reason: promise.reason,
+ domain,
+ workspaceId,
+ });
+ }
+ });
+
+ const remainingLinks = await prisma.link.count({
+ where: {
+ domain,
+ },
});
- return new Response("Domain deleted.");
+ console.log("remainingLinks", remainingLinks);
+
+ if (remainingLinks > 0) {
+ await queueDomainDeletion({
+ workspaceId,
+ domain,
+ delay: 2,
+ });
+ return new Response(
+ `Deleted ${links.length} links, ${remainingLinks} remaining. Starting next batch...`,
+ );
+ }
+
+ // After all links are deleted, delete the domain and image
+ await Promise.all([
+ prisma.domain.delete({
+ where: {
+ slug: domain,
+ },
+ }),
+ domainRecord.logo &&
+ storage.delete(domainRecord.logo.replace(`${R2_URL}/`, "")),
+ ]);
+
+ return new Response(
+ `Deleted ${links.length} links, no more links remaining. Domain deleted.`,
+ );
} catch (error) {
return handleAndReturnErrorResponse(error);
}
diff --git a/apps/web/lib/api/domains/claim-dot-link-domain.ts b/apps/web/lib/api/domains/claim-dot-link-domain.ts
index f940a782e0..f84d71fe2d 100644
--- a/apps/web/lib/api/domains/claim-dot-link-domain.ts
+++ b/apps/web/lib/api/domains/claim-dot-link-domain.ts
@@ -13,7 +13,7 @@ import { waitUntil } from "@vercel/functions";
import { sendEmail } from "emails";
import DomainClaimed from "emails/domain-claimed";
import { addDomainToVercel } from "./add-domain-vercel";
-import { markDomainAsDeleted } from "./delete-domain-links";
+import { markDomainAsDeleted } from "./mark-domain-deleted";
export async function claimDotLinkDomain({
domain,
diff --git a/apps/web/lib/api/domains/delete-domain-links.ts b/apps/web/lib/api/domains/delete-domain-links.ts
deleted file mode 100644
index ef2fc1c2e4..0000000000
--- a/apps/web/lib/api/domains/delete-domain-links.ts
+++ /dev/null
@@ -1,147 +0,0 @@
-import { prisma } from "@dub/prisma";
-import { R2_URL } from "@dub/utils";
-import { storage } from "../../storage";
-import { recordLink } from "../../tinybird";
-import { linkCache } from "../links/cache";
-import { queueDomainDeletion } from "./queue";
-import { removeDomainFromVercel } from "./remove-domain-vercel";
-
-// Delete a domain and all links & images associated with it
-export async function deleteDomainAndLinks({
- domain,
- workspaceId,
-}: {
- domain: string;
- workspaceId: string;
-}) {
- const links = await prisma.link.findMany({
- where: {
- domain,
- },
- include: {
- tags: true,
- },
- take: 100, // TODO: We can adjust this number based on the performance
- });
-
- if (links.length === 0) {
- return;
- }
-
- const response = await Promise.allSettled([
- // Record link in the Tinybird
- recordLink(
- links.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.id),
- workspace_id: workspaceId,
- program_id: link.programId ?? "",
- created_at: link.createdAt,
- deleted: true,
- })),
- ),
-
- // Remove image from R2 storage if it exists
- links
- .filter((link) => link.image?.startsWith(`${R2_URL}/images/${link.id}`))
- .map((link) => storage.delete(link.image!.replace(`${R2_URL}/`, ""))),
-
- // Remove the link from Redis
- linkCache.deleteMany(links),
-
- // Remove the link from MySQL
- prisma.link.deleteMany({
- where: {
- id: { in: links.map((link) => link.id) },
- },
- }),
- ]);
-
- response.forEach((promise) => {
- if (promise.status === "rejected") {
- console.error("deleteDomainAndLinks", {
- reason: promise.reason,
- domain,
- workspaceId,
- });
- }
- });
-
- const remainingLinks = await prisma.link.count({
- where: {
- domain,
- },
- });
-
- if (remainingLinks > 0) {
- return await queueDomainDeletion({
- workspaceId,
- domain,
- delay: 2,
- });
- }
-
- // After all links are deleted, delete the domain and image
- const domainRecord = await prisma.domain.delete({
- where: {
- slug: domain,
- },
- });
-
- if (domainRecord.logo) {
- await storage.delete(domainRecord.logo.replace(`${R2_URL}/`, ""));
- }
-}
-
-// Mark the domain as deleted
-// We'll delete the domain and its links via a cron job
-export async function markDomainAsDeleted({
- domain,
- workspaceId,
- delay,
-}: {
- domain: string;
- workspaceId: string;
- delay?: number; // delay the cron job to avoid hitting rate limits
-}) {
- const links = await prisma.link.updateMany({
- where: {
- domain,
- },
- data: {
- projectId: null,
- },
- });
-
- const response = await Promise.allSettled([
- removeDomainFromVercel(domain),
-
- prisma.domain.update({
- where: {
- slug: domain,
- },
- data: {
- projectId: null,
- },
- }),
- ]);
-
- await queueDomainDeletion({
- workspaceId,
- domain,
- delay,
- });
-
- response.forEach((promise) => {
- if (promise.status === "rejected") {
- console.error("markDomainAsDeleted", {
- reason: promise.reason,
- domain,
- workspaceId,
- });
- }
- });
-}
diff --git a/apps/web/lib/api/domains/index.ts b/apps/web/lib/api/domains/index.ts
index 416d21d6e6..bda835ee4a 100644
--- a/apps/web/lib/api/domains/index.ts
+++ b/apps/web/lib/api/domains/index.ts
@@ -1,7 +1,7 @@
export * from "./add-domain-vercel";
-export * from "./delete-domain-links";
export * from "./get-config-response";
export * from "./get-domain-response";
+export * from "./mark-domain-deleted";
export * from "./remove-domain-vercel";
export * from "./utils";
export * from "./verify-domain";
diff --git a/apps/web/lib/api/domains/mark-domain-deleted.ts b/apps/web/lib/api/domains/mark-domain-deleted.ts
new file mode 100644
index 0000000000..6ad48f8b2d
--- /dev/null
+++ b/apps/web/lib/api/domains/mark-domain-deleted.ts
@@ -0,0 +1,53 @@
+import { prisma } from "@dub/prisma";
+import { queueDomainDeletion } from "./queue";
+import { removeDomainFromVercel } from "./remove-domain-vercel";
+
+// Mark the domain as deleted
+// We'll delete the domain and its links via a cron job
+export async function markDomainAsDeleted({
+ domain,
+ workspaceId,
+ delay,
+}: {
+ domain: string;
+ workspaceId: string;
+ delay?: number; // delay the cron job to avoid hitting rate limits
+}) {
+ const links = await prisma.link.updateMany({
+ where: {
+ domain,
+ },
+ data: {
+ projectId: null,
+ },
+ });
+
+ const response = await Promise.allSettled([
+ removeDomainFromVercel(domain),
+
+ prisma.domain.update({
+ where: {
+ slug: domain,
+ },
+ data: {
+ projectId: null,
+ },
+ }),
+ ]);
+
+ await queueDomainDeletion({
+ workspaceId,
+ domain,
+ delay,
+ });
+
+ response.forEach((promise) => {
+ if (promise.status === "rejected") {
+ console.error("markDomainAsDeleted", {
+ reason: promise.reason,
+ domain,
+ workspaceId,
+ });
+ }
+ });
+}