Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobellerbrock committed Sep 12, 2024
2 parents 088fa68 + 19f13a9 commit 8f189f7
Show file tree
Hide file tree
Showing 81 changed files with 5,855 additions and 1,170 deletions.
13 changes: 6 additions & 7 deletions apps/bot/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { serve } from "bun";
import c from "config";
import { db } from "db";
import { eq } from "db/drizzle";
import { discordVerification, users } from "db/schema";
import { discordVerification } from "db/schema";
import { getHacker } from "db/functions";
import { nanoid } from "nanoid";
import { z } from "zod";

/* DISCORD BOT */

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

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 All @@ -210,7 +209,7 @@ app.post("/api/checkDiscordVerification", async (h) => {

const { discordRole: userGroupRoleName } = (
c.groups as Record<string, { discordRole: string }>
)[Object.keys(c.groups)[user.group]];
)[Object.keys(c.groups)[user.hackerData.group]];

const guild = client.guilds.cache.get(verification.guild);
if (!guild) {
Expand All @@ -228,7 +227,7 @@ app.post("/api/checkDiscordVerification", async (h) => {
if (!role || !userGroupRole) {
console.log(
"failed cause could not find a role, was looking for group " +
user.group +
user.hackerData.group +
" called " +
userGroupRoleName,
);
Expand Down
30 changes: 9 additions & 21 deletions apps/web/src/actions/admin/scanner-admin-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { adminAction } from "@/lib/safe-action";
import { z } from "zod";
import { db } from "db";
import { scans, users } from "db/schema";
import { db, sql } from "db";
import { scans, userCommonData } from "db/schema";
import { eq, and } from "db/drizzle";
export const createScan = adminAction(
z.object({
Expand Down Expand Up @@ -46,22 +46,10 @@ export const getScan = adminAction(
},
);

export const checkInUser = adminAction(
z.string(),
async (user, { userId: adminUserID }) => {
// Check if scanner is an admin
const isAdmin = ["admin", "super_admin"].includes(
(
await db
.select({ role: users.role })
.from(users)
.where(eq(users.clerkID, adminUserID))
)[0].role,
);
// Set checkedIn to true
return await db
.update(users)
.set({ checkedIn: true })
.where(eq(users.clerkID, user));
},
);
export const checkInUser = adminAction(z.string(), async (user) => {
// Set checkinTimestamp
return await db
.update(userCommonData)
.set({ checkinTimestamp: sql`now()` })
.where(eq(userCommonData.clerkID, user));
});
12 changes: 6 additions & 6 deletions apps/web/src/actions/admin/user-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { adminAction } from "@/lib/safe-action";
import { z } from "zod";
import { perms } from "config";
import { users } from "db/schema";
import { userCommonData } from "db/schema";
import { db } from "db";
import { eq } from "db/drizzle";
import { revalidatePath } from "next/cache";
Expand All @@ -21,9 +21,9 @@ export const updateRole = adminAction(
throw new Error("You are not allowed to do this");
}
await db
.update(users)
.update(userCommonData)
.set({ role: roleToSet })
.where(eq(users.clerkID, userIDToUpdate));
.where(eq(userCommonData.clerkID, userIDToUpdate));
revalidatePath(`/admin/users/${userIDToUpdate}`);
return { success: true };
},
Expand All @@ -37,9 +37,9 @@ export const setUserApproval = adminAction(

async ({ userIDToUpdate, approved }, { user, userId }) => {
await db
.update(users)
.set({ approved })
.where(eq(users.clerkID, userIDToUpdate));
.update(userCommonData)
.set({ isApproved: approved })
.where(eq(userCommonData.clerkID, userIDToUpdate));
revalidatePath(`/admin/users/${userIDToUpdate}`);
return { success: true };
},
Expand Down
14 changes: 7 additions & 7 deletions apps/web/src/actions/rsvp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { authenticatedAction } from "@/lib/safe-action";
import { z } from "zod";
import { db } from "db";
import { eq } from "db/drizzle";
import { users } from "db/schema";
import { userCommonData } from "db/schema";
import { getUser } from "db/functions";

export const rsvpMyself = authenticatedAction(
z.any(),
async (_, { userId }) => {
const user = await db.query.users.findFirst({
where: eq(users.clerkID, userId),
});
const user = await getUser(userId);
if (!user) throw new Error("User not found");

await db
.update(users)
.set({ rsvp: true })
.where(eq(users.clerkID, userId));
.update(userCommonData)
.set({ isRSVPed: true })
.where(eq(userCommonData.clerkID, userId));
return { success: true };
},
);
34 changes: 15 additions & 19 deletions apps/web/src/actions/teams.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
"use server";

// TODO: update team /api enpoints to be actions
// TODO: update team /api endpoints to be actions

import { authenticatedAction } from "@/lib/safe-action";
import { z } from "zod";
import { db } from "db";
import { users, teams, invites } from "db/schema";
import { userHackerData, teams, invites } from "db/schema";
import { eq } from "db/drizzle";
import { revalidatePath } from "next/cache";
import { toCalendar } from "@internationalized/date";
import { getHacker } from "db/functions";

export const leaveTeam = authenticatedAction(
z.null(),
async (_, { userId }) => {
const user = await db.query.users.findFirst({
where: eq(users.clerkID, userId),
with: {
team: 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.team === null || user.team === undefined) {
if (!user.hackerData.teamID) {
revalidatePath("/dash/team");
return {
success: false,
Expand All @@ -34,13 +26,17 @@ export const leaveTeam = authenticatedAction(

const result = await db.transaction(async (tx) => {
await tx
.update(users)
.update(userHackerData)
.set({ teamID: null })
.where(eq(users.clerkID, userId));
.where(eq(userHackerData.clerkID, user.clerkID));
const team = await tx.query.teams.findFirst({
where: eq(teams.id, user.team?.id as string), // Added null check for user.team. Converted to string since TS does not realise for some reason that we checked above.
where: eq(teams.id, user.hackerData.teamID as string), // Converted to string since TS does not realise for some reason that we checked above.
with: {
members: true,
members: {
with: {
commonData: true,
},
},
},
});

Expand Down Expand Up @@ -71,7 +67,7 @@ export const leaveTeam = authenticatedAction(
revalidatePath("/dash/team");
return {
success: true,
message: `Team has been left. Ownership has been transferred to ${team.members[0].firstName} ${team.members[0].lastName}.`,
message: `Team has been left. Ownership has been transferred to ${team.members[0].commonData.firstName} ${team.members[0].commonData.lastName}.`,
};
}
revalidatePath("/dash/team");
Expand Down
29 changes: 13 additions & 16 deletions apps/web/src/actions/user-profile-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import { authenticatedAction } from "@/lib/safe-action";
import { z } from "zod";
import { db } from "db";
import { users, profileData, registrationData } from "db/schema";
import { userCommonData } from "db/schema";
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,14 +17,13 @@ export const modifyRegistrationData = authenticatedAction(
skills: z.string().max(100),
}),
async ({ bio, skills }, { userId }) => {
const user = await db.query.users.findFirst({
where: eq(users.clerkID, userId),
});
const user = await getUser(userId);
if (!user) throw new Error("User not found");

await db
.update(profileData)
.update(userCommonData)
.set({ bio })
.where(eq(profileData.hackerTag, user.hackerTag));
.where(eq(userCommonData.clerkID, user.clerkID));
return { success: true, newbio: bio };
},
);
Expand All @@ -34,14 +34,13 @@ export const modifyAccountSettings = authenticatedAction(
lastName: z.string().min(1).max(50),
}),
async ({ firstName, lastName }, { userId }) => {
const user = await db.query.users.findFirst({
where: eq(users.clerkID, userId),
});
const user = await getUser(userId);
if (!user) throw new Error("User not found");

await db
.update(users)
.update(userCommonData)
.set({ firstName, lastName })
.where(eq(users.clerkID, userId));
.where(eq(userCommonData.clerkID, userId));
return {
success: true,
newFirstName: firstName,
Expand All @@ -54,16 +53,14 @@ 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.users.findFirst({
where: eq(users.clerkID, userId),
});
const user = await getUser(userId);
if (!user) throw new Error("User not found");

const blobUpload = await put(image.name, image, { access: "public" });
await db
.update(profileData)
.update(userCommonData)
.set({ profilePhoto: blobUpload.url })
.where(eq(profileData.hackerTag, user.hackerTag));
.where(eq(userCommonData.clerkID, user.clerkID));
revalidatePath("/settings/profile");
return { success: true };
},
Expand Down
80 changes: 20 additions & 60 deletions apps/web/src/app/admin/check-in/page.tsx
Original file line number Diff line number Diff line change
@@ -1,83 +1,43 @@
import CheckinScanner from "@/components/admin/scanner/CheckinScanner";
import FullScreenMessage from "@/components/shared/FullScreenMessage";
import { db } from "db";
import { eq, and } from "db/drizzle";
import { events, users, scans } from "db/schema";
import { getUser } from "db/functions";

export default async function Page({
searchParams,
}: {
searchParams: { [key: string]: string | undefined };
}) {
// TODO: maybe move event existant check into a layout so it holds state?

// if (!params || !params.id || isNaN(parseInt(params.id))) {
// return (
// <FullScreenMessage
// title={"Invalid ID"}
// message={"The Event ID in the URL is invalid."}
// />
// );
// }

// const event = await db.query.events.findFirst({
// where: eq(events.id, parseInt(params.id)),
// });

// if (!event) {
// return (
// <FullScreenMessage
// title={"Invalid ID"}
// message={"The Event ID in the URL is invalid."}
// />
// );
// }

// Returns only if search params exist
if (searchParams.user) {
const [isChecked, scanUser, hasRSVPed] = await db.transaction(
async (tx) => {
const scanUser = await tx.query.users.findFirst({
where: eq(users.clerkID, searchParams.user ?? "unknown"),
});
if (!scanUser) {
return [null, null, null];
}
const scan = await tx
.select({
isChecked: users.checkedIn,
hasRSVPed: users.rsvp,
})
.from(users)
.where(eq(users.clerkID, searchParams.user!));
if (scan) {
return [scan[0].isChecked, scanUser, scan[0].hasRSVPed];
} else {
return [null, scanUser, null];
}
},
if (!searchParams.user)
return (
<div>
<CheckinScanner
hasScanned={false}
checkedIn={null}
scanUser={null}
hasRSVP={null}
/>
</div>
);

const scanUser = await getUser(searchParams.user);
if (!scanUser)
return (
<div>
<CheckinScanner
checkedIn={isChecked}
hasScanned={true}
scanUser={scanUser}
hasRSVP={hasRSVPed}
checkedIn={null}
scanUser={null}
hasRSVP={null}
/>
</div>
);
}

// Fall through case
return (
<div>
<CheckinScanner
hasScanned={false}
checkedIn={null}
scanUser={null}
hasRSVP={null}
hasScanned={true}
checkedIn={scanUser.checkinTimestamp != null}
scanUser={scanUser}
hasRSVP={scanUser.isRSVPed}
/>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/admin/events/new/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NewEventForm from "@/components/admin/events/NewEventForm";
import NewEventForm from "@/components/events/admin/NewEventForm";

export default function Page() {
const defaultDate = new Date();
Expand Down
Loading

0 comments on commit 8f189f7

Please sign in to comment.