From 74db1ba6e23c7d5a3c2f040213f115ee4bb33675 Mon Sep 17 00:00:00 2001 From: xwilson03 Date: Sun, 11 Aug 2024 15:23:06 -0500 Subject: [PATCH] substitutes getUser across files --- apps/web/src/actions/rsvp.ts | 8 ++++---- apps/web/src/actions/user-profile-mod.ts | 15 ++++++--------- apps/web/src/app/admin/layout.tsx | 8 ++------ apps/web/src/app/admin/page.tsx | 18 +++--------------- apps/web/src/app/admin/users/[slug]/page.tsx | 7 ++----- .../src/app/api/admin/events/create/route.ts | 14 ++++---------- apps/web/src/app/api/admin/export/route.ts | 13 +++---------- .../src/app/api/registration/create/route.ts | 7 +++---- apps/web/src/app/dash/layout.tsx | 9 ++------- apps/web/src/app/dash/page.tsx | 8 ++------ apps/web/src/app/register/page.tsx | 15 +++------------ apps/web/src/app/rsvp/page.tsx | 10 +++------- .../components/dash/shared/ProfileButton.tsx | 11 +++-------- .../src/components/shared/ProfileButton.tsx | 8 ++------ apps/web/src/lib/safe-action.ts | 9 +++------ 15 files changed, 45 insertions(+), 115 deletions(-) diff --git a/apps/web/src/actions/rsvp.ts b/apps/web/src/actions/rsvp.ts index e71ccb82..fd63cb04 100644 --- a/apps/web/src/actions/rsvp.ts +++ b/apps/web/src/actions/rsvp.ts @@ -5,14 +5,14 @@ import { z } from "zod"; import { db } from "db"; import { eq } from "db/drizzle"; import { userCommonData } from "db/schema"; +import { getUser } from "db/functions" export const rsvpMyself = authenticatedAction( z.any(), async (_, { userId }) => { - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); - if (!user) throw new Error("User not found"); + const user = await getUser(userId); + if (!user) throw new Error("User not found"); + await db .update(userCommonData) .set({ isRSVPed: true }) diff --git a/apps/web/src/actions/user-profile-mod.ts b/apps/web/src/actions/user-profile-mod.ts index fa0b3b29..8315bf98 100644 --- a/apps/web/src/actions/user-profile-mod.ts +++ b/apps/web/src/actions/user-profile-mod.ts @@ -8,6 +8,7 @@ import { eq } from "db/drizzle"; import { put } from "@vercel/blob"; import { decodeBase64AsFile } from "@/lib/utils/shared/files"; import { revalidatePath } from "next/cache"; +import { getUser } from "db/functions"; // TODO: Add skill updating export const modifyRegistrationData = authenticatedAction( @@ -16,10 +17,9 @@ export const modifyRegistrationData = authenticatedAction( skills: z.string().max(100), }), async ({ bio, skills }, { userId }) => { - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); + const user = await getUser(userId); if (!user) throw new Error("User not found"); + await db .update(userCommonData) .set({ bio }) @@ -34,10 +34,9 @@ export const modifyAccountSettings = authenticatedAction( lastName: z.string().min(1).max(50), }), async ({ firstName, lastName }, { userId }) => { - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); + const user = await getUser(userId); if (!user) throw new Error("User not found"); + await db .update(userCommonData) .set({ firstName, lastName }) @@ -54,9 +53,7 @@ export const updateProfileImage = authenticatedAction( z.object({ fileBase64: z.string(), fileName: z.string() }), async ({ fileBase64, fileName }, { userId }) => { const image = await decodeBase64AsFile(fileBase64, fileName); - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); + const user = await getUser(userId); if (!user) throw new Error("User not found"); const blobUpload = await put(image.name, image, { access: "public" }); diff --git a/apps/web/src/app/admin/layout.tsx b/apps/web/src/app/admin/layout.tsx index 3ac49e5b..177a090c 100644 --- a/apps/web/src/app/admin/layout.tsx +++ b/apps/web/src/app/admin/layout.tsx @@ -1,17 +1,15 @@ import c from "config"; import Image from "next/image"; -import { db } from "db"; import { auth } from "@clerk/nextjs"; import Link from "next/link"; import { Button } from "@/components/shadcn/ui/button"; import DashNavItem from "@/components/dash/shared/DashNavItem"; -import { eq } from "db/drizzle"; -import { userCommonData } from "db/schema"; import FullScreenMessage from "@/components/shared/FullScreenMessage"; import ProfileButton from "@/components/shared/ProfileButton"; import { Suspense } from "react"; import ClientToast from "@/components/shared/ClientToast"; import { redirect } from "next/navigation"; +import { getUser } from "db/functions"; interface AdminLayoutProps { children: React.ReactNode; @@ -24,9 +22,7 @@ export default async function AdminLayout({ children }: AdminLayoutProps) { return redirect("/sign-in"); } - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); + const user = await getUser(userId); if (!user || (user.role !== "admin" && user.role !== "super_admin")) { console.log("Denying admin access to user", user); diff --git a/apps/web/src/app/admin/page.tsx b/apps/web/src/app/admin/page.tsx index 519480b5..f4d44567 100644 --- a/apps/web/src/app/admin/page.tsx +++ b/apps/web/src/app/admin/page.tsx @@ -7,31 +7,19 @@ import { CardDescription, } from "@/components/shadcn/ui/card"; import { db } from "db"; -import { eq, desc } from "db/drizzle"; -import { userCommonData } from "db/schema"; import { Users, UserCheck, User2, TimerReset, MailCheck } from "lucide-react"; import type { User } from "db/types"; import { auth } from "@clerk/nextjs"; import { notFound } from "next/navigation"; +import { getUser } from "db/functions"; export default async function Page() { - // const getCachedUsers = unstable_cache(getUsers, [`global_users_${env.INTERNAL_AUTH_KEY}`], { - // revalidate: 30, - // }); const { userId } = auth(); - if (!userId) return notFound(); - const adminUser = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - orderBy: desc(userCommonData.signupTime), - }); - - if ( - !adminUser || - (adminUser.role !== "admin" && adminUser.role !== "super_admin") - ) { + const adminUser = await getUser(userId); + if (!adminUser || (adminUser.role !== "admin" && adminUser.role !== "super_admin")) { return notFound(); } diff --git a/apps/web/src/app/admin/users/[slug]/page.tsx b/apps/web/src/app/admin/users/[slug]/page.tsx index 48756a5d..c618597c 100644 --- a/apps/web/src/app/admin/users/[slug]/page.tsx +++ b/apps/web/src/app/admin/users/[slug]/page.tsx @@ -5,7 +5,6 @@ import Image from "next/image"; import { Button } from "@/components/shadcn/ui/button"; import { Badge } from "@/components/shadcn/ui/badge"; import { Info } from "lucide-react"; - import Link from "next/link"; import UpdateRoleDialog from "@/components/admin/users/UpdateRoleDialog"; import { @@ -19,16 +18,14 @@ import { notFound } from "next/navigation"; import { isUserAdmin } from "@/lib/utils/server/admin"; import ApproveUserButton from "@/components/admin/users/ApproveUserButton"; import c from "config"; +import { getUser } from "db/functions"; export default async function Page({ params }: { params: { slug: string } }) { const { userId } = auth(); if (!userId) return notFound(); - const admin = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); - + const admin = await getUser(userId); if (!admin || !isUserAdmin(admin)) return notFound(); const user = await db.query.userCommonData.findFirst({ diff --git a/apps/web/src/app/api/admin/events/create/route.ts b/apps/web/src/app/api/admin/events/create/route.ts index bfb68f73..559f6699 100644 --- a/apps/web/src/app/api/admin/events/create/route.ts +++ b/apps/web/src/app/api/admin/events/create/route.ts @@ -1,27 +1,21 @@ import { auth } from "@clerk/nextjs"; -import { eq } from "db/drizzle"; import { db } from "db"; -import { userCommonData, events } from "db/schema"; +import { events } from "db/schema"; import { newEventValidator } from "@/validators/shared/newEvent"; import { BasicRedirValidator } from "@/validators/shared/basicRedir"; import { NextResponse } from "next/server"; import { z } from "zod"; import superjson from "superjson"; import c from "config"; +import { getUser } from "db/functions"; export async function POST(req: Request) { const { userId } = auth(); if (!userId) return new Response("Unauthorized", { status: 401 }); - const reqUserRecord = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); - - if ( - !reqUserRecord || - (reqUserRecord.role !== "super_admin" && reqUserRecord.role !== "admin") - ) { + const reqUserRecord = await getUser(userId); + if (!reqUserRecord || (reqUserRecord.role !== "super_admin" && reqUserRecord.role !== "admin")) { return new Response("Unauthorized", { status: 401 }); } diff --git a/apps/web/src/app/api/admin/export/route.ts b/apps/web/src/app/api/admin/export/route.ts index 72fe0072..7db4b2c8 100644 --- a/apps/web/src/app/api/admin/export/route.ts +++ b/apps/web/src/app/api/admin/export/route.ts @@ -1,7 +1,6 @@ import { db } from "db"; -import { eq } from "db/drizzle"; -import { userCommonData } from "db/schema"; import { auth } from "@clerk/nextjs"; +import { getUser } from "db/functions"; function escape(value: any) { if (value === null) return "None"; @@ -37,14 +36,8 @@ export async function GET() { if (!userId) return new Response("Unauthorized", { status: 401 }); - const reqUserRecord = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); - - if ( - !reqUserRecord || - (reqUserRecord.role !== "super_admin" && reqUserRecord.role !== "admin") - ) { + const reqUserRecord = await getUser(userId); + if (!reqUserRecord || (reqUserRecord.role !== "super_admin" && reqUserRecord.role !== "admin")) { return new Response("Unauthorized", { status: 401 }); } diff --git a/apps/web/src/app/api/registration/create/route.ts b/apps/web/src/app/api/registration/create/route.ts index dfbd3a55..1d86c24f 100644 --- a/apps/web/src/app/api/registration/create/route.ts +++ b/apps/web/src/app/api/registration/create/route.ts @@ -6,6 +6,7 @@ import { userCommonData, userHackerData } from "db/schema"; import { RegisterFormValidator } from "@/validators/shared/RegisterForm"; import c from "config"; import { z } from "zod"; +import { getUser } from "db/functions"; export async function POST(req: Request) { const rawBody = await req.json(); @@ -48,11 +49,9 @@ export async function POST(req: Request) { ); } - // TODO: Might be removable? Not sure if this is needed. In every case, the sure should have a peice of metadata that says if they are registered or not. + // TODO: Might be removable? Not sure if this is needed. In every case, the sure should have a piece of metadata that says if they are registered or not. - const lookupByUserID = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, user.id), - }); + const lookupByUserID = await getUser(user.id); if (lookupByUserID) { return NextResponse.json( diff --git a/apps/web/src/app/dash/layout.tsx b/apps/web/src/app/dash/layout.tsx index c784403d..6e258c2d 100644 --- a/apps/web/src/app/dash/layout.tsx +++ b/apps/web/src/app/dash/layout.tsx @@ -10,9 +10,7 @@ import ProfileButton from "@/components/shared/ProfileButton"; import ClientToast from "@/components/shared/ClientToast"; import { TRPCReactProvider } from "@/trpc/react"; -import { db } from "db"; -import { eq } from "db/drizzle"; -import { userCommonData } from "db/schema"; +import { getUser } from "db/functions"; interface DashLayoutProps { children: React.ReactNode; @@ -25,10 +23,7 @@ export default async function DashLayout({ children }: DashLayoutProps) { return redirect("/register"); } - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, clerkUser.id), - }); - + const user = await getUser(clerkUser.id); if (!user) return redirect("/register"); if ( diff --git a/apps/web/src/app/dash/page.tsx b/apps/web/src/app/dash/page.tsx index a8a6dc52..e7fc5e28 100644 --- a/apps/web/src/app/dash/page.tsx +++ b/apps/web/src/app/dash/page.tsx @@ -1,7 +1,4 @@ import { auth } from "@clerk/nextjs"; -import { db } from "db"; -import { userCommonData } from "db/schema"; -import { eq } from "db/drizzle"; import c from "config"; import { createQRpayload } from "@/lib/utils/shared/qr"; @@ -13,13 +10,12 @@ import { TitleBubble, QuickQR, } from "@/components/dash/overview/ServerBubbles"; +import { getUser } from "db/functions"; export default async function Page() { const { userId } = auth(); if (!userId) return null; - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); + const user = await getUser(userId); if (!user) return null; const qrPayload = createQRpayload({ diff --git a/apps/web/src/app/register/page.tsx b/apps/web/src/app/register/page.tsx index d7caf51a..db808b98 100644 --- a/apps/web/src/app/register/page.tsx +++ b/apps/web/src/app/register/page.tsx @@ -1,32 +1,23 @@ import c from "config"; import RegisterForm from "@/components/registration/RegisterForm"; import { auth, currentUser } from "@clerk/nextjs"; -import { db } from "db"; -import { userCommonData } from "db/schema"; -import { eq } from "db/drizzle"; import { redirect } from "next/navigation"; import Navbar from "@/components/shared/Navbar"; import Link from "next/link"; import { kv } from "@vercel/kv"; import { parseRedisBoolean } from "@/lib/utils/server/redis"; import { Button } from "@/components/shadcn/ui/button"; +import { getUser } from "db/functions"; export default async function Page() { const { userId } = auth(); - if (!userId) return redirect("/sign-up"); const user = await currentUser(); - if (!user) return redirect("/sign-up"); - const registration = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, user.id), - }); - - if (registration) { - return redirect("/dash"); - } + const registration = await getUser(userId); + if (registration) return redirect("/dash"); const [defaultRegistrationEnabled, defaultSecretRegistrationEnabled]: ( | string diff --git a/apps/web/src/app/rsvp/page.tsx b/apps/web/src/app/rsvp/page.tsx index 599473ee..1b0b82de 100644 --- a/apps/web/src/app/rsvp/page.tsx +++ b/apps/web/src/app/rsvp/page.tsx @@ -11,6 +11,7 @@ import { kv } from "@vercel/kv"; import { parseRedisBoolean } from "@/lib/utils/server/redis"; import Link from "next/link"; import { Button } from "@/components/shadcn/ui/button"; +import { getUser } from "db/functions"; export default async function RsvpPage({ searchParams, @@ -28,13 +29,8 @@ export default async function RsvpPage({ ); } - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); - - if (!user) { - return redirect("/register"); - } + const user = await getUser(userId); + if (!user) return redirect("/register"); if ( (c.featureFlags.core.requireUsersApproval as boolean) === true && diff --git a/apps/web/src/components/dash/shared/ProfileButton.tsx b/apps/web/src/components/dash/shared/ProfileButton.tsx index 3a1eda43..2ae43017 100644 --- a/apps/web/src/components/dash/shared/ProfileButton.tsx +++ b/apps/web/src/components/dash/shared/ProfileButton.tsx @@ -5,7 +5,6 @@ import { DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, - DropdownMenuShortcut, DropdownMenuTrigger, } from "@/components/shadcn/ui/dropdown-menu"; import { @@ -15,20 +14,16 @@ import { } from "@/components/shadcn/ui/avatar"; import { Button } from "@/components/shadcn/ui/button"; import { auth, SignOutButton } from "@clerk/nextjs"; -import { db } from "db"; -import { userCommonData } from "db/schema"; -import { eq } from "db/drizzle"; import Link from "next/link"; import { DropdownSwitcher } from "@/components/shared/ThemeSwitcher"; +import { getUser } from "db/functions"; export default async function ProfileButton() { - const clerkUser = await auth(); + const clerkUser = auth(); const { userId } = clerkUser; if (!userId) return null; - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); + const user = await getUser(userId); if (!user && !userId) return null; if (!user) { diff --git a/apps/web/src/components/shared/ProfileButton.tsx b/apps/web/src/components/shared/ProfileButton.tsx index 1a3540a9..80479377 100644 --- a/apps/web/src/components/shared/ProfileButton.tsx +++ b/apps/web/src/components/shared/ProfileButton.tsx @@ -14,13 +14,11 @@ import { } from "@/components/shadcn/ui/avatar"; import { Button } from "@/components/shadcn/ui/button"; import { auth, SignOutButton } from "@clerk/nextjs"; -import { db } from "db"; -import { userCommonData } from "db/schema"; -import { eq } from "db/drizzle"; import Link from "next/link"; import { DropdownSwitcher } from "@/components/shared/ThemeSwitcher"; import DefaultDropdownTrigger from "../dash/shared/DefaultDropDownTrigger"; import MobileNavBarLinks from "./MobileNavBarLinks"; +import { getUser } from "db/functions"; export default async function ProfileButton() { const clerkUser = await auth(); @@ -69,9 +67,7 @@ export default async function ProfileButton() { } // Make request with the clerk data that we may or may not have - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); + const user = await getUser(userId); // If we do not have a fully fledged user, encourage them to complete registration if (!user) { diff --git a/apps/web/src/lib/safe-action.ts b/apps/web/src/lib/safe-action.ts index e4317315..969324d0 100644 --- a/apps/web/src/lib/safe-action.ts +++ b/apps/web/src/lib/safe-action.ts @@ -1,8 +1,6 @@ import { createSafeActionClient } from "next-safe-action"; -import { db } from "db"; -import { eq } from "db/drizzle"; -import { userCommonData } from "db/schema"; import { auth } from "@clerk/nextjs"; +import { getUser } from "db/functions"; export const publicAction = createSafeActionClient(); @@ -10,9 +8,8 @@ export const adminAction = createSafeActionClient({ async middleware() { const { userId } = auth(); if (!userId) throw new Error("Unauthorized (No UserID)"); - const user = await db.query.userCommonData.findFirst({ - where: eq(userCommonData.clerkID, userId), - }); + + const user = await getUser(userId); if (!user || (user.role !== "admin" && user.role !== "super_admin")) { throw new Error("Unauthorized (Not Admin)"); }