Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions chat-system/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ button {
::-webkit-scrollbar-thumb:hover {
background: rgba(170, 191, 255, 0.62);
}

html { scroll-behavior: smooth; }
Comment thread
saurabhhhcodes marked this conversation as resolved.
Comment thread
saurabhhhcodes marked this conversation as resolved.
*:focus-visible { outline: 2px solid #6366f1; outline-offset: 2px; border-radius: 4px; }
.skeleton { background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%); background-size: 200% 100%; animation: shimmer 1.5s infinite; border-radius: 8px; }
Comment thread
saurabhhhcodes marked this conversation as resolved.
@keyframes shimmer { 0% { background-position: -200% 0; } 100% { background-position: 200% 0; } }
Comment thread
saurabhhhcodes marked this conversation as resolved.
76 changes: 47 additions & 29 deletions src/app/api/invites/[token]/join/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextResponse } from "next/server";
import { currentUser } from "@clerk/nextjs/server";
import { and, eq, sql } from "drizzle-orm";
import { and, eq, or, sql } from "drizzle-orm";

import { db } from "@/configs/db";
import {
Expand Down Expand Up @@ -78,27 +78,58 @@ export async function POST(
);
}

if (
inviteData.maxUses !== null &&
inviteData.usedCount >= inviteData.maxUses
) {
const joinResult = await db.transaction(async (tx) => {
const [existingMember] = await tx
.select()
.from(membershipsTable)
.where(
and(
eq(membershipsTable.userEmail, email),
eq(membershipsTable.classroomId, inviteData.classroomId),
),
);

if (existingMember) {
return { alreadyMember: true as const };
}

const [updatedInvite] = await tx
.update(classroomInvitesTable)
.set({
usedCount: sql`${classroomInvitesTable.usedCount} + 1`,
})
.where(
and(
eq(classroomInvitesTable.id, inviteData.inviteId),
or(
sql`${classroomInvitesTable.maxUses} IS NULL`,
sql`${classroomInvitesTable.usedCount} < ${classroomInvitesTable.maxUses}`,
),
),
)
.returning({ id: classroomInvitesTable.id });

if (!updatedInvite) {
return { inviteExpired: true as const };
}

await tx.insert(membershipsTable).values({
userEmail: email,
classroomId: inviteData.classroomId,
role: "student",
});

return { success: true as const };
});

if ("inviteExpired" in joinResult) {
return NextResponse.json(
{ error: "This invite link has reached its usage limit" },
{ status: 410 },
);
}

const [existingMember] = await db
.select()
.from(membershipsTable)
.where(
and(
eq(membershipsTable.userEmail, email),
eq(membershipsTable.classroomId, inviteData.classroomId),
),
);

if (existingMember) {
if ("alreadyMember" in joinResult) {
return NextResponse.json({
success: true,
alreadyMember: true,
Expand All @@ -111,19 +142,6 @@ export async function POST(
});
}

await db.insert(membershipsTable).values({
userEmail: email,
classroomId: inviteData.classroomId,
role: "student",
});

await db
.update(classroomInvitesTable)
.set({
usedCount: sql`${classroomInvitesTable.usedCount} + 1`,
})
.where(eq(classroomInvitesTable.id, inviteData.inviteId));

return NextResponse.json({
success: true,
classroom: {
Expand Down
Loading