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.

improve admin feature

Steven Tey2d0b4fbe4918
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/components/delete-partner-account.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/components/delete-partner-account.tsx
index 7c6df201da..607d57d715 100644
--- a/apps/web/app/(ee)/admin.dub.co/(dashboard)/components/delete-partner-account.tsx
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/components/delete-partner-account.tsx
@@ -10,14 +10,30 @@ export default function DeletePartnerAccount() {
<div className="flex flex-col space-y-5">
<form
action={async (formData) => {
+ const deletePartnerAccount =
+ formData.get("deletePartnerAccount") === "on";
+ const message = deletePartnerAccount
+ ? "Are you sure you want to delete this partner account completely? This will also delete the partner account along with their Stripe express account. This action cannot be undone."
+ : "Are you sure you want to delete this partner's Stripe express account? This action cannot be undone.";
+ const confirmed = window.confirm(message);
+ if (!confirmed) {
+ return;
+ }
+
await fetch("/api/admin/delete-partner-account", {
method: "POST",
body: JSON.stringify({
email: formData.get("email"),
+ deletePartnerAccount:
+ formData.get("deletePartnerAccount") === "on",
}),
}).then(async (res) => {
if (res.ok) {
- toast.success("Partner account deleted!");
+ toast.success(
+ deletePartnerAccount
+ ? "Partner account deleted!"
+ : "Stripe express account deleted!",
+ );
} else {
const error = await res.text();
toast.error(error);
@@ -35,34 +51,51 @@ const Form = () => {
const { pending } = useFormStatus();
return (
- <div className="relative flex w-full rounded-md shadow-sm">
- <input
- name="email"
- id="email"
- type="email"
- required
- disabled={pending}
- autoComplete="off"
- className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- pending && "bg-neutral-100",
+ <div className="flex flex-col space-y-4">
+ <div className="relative flex w-full rounded-md shadow-sm">
+ <input
+ name="email"
+ id="email"
+ type="email"
+ required
+ disabled={pending}
+ autoComplete="off"
+ className={cn(
+ "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ pending && "bg-neutral-100",
+ )}
+ onPaste={(e: React.ClipboardEvent<HTMLInputElement>) => {
+ // remove mailto: on paste
+ e.preventDefault();
+ const text = e.clipboardData.getData("text/plain");
+ if (text.startsWith("mailto:")) {
+ e.currentTarget.value = text.replace("mailto:", "");
+ } else {
+ e.currentTarget.value = text;
+ }
+ }}
+ placeholder="[email protected]"
+ aria-invalid="true"
+ />
+ {pending && (
+ <LoadingSpinner className="absolute inset-y-0 right-2 my-auto h-full w-5 text-neutral-400" />
)}
- onPaste={(e: React.ClipboardEvent<HTMLInputElement>) => {
- // remove mailto: on paste
- e.preventDefault();
- const text = e.clipboardData.getData("text/plain");
- if (text.startsWith("mailto:")) {
- e.currentTarget.value = text.replace("mailto:", "");
- } else {
- e.currentTarget.value = text;
- }
- }}
- placeholder="[email protected]"
- aria-invalid="true"
- />
- {pending && (
- <LoadingSpinner className="absolute inset-y-0 right-2 my-auto h-full w-5 text-neutral-400" />
- )}
+ </div>
+ <div className="flex items-center space-x-2">
+ <input
+ name="deletePartnerAccount"
+ id="deletePartnerAccount"
+ type="checkbox"
+ disabled={pending}
+ className="h-4 w-4 rounded border-neutral-300 text-neutral-600 focus:ring-neutral-500"
+ />
+ <label
+ htmlFor="deletePartnerAccount"
+ className="text-sm text-neutral-700"
+ >
+ Delete partner account as well
+ </label>
+ </div>
</div>
);
};
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/page.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/page.tsx
index 5e66894145..f219885fb6 100644
--- a/apps/web/app/(ee)/admin.dub.co/(dashboard)/page.tsx
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/page.tsx
@@ -31,17 +31,16 @@ export default function AdminPage() {
<BanLink />
</div>
<div className="flex flex-col space-y-4 px-5 py-10">
- <h2 className="text-xl font-semibold">Delete Partner Account</h2>
+ <h2 className="text-xl font-semibold">Delete Stripe Express Account</h2>
<p className="text-sm text-neutral-500">
- Delete a partner's account and associated Stripe express account.{" "}
- <br />
+ Delete a partner's Stripe express account (and potentially their
+ partner account as well). <br />
<br />
Caveats:
- <br />
- - If partner has already received commission on Dub, the partner
- profile won't be deleted.
- <br />- If partner has already received payouts via Stripe, the Stripe
- express account won't be deleted.
+ <br />- If the partner has already received payouts via Stripe, their
+ Stripe Express account won't be deleted.
+ <br />- If the partner has already received commissions or leads on
+ Dub, their partner account won't be deleted.
</p>
<DeletePartnerAccount />
</div>
diff --git a/apps/web/app/(ee)/api/admin/delete-partner-account/route.ts b/apps/web/app/(ee)/api/admin/delete-partner-account/route.ts
index 53bb3b11da..811346c4f7 100644
--- a/apps/web/app/(ee)/api/admin/delete-partner-account/route.ts
+++ b/apps/web/app/(ee)/api/admin/delete-partner-account/route.ts
@@ -6,7 +6,7 @@ import { NextResponse } from "next/server";
// POST /api/admin/delete-partner-account
export const POST = withAdmin(async ({ req }) => {
- const { email } = await req.json();
+ const { email, deletePartnerAccount } = await req.json();
const partner = await prisma.partner.findUnique({
where: {
@@ -28,10 +28,58 @@ export const POST = withAdmin(async ({ req }) => {
return new Response("Partner not found", { status: 404 });
}
- if (partner.commissions.length === 0) {
- console.log(
- "Partner has no commissions yet, deleting program links and customers...",
- );
+ if (partner.stripeConnectId) {
+ // check if stripe express account has received payouts before
+ const transfers = await stripe.transfers.list({
+ destination: partner.stripeConnectId,
+ limit: 1,
+ });
+
+ if (transfers.data.length > 0) {
+ return new Response(
+ "Stripe express account has received payouts before and cannot be deleted.",
+ {
+ status: 400,
+ },
+ );
+ }
+
+ try {
+ await stripe.accounts.del(partner.stripeConnectId);
+ console.log("Deleted Stripe express account: ", partner.stripeConnectId);
+ await prisma.partner.update({
+ where: {
+ id: partner.id,
+ },
+ data: {
+ stripeConnectId: null,
+ payoutsEnabledAt: null,
+ payoutMethodHash: null,
+ },
+ });
+ } catch (error) {}
+ }
+
+ if (deletePartnerAccount) {
+ if (partner.commissions.length > 0) {
+ return new Response(
+ "Partner has already received commissions and cannot be deleted.",
+ {
+ status: 400,
+ },
+ );
+ }
+ if (
+ partner.programs.some(({ links }) => links.some((link) => link.leads > 0))
+ ) {
+ return new Response(
+ "Partner has already received leads and cannot be deleted.",
+ {
+ status: 400,
+ },
+ );
+ }
+
if (partner.programs.length > 0) {
for (const { program, links, groupId } of partner.programs) {
if (links.length > 0) {
@@ -50,16 +98,9 @@ export const POST = withAdmin(async ({ req }) => {
})),
{ deleted: true },
),
- prisma.customer.deleteMany({
- where: {
- linkId: {
- in: links.map((link) => link.id),
- },
- },
- }),
]);
console.log(
- `Deleted ${links.length} links and it's customers for program ${program.name} (${program.slug})`,
+ `Deleted ${links.length} links for program ${program.name} (${program.slug})`,
);
}
}
@@ -71,42 +112,6 @@ export const POST = withAdmin(async ({ req }) => {
},
});
console.log("Deleted partner", deletedPartner);
- } else {
- console.log(
- `Partner has already received ${partner.commissions.length} commissions (total: $${partner.commissions.reduce((acc, commission) => acc + commission.amount, 0)}) and cannot be deleted...`,
- );
- }
-
- if (partner.stripeConnectId) {
- // check if stripe express account has received payouts before
- const transfers = await stripe.transfers.list({
- destination: partner.stripeConnectId,
- limit: 1,
- });
-
- if (transfers.data.length > 0) {
- return new Response(
- "Stripe express account has received payouts before and cannot be deleted.",
- {
- status: 400,
- },
- );
- }
-
- try {
- await stripe.accounts.del(partner.stripeConnectId);
- console.log("Deleted Stripe express account: ", partner.stripeConnectId);
- await prisma.partner.update({
- where: {
- id: partner.id,
- },
- data: {
- stripeConnectId: null,
- payoutsEnabledAt: null,
- payoutMethodHash: null,
- },
- });
- } catch (error) {}
}
return NextResponse.json({ success: true });