Skip to content

Commit

Permalink
substitutes getUser across files
Browse files Browse the repository at this point in the history
  • Loading branch information
xwilson03 committed Aug 16, 2024
1 parent 33655d9 commit 74db1ba
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 115 deletions.
8 changes: 4 additions & 4 deletions apps/web/src/actions/rsvp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
15 changes: 6 additions & 9 deletions apps/web/src/actions/user-profile-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 })
Expand All @@ -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 })
Expand All @@ -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" });
Expand Down
8 changes: 2 additions & 6 deletions apps/web/src/app/admin/layout.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
18 changes: 3 additions & 15 deletions apps/web/src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
7 changes: 2 additions & 5 deletions apps/web/src/app/admin/users/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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({
Expand Down
14 changes: 4 additions & 10 deletions apps/web/src/app/api/admin/events/create/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}

Expand Down
13 changes: 3 additions & 10 deletions apps/web/src/app/api/admin/export/route.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 });
}

Expand Down
7 changes: 3 additions & 4 deletions apps/web/src/app/api/registration/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 2 additions & 7 deletions apps/web/src/app/dash/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 (
Expand Down
8 changes: 2 additions & 6 deletions apps/web/src/app/dash/page.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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({
Expand Down
15 changes: 3 additions & 12 deletions apps/web/src/app/register/page.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 3 additions & 7 deletions apps/web/src/app/rsvp/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 &&
Expand Down
11 changes: 3 additions & 8 deletions apps/web/src/components/dash/shared/ProfileButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuShortcut,
DropdownMenuTrigger,
} from "@/components/shadcn/ui/dropdown-menu";
import {
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 74db1ba

Please sign in to comment.