fix: normalize reused tournament code lifecycle

This commit is contained in:
dahyeon committed 2026-07-13 04:52:29 +00:00
1 parent 6b1c450752
commit 489d040d8f
3 files changed
+188 -21

No files matched your search

@@ -2042,14 +2042,18 @@ function buildAssignments(
continue;
}
const tournamentCodeId = opaqueString(code._id);
const riotMatchId =
code.state === 'used' || code.state === 'voided'
? code.riotMatchId
: undefined;
const gameFact = facts.find(
(fact): fact is MigrationGameAcceptedFact =>
fact.type === 'game-accepted' &&
code.riotMatchId !== undefined &&
riotMatchId !== undefined &&
((fact.evidence.type === 'riot' &&
fact.evidence.riotMatchId === code.riotMatchId) ||
fact.evidence.riotMatchId === riotMatchId) ||
(fact.evidence.type === 'legacy' &&
fact.evidence.legacyReference.endsWith(`:${code.riotMatchId}`))),
fact.evidence.legacyReference.endsWith(`:${riotMatchId}`))),
);
const resolution = resolveTournamentCodeAssignmentTarget({
input,
@@ -2081,9 +2085,9 @@ function buildAssignments(
continue;
}
if (
code.riotMatchId &&
riotMatchId &&
acceptedGame &&
!acceptedGameHasRiotIdentity(acceptedGame, code.riotMatchId)
!acceptedGameHasRiotIdentity(acceptedGame, riotMatchId)
) {
reasons.push(
issue(
@@ -2092,7 +2096,7 @@ function buildAssignments(
{
tournamentCodeId,
gameSlotId: slot.id,
riotMatchId: code.riotMatchId,
riotMatchId,
},
),
);
@@ -2117,7 +2121,7 @@ function buildAssignments(
tournamentCodeId,
state: code.state,
...timestamps,
...(code.riotMatchId ? { riotMatchId: code.riotMatchId } : {}),
...(riotMatchId ? { riotMatchId } : {}),
commandId: stableId(
competitionId,
'migration-code-command',
@@ -2176,10 +2180,17 @@ function tournamentCodeTimestamps(
const raw = {
createdAt: optionalDate(code.createdAt),
reservedAt: optionalDate(code.reservedAt),
usedAt: optionalDate(code.usedAt),
releasedAt: optionalDate(code.releasedAt),
voidedAt: optionalDate(code.voidedAt),
updatedAt: optionalDate(code.updatedAt),
usedAt:
code.state === 'used' || code.state === 'voided'
? optionalDate(code.usedAt)
: undefined,
releasedAt:
code.state === 'released' ? optionalDate(code.releasedAt) : undefined,
voidedAt: code.state === 'voided' ? optionalDate(code.voidedAt) : undefined,
updatedAt:
code.state === 'voided' && code.voidedAt == null
? optionalDate(code.updatedAt)
: undefined,
};
if (Object.values(raw).some((value) => value === null)) {
reasons.push(
@@ -2192,20 +2203,29 @@ function tournamentCodeTimestamps(
return null;
}
// TournamentCode pool documents can be restored and reserved again while
// terminal timestamps from a previous binding remain on the document. A
// Competition assignment represents only the current binding, so preserve
// the lifecycle fields that are reachable for its current state.
const reservedAt =
raw.reservedAt ??
raw.createdAt ??
raw.usedAt ??
raw.releasedAt ??
raw.voidedAt ??
(code.state === 'used' || code.state === 'voided'
? raw.usedAt
: undefined) ??
(code.state === 'released' ? raw.releasedAt : undefined) ??
(code.state === 'voided' ? raw.voidedAt : undefined) ??
sourceFallbackDate(input);
const usedAt = raw.usedAt ?? undefined;
const releasedAt = raw.releasedAt ?? undefined;
const usedAt =
code.state === 'used' || code.state === 'voided'
? (raw.usedAt ?? undefined)
: undefined;
const releasedAt =
code.state === 'released' ? (raw.releasedAt ?? undefined) : undefined;
const voidedAt =
raw.voidedAt ??
(code.state === 'voided'
? (raw.updatedAt ?? releasedAt ?? usedAt ?? reservedAt)
: undefined);
code.state === 'voided'
? (raw.voidedAt ?? raw.updatedAt ?? usedAt ?? reservedAt)
: undefined;
const ordered = [reservedAt, usedAt, releasedAt, voidedAt].filter(
(value): value is string => value !== undefined,
);
@@ -2214,7 +2234,14 @@ function tournamentCodeTimestamps(
issue(
'INVALID_TOURNAMENT_CODE_TIMELINE',
'TournamentCode lifecycle timestamps are not chronological.',
{ tournamentCodeId },
{
tournamentCodeId,
state: code.state,
reservedAt,
...(usedAt ? { usedAt } : {}),
...(releasedAt ? { releasedAt } : {}),
...(voidedAt ? { voidedAt } : {}),
},
),
);
return null;