refactor: remove legacy direct-write fallback in batch-add-results
Every qualifying scoring_event now has tournament_id (check constraint enforces it), so the non-canonical fallback is dead. Replace with an explicit error surface so any configuration regression becomes visible instead of silently bypassing canonical. Phase 4 Task 4. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
549b437466
commit
e2a62bd05b
1 changed files with 26 additions and 39 deletions
|
|
@ -384,52 +384,39 @@ export async function action({ request, params }: Route.ActionArgs) {
|
||||||
return { error: "Scoring event not found" };
|
return { error: "Scoring event not found" };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scoringEvent.tournamentId) {
|
// Qualifying events always have a canonical tournament link (check
|
||||||
// Canonical path: translate season_participant.id → canonical participant.id
|
// constraint). Write results canonically and fan out.
|
||||||
const spRows = allSeasonParticipants.filter((sp) =>
|
if (!scoringEvent.tournamentId) {
|
||||||
incoming.some((r) => r.participantId === sp.id),
|
|
||||||
);
|
|
||||||
const missingCanonical = spRows.filter((sp) => !sp.participantId);
|
|
||||||
if (missingCanonical.length > 0) {
|
|
||||||
return {
|
|
||||||
error: `Some season participants are not linked to canonical participants: ${missingCanonical.map((sp) => sp.name).join(", ")}`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const row of incoming) {
|
|
||||||
const sp = spRows.find((s) => s.id === row.participantId);
|
|
||||||
if (!sp?.participantId) continue;
|
|
||||||
await upsertTournamentResult({
|
|
||||||
tournamentId: scoringEvent.tournamentId,
|
|
||||||
participantId: sp.participantId,
|
|
||||||
placement: row.placement,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const syncReport = await syncTournamentResults(scoringEvent.tournamentId);
|
|
||||||
return {
|
return {
|
||||||
success: `${incoming.length} result${incoming.length === 1 ? "" : "s"} saved canonically. Synced to ${syncReport.windowsSynced} window${syncReport.windowsSynced === 1 ? "" : "s"}${syncReport.windowsFailed > 0 ? ` (${syncReport.windowsFailed} failed)` : ""}.`,
|
error:
|
||||||
syncReport,
|
"This scoring event has no canonical tournament link — cannot save batch results. Contact a developer.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy direct-write path for non-canonical events (team sports, etc.).
|
const spRows = allSeasonParticipants.filter((sp) =>
|
||||||
const existingResults = await getEventResults(params.eventId);
|
incoming.some((r) => r.participantId === sp.id),
|
||||||
const existingIds = new Set(existingResults.map((r) => r.seasonParticipantId));
|
);
|
||||||
const newResults = filterNewResults(incoming, existingIds);
|
const missingCanonical = spRows.filter((sp) => !sp.participantId);
|
||||||
|
if (missingCanonical.length > 0) {
|
||||||
if (newResults.length > 0) {
|
return {
|
||||||
await createEventResultsBulk(
|
error: `Some season participants are not linked to canonical participants: ${missingCanonical.map((sp) => sp.name).join(", ")}`,
|
||||||
newResults.map((r) => ({
|
};
|
||||||
scoringEventId: params.eventId,
|
|
||||||
participantId: r.participantId,
|
|
||||||
placement: r.placement,
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const row of incoming) {
|
||||||
|
const sp = spRows.find((s) => s.id === row.participantId);
|
||||||
|
if (!sp?.participantId) continue;
|
||||||
|
await upsertTournamentResult({
|
||||||
|
tournamentId: scoringEvent.tournamentId,
|
||||||
|
participantId: sp.participantId,
|
||||||
|
placement: row.placement,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const syncReport = await syncTournamentResults(scoringEvent.tournamentId);
|
||||||
return {
|
return {
|
||||||
success: `${newResults.length} result${newResults.length === 1 ? "" : "s"} saved${incoming.length - newResults.length > 0 ? ` (${incoming.length - newResults.length} duplicate${incoming.length - newResults.length === 1 ? "" : "s"} skipped)` : ""}.`,
|
success: `${incoming.length} result${incoming.length === 1 ? "" : "s"} saved canonically. Synced to ${syncReport.windowsSynced} window${syncReport.windowsSynced === 1 ? "" : "s"}${syncReport.windowsFailed > 0 ? ` (${syncReport.windowsFailed} failed)` : ""}.`,
|
||||||
|
syncReport,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error saving batch results:", error);
|
logger.error("Error saving batch results:", error);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue