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.

Revert "Delete delete-partner.ts"

Kiran K2636391302e5
diff --git a/apps/web/lib/api/partners/delete-partner.ts b/apps/web/lib/api/partners/delete-partner.ts
new file mode 100644
index 0000000000..05b818139c
--- /dev/null
+++ b/apps/web/lib/api/partners/delete-partner.ts
@@ -0,0 +1,85 @@
+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
+// 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,
+ },
+ include: {
+ programs: {
+ select: {
+ links: true,
+ },
+ },
+ },
+ });
+
+ if (!partner) {
+ console.error(`Partner with id ${partnerId} not found.`);
+ return;
+ }
+
+ const links = partner.programs.length > 0 ? partner.programs[0].links : [];
+
+ if (links.length > 0) {
+ await prisma.customer.deleteMany({
+ where: {
+ linkId: {
+ in: links.map((link) => link.id),
+ },
+ },
+ });
+
+ await bulkDeleteLinks({
+ links,
+ workspace,
+ });
+
+ await prisma.link.deleteMany({
+ where: {
+ id: {
+ in: links.map((link) => link.id),
+ },
+ },
+ });
+ }
+
+ await prisma.commission.deleteMany({
+ where: {
+ partnerId: partner.id,
+ },
+ });
+
+ await prisma.payout.deleteMany({
+ where: {
+ partnerId: partner.id,
+ },
+ });
+
+ await prisma.partner.delete({
+ where: {
+ id: partner.id,
+ },
+ });
+
+ if (partner.stripeConnectId) {
+ await stripe.accounts.del(partner.stripeConnectId);
+ }
+
+ if (partner.image && partner.image.startsWith(R2_URL)) {
+ await storage.delete(partner.image.replace(`${R2_URL}/`, ""));
+ }
+}