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.

improvement

Steven Tey8995c4d4d4ca
diff --git a/app/admin/actions.ts b/app/admin/actions.ts
index f080cc5354..f2e79917ab 100644
--- a/app/admin/actions.ts
+++ b/app/admin/actions.ts
@@ -1,5 +1,6 @@
"use server";
import { deleteUserLinks } from "#/lib/api/links";
+import { deleteProject } from "#/lib/api/project";
import { hashToken } from "#/lib/auth";
import { DUB_PROJECT_ID } from "#/lib/constants";
import prisma from "#/lib/prisma";
@@ -150,6 +151,7 @@ export async function getUserByKey(data: FormData) {
select: {
name: true,
slug: true,
+ plan: true,
domains: {
select: {
slug: true,
@@ -186,6 +188,10 @@ export async function getUserByKey(data: FormData) {
return {
email: response?.email as string,
hostnames: Array.from(hostnames),
+ proProjectSlugs:
+ projects
+ .filter(({ project }) => project.plan === "pro")
+ .map(({ project }) => project.slug) || [],
verifiedDomains: verifiedDomains || [],
impersonateUrl: await getImpersonateUrl(email),
};
@@ -207,6 +213,21 @@ export async function banUser(data: FormData) {
},
select: {
id: true,
+ projects: {
+ where: {
+ role: "owner",
+ },
+ select: {
+ project: {
+ select: {
+ id: true,
+ slug: true,
+ logo: true,
+ stripeId: true,
+ },
+ },
+ },
+ },
},
});
@@ -245,6 +266,14 @@ export async function banUser(data: FormData) {
}),
},
).then((res) => res.json()),
+ ...user.projects.map(({ project }) =>
+ deleteProject({
+ id: project.id,
+ slug: project.slug,
+ stripeId: project.stripeId || undefined,
+ logo: project.logo || undefined,
+ }),
+ ),
]);
const response = await prisma.user.delete({
diff --git a/app/admin/components/ban.tsx b/app/admin/components/ban.tsx
index 447c382d24..532b201818 100644
--- a/app/admin/components/ban.tsx
+++ b/app/admin/components/ban.tsx
@@ -14,12 +14,15 @@ export default function BanUser() {
const [data, setData] = useState<{
email: string;
hostnames: string[];
+ proProjectSlugs: string[];
verifiedDomains: string[];
impersonateUrl: string;
} | null>(null);
const [copied, setCopied] = useState(false);
const router = useRouter();
+ console.log(data?.proProjectSlugs);
+
return (
<div className="flex flex-col space-y-5">
<form
@@ -38,12 +41,24 @@ export default function BanUser() {
</form>
{data && (
<form
- action={(data) =>
- banUser(data).then(() => {
+ action={(formData) => {
+ if (
+ data.proProjectSlugs.length > 0 ||
+ data.verifiedDomains.length > 0
+ ) {
+ if (
+ !confirm(
+ `This user has ${data.proProjectSlugs.length} pro projects and ${data.verifiedDomains.length} verified domains. Are you sure you want to ban them?`,
+ )
+ ) {
+ return;
+ }
+ }
+ banUser(formData).then(() => {
toast.success("Successfully banned user");
router.refresh();
- })
- }
+ });
+ }}
className="flex w-full flex-col space-y-4 rounded-md border-t border-gray-200 py-4"
>
<div className="flex w-full items-center space-x-3">
@@ -74,6 +89,19 @@ export default function BanUser() {
)}
</button>
</div>
+ {data.proProjectSlugs.length > 0 && (
+ <div className="flex flex-col space-y-2">
+ <p className="text-sm font-semibold">Pro Projects</p>
+ {data.proProjectSlugs.map((slug) => (
+ <div
+ key={slug}
+ className="rounded-md border border-gray-300 p-2 text-sm text-gray-500"
+ >
+ {slug}
+ </div>
+ ))}
+ </div>
+ )}
{data.verifiedDomains.length > 0 && (
<div className="flex flex-col space-y-2">
<p className="text-sm font-semibold">Verified Domains</p>
@@ -99,7 +127,7 @@ export default function BanUser() {
<label htmlFor={hostname}>{hostname}</label>
</div>
))}
- <BanButton disabled={data.verifiedDomains.length > 0} />
+ <BanButton />
</form>
)}
</div>
@@ -146,14 +174,7 @@ const Form = () => {
);
};
-const BanButton = ({ disabled }: { disabled?: boolean }) => {
+const BanButton = () => {
const { pending } = useFormStatus();
- return (
- <Button
- text="Confirm Ban"
- loading={pending}
- disabled={disabled}
- variant="danger"
- />
- );
+ return <Button text="Confirm Ban" loading={pending} variant="danger" />;
};
diff --git a/lib/api/project.ts b/lib/api/project.ts
new file mode 100644
index 0000000000..805217a3b7
--- /dev/null
+++ b/lib/api/project.ts
@@ -0,0 +1,51 @@
+import prisma from "#/lib/prisma";
+import { ProjectProps } from "../types";
+import { deleteDomainAndLinks } from "#/lib/api/domains";
+import { cancelSubscription } from "#/lib/stripe";
+import cloudinary from "cloudinary";
+
+export async function deleteProject(
+ project: Pick<ProjectProps, "id" | "slug" | "stripeId" | "logo">,
+) {
+ const domains = (
+ await prisma.domain.findMany({
+ where: {
+ projectId: project.id,
+ },
+ select: {
+ slug: true,
+ },
+ })
+ ).map((domain) => domain.slug);
+
+ // delete all domains, links, and uploaded images associated with the project
+ const deleteDomainsResponse = await Promise.allSettled(
+ domains.map((domain) =>
+ deleteDomainAndLinks(domain, {
+ // here, we don't need to delete in prisma because we're deleting the project later and have onDelete: CASCADE set
+ skipPrismaDelete: true,
+ }),
+ ),
+ );
+
+ const deleteProjectResponse = await Promise.all([
+ // delete project logo from Cloudinary
+ project.logo &&
+ cloudinary.v2.uploader.destroy(`logos/${project.id}`, {
+ invalidate: true,
+ }),
+ // if they have a Stripe subscription, cancel it
+ project.stripeId && cancelSubscription(project.stripeId),
+ // delete the project
+ prisma.project.delete({
+ where: {
+ slug: project.slug,
+ },
+ }),
+ ]);
+
+ return {
+ deleteProjectResponse,
+ deleteDomainsResponse,
+ };
+}
diff --git a/pages/api/projects/[slug]/index.ts b/pages/api/projects/[slug]/index.ts
index c00dc85286..4ab2817a18 100644
--- a/pages/api/projects/[slug]/index.ts
+++ b/pages/api/projects/[slug]/index.ts
@@ -3,6 +3,7 @@ import { deleteDomainAndLinks } from "#/lib/api/domains";
import prisma from "#/lib/prisma";
import { cancelSubscription } from "#/lib/stripe";
import cloudinary from "cloudinary";
+import { deleteProject } from "#/lib/api/project";
export default withProjectAuth(
async (req, res, project, session) => {
@@ -31,47 +32,9 @@ export default withProjectAuth(
}
// DELETE /api/projects/[slug] – delete a project
} else if (req.method === "DELETE") {
- const domains = (
- await prisma.domain.findMany({
- where: {
- projectId: project.id,
- },
- select: {
- slug: true,
- },
- })
- ).map((domain) => domain.slug);
-
- // delete all domains, links, and uploaded images associated with the project
- const deleteDomainsResponse = await Promise.allSettled(
- domains.map((domain) =>
- deleteDomainAndLinks(domain, {
- // here, we don't need to delete in prisma because we're deleting the project later and have onDelete: CASCADE set
- skipPrismaDelete: true,
- }),
- ),
- );
-
- const deleteProjectResponse = await Promise.all([
- // delete project logo from Cloudinary
- project.logo &&
- cloudinary.v2.uploader.destroy(`logos/${project.id}`, {
- invalidate: true,
- }),
- // if they have a Stripe subscription, cancel it
- project.stripeId && cancelSubscription(project.stripeId),
- // delete the project
- prisma.project.delete({
- where: {
- slug: project.slug,
- },
- }),
- ]);
+ const response = await deleteProject(project);
- return res.status(200).json({
- deleteProjectResponse,
- deleteDomainsResponse,
- });
+ return res.status(200).json(response);
} else {
res.setHeader("Allow", ["GET", "PUT", "DELETE"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);