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.

fix self-removal bug

Steven Tey9a16c0ef6130
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
index 0e58fcf508..957b944c07 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
@@ -72,24 +72,37 @@ export const PUT = withAuth(
export const DELETE = withAuth(
async ({ searchParams, workspace, session }) => {
const { userId } = removeUserSchema.parse(searchParams);
- const projectUser = await prisma.projectUsers.findUnique({
- where: {
- userId_projectId: {
+ const [projectUser, totalOwners] = await Promise.all([
+ prisma.projectUsers.findUnique({
+ where: {
+ userId_projectId: {
+ projectId: workspace.id,
+ userId,
+ },
+ },
+ select: {
+ role: true,
+ },
+ }),
+ prisma.projectUsers.count({
+ where: {
projectId: workspace.id,
- userId,
+ role: "owner",
},
- },
- select: {
- role: true,
- },
- });
+ }),
+ ]);
if (!projectUser) {
throw new DubApiError({
code: "not_found",
message: "User not found",
});
}
- if (projectUser.role === "owner" && userId === session.user.id) {
+ // If there is only one owner and the user is an owner and the user is trying to remove themselves
+ if (
+ totalOwners === 1 &&
+ projectUser.role === "owner" &&
+ userId === session.user.id
+ ) {
throw new DubApiError({
code: "bad_request",
message: