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.

accounted for more cases

Steven Tey28f8a9b08085
diff --git a/apps/web/app/api/projects/[slug]/invites/route.tsx b/apps/web/app/api/projects/[slug]/invites/route.tsx
index 8598f529aa..6f16fd7a34 100644
--- a/apps/web/app/api/projects/[slug]/invites/route.tsx
+++ b/apps/web/app/api/projects/[slug]/invites/route.tsx
@@ -1,5 +1,5 @@
import { exceededLimitError } from "@/lib/api/errors";
-import { inviteChecks, inviteUser } from "@/lib/api/users";
+import { inviteUser } from "@/lib/api/users";
import { withAuth } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { NextResponse } from "next/server";
@@ -22,10 +22,28 @@ export const GET = withAuth(async ({ project }) => {
export const POST = withAuth(
async ({ req, project, session }) => {
const { email } = await req.json();
- const [alreadyInTeam, projectUserCount] = await inviteChecks({
- projectId: project.id,
- email,
- });
+
+ const [alreadyInTeam, projectUserCount, projectInviteCount] =
+ await Promise.all([
+ prisma.projectUsers.findFirst({
+ where: {
+ projectId: project.id,
+ user: {
+ email,
+ },
+ },
+ }),
+ prisma.projectUsers.count({
+ where: {
+ projectId: project.id,
+ },
+ }),
+ prisma.projectInvite.count({
+ where: {
+ projectId: project.id,
+ },
+ }),
+ ]);
if (alreadyInTeam) {
return new Response("User already exists in this project.", {
@@ -33,7 +51,7 @@ export const POST = withAuth(
});
}
- if (projectUserCount >= project.usersLimit) {
+ if (projectUserCount + projectInviteCount >= project.usersLimit) {
return new Response(
exceededLimitError({
plan: project.plan,
diff --git a/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx b/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx
index df2845a17d..47a2ca1435 100644
--- a/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/invites/[code]/page.tsx
@@ -1,4 +1,3 @@
-import { inviteChecks } from "@/lib/api/users";
import { getSession } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { LoadingSpinner, Logo } from "@dub/ui";
@@ -8,6 +7,17 @@ import { Suspense } from "react";
export const runtime = "nodejs";
+const PageCopy = ({ title, message }: { title: string; message: string }) => {
+ return (
+ <>
+ <h1 className="font-display text-3xl font-bold sm:text-4xl">{title}</h1>
+ <p className="max-w-lg text-gray-600 [text-wrap:balance] sm:text-lg">
+ {message}
+ </p>
+ </>
+ );
+};
+
export default function InvitesPage({
params,
}: {
@@ -21,13 +31,10 @@ export default function InvitesPage({
<Suspense
fallback={
<>
- <h1 className="font-display text-4xl font-bold">
- Verifying Invite
- </h1>
- <p className="text-lg text-gray-600">
- {APP_NAME} is verifying your invite link.
- <br /> Please wait...
- </p>
+ <PageCopy
+ title="Verifying Invite"
+ message={`${APP_NAME} is verifying your invite link...`}
+ />
<LoadingSpinner className="h-7 w-7" />
</>
}
@@ -45,39 +52,53 @@ async function VerifyInvite({ code }: { code: string }) {
redirect("/login");
}
- // fake promise 3s
const project = await prisma.project.findUnique({
+ where: {
+ inviteCode: code,
+ },
select: {
id: true,
slug: true,
usersLimit: true,
- },
- where: {
- inviteCode: code,
+ users: {
+ where: {
+ userId: session.user.id,
+ },
+ select: {
+ role: true,
+ },
+ },
+ _count: {
+ select: {
+ users: true,
+ invites: true,
+ },
+ },
},
});
if (!project) {
- redirect("/");
+ return (
+ <>
+ <PageCopy
+ title="Invalid Invite"
+ message="The invite link you are trying to use is invalid. Please contact the project owner for a new invite."
+ />
+ </>
+ );
}
- const [alreadyInTeam, projectUserCount] = await inviteChecks({
- projectId: project.id,
- email: session.user.email,
- });
-
- if (alreadyInTeam) {
+ // check if user is already in the project
+ if (project.users.length > 0) {
redirect(`/${project.slug}`);
}
- if (projectUserCount >= project.usersLimit) {
+ if (project._count.users + project._count.invites >= project.usersLimit) {
return (
- <>
- <h1 className="font-display text-4xl font-bold">User Limit Reached</h1>
- <p className="text-lg text-gray-600">
- This project has reached its user limit.
- </p>
- </>
+ <PageCopy
+ title="User Limit Reached"
+ message="The project you are trying to join is currently full. Please contact the project owner for more information."
+ />
);
}
@@ -93,8 +114,7 @@ async function VerifyInvite({ code }: { code: string }) {
if (e.code !== "P2002") {
return (
<>
- <h1 className="font-display text-4xl font-bold">Error</h1>
- <p className="text-lg text-gray-600">{e.message}</p>
+ <PageCopy title="Error" message={e.message} />
</>
);
}
diff --git a/apps/web/lib/api/users.ts b/apps/web/lib/api/users.ts
index ca340e9105..4bbbf1cdbf 100644
--- a/apps/web/lib/api/users.ts
+++ b/apps/web/lib/api/users.ts
@@ -6,30 +6,6 @@ import { randomBytes } from "crypto";
import { sendEmail } from "emails";
import ProjectInvite from "emails/project-invite";
-export async function inviteChecks({
- projectId,
- email,
-}: {
- projectId: string;
- email: string;
-}) {
- return await Promise.all([
- prisma.projectUsers.findFirst({
- where: {
- projectId,
- user: {
- email,
- },
- },
- }),
- prisma.projectUsers.count({
- where: {
- projectId,
- },
- }),
- ]);
-}
-
export async function inviteUser({
email,
project,