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.

Refactor bulk link deletion to include workspace across multiple API routes.

Kiran Kb5d55b63cc13
diff --git a/apps/web/app/(ee)/api/cron/cleanup/e2e-tests/route.ts b/apps/web/app/(ee)/api/cron/cleanup/e2e-tests/route.ts
index 2c82bc0132..e68df4270c 100644
--- a/apps/web/app/(ee)/api/cron/cleanup/e2e-tests/route.ts
+++ b/apps/web/app/(ee)/api/cron/cleanup/e2e-tests/route.ts
@@ -24,77 +24,88 @@ export async function GET(req: Request) {
const oneHourAgo = new Date(Date.now() - 1000 * 60 * 60);
- const [links, domains, tags, partners, users] = await Promise.all([
- prisma.link.findMany({
- where: {
- userId: E2E_USER_ID,
- projectId: E2E_WORKSPACE_ID,
- createdAt: {
- lt: oneHourAgo,
+ const [links, domains, tags, partners, users, workspace] =
+ await Promise.all([
+ prisma.link.findMany({
+ where: {
+ userId: E2E_USER_ID,
+ projectId: E2E_WORKSPACE_ID,
+ createdAt: {
+ lt: oneHourAgo,
+ },
},
- },
- include: {
- tags: {
- select: {
- tag: true,
+ include: {
+ tags: {
+ select: {
+ tag: true,
+ },
},
},
- },
- take: 100,
- }),
-
- prisma.domain.findMany({
- where: {
- projectId: E2E_WORKSPACE_ID,
- slug: {
- endsWith: ".dub-internal-test.com",
+ take: 100,
+ }),
+
+ prisma.domain.findMany({
+ where: {
+ projectId: E2E_WORKSPACE_ID,
+ slug: {
+ endsWith: ".dub-internal-test.com",
+ },
+ createdAt: {
+ lt: oneHourAgo,
+ },
},
- createdAt: {
- lt: oneHourAgo,
+ select: {
+ slug: true,
},
- },
- select: {
- slug: true,
- },
- }),
+ }),
- prisma.tag.findMany({
- where: {
- projectId: E2E_WORKSPACE_ID,
- name: {
- startsWith: "e2e-",
- },
- createdAt: {
- lt: oneHourAgo,
+ prisma.tag.findMany({
+ where: {
+ projectId: E2E_WORKSPACE_ID,
+ name: {
+ startsWith: "e2e-",
+ },
+ createdAt: {
+ lt: oneHourAgo,
+ },
},
- },
- }),
+ }),
- prisma.partner.findMany({
- where: {
- email: {
- endsWith: "@dub-internal-test.com",
+ prisma.partner.findMany({
+ where: {
+ email: {
+ endsWith: "@dub-internal-test.com",
+ },
+ createdAt: {
+ lt: oneHourAgo,
+ },
},
- createdAt: {
- lt: oneHourAgo,
+ select: {
+ id: true,
},
- },
- select: {
- id: true,
- },
- }),
+ }),
- prisma.user.findMany({
- where: {
- email: {
- endsWith: "@dub-internal-test.com",
+ prisma.user.findMany({
+ where: {
+ email: {
+ endsWith: "@dub-internal-test.com",
+ },
+ createdAt: {
+ lt: oneHourAgo,
+ },
+ },
+ }),
+
+ prisma.project.findUniqueOrThrow({
+ where: {
+ id: E2E_WORKSPACE_ID,
},
- createdAt: {
- lt: oneHourAgo,
+ select: {
+ id: true,
+ stripeConnectId: true,
},
- },
- }),
- ]);
+ }),
+ ]);
// Delete the links
if (links.length > 0) {
@@ -107,7 +118,10 @@ export async function GET(req: Request) {
});
// Post delete cleanup
- await bulkDeleteLinks(links);
+ await bulkDeleteLinks({
+ links,
+ workspace,
+ });
}
// Delete the domains
@@ -136,6 +150,7 @@ export async function GET(req: Request) {
if (partners.length > 0) {
await bulkDeletePartners({
partnerIds: partners.map((partner) => partner.id),
+ workspace,
});
}
diff --git a/apps/web/app/(ee)/api/cron/workspaces/delete/route.ts b/apps/web/app/(ee)/api/cron/workspaces/delete/route.ts
index ec101cdf02..404ad76a0d 100644
--- a/apps/web/app/(ee)/api/cron/workspaces/delete/route.ts
+++ b/apps/web/app/(ee)/api/cron/workspaces/delete/route.ts
@@ -55,7 +55,10 @@ export async function POST(req: Request) {
},
}),
- bulkDeleteLinks(links),
+ bulkDeleteLinks({
+ links,
+ workspace,
+ }),
]);
console.log(res);
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 0bfc4a5c68..a2dc40bf0a 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -549,7 +549,12 @@ export const DELETE = withWorkspace(
},
});
- waitUntil(bulkDeleteLinks(links));
+ waitUntil(
+ bulkDeleteLinks({
+ links,
+ workspace,
+ }),
+ );
return NextResponse.json(
{
diff --git a/apps/web/lib/actions/partners/delete-program-invite.ts b/apps/web/lib/actions/partners/delete-program-invite.ts
index 6cea83dc06..63d2e17fc5 100644
--- a/apps/web/lib/actions/partners/delete-program-invite.ts
+++ b/apps/web/lib/actions/partners/delete-program-invite.ts
@@ -55,7 +55,10 @@ export const deleteProgramInviteAction = authActionClient
where: { id: { in: linksToDelete.map((link) => link.id) } },
}),
- bulkDeleteLinks(linksToDelete),
+ bulkDeleteLinks({
+ links: linksToDelete,
+ workspace,
+ }),
recordAuditLog({
workspaceId: workspace.id,
diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.ts
index 413250e225..5982233525 100644
--- a/apps/web/lib/api/links/bulk-delete-links.ts
+++ b/apps/web/lib/api/links/bulk-delete-links.ts
@@ -1,16 +1,24 @@
import { storage } from "@/lib/storage";
+import { disableStripePromotionCode } from "@/lib/stripe/disable-promotion-code";
import { recordLinkTB, transformLinkTB } from "@/lib/tinybird";
+import { WorkspaceProps } from "@/lib/types";
import { prisma } from "@dub/prisma";
import { R2_URL } from "@dub/utils";
import { linkCache } from "./cache";
import { ExpandedLink } from "./utils";
-export async function bulkDeleteLinks(links: ExpandedLink[]) {
+export async function bulkDeleteLinks({
+ links,
+ workspace,
+}: {
+ links: ExpandedLink[];
+ workspace: Pick<WorkspaceProps, "id" | "stripeConnectId">;
+}) {
if (links.length === 0) {
return;
}
- return await Promise.all([
+ return await Promise.allSettled([
// Delete the links from Redis
linkCache.deleteMany(links),
@@ -30,11 +38,20 @@ export async function bulkDeleteLinks(links: ExpandedLink[]) {
// Update totalLinks for the workspace
prisma.project.update({
where: {
- id: links[0].projectId!,
+ id: workspace.id,
},
data: {
totalLinks: { decrement: links.length },
},
}),
+
+ links
+ .filter((link) => link.couponCode)
+ .map((link) =>
+ disableStripePromotionCode({
+ code: link.couponCode,
+ stripeConnectId: workspace.stripeConnectId,
+ }),
+ ),
]);
}
diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.ts
index c498a8b580..bf373b69bb 100644
--- a/apps/web/lib/api/links/delete-link.ts
+++ b/apps/web/lib/api/links/delete-link.ts
@@ -1,4 +1,5 @@
import { storage } from "@/lib/storage";
+import { disableStripePromotionCode } from "@/lib/stripe/disable-promotion-code";
import { recordLinkTB, transformLinkTB } from "@/lib/tinybird";
import { prisma } from "@dub/prisma";
import { R2_URL } from "@dub/utils";
@@ -14,6 +15,11 @@ export async function deleteLink(linkId: string) {
},
include: {
...includeTags,
+ project: {
+ select: {
+ stripeConnectId: true,
+ },
+ },
},
});
@@ -42,6 +48,13 @@ export async function deleteLink(linkId: string) {
totalLinks: { decrement: 1 },
},
}),
+
+ link.couponCode &&
+ link.project &&
+ disableStripePromotionCode({
+ code: link.couponCode,
+ stripeConnectId: link.project.stripeConnectId,
+ }),
]),
);
diff --git a/apps/web/lib/api/partners/bulk-delete-partners.ts b/apps/web/lib/api/partners/bulk-delete-partners.ts
index c4bc69d084..ddba02bd27 100644
--- a/apps/web/lib/api/partners/bulk-delete-partners.ts
+++ b/apps/web/lib/api/partners/bulk-delete-partners.ts
@@ -1,3 +1,4 @@
+import { WorkspaceProps } from "@/lib/types";
import { prisma } from "@dub/prisma";
import { bulkDeleteLinks } from "../links/bulk-delete-links";
@@ -5,8 +6,10 @@ import { bulkDeleteLinks } from "../links/bulk-delete-links";
// currently only used for the cron/cleanup/e2e-tests job
export async function bulkDeletePartners({
partnerIds,
+ workspace,
}: {
partnerIds: string[];
+ workspace: Pick<WorkspaceProps, "id" | "stripeConnectId">;
}) {
const partners = await prisma.partner.findMany({
where: {
@@ -38,7 +41,10 @@ export async function bulkDeletePartners({
},
});
- await bulkDeleteLinks(linksToDelete);
+ await bulkDeleteLinks({
+ links: linksToDelete,
+ workspace,
+ });
await prisma.link.deleteMany({
where: {
diff --git a/apps/web/lib/api/partners/delete-partner.ts b/apps/web/lib/api/partners/delete-partner.ts
index 19f584bf7b..05b818139c 100644
--- a/apps/web/lib/api/partners/delete-partner.ts
+++ b/apps/web/lib/api/partners/delete-partner.ts
@@ -1,12 +1,19 @@
import { storage } from "@/lib/storage";
import { stripe } from "@/lib/stripe";
+import { WorkspaceProps } from "@/lib/types";
import { prisma } from "@dub/prisma";
import { R2_URL } from "@dub/utils";
import { bulkDeleteLinks } from "../links/bulk-delete-links";
// delete partner and all associated links, customers, payouts, and commissions
-// currently only used for the cron/cleanup/e2e-tests job
-export async function deletePartner({ partnerId }: { partnerId: string }) {
+// Not using this anymore
+export async function deletePartner({
+ partnerId,
+ workspace,
+}: {
+ partnerId: string;
+ workspace: Pick<WorkspaceProps, "id" | "stripeConnectId">;
+}) {
const partner = await prisma.partner.findUnique({
where: {
id: partnerId,
@@ -36,7 +43,10 @@ export async function deletePartner({ partnerId }: { partnerId: string }) {
},
});
- await bulkDeleteLinks(links);
+ await bulkDeleteLinks({
+ links,
+ workspace,
+ });
await prisma.link.deleteMany({
where: {
diff --git a/apps/web/lib/stripe/disable-promotion-code.ts b/apps/web/lib/stripe/disable-promotion-code.ts
index 0d0ce4e66a..34ff734f4d 100644
--- a/apps/web/lib/stripe/disable-promotion-code.ts
+++ b/apps/web/lib/stripe/disable-promotion-code.ts
@@ -9,13 +9,14 @@ export async function disableStripePromotionCode({
code,
stripeConnectId,
}: {
- code: string;
+ code: string | null;
stripeConnectId: string | null;
}) {
+ if (!code) {
+ return;
+ }
+
if (!stripeConnectId) {
- console.error(
- "stripeConnectId not found for the workspace. Stripe promotion code update skipped.",
- );
return;
}
@@ -25,9 +26,6 @@ export async function disableStripePromotionCode({
});
if (promotionCodes.data.length === 0) {
- console.error(
- `Promotion code ${code} not found in the connected account ${stripeConnectId}. Stripe promotion code update skipped.`,
- );
return;
}