brackt/app/routes/admin.sports-seasons.$id.expected-values.server.ts
Chris Parsons d5aa8a3de4
fix: address VORP code review issues (#283)
* fix: address VORP code review issues

- Switch admin EV form to batchUpsertParticipantEVs to avoid N×N
  concurrent UPDATE storm (was calling upsertParticipantEV per
  participant, each triggering a full syncVorpForSeason)
- deleteParticipantEV now resets vorpValue to "0" and calls
  syncVorpForSeason so remaining participants' ranks stay correct
- syncVorpForSeason issues a single bulk CASE UPDATE instead of
  N individual UPDATE statements
- Add doc warning on recalculateEV that callers must sync VORP manually
- Extract REPLACEMENT_LEVEL_START/END_IDX constants; clarify comment
  that 12-14 is a fixed product decision, not derived from league size
- Include vorpValue in draft room participant select projection
- Update drizzle-orm mock to support sql.join; update test assertions
  to reflect single bulk-update call

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: resolve lint errors (toSorted, unused var)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-08 23:20:02 -04:00

76 lines
2.8 KiB
TypeScript

import type { Route } from "./+types/admin.sports-seasons.$id.expected-values";
import { logger } from "~/lib/logger";
import { findSportsSeasonById } from "~/models/sports-season";
import { findParticipantsBySportsSeasonId } from "~/models/participant";
import {
batchUpsertParticipantEVs,
getAllParticipantEVsForSeason
} from "~/models/participant-expected-value";
export async function loader({ params }: Route.LoaderArgs) {
const sportsSeason = await findSportsSeasonById(params.id);
if (!sportsSeason) {
throw new Response("Sports season not found", { status: 404 });
}
const participants = await findParticipantsBySportsSeasonId(params.id);
const existingEVs = await getAllParticipantEVsForSeason(params.id);
// Create a map of participant ID to EV data
const evMap = new Map(existingEVs.map(ev => [ev.participantId, ev]));
return {
sportsSeason: sportsSeason as typeof sportsSeason & { sport: { id: string; name: string; type: string; slug: string } },
participants,
existingEVs: evMap,
};
}
const scoringRules = {
pointsFor1st: 100,
pointsFor2nd: 70,
pointsFor3rd: 45,
pointsFor4th: 45,
pointsFor5th: 20,
pointsFor6th: 20,
pointsFor7th: 20,
pointsFor8th: 20,
};
export async function action({ request, params }: Route.ActionArgs) {
const formData = await request.formData();
const participants = await findParticipantsBySportsSeasonId(params.id);
const participantIds = participants.map((p: { id: string }) => p.id);
try {
const inputs = participantIds.map((participantId) => ({
participantId,
sportsSeasonId: params.id,
probabilities: {
probFirst: parseFloat(formData.get(`probFirst_${participantId}`) as string || "0") / 100,
probSecond: parseFloat(formData.get(`probSecond_${participantId}`) as string || "0") / 100,
probThird: parseFloat(formData.get(`probThird_${participantId}`) as string || "0") / 100,
probFourth: parseFloat(formData.get(`probFourth_${participantId}`) as string || "0") / 100,
probFifth: parseFloat(formData.get(`probFifth_${participantId}`) as string || "0") / 100,
probSixth: parseFloat(formData.get(`probSixth_${participantId}`) as string || "0") / 100,
probSeventh: parseFloat(formData.get(`probSeventh_${participantId}`) as string || "0") / 100,
probEighth: parseFloat(formData.get(`probEighth_${participantId}`) as string || "0") / 100,
},
scoringRules,
source: "manual" as const,
}));
const results = await batchUpsertParticipantEVs(inputs);
const totalEV = results.reduce((sum, r) => sum + parseFloat(r.expectedValue), 0);
return { success: true, totalEV };
} catch (error) {
logger.error("Error saving probabilities:", error);
return {
error: error instanceof Error ? error.message : "Failed to save probabilities"
};
}
}