brackt/app/routes/api/draft.pause.ts
Chris Parsons 677b8ce816
All checks were successful
🚀 Deploy / 🧪 Test (pull_request) Successful in 2m34s
🚀 Deploy / ʦ🔍 Typecheck & Lint (pull_request) Successful in 1m19s
🚀 Deploy / 🐳 Build (pull_request) Has been skipped
🚀 Deploy / 🚀 Deploy (pull_request) Has been skipped
Fix three timer-pause gaps found in code review
- pauseDraftOnError now calls onDraftPaused() before emitting so the
  error-path pause carries the same snapshotted timer data as the
  manual-pause route (fixes inconsistency between the two paths)
- Parallelize DB updates in onDraftPaused with Promise.all instead of
  sequential awaits
- Extract clientExpiresAt() helper to draft-timer.ts and use it in all
  three places that re-anchor timeRemaining to the client clock, replacing
  duplicated Date.now() + timeRemaining * 1000 expressions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-06 08:52:04 -07:00

89 lines
2.5 KiB
TypeScript

import { auth } from "~/lib/auth.server";
import { eq } from "drizzle-orm";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
import { isCommissioner } from "~/models/commissioner";
import { logCommissionerAction } from "~/models/audit-log";
import { getSocketIO } from "../../../server/socket";
import { onDraftPaused } from "../../../server/timer";
import { logger } from "~/lib/logger";
import type { ActionFunctionArgs } from "react-router";
export async function action(args: ActionFunctionArgs) {
const { request } = args;
const session = await auth.api.getSession({ headers: args.request.headers });
const userId = session?.user.id ?? null;
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: "Season ID is required" }, { status: 400 });
}
const db = database();
// Get season
const season = await db.query.seasons.findFirst({
where: eq(schema.seasons.id, seasonId),
with: {
league: true,
},
});
if (!season) {
return Response.json({ error: "Season not found" }, { status: 404 });
}
// Verify user is commissioner
if (!(await isCommissioner(season.leagueId, userId))) {
return Response.json(
{ error: "Only commissioners can pause the draft" },
{ status: 403 }
);
}
// Check if draft is active
if (season.status !== "draft") {
return Response.json({ error: "Draft is not active" }, { status: 400 });
}
// Pause the draft
await db
.update(schema.seasons)
.set({ draftPaused: true })
.where(eq(schema.seasons.id, seasonId));
await logCommissionerAction({
seasonId,
leagueId: season.leagueId,
actorUserId: userId,
action: "draft_paused",
details: { pickNumber: season.currentPickNumber },
});
// Cancel the in-memory timeout and snapshot remaining time so resume is accurate.
let snapshots: Array<{ teamId: string; timeRemaining: number }> = [];
try {
snapshots = await onDraftPaused(seasonId);
} catch (err) {
logger.error("[Pause] onDraftPaused failed:", err);
}
// Emit socket event with snapshotted timer values so clients sync immediately.
try {
getSocketIO().to(`draft-${seasonId}`).emit("draft-paused", {
seasonId,
paused: true,
timers: snapshots,
});
} catch (error) {
logger.error("Socket.IO error:", error);
}
return Response.json({ success: true, paused: true });
}