Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
112 changes: 112 additions & 0 deletions drizzle/seedScores.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import crypto from "crypto";
import { eq } from "drizzle-orm";
import { db } from "@/server/db";
import { organization, user } from "@/server/db/auth-schema";
import {
hackathonSettings,
judgingAssignments,
judgingRoomStaff,
judgingRooms,
judgingRounds
} from "@/server/db/schema";
import { criteria, scores } from "@/server/db/scores-schema";

async function main() {
console.log("Starting scoring seed...");

try {
// Create judging round
const roundId = crypto.randomUUID();
await db.insert(judgingRounds).values({
id: roundId,
name: "Round 1",
startTime: new Date(),
endTime: new Date(Date.now() + 2 * 60 * 60 * 1000),
createdAt: new Date(),
updatedAt: new Date()
});

// Insert criteria
const criteriaList = [
{ name: "Innovation", maxScore: 10 },
{ name: "Technical Complexity", maxScore: 10 },
{ name: "Design", maxScore: 10 },
{ name: "Presentation", maxScore: 10 }
];

const createdCriteria = [];
for (const c of criteriaList) {
const id = crypto.randomUUID();
await db.insert(criteria).values({
id,
name: c.name,
maxScore: c.maxScore,
isSidepot: false
});
createdCriteria.push({ id, ...c });
}

// Get judges and teams
const judges = await db.select().from(user).where(eq(user.role, "judge"));
if (judges.length === 0) throw new Error("No judges found");

const allTeams = await db.select().from(organization);
if (allTeams.length === 0) throw new Error("No teams found");

// Each judge gets their own room, all teams are assigned to each room
for (const judge of judges) {
const roomId = crypto.randomUUID();

// Create a room for this judge
await db.insert(judgingRooms).values({
id: roomId,
roundId,
roomLink: `https://meet.example.com/room-${roomId}`,
createdAt: new Date(),
updatedAt: new Date()
});

// Assign judge as staff for this room
await db.insert(judgingRoomStaff).values({
id: crypto.randomUUID(),
roomId,
staffId: judge.id,
createdAt: new Date()
});

// Assign every team to this room and generate scores
for (const team of allTeams) {
const assignmentId = crypto.randomUUID();

await db.insert(judgingAssignments).values({
id: assignmentId,
teamId: team.id,
roomId,
createdAt: new Date()
});

for (const c of createdCriteria) {
const randomScore = Math.floor(Math.random() * (c.maxScore + 1));
await db.insert(scores).values({
id: crypto.randomUUID(),
assignmentId,
criteriaId: c.id,
value: randomScore,
createdAt: new Date()
});
}
}
}

// Set active round in hackathon settings
await db.update(hackathonSettings).set({ currentRoundId: roundId });

console.log("Scoring seed completed successfully");
process.exit(0);
} catch (error) {
console.error("Scoring seed failed:", error);
process.exit(1);
}
}

main();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "next build",
"check": "biome check .",
"seed": "bun drizzle/seed.ts",
"seed:scores": "bun drizzle/seedScores.ts",
"check:ci": "biome ci",
"check:unsafe": "biome check --write --unsafe .",
"check:write": "biome check --write .",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import TeamTable from "@/app/components/admin/teamtable/TeamTable";
import UserTable from "@/app/components/admin/usertable";
import { requireRole } from "@/server/better-auth/auth-helpers/helpers";
import { Role } from "@/types/types";
import ScoreTable from "../components/admin/scoreTable/ScoreTable";
import styles from "../dashboard.module.scss";

export default async function AdminPage() {
Expand Down Expand Up @@ -31,6 +32,10 @@ export default async function AdminPage() {
<h2 className={styles.sectionTitle}>Teams</h2>
<TeamTable />
</div>
<div>
<h2 className={styles.sectionTitle}>Scores</h2>
<ScoreTable />
</div>
</div>
</main>
);
Expand Down
68 changes: 68 additions & 0 deletions src/app/components/admin/scoreTable/ScoreTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"use client";

import type { ColDef } from "ag-grid-community";
import {
AllCommunityModule,
ModuleRegistry,
themeQuartz
} from "ag-grid-community";
import { AgGridReact } from "ag-grid-react";
import { useMemo } from "react";
import { api } from "@/trpc/react";
import { TABLE_THEME_PARAMS } from "@/types/teamTableConstants";
import type { TeamRanking } from "@/types/types";

ModuleRegistry.registerModules([AllCommunityModule]);

export default function ScoreTable() {
const { data, isLoading } = api.teams.getRankings.useQuery();

const theme = themeQuartz.withParams(TABLE_THEME_PARAMS);

const columnDefs = useMemo<ColDef<TeamRanking>[]>(
() => [
{
headerName: "Rank",
width: 100,
sortable: false,
valueGetter: (params) => (params.node ? params.node.rowIndex! + 1 : "")
},
{
headerName: "Team Name",
field: "name",
sortable: true,
filter: true
},
{
headerName: "Total Score",
field: "totalScore",
sort: "desc", // 👈 default descending
filter: "agNumberColumnFilter",
sortable: true
}
],
[]
);

const defaultColDef = useMemo<ColDef<TeamRanking>>(
() => ({
flex: 1,
resizable: true
}),
[]
);

// TODO: remove height and width inline style
return (
<div style={{ height: 600, width: "100%" }}>
Comment thread
Essam-Khawaja marked this conversation as resolved.
<AgGridReact
columnDefs={columnDefs}
defaultColDef={defaultColDef}
getRowId={({ data }) => data.id}
loading={isLoading}
rowData={data ?? []}
theme={theme}
/>
</div>
);
}
26 changes: 24 additions & 2 deletions src/server/api/routers/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

import crypto from "node:crypto";
import { TRPCError } from "@trpc/server";
import { and, desc, eq } from "drizzle-orm";
// import { and, desc, eq } from "drizzle-orm";
import { and, desc, eq, sql } from "drizzle-orm";
import { z } from "zod";
import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc";
import type { db as dbType } from "@/server/db";
Expand All @@ -32,6 +33,7 @@ import {
organization,
user
} from "@/server/db/auth-schema";
import type { TeamRanking } from "@/types/types";
import { MEMBER_ROLES } from "@/types/types";

const teamNameSchema = z
Expand Down Expand Up @@ -371,5 +373,25 @@ export const teamsRouter = createTRPCRouter({
.where(eq(organization.id, id))
.returning();
return updated;
})
}),
// New feature: Querying descending for score
getRankings: protectedProcedure.query(async ({ ctx }) => {
const result = await ctx.db.execute(
sql<TeamRanking>`
SELECT
o.id,
o.name,
COALESCE(SUM(s.value), 0) AS "totalScore"
FROM hackathon_organization o
LEFT JOIN hackathon_judging_assignment a
ON a.team_id = o.id
LEFT JOIN hackathon_score s
ON s.assignment_id = a.id
GROUP BY o.id, o.name
ORDER BY "totalScore" DESC
`
);

return result as unknown as TeamRanking[]; // Fixes the CI/CD error in ScoreTable.tsx where the type of data was not being recognized as TeamRanking[]
})
});
6 changes: 6 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export const MEMBER_ROLES = {
ADMIN: "admin"
} as const;

export type TeamRanking = {
id: string;
name: string;
totalScore: number;
};

export const ALL_ROLES: Role[] = [Role.ADMIN, Role.JUDGE, Role.PARTICIPANT];

export const ALL_ORGANIZATION_ROLES: OrganizationRole[] = [
Expand Down
Loading