From dff44bf39b136cea134b14372fbc180f96614188 Mon Sep 17 00:00:00 2001 From: xwilson03 Date: Sun, 8 Sep 2024 06:12:32 -0500 Subject: [PATCH 1/4] moves invite relation to hacker data --- packages/db/schema.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/db/schema.ts b/packages/db/schema.ts index 78afdd00..d7207884 100644 --- a/packages/db/schema.ts +++ b/packages/db/schema.ts @@ -101,7 +101,6 @@ export const userCommonRelations = relations( }), files: many(files), scans: many(scans), - invites: many(invites), tickets: many(ticketsToUsers), chats: many(chatsToUsers), messages: many(chatMessages), @@ -138,7 +137,7 @@ export const userHackerData = pgTable("user_hacker_data", { isEmailable: boolean("is_emailable").notNull(), }); -export const userHackerRelations = relations(userHackerData, ({ one }) => ({ +export const userHackerRelations = relations(userHackerData, ({ one, many }) => ({ commonData: one(userCommonData, { fields: [userHackerData.clerkID], references: [userCommonData.clerkID], @@ -147,6 +146,7 @@ export const userHackerRelations = relations(userHackerData, ({ one }) => ({ fields: [userHackerData.teamID], references: [teams.id], }), + invites: many(invites), })); export const events = pgTable("events", { @@ -234,9 +234,9 @@ export const invites = pgTable( ); export const invitesRelations = relations(invites, ({ one }) => ({ - invitee: one(userCommonData, { + invitee: one(userHackerData, { fields: [invites.inviteeID], - references: [userCommonData.clerkID], + references: [userHackerData.clerkID], }), team: one(teams, { fields: [invites.teamID], From 82425a1620881a63c3489a9b8a7a0a357099a66b Mon Sep 17 00:00:00 2001 From: xwilson03 Date: Sun, 8 Sep 2024 06:25:22 -0500 Subject: [PATCH 2/4] updates references to invite relation --- .../src/app/api/team/invite/accept/route.ts | 19 +++++++++++-------- .../src/app/api/team/invite/create/route.ts | 8 ++++---- .../src/app/api/team/invite/decline/route.ts | 16 +++++++++++----- apps/web/src/app/dash/team/page.tsx | 14 +++++++------- 4 files changed, 33 insertions(+), 24 deletions(-) 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..ed92014e 100644 --- a/apps/web/src/app/api/team/invite/accept/route.ts +++ b/apps/web/src/app/api/team/invite/accept/route.ts @@ -31,10 +31,13 @@ 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), + }, + } + }, }, }); if (!user) return NextResponse.json("Unauthorized", { status: 401 }); @@ -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 7501a5a9..43475f85 100644 --- a/apps/web/src/app/api/team/invite/create/route.ts +++ b/apps/web/src/app/api/team/invite/create/route.ts @@ -55,11 +55,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), - }, }, }); @@ -71,7 +71,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..54471c29 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..3ab7d900 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() {

Invitations

- {user.invites.length > 0 ? ( - user.invites.map((invite) => ( + {user.hackerData.invites.length > 0 ? ( + user.hackerData.invites.map((invite) => (
Date: Sun, 8 Sep 2024 06:27:18 -0500 Subject: [PATCH 3/4] trims schema imports --- packages/db/schema.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/db/schema.ts b/packages/db/schema.ts index d7207884..64c2e24a 100644 --- a/packages/db/schema.ts +++ b/packages/db/schema.ts @@ -12,7 +12,6 @@ import { bigserial, text, varchar, - uniqueIndex, boolean, timestamp, integer, @@ -20,7 +19,6 @@ import { pgEnum, primaryKey, pgTable, - serial, } from "drizzle-orm/pg-core"; import { relations } from "drizzle-orm"; From a8488a680eafdd25a9c2b0f5fc785d58af3dc6d3 Mon Sep 17 00:00:00 2001 From: xwilson03 Date: Sun, 8 Sep 2024 06:28:55 -0500 Subject: [PATCH 4/4] reruns formatter --- .../src/app/api/team/invite/accept/route.ts | 12 +++++----- .../src/app/api/team/invite/create/route.ts | 6 ++--- .../src/app/api/team/invite/decline/route.ts | 18 +++++++-------- apps/web/src/app/dash/team/page.tsx | 10 ++++---- packages/db/schema.ts | 23 +++++++++++-------- 5 files changed, 36 insertions(+), 33 deletions(-) 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 ed92014e..e3e9fd31 100644 --- a/apps/web/src/app/api/team/invite/accept/route.ts +++ b/apps/web/src/app/api/team/invite/accept/route.ts @@ -32,12 +32,12 @@ export async function POST( where: eq(userCommonData.clerkID, userId), with: { hackerData: { - with: { - invites: { - where: eq(invites.teamID, body.data.teamInviteID), - }, - } - }, + with: { + invites: { + where: eq(invites.teamID, body.data.teamInviteID), + }, + }, + }, }, }); if (!user) return NextResponse.json("Unauthorized", { status: 401 }); 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 43475f85..f6e2ddca 100644 --- a/apps/web/src/app/api/team/invite/create/route.ts +++ b/apps/web/src/app/api/team/invite/create/route.ts @@ -55,9 +55,9 @@ export async function POST( hackerData: { with: { team: true, - invites: { - where: eq(invites.teamID, user.hackerData.teamID), - }, + invites: { + where: eq(invites.teamID, user.hackerData.teamID), + }, }, }, }, 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 54471c29..3f5164e8 100644 --- a/apps/web/src/app/api/team/invite/decline/route.ts +++ b/apps/web/src/app/api/team/invite/decline/route.ts @@ -23,23 +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 + // 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: { - hackerData: { - 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 + // TODO(xander): get invite using body data here to avoid joins above await db .update(invites) .set({ diff --git a/apps/web/src/app/dash/team/page.tsx b/apps/web/src/app/dash/team/page.tsx index 3ab7d900..c695aa7a 100644 --- a/apps/web/src/app/dash/team/page.tsx +++ b/apps/web/src/app/dash/team/page.tsx @@ -31,11 +31,11 @@ export default async function Page() { }, }, }, - invites: { - with: { - team: true, - }, - }, + invites: { + with: { + team: true, + }, + }, }, }, }, diff --git a/packages/db/schema.ts b/packages/db/schema.ts index 64c2e24a..945c5403 100644 --- a/packages/db/schema.ts +++ b/packages/db/schema.ts @@ -135,17 +135,20 @@ export const userHackerData = pgTable("user_hacker_data", { isEmailable: boolean("is_emailable").notNull(), }); -export const userHackerRelations = relations(userHackerData, ({ one, many }) => ({ - commonData: one(userCommonData, { - fields: [userHackerData.clerkID], - references: [userCommonData.clerkID], - }), - team: one(teams, { - fields: [userHackerData.teamID], - references: [teams.id], +export const userHackerRelations = relations( + userHackerData, + ({ one, many }) => ({ + commonData: one(userCommonData, { + fields: [userHackerData.clerkID], + references: [userCommonData.clerkID], + }), + team: one(teams, { + fields: [userHackerData.teamID], + references: [teams.id], + }), + invites: many(invites), }), - invites: many(invites), -})); +); export const events = pgTable("events", { id: bigserial("id", { mode: "number" }).notNull().primaryKey().unique(),