Phase 3: admin magic-link auth and event setup screens

- /login sends a Supabase magic link (admin emails only); /auth/confirm
  completes sign-in; proxy.ts guards /admin and every server action
  re-checks with requireAdmin(), since actions are plain POST endpoints.
- /admin lists, creates, duplicates and deletes events.
- /admin/events/[eventId] edits teams (colour, order) and games with
  criteria, presets and notes. All input goes through zod schemas that
  mirror the database constraints so errors read as plain sentences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-03 13:47:36 +02:00
co-authored by Claude Opus 5.5
parent 0c66e9b80e
commit b60c9db4f4
30 changed files with 1930 additions and 2 deletions
+99
View File
@@ -0,0 +1,99 @@
import { z } from "zod";
import { idSchema } from "@/lib/tokens";
// Limits mirror the CHECK constraints in supabase/migrations/0001_init.sql so that
// the admin sees a friendly message instead of a database error.
const text = (max: number, what: string) =>
z
.string()
.trim()
.min(1, `${what} can't be empty.`)
.max(max, `${what} can be at most ${max} characters.`);
const optionalText = (max: number, what: string) =>
z.string().trim().max(max, `${what} can be at most ${max} characters.`);
const number = (what: string) => z.number(`${what} must be a number.`);
export const eventSchema = z.object({
name: text(120, "Event name"),
eventDate: z.union([z.iso.date("Pick a valid date."), z.literal("")]),
});
export const teamSchema = z.object({
name: text(60, "Team name"),
color: z.string().regex(/^#[0-9A-Fa-f]{6}$/, "Pick a colour."),
});
/** Is (max − min) a whole number of steps? Tolerates float error for steps like 0.5. */
const onStep = (min: number, max: number, step: number) => {
const steps = (max - min) / step;
return Math.abs(steps - Math.round(steps)) < 1e-9;
};
export const criterionSchema = z
.object({
id: idSchema.optional(),
label: text(60, "Criterion label"),
description: optionalText(300, "Criterion description"),
inputType: z.enum(["blocks", "number"]),
min: number("Min").min(-999999).max(999999),
max: number("Max").min(-999999).max(999999),
step: number("Step").positive("Step must be more than 0.").max(999999),
weight: number("Weight").min(0, "Weight can't be negative.").max(9999),
})
.superRefine((criterion, context) => {
const name = criterion.label || "A criterion";
if (criterion.max <= criterion.min) {
context.addIssue({ code: "custom", message: `${name}: max must be more than min.` });
return;
}
if (!onStep(criterion.min, criterion.max, criterion.step)) {
context.addIssue({
code: "custom",
message: `${name}: the step must divide evenly into the range from min to max.`,
});
return;
}
if (
criterion.inputType === "blocks" &&
(criterion.max - criterion.min) / criterion.step > 10
) {
context.addIssue({
code: "custom",
message: `${name}: blocks can show at most 11 buttons. Use a number input or a bigger step.`,
});
}
});
export const gameSchema = z.object({
title: text(120, "Game title"),
description: optionalText(2000, "Description"),
scoringMode: z.enum(["judged", "direct"]),
pointsMode: z.enum(["raw", "placement"]),
multiplier: number("Multiplier").min(0, "Multiplier can't be negative.").max(9999),
placementPoints: z
.array(z.number().int("Placement points must be whole numbers.").min(0).max(100000))
.min(1, "Add at least one placement value, like 10, 7, 5, 3.")
.max(30),
adminNotes: optionalText(4000, "Admin notes"),
judgeNotes: optionalText(4000, "Judge notes"),
criteria: z.array(criterionSchema).min(1, "A game needs at least one criterion.").max(20),
});
export type CriterionDraft = z.input<typeof criterionSchema>;
export type GameDraft = z.input<typeof gameSchema>;
/** Parses "10, 7, 5, 3" into numbers; invalid parts become NaN so validation reports them. */
export function parsePlacementPoints(value: string): number[] {
return value
.split(/[,\s]+/)
.filter(Boolean)
.map((part) => Number(part));
}
export function firstError(error: z.ZodError): string {
return error.issues[0]?.message ?? "Something in the form isn't valid.";
}