diff --git a/src/server/services/round-engine.ts b/src/server/services/round-engine.ts index 9e063f6..348085d 100644 --- a/src/server/services/round-engine.ts +++ b/src/server/services/round-engine.ts @@ -235,6 +235,42 @@ export async function closeRound( // Expire pending intents await expireIntentsForRound(roundId, actorId) + // Auto-close any preceding active rounds (lower sortOrder, same competition) + const precedingActiveRounds = await tx.round.findMany({ + where: { + competitionId: round.competitionId, + sortOrder: { lt: round.sortOrder }, + status: 'ROUND_ACTIVE', + }, + orderBy: { sortOrder: 'asc' }, + }) + + for (const prev of precedingActiveRounds) { + await tx.round.update({ + where: { id: prev.id }, + data: { status: 'ROUND_CLOSED' }, + }) + await tx.decisionAuditLog.create({ + data: { + eventType: 'round.closed', + entityType: 'Round', + entityId: prev.id, + actorId, + detailsJson: { + roundName: prev.name, + roundType: prev.roundType, + previousStatus: 'ROUND_ACTIVE', + closedBy: 'cascade', + triggeringRoundId: roundId, + }, + snapshotJson: { + timestamp: new Date().toISOString(), + emittedBy: 'round-engine', + }, + }, + }) + } + await tx.decisionAuditLog.create({ data: { eventType: 'round.closed', @@ -245,6 +281,7 @@ export async function closeRound( roundName: round.name, roundType: round.roundType, previousStatus: 'ROUND_ACTIVE', + cascadeClosed: precedingActiveRounds.map((r: any) => r.name), }, snapshotJson: { timestamp: new Date().toISOString(),