brackt/app/routes/api/draft.force-autopick.ts
Chris Parsons 01f1480159
Claude/fix pick timer ghll n (#29)
* Fix force-manual-pick resetting next team's timer to initial time

When a commissioner forced a manual pick, the next team's timer was
being reset to the initial time (2 minutes) instead of carrying
forward their existing time bank balance.

This aligns force-manual-pick with the behavior of regular user picks
and force-autopick: the picking team gets their increment added, and
the next team's timer is left untouched so their bank carries forward.

https://claude.ai/code/session_01X7gwWmafUSEvVHcV7Raz5p

* Add regression tests for draft.force-manual-pick timer behavior

18 tests across 5 describe blocks covering:
- Authorization (401/403)
- Input validation (missing fields, bad participant, ineligible sport)
- Successful pick (response shape, draft-complete detection, socket events)
- Timer behavior (increment added to picking team, new timer creation, additive not reset)
- Two regression tests confirming the next team's timer is never touched:
  draftTimers.findFirst called exactly once, no timer-update emitted for
  next team, db.update called exactly twice (not three times)

https://claude.ai/code/session_01X7gwWmafUSEvVHcV7Raz5p

* Add TypeScript types and improve draft validation (#28)

* Code review fixes: type safety, security hardening, and dead code removal

- Fix Socket.IO event types: draft-paused and draft-resumed were typed as
  () => void but are emitted with { seasonId, paused } data payloads

- Fix draft.force-manual-pick: add missing season.status === "draft" guard
  so commissioners cannot force picks outside an active draft; add duplicate
  pick-number check so a slot cannot be assigned two picks (the previous
  code only checked participant uniqueness, not slot uniqueness)

- Replace args: any with ActionFunctionArgs / Route.LoaderArgs across all
  API routes and league loaders; replace (auth as any).userId casts with
  proper const { userId } = await getAuth(args) destructuring

- Remove unused isSnakeDraft = true dead variable from draft.make-pick

- Replace autodraftSettings: any and draftSlots: any[] in draft-utils with
  properly typed InferSelectModel / DraftSlot types

- Update force-manual-pick tests: sequence draftPicks.findFirst mock for
  the two-call flow; add new tests for status-check and slot-uniqueness

https://claude.ai/code/session_01FKq2gPFYpgdfxr8cw4Z2AZ

* Fix RouterContextProvider type errors in action test files

Cast context argument to RouterContextProvider in test helpers so
ActionFunctionArgs strict typing is satisfied without weakening the
production action signatures back to any.

https://claude.ai/code/session_01FKq2gPFYpgdfxr8cw4Z2AZ

---------

Co-authored-by: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-22 19:29:29 -08:00

69 lines
2 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 { executeAutoPick } from "~/models/draft-utils";
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;
const teamId = formData.get("teamId") as string;
const pickNumber = parseInt(formData.get("pickNumber") as string);
if (!seasonId || !teamId || !pickNumber) {
return Response.json({ error: "Missing required fields" }, { status: 400 });
}
const db = database();
// Get season details to verify commissioner permissions
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 force autopick" }, { status: 403 });
}
// Execute the autopick using the unified function
const result = await executeAutoPick({
seasonId,
teamId,
pickNumber,
triggeredBy: "commissioner",
commissionerUserId: userId,
db,
});
if (!result.success) {
return Response.json({ error: result.error }, { status: 400 });
}
return Response.json({
success: true,
pick: result.pick,
participant: result.participant,
nextPickNumber: result.nextPickNumber,
isDraftComplete: result.isDraftComplete,
});
}