fix: make competition reversals replayable

This commit is contained in:
dahyeon committed 2026-07-12 19:47:34 +00:00
1 parent 4d6fa2625e
commit ce07bfa0c8
6 files changed
+169 -13

No files matched your search

@@ -508,13 +508,57 @@ function assertNoSeriesActivity(
seriesId: string,
facts: CompetitionFact[],
): void {
const hasActivity = facts.some(
(fact) =>
(fact.type === 'game-accepted' || fact.type === 'series-adjudicated') &&
fact.seriesId === seriesId,
const acceptedSeriesById = new Map(
facts
.filter((fact): fact is GameAcceptedFact => fact.type === 'game-accepted')
.map((fact) => [fact.id, fact.seriesId]),
);
const adjudicationSeriesById = new Map(
facts
.filter(
(fact): fact is SeriesAdjudicatedFact =>
fact.type === 'series-adjudicated',
)
.map((fact) => [fact.id, fact.seriesId]),
);
const activeGames = new Set<string>();
const activeAdjudications = new Set<string>();
for (const fact of facts) {
switch (fact.type) {
case 'game-accepted':
if (fact.seriesId === seriesId) activeGames.add(fact.id);
break;
case 'game-voided':
if (acceptedSeriesById.get(fact.gameAcceptedFactId) === seriesId) {
assertCompetitionInvariant(
activeGames.delete(fact.gameAcceptedFactId),
'FACT_ALREADY_REVERSED',
`Game fact ${fact.gameAcceptedFactId} is already voided`,
{ factId: fact.id, referencedFactId: fact.gameAcceptedFactId },
);
}
break;
case 'series-adjudicated':
if (fact.seriesId === seriesId) activeAdjudications.add(fact.id);
break;
case 'adjudication-reversed':
if (
adjudicationSeriesById.get(fact.adjudicationFactId) === seriesId
) {
assertCompetitionInvariant(
activeAdjudications.delete(fact.adjudicationFactId),
'FACT_ALREADY_REVERSED',
`Adjudication fact ${fact.adjudicationFactId} is already reversed`,
{ factId: fact.id, referencedFactId: fact.adjudicationFactId },
);
}
break;
}
}
assertCompetitionInvariant(
!hasActivity,
activeGames.size === 0 && activeAdjudications.size === 0,
'SERIES_PARTICIPANTS_INVALID',
`Series ${seriesId} cannot contain facts before both participants are active`,
{ seriesId },