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.

Merge branch 'main' into redis

Kiran K16f5ce653749
diff --cc apps/web/lib/api/domains/delete-domain-links.ts
index 8d9b71e097,7390a97672..9e51d48093
--- a/apps/web/lib/api/domains/delete-domain-links.ts
+++ b/apps/web/lib/api/domains/delete-domain-links.ts
@@@ -1,120 -1,15 +1,121 @@@
+import { qstash } from "@/lib/cron";
import { prisma } from "@/lib/prisma";
-import { redis } from "@/lib/upstash";
-import { R2_URL } from "@dub/utils";
-import { waitUntil } from "@vercel/functions";
+import { APP_DOMAIN_WITH_NGROK, R2_URL } from "@dub/utils";
import { storage } from "../../storage";
import { recordLink } from "../../tinybird";
+import { linkCache } from "../links/cache";
import { removeDomainFromVercel } from "./remove-domain-vercel";
-/* Delete a domain and all links & images associated with it */
-export async function deleteDomainAndLinks(domain: string) {
- const [domainData, allLinks] = await Promise.all([
- prisma.domain.findUnique({
+// 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
+ await prisma.domain.delete({
+ where: {
+ slug: domain,
+ },
+ });
+}
+
+// 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,
},
diff --cc apps/web/lib/api/links/bulk-delete-links.ts
index 8717d9b4d5,e5521af014..f9d4109641
--- a/apps/web/lib/api/links/bulk-delete-links.ts
+++ b/apps/web/lib/api/links/bulk-delete-links.ts
@@@ -1,16 -1,13 +1,16 @@@
+import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird";
-import { redis } from "@/lib/upstash";
import { R2_URL } from "@dub/utils";
- import { Link, Tag } from "@prisma/client";
+ import { Link } from "@prisma/client";
+import { linkCache } from "./cache";
export async function bulkDeleteLinks({
links,
+ workspaceId,
}: {
- links: (Link & { tags: Pick<Tag, "id">[] })[];
+ links: (Link & { tags: { tagId: string }[] })[];
+ workspaceId: string;
}) {
if (links.length === 0) {
return;