Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors user data queries #98

Merged
merged 79 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 77 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
ac57850
renames users table
xwilson03 Aug 3, 2024
6eb7a3c
moves registration data fields to user data
xwilson03 Aug 3, 2024
12691fa
moves all profile data into user data
xwilson03 Aug 3, 2024
bdc6d33
moves some user data into registration data
xwilson03 Aug 3, 2024
4e391d8
TEMP: removes migrations:apply step from build
xwilson03 Aug 3, 2024
a4a84ad
removes checkedIn from user data
xwilson03 Aug 3, 2024
e128066
organizes fields for user and reg data
xwilson03 Aug 3, 2024
0b01091
renames reg data to hacker data
xwilson03 Aug 3, 2024
1f1af2f
adds serial PK to user data
xwilson03 Aug 3, 2024
fe47e2a
shortens lengthy column names
xwilson03 Aug 3, 2024
4488f94
updates userdata-hackerdata relation name
xwilson03 Aug 3, 2024
b6f174b
renames booleans
xwilson03 Aug 3, 2024
4eaee36
updates package lock
xwilson03 Aug 3, 2024
01a10b8
changes naming convention for user data tables
xwilson03 Aug 3, 2024
9eeafa2
updates trivial instances of changed schema columns
xwilson03 Aug 3, 2024
f8760a9
removes commented references to old schema
xwilson03 Aug 3, 2024
34088c0
updates scanner checkin action
xwilson03 Aug 3, 2024
6a3fdc8
updates more trivial substitutions
xwilson03 Aug 4, 2024
fd75399
reverts serial id change
xwilson03 Aug 4, 2024
b1b25dd
fixes team members relation
xwilson03 Aug 4, 2024
5e8c1e6
adds db migration
xwilson03 Aug 4, 2024
1cb2689
updates checkin page
xwilson03 Aug 5, 2024
d118050
slightly refactors checkin
xwilson03 Aug 5, 2024
b61c3c9
updates user interface
xwilson03 Aug 5, 2024
11468ac
updates and simplifies admin user table component
xwilson03 Aug 5, 2024
aebcde3
restructures user type interface
xwilson03 Aug 6, 2024
1478e2e
updates server sections of admin user table
xwilson03 Aug 6, 2024
a70247a
adds commonData relation to hackerData
xwilson03 Aug 6, 2024
bb6ce86
updates team page
xwilson03 Aug 6, 2024
e4a9950
updates trivial schema references
xwilson03 Aug 6, 2024
5ffeffd
updates last known references
xwilson03 Aug 7, 2024
3d609b2
deletes unused code in event columns
xwilson03 Aug 7, 2024
5c8d395
replaces last profileData refs
xwilson03 Aug 7, 2024
209e243
updates pass scanner
xwilson03 Aug 7, 2024
0dff0ea
runs formatter
xwilson03 Aug 7, 2024
5d66688
Merge remote-tracking branch 'origin/dev' into feat/refactor-user-schema
xwilson03 Aug 11, 2024
6b620f9
fixes schoolID rename
xwilson03 Aug 13, 2024
6cd422a
patcches user section of admin panel
xwilson03 Aug 14, 2024
5c146bd
slightly refactors scanner code
xwilson03 Aug 14, 2024
73a92be
slightly simplifies leaveTeam action
xwilson03 Aug 14, 2024
5a5edad
fixes scanner page
xwilson03 Aug 15, 2024
45febfc
adjusts formatting
xwilson03 Aug 15, 2024
a277824
adjusts whitespace
xwilson03 Aug 15, 2024
2179bdb
fixes admin export
xwilson03 Aug 15, 2024
fc1e67a
reruns formatter
xwilson03 Aug 15, 2024
828e842
moves and renames schema types
xwilson03 Aug 10, 2024
6970cea
adds prepared getUser function
xwilson03 Aug 10, 2024
d6ab8f6
adds prepared getHacker function
xwilson03 Aug 10, 2024
33655d9
converts types to interfaces for intellisense
xwilson03 Aug 11, 2024
74db1ba
substitutes getUser across files
xwilson03 Aug 11, 2024
68abd1e
makes hacker team data optional
xwilson03 Aug 17, 2024
ba19c25
shortens admin scanner imports
xwilson03 Aug 17, 2024
083b576
removes withTeam default from getHacker
xwilson03 Aug 17, 2024
02756a1
integrates getHacker function
xwilson03 Aug 17, 2024
d65bad3
substitutes InferModel calls
xwilson03 Aug 17, 2024
53645f8
refactors scanner queries
xwilson03 Aug 18, 2024
2f92331
restructures helper functions
xwilson03 Aug 18, 2024
fee83c4
adds search by hackerTag to hacker functions
xwilson03 Aug 18, 2024
6542e91
adds user search by tag
xwilson03 Aug 18, 2024
6f5489d
adds getAllUsers function
xwilson03 Aug 18, 2024
5b1f104
adds getAllHackers functions
xwilson03 Aug 18, 2024
35c7f26
integrates getByTag functions
xwilson03 Aug 18, 2024
1c19eb6
integrates getAll functions
xwilson03 Aug 18, 2024
493b984
refactors admin checkin in-place
xwilson03 Aug 18, 2024
3a7bf22
adds team interface definition
xwilson03 Aug 20, 2024
c1b7e7c
runs formatter
xwilson03 Aug 22, 2024
dff44bf
moves invite relation to hacker data
xwilson03 Sep 8, 2024
82425a1
updates references to invite relation
xwilson03 Sep 8, 2024
a422b9b
trims schema imports
xwilson03 Sep 8, 2024
a8488a6
reruns formatter
xwilson03 Sep 8, 2024
b7393b4
merges latest 'feat/refactor-user-schema' changes
xwilson03 Sep 8, 2024
fc51ac1
comments out prepared functions
xwilson03 Sep 10, 2024
0803b8c
reruns formatter
xwilson03 Sep 10, 2024
aa8392b
Merge remote-tracking branch 'origin/dev' into chore/adds-user-query-…
christianhelp Sep 11, 2024
c1b8e70
updates formschema
christianhelp Sep 11, 2024
88a2997
updates lock file
christianhelp Sep 11, 2024
92993aa
updates scanner types
christianhelp Sep 11, 2024
eb28734
fixes import issue
christianhelp Sep 11, 2024
4461ad4
removes unused type
christianhelp Sep 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions apps/bot/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { serve } from "bun";
import c from "config";
import { db } from "db";
import { eq } from "db/drizzle";
import { discordVerification, userCommonData } from "db/schema";
import { discordVerification } from "db/schema";
import { getHacker } from "db/functions";
import { nanoid } from "nanoid";

/* DISCORD BOT */
Expand Down Expand Up @@ -198,10 +199,8 @@ app.post("/api/checkDiscordVerification", async (h) => {
return h.json({ success: false });
}
console.log("got here 2");
const user = await db.query.userCommonData.findFirst({
where: eq(userCommonData.clerkID, verification.clerkID),
with: { hackerData: true },
});

const user = await getHacker(verification.clerkID, false);
console.log("got here 2 with user", user);
if (!user) {
console.log("failed cause of no user in db");
Expand Down
6 changes: 3 additions & 3 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),
});
const user = await getUser(userId);
if (!user) throw new Error("User not found");

await db
.update(userCommonData)
.set({ isRSVPed: true })
Expand Down
13 changes: 4 additions & 9 deletions apps/web/src/actions/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
import { authenticatedAction } from "@/lib/safe-action";
import { z } from "zod";
import { db } from "db";
import { userCommonData, userHackerData, teams, invites } from "db/schema";
import { userHackerData, teams, invites } from "db/schema";
import { eq } from "db/drizzle";
import { revalidatePath } from "next/cache";
import { getHacker } from "db/functions";

export const leaveTeam = authenticatedAction(
z.null(),
async (_, { userId }) => {
const user = await db.query.userCommonData.findFirst({
where: eq(userCommonData.clerkID, userId),
with: { hackerData: true },
});

if (!user) {
throw new Error("User not found");
}
const user = await getHacker(userId, false);
if (!user) throw new Error("User not found");

if (!user.hackerData.teamID) {
revalidatePath("/dash/team");
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
53 changes: 16 additions & 37 deletions apps/web/src/app/admin/check-in/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import CheckinScanner from "@/components/admin/scanner/CheckinScanner";
import { db } from "db";
import { eq } from "db/drizzle";
import { userCommonData } from "db/schema";
import { getUser } from "db/functions";

export default async function Page({
searchParams,
}: {
searchParams: { [key: string]: string | undefined };
}) {
// Check for missing searchParams
if (!searchParams.user) {
if (!searchParams.user)
return (
<div>
<CheckinScanner
Expand All @@ -20,45 +17,27 @@ export default async function Page({
/>
</div>
);
}

const [isChecked, scanUser, hasRSVPed] = await db.transaction(
async (tx) => {
const scanUser = await tx.query.userCommonData.findFirst({
where: eq(
userCommonData.clerkID,
searchParams.user ?? "unknown",
),
});
if (!scanUser) {
return [null, null, null];
}
const scan = await tx
.select({
checkinTimestamp: userCommonData.checkinTimestamp,
hasRSVPed: userCommonData.isRSVPed,
})
.from(userCommonData)
.where(eq(userCommonData.clerkID, searchParams.user!));
if (scan) {
return [
scan[0].checkinTimestamp != null,
scanUser,
scan[0].hasRSVPed,
];
} else {
return [null, scanUser, null];
}
},
);
const scanUser = await getUser(searchParams.user);
if (!scanUser)
return (
<div>
<CheckinScanner
hasScanned={true}
checkedIn={null}
scanUser={null}
hasRSVP={null}
/>
</div>
);

return (
<div>
<CheckinScanner
checkedIn={isChecked}
hasScanned={true}
checkedIn={scanUser.checkinTimestamp != null}
scanUser={scanUser}
hasRSVP={hasRSVPed}
hasRSVP={scanUser.isRSVPed}
/>
</div>
);
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
26 changes: 5 additions & 21 deletions apps/web/src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,25 @@ import {
CardTitle,
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 { userType } from "@/lib/utils/shared/types";
import type { User } from "db/types";
import { auth } from "@clerk/nextjs";
import { notFound } from "next/navigation";
import { getAllUsers, 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),
});

const adminUser = await getUser(userId);
if (
!adminUser ||
(adminUser.role !== "admin" && adminUser.role !== "super_admin")
) {
return notFound();
}

const allUsers = await getUsers();
const allUsers = (await getAllUsers()) ?? [];

const { rsvpCount, checkinCount, recentSignupCount } =
getRecentRegistrationData(allUsers);
Expand Down Expand Up @@ -140,7 +129,7 @@ export default async function Page() {
);
}

function getRecentRegistrationData(users: userType[]) {
function getRecentRegistrationData(users: User[]) {
type DateNumberMap = { [key: string]: number };

let rsvpCount = 0;
Expand Down Expand Up @@ -171,9 +160,4 @@ function getRecentRegistrationData(users: userType[]) {
return { rsvpCount, checkinCount, recentSignupCount };
}

async function getUsers() {
const usersReq = await db.query.userCommonData.findMany();
return usersReq;
}

export const runtime = "edge";
41 changes: 13 additions & 28 deletions apps/web/src/app/admin/scanner/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import PassScanner from "@/components/admin/scanner/PassScanner";
import FullScreenMessage from "@/components/shared/FullScreenMessage";
import { db } from "db";
import { eq, and } from "db/drizzle";
import { getHacker } from "db/functions";
import { events, userCommonData, scans } from "db/schema";

export default async function Page({
Expand Down Expand Up @@ -48,40 +49,24 @@ export default async function Page({
);
}

const [scan, scanUser] = await db.transaction(async (tx) => {
const scanUser = await tx.query.userCommonData.findFirst({
where: eq(userCommonData.clerkID, searchParams.user!),
with: {
hackerData: {
with: {
team: true,
},
},
},
});
if (!scanUser) {
return [null, null];
}
const scan = await tx.query.scans.findFirst({
where: and(
eq(scans.eventID, event.id),
eq(scans.userID, scanUser.clerkID),
),
});
if (scan) {
return [scan, scanUser];
} else {
return [null, scanUser];
}
});
const scanUser = await getHacker(searchParams.user, false);

const scan = !scanUser
? null
: await db.query.scans.findFirst({
where: and(
eq(scans.eventID, event.id),
eq(scans.userID, scanUser.clerkID),
),
});

return (
<div>
<PassScanner
event={event}
hasScanned={true}
scan={scan}
scanUser={scanUser}
scan={scan ?? null}
scanUser={scanUser ?? null}
/>
</div>
);
Expand Down
15 changes: 3 additions & 12 deletions apps/web/src/app/admin/users/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { db } from "db";
import { userCommonData } from "db/schema";
import { eq } from "db/drizzle";
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,22 +15,17 @@ import { notFound } from "next/navigation";
import { isUserAdmin } from "@/lib/utils/server/admin";
import ApproveUserButton from "@/components/admin/users/ApproveUserButton";
import c from "config";
import { getHacker, 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({
where: eq(userCommonData.clerkID, params.slug),
with: { hackerData: { with: { team: true } } },
});
const user = await getHacker(params.slug, true);

if (!user) {
return <p className="text-center font-bold">User Not Found</p>;
Expand Down
Loading
Loading