diff --git a/apps/web/src/app/api/team/invite/accept/route.ts b/apps/web/src/app/api/team/invite/accept/route.ts index 298e23b6..e3e9fd31 100644 --- a/apps/web/src/app/api/team/invite/accept/route.ts +++ b/apps/web/src/app/api/team/invite/accept/route.ts @@ -31,9 +31,12 @@ export async function POST( const user = await db.query.userCommonData.findFirst({ where: eq(userCommonData.clerkID, userId), with: { - hackerData: true, - invites: { - where: eq(invites.teamID, body.data.teamInviteID), + hackerData: { + with: { + invites: { + where: eq(invites.teamID, body.data.teamInviteID), + }, + }, }, }, }); @@ -47,7 +50,7 @@ export async function POST( }); } - if (user.invites.length === 0) { + if (user.hackerData.invites.length === 0) { return NextResponse.json({ success: false, message: "You have not been invited to this team.", @@ -56,7 +59,7 @@ export async function POST( } const team = await db.query.teams.findFirst({ - where: eq(teams.id, user.invites[0].teamID), + where: eq(teams.id, user.hackerData.invites[0].teamID), with: { members: true, }, @@ -80,7 +83,7 @@ export async function POST( await db .update(userHackerData) - .set({ teamID: user.invites[0].teamID }) + .set({ teamID: user.hackerData.invites[0].teamID }) .where(eq(userHackerData.clerkID, userId)); // TODO: would be interesting to see if the and() could be removed here in favor of directly looking up the composite key. @@ -89,7 +92,7 @@ export async function POST( .set({ status: "accepted" }) .where( and( - eq(invites.teamID, user.invites[0].teamID), + eq(invites.teamID, user.hackerData.invites[0].teamID), eq(invites.inviteeID, userId), ), ); diff --git a/apps/web/src/app/api/team/invite/create/route.ts b/apps/web/src/app/api/team/invite/create/route.ts index ce8d7bf0..c6b59277 100644 --- a/apps/web/src/app/api/team/invite/create/route.ts +++ b/apps/web/src/app/api/team/invite/create/route.ts @@ -54,11 +54,11 @@ export async function POST( hackerData: { with: { team: true, + invites: { + where: eq(invites.teamID, user.hackerData.teamID), + }, }, }, - invites: { - where: eq(invites.teamID, user.hackerData.teamID), - }, }, }); @@ -70,7 +70,7 @@ export async function POST( }); } - if (invitee.invites.length > 0) { + if (invitee.hackerData.invites.length > 0) { return NextResponse.json({ success: false, message: "That user already has an invite.", diff --git a/apps/web/src/app/api/team/invite/decline/route.ts b/apps/web/src/app/api/team/invite/decline/route.ts index 1c9019eb..3f5164e8 100644 --- a/apps/web/src/app/api/team/invite/decline/route.ts +++ b/apps/web/src/app/api/team/invite/decline/route.ts @@ -2,7 +2,7 @@ import { auth } from "@clerk/nextjs"; import { NextResponse } from "next/server"; import { z } from "zod"; import { db } from "db"; -import { userCommonData, invites, teams } from "db/schema"; +import { userCommonData, invites } from "db/schema"; import { eq, and } from "db/drizzle"; const inviteDeclineValidator = z.object({ @@ -23,17 +23,23 @@ export async function POST(req: Request) { }); } + // TODO(xander): adjust logic here. null check shouldnt require a join, and invite can be queried directly const user = await db.query.userCommonData.findFirst({ where: eq(userCommonData.clerkID, userId), with: { - invites: { - where: eq(invites.teamID, body.data.teamInviteID), + hackerData: { + with: { + invites: { + where: eq(invites.teamID, body.data.teamInviteID), + }, + }, }, }, }); if (!user) return NextResponse.json("Unauthorized", { status: 401 }); + // TODO(xander): get invite using body data here to avoid joins above await db .update(invites) .set({ @@ -41,7 +47,7 @@ export async function POST(req: Request) { }) .where( and( - eq(invites.teamID, user.invites[0].teamID), + eq(invites.teamID, user.hackerData.invites[0].teamID), eq(invites.inviteeID, userId), ), ); diff --git a/apps/web/src/app/dash/team/page.tsx b/apps/web/src/app/dash/team/page.tsx index dbdfc341..c695aa7a 100644 --- a/apps/web/src/app/dash/team/page.tsx +++ b/apps/web/src/app/dash/team/page.tsx @@ -20,11 +20,6 @@ export default async function Page() { const user = await db.query.userCommonData.findFirst({ where: eq(userCommonData.clerkID, userId), with: { - invites: { - with: { - team: true, - }, - }, hackerData: { with: { team: { @@ -36,6 +31,11 @@ export default async function Page() { }, }, }, + invites: { + with: { + team: true, + }, + }, }, }, }, @@ -74,8 +74,8 @@ export default async function Page() {