Skip to content

Commit

Permalink
merges latest 'feat/refactor-user-schema' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xwilson03 committed Sep 8, 2024
2 parents c1b7e7c + a8488a6 commit b7393b4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
17 changes: 10 additions & 7 deletions apps/web/src/app/api/team/invite/accept/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
},
},
});
Expand All @@ -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.",
Expand All @@ -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,
},
Expand All @@ -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.
Expand All @@ -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),
),
);
Expand Down
8 changes: 4 additions & 4 deletions apps/web/src/app/api/team/invite/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
});

Expand All @@ -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.",
Expand Down
14 changes: 10 additions & 4 deletions apps/web/src/app/api/team/invite/decline/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -23,25 +23,31 @@ 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({
status: "declined",
})
.where(
and(
eq(invites.teamID, user.invites[0].teamID),
eq(invites.teamID, user.hackerData.invites[0].teamID),
eq(invites.inviteeID, userId),
),
);
Expand Down
14 changes: 7 additions & 7 deletions apps/web/src/app/dash/team/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -36,6 +31,11 @@ export default async function Page() {
},
},
},
invites: {
with: {
team: true,
},
},
},
},
},
Expand Down Expand Up @@ -74,8 +74,8 @@ export default async function Page() {
<h2 className="font-xl mb-5 text-2xl font-bold">
Invitations
</h2>
{user.invites.length > 0 ? (
user.invites.map((invite) => (
{user.hackerData.invites.length > 0 ? (
user.hackerData.invites.map((invite) => (
<div
className="grid h-16 w-full grid-cols-3 rounded-xl px-2"
key={invite.teamID}
Expand Down
29 changes: 15 additions & 14 deletions packages/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import {
bigserial,
text,
varchar,
uniqueIndex,
boolean,
timestamp,
integer,
json,
pgEnum,
primaryKey,
pgTable,
serial,
} from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";

Expand Down Expand Up @@ -101,7 +99,6 @@ export const userCommonRelations = relations(
}),
files: many(files),
scans: many(scans),
invites: many(invites),
tickets: many(ticketsToUsers),
chats: many(chatsToUsers),
messages: many(chatMessages),
Expand Down Expand Up @@ -138,16 +135,20 @@ export const userHackerData = pgTable("user_hacker_data", {
isEmailable: boolean("is_emailable").notNull(),
});

export const userHackerRelations = relations(userHackerData, ({ one }) => ({
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),
}),
}));
);

export const events = pgTable("events", {
id: bigserial("id", { mode: "number" }).notNull().primaryKey().unique(),
Expand Down Expand Up @@ -234,9 +235,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],
Expand Down

0 comments on commit b7393b4

Please sign in to comment.