- Fix settings action silently resetting timer values when draft speed select is disabled (add null guard before overwriting draftInitialTime/ draftIncrementTime) - Make timer decrement and pick increment atomic using SQL expressions to prevent read-modify-write races between the timer loop and HTTP handlers - Add UNIQUE INDEX on (season_id, pick_number) in draft_picks to prevent duplicate picks from concurrent requests (TOCTOU guard) - Add socket join-draft team ownership validation via DB query - Add iteration cap to autodraft chain while loop (max = totalTeams) - Add draftPaused re-check before firing autodraft chain in make-pick and force-manual-pick - Consolidate all snake draft calculations into calculatePickInfo (DRY); remove duplicated logic from timer.ts, make-pick, force-manual-pick, executeAutoPick, and checkAndTriggerNextAutodraft - Fix calculatePickInfo to return snake-adjusted pickInRound matching draftOrder values instead of raw pre-snake value - Fix timer-update socket events to emit nextPickNumber after a pick instead of the already-completed currentPickNumber - Fix force-manual-pick to validate submitted teamId matches the team whose turn it actually is at the given pick number - Replace inline timer init in draft.start with deleteSeasonTimers + initializeDraftTimers model functions (dead code fix) - Fix draft loader to not crash when getTeamQueue fails (returns []) - Fix misleading 403 message for commissioner picks - Remove dead hidden inputs from league settings form - Document connectedTeams single-instance limitation in socket.ts Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
import { getAuth } from "@clerk/react-router/server";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
import { eq, and } from "drizzle-orm";
|
|
import { deleteSeasonTimers, initializeDraftTimers } from "~/models/draft-timer";
|
|
import { getSocketIO } from "../../../server/socket";
|
|
|
|
import type { ActionFunctionArgs } from "react-router";
|
|
export async function action(args: ActionFunctionArgs) {
|
|
const { request } = args;
|
|
const { userId } = await getAuth(args);
|
|
|
|
if (!userId) {
|
|
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const formData = await request.formData();
|
|
const seasonId = formData.get("seasonId") as string;
|
|
|
|
if (!seasonId) {
|
|
return Response.json({ error: "Missing seasonId" }, { status: 400 });
|
|
}
|
|
|
|
const db = database();
|
|
|
|
// Get season details
|
|
const season = await db.query.seasons.findFirst({
|
|
where: eq(schema.seasons.id, seasonId),
|
|
});
|
|
|
|
if (!season) {
|
|
return Response.json({ error: "Season not found" }, { status: 404 });
|
|
}
|
|
|
|
// Check if user is commissioner
|
|
const isCommissioner = await db.query.commissioners.findFirst({
|
|
where: and(
|
|
eq(schema.commissioners.leagueId, season.leagueId),
|
|
eq(schema.commissioners.userId, userId)
|
|
),
|
|
});
|
|
|
|
if (!isCommissioner) {
|
|
return Response.json({ error: "Only commissioners can start the draft" }, { status: 403 });
|
|
}
|
|
|
|
// Check if draft already started
|
|
if (season.status === "draft" || season.status === "active" || season.status === "completed") {
|
|
return Response.json({ error: "Draft already started or completed" }, { status: 400 });
|
|
}
|
|
|
|
// Validate draft slots exist before modifying any state
|
|
const draftSlots = await db.query.draftSlots.findMany({
|
|
where: eq(schema.draftSlots.seasonId, seasonId),
|
|
});
|
|
|
|
if (draftSlots.length === 0) {
|
|
return Response.json({ error: "No draft slots found for this season" }, { status: 400 });
|
|
}
|
|
|
|
// Update season status to draft
|
|
await db
|
|
.update(schema.seasons)
|
|
.set({
|
|
status: "draft",
|
|
currentPickNumber: 1,
|
|
})
|
|
.where(eq(schema.seasons.id, seasonId));
|
|
|
|
const initialTime = season.draftInitialTime || 120;
|
|
|
|
// Reset timers for all teams
|
|
await deleteSeasonTimers(seasonId);
|
|
await initializeDraftTimers(
|
|
seasonId,
|
|
draftSlots.map((slot) => ({ id: slot.teamId })),
|
|
initialTime
|
|
);
|
|
|
|
// Emit socket event
|
|
try {
|
|
getSocketIO().to(`draft-${seasonId}`).emit("draft-started", {
|
|
seasonId,
|
|
currentPickNumber: 1,
|
|
});
|
|
} catch (error) {
|
|
console.error("Socket.IO error:", error);
|
|
}
|
|
|
|
return Response.json({ success: true });
|
|
}
|