diff --git a/apps/api/src/modules/global/database/migrations/20260627-tournament-code-unique-indexes.spec.ts b/apps/api/src/modules/global/database/migrations/20260627-tournament-code-unique-indexes.spec.ts new file mode 100644 index 00000000..4d2ba885 --- /dev/null +++ b/apps/api/src/modules/global/database/migrations/20260627-tournament-code-unique-indexes.spec.ts @@ -0,0 +1,572 @@ +import { migrateTournamentCodeUniqueIndexes } from './20260627-tournament-code-unique-indexes'; + +type Doc = Record & { _id: string }; +type FakeIndex = { + commitQuorum?: number | string; + key: Record; + maxTimeMS?: number; + name: string; + partialFilterExpression?: Record; + unique?: boolean; +}; +type Projection = Record; + +function createDb(params: { + codes?: Doc[]; + indexes?: FakeIndex[]; + matches?: Doc[]; + missingTournamentCodeNamespace?: boolean; +}) { + const collections = { + match: params.matches ?? [], + tournamentCode: params.codes ?? [], + }; + const indexes = [...(params.indexes ?? [])]; + const collectionNames: string[] = []; + let tournamentCodeNamespaceMissing = + params.missingTournamentCodeNamespace ?? false; + + return { + collectionNames, + collections, + indexes, + db: { + collection(name: string) { + collectionNames.push(name); + const docs = + name === 'tournamentCode' + ? collections.tournamentCode + : name === 'match' + ? collections.match + : []; + + return { + find( + filter: Record = {}, + options?: { projection?: Projection }, + ) { + return { + limit() { + return { + async *[Symbol.asyncIterator]() { + for (const doc of docs) { + if (!matchesFilter(doc, filter)) continue; + yield applyProjection(doc, options?.projection); + } + }, + }; + }, + }; + }, + async updateMany( + filter: Record, + update: { + $pull?: Record; + $set?: Record; + $unset?: Record; + }, + ) { + let matchedCount = 0; + let modifiedCount = 0; + + for (const doc of docs) { + if (!matchesFilter(doc, filter)) continue; + matchedCount += 1; + const before = JSON.stringify(doc); + applyUpdate(doc, update); + if (JSON.stringify(doc) !== before) modifiedCount += 1; + } + + return { matchedCount, modifiedCount }; + }, + async indexes() { + if (name === 'tournamentCode' && tournamentCodeNamespaceMissing) { + const error = new Error( + 'ns does not exist: lolympus.tournamentCode', + ) as Error & { code: number; codeName: string }; + error.code = 26; + error.codeName = 'NamespaceNotFound'; + throw error; + } + return indexes.map((index) => ({ ...index })); + }, + async dropIndex(name: string) { + const index = indexes.findIndex((item) => item.name === name); + if (index >= 0) indexes.splice(index, 1); + }, + async createIndex( + key: Record, + options: { + name: string; + partialFilterExpression?: Record; + unique?: boolean; + }, + ) { + if (name === 'tournamentCode') { + tournamentCodeNamespaceMissing = false; + } + indexes.push({ key, ...options }); + return options.name; + }, + }; + }, + }, + }; +} + +function matchesFilter(doc: Doc, filter: Record): boolean { + const clauses = filter.$or; + if (Array.isArray(clauses)) { + return clauses.some((clause) => matchesFilter(doc, clause)); + } + + return Object.entries(filter).every(([key, value]) => { + if (key === '$or') return true; + return matchesValue(readPath(doc, key), value); + }); +} + +function matchesValue(actual: unknown, expectedValue: unknown): boolean { + if (isRecord(expectedValue)) { + if ('$exists' in expectedValue) { + const exists = actual !== undefined; + if (exists !== expectedValue.$exists) return false; + } + if ('$in' in expectedValue) { + const expected = (expectedValue.$in as unknown[]).map(String); + if (Array.isArray(actual)) { + if (!actual.some((item) => expected.includes(String(item)))) { + return false; + } + } else if (!expected.includes(String(actual))) { + return false; + } + } + if ('$ne' in expectedValue) { + if (JSON.stringify(actual) === JSON.stringify(expectedValue.$ne)) { + return false; + } + } + if ('$type' in expectedValue) { + if (expectedValue.$type === 'string' && typeof actual !== 'string') { + return false; + } + } + return true; + } + + return actual === expectedValue; +} + +function applyUpdate( + doc: Doc, + update: { + $pull?: Record; + $set?: Record; + $unset?: Record; + }, +) { + Object.assign(doc, update.$set ?? {}); + + for (const key of Object.keys(update.$unset ?? {})) { + delete doc[key]; + } + + for (const [key, pull] of Object.entries(update.$pull ?? {})) { + const current = doc[key]; + if (!Array.isArray(current)) continue; + if (!isRecord(pull) || !('$in' in pull)) continue; + const pulled = (pull.$in as unknown[]).map(String); + doc[key] = current.filter((item) => !pulled.includes(String(item))); + } +} + +function applyProjection(doc: Doc, projection?: Projection): Doc { + if (!projection) return doc; + const projected: Doc = { _id: doc._id }; + for (const [key, value] of Object.entries(projection)) { + if (value !== 1 || key === '_id') continue; + const actual = readPath(doc, key); + if (actual !== undefined) projected[key] = actual; + } + return projected; +} + +function readPath(doc: Record, path: string): unknown { + const parts = path.split('.'); + let current: unknown = doc; + for (const part of parts) { + if (Array.isArray(current) && /^\d+$/.test(part)) { + current = current[Number(part)]; + continue; + } + if (!isRecord(current)) return undefined; + current = current[part]; + } + return current; +} + +function isRecord(value: unknown): value is Record { + return Boolean(value && typeof value === 'object' && !Array.isArray(value)); +} + +describe('migrateTournamentCodeUniqueIndexes', () => { + it('dry-runs duplicate cleanup without mutating documents or indexes', async () => { + const { db, collectionNames, collections, indexes } = createDb({ + codes: [ + { + _id: 'code-1', + code: 'A', + matchId: 'match-1', + gameNumber: 1, + reservedAt: new Date('2026-06-01T00:00:00.000Z'), + state: 'reserved', + }, + { + _id: 'code-2', + code: 'B', + matchId: 'match-1', + gameNumber: 1, + reservedAt: new Date('2026-06-02T00:00:00.000Z'), + state: 'reserved', + }, + { + _id: 'code-3', + code: 'C', + riotMatchId: 'KR_1', + state: 'released', + }, + { + _id: 'code-4', + code: 'D', + riotMatchId: 'KR_1', + state: 'used', + }, + { + _id: 'code-5', + code: 'E', + riotMatchId: ' ', + state: 'reserved', + }, + ], + indexes: [{ key: { riotMatchId: 1 }, name: 'riotMatchId_1' }], + matches: [{ _id: 'match-1', tournamentCodeIds: ['code-1', 'code-2'] }], + }); + + const beforeCodes = JSON.stringify(collections.tournamentCode); + const beforeIndexes = JSON.stringify(indexes); + const progressPhases: string[] = []; + const result = await migrateTournamentCodeUniqueIndexes(db, { + dryRun: true, + onProgress: (progress) => progressPhases.push(progress.phase), + }); + + expect(result.stats).toMatchObject({ + blankRiotMatchIdsToUnset: 1, + blankRiotMatchIdsUnset: 0, + duplicateCodesReleased: 0, + duplicateCodesToRelease: 2, + duplicateGameNumberGroups: 1, + duplicateRiotMatchGroups: 1, + indexesCreated: 0, + indexesDropped: 0, + scannedCodes: 5, + scannedMatches: 1, + }); + expect(result.sampleDuplicateGroups).toHaveLength(2); + expect(progressPhases).toEqual( + expect.arrayContaining(['scanMatches', 'scanTournamentCodes']), + ); + expect(collectionNames).toContain('tournamentCode'); + expect(collectionNames).not.toContain('tournament_code'); + expect(JSON.stringify(collections.tournamentCode)).toBe(beforeCodes); + expect(JSON.stringify(indexes)).toBe(beforeIndexes); + }); + + it('releases duplicate losers, pulls them from matches, and replaces old indexes', async () => { + const now = new Date('2026-06-27T00:00:00.000Z'); + const { db, collections, indexes } = createDb({ + codes: [ + { + _id: 'code-1', + code: 'A', + matchId: 'match-1', + gameNumber: 1, + reservedAt: new Date('2026-06-01T00:00:00.000Z'), + state: 'reserved', + }, + { + _id: 'code-2', + code: 'B', + matchId: 'match-1', + gameNumber: 1, + reservedAt: new Date('2026-06-02T00:00:00.000Z'), + state: 'reserved', + }, + { + _id: 'code-3', + code: 'C', + riotMatchId: 'KR_1', + state: 'used', + }, + { + _id: 'code-4', + code: 'D', + riotMatchId: 'KR_1', + state: 'released', + }, + { + _id: 'code-5', + code: 'E', + riotMatchId: '', + state: 'reserved', + }, + ], + indexes: [ + { key: { matchId: 1, gameNumber: 1 }, name: 'matchId_1_gameNumber_1' }, + { + key: { matchId: 1, bracketMatchId: 1 }, + name: 'matchId_1_bracketMatchId_1', + }, + { key: { riotMatchId: 1 }, name: 'riotMatchId_1' }, + ], + matches: [{ _id: 'match-1', tournamentCodeIds: ['code-1', 'code-4'] }], + }); + + const result = await migrateTournamentCodeUniqueIndexes(db, { + dryRun: false, + now, + }); + + expect(result.stats).toMatchObject({ + blankRiotMatchIdsToUnset: 1, + blankRiotMatchIdsUnset: 1, + duplicateCodesReleased: 2, + duplicateCodesToRelease: 2, + indexesCreated: 3, + indexesDropped: 3, + matchesUpdated: 1, + }); + expect(collections.tournamentCode.find((code) => code._id === 'code-2')).toMatchObject({ + code: 'B', + releasedAt: now, + state: 'released', + }); + expect(collections.tournamentCode.find((code) => code._id === 'code-2')).not.toHaveProperty( + 'matchId', + ); + expect(collections.tournamentCode.find((code) => code._id === 'code-4')).not.toHaveProperty( + 'riotMatchId', + ); + expect(collections.tournamentCode.find((code) => code._id === 'code-5')).not.toHaveProperty( + 'riotMatchId', + ); + expect(collections.match[0].tournamentCodeIds).toEqual(['code-1']); + expect(indexes).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + key: { matchId: 1, gameNumber: 1 }, + name: 'matchId_1_gameNumber_1', + partialFilterExpression: expect.objectContaining({ + state: { $in: ['reserved', 'used'] }, + }), + unique: true, + }), + expect.objectContaining({ + key: { matchId: 1, bracketMatchId: 1 }, + name: 'matchId_1_bracketMatchId_1', + unique: true, + }), + expect.objectContaining({ + key: { riotMatchId: 1 }, + name: 'riotMatchId_1', + partialFilterExpression: { riotMatchId: { $type: 'string' } }, + unique: true, + }), + ]), + ); + }); + + it('creates indexes when the tournament code collection does not exist yet', async () => { + const { db, indexes } = createDb({ + missingTournamentCodeNamespace: true, + }); + const progress: string[] = []; + + const result = await migrateTournamentCodeUniqueIndexes(db, { + dryRun: false, + indexCommitQuorum: 1, + indexMaxTimeMS: 60_000, + onProgress: (event) => + progress.push(`${event.action ?? ''}:${event.indexName ?? ''}`), + }); + + expect(result.stats).toMatchObject({ + duplicateCodesReleased: 0, + indexesAlreadyCorrect: 0, + indexesCreated: 3, + indexesDropped: 0, + scannedCodes: 0, + scannedMatches: 0, + }); + expect(indexes).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + commitQuorum: 1, + key: { matchId: 1, gameNumber: 1 }, + maxTimeMS: 60_000, + name: 'matchId_1_gameNumber_1', + unique: true, + }), + expect.objectContaining({ + key: { matchId: 1, bracketMatchId: 1 }, + name: 'matchId_1_bracketMatchId_1', + unique: true, + }), + expect.objectContaining({ + key: { riotMatchId: 1 }, + name: 'riotMatchId_1', + unique: true, + }), + ]), + ); + expect(progress).toEqual( + expect.arrayContaining([ + 'create:matchId_1_gameNumber_1', + 'created:matchId_1_gameNumber_1', + ]), + ); + }); + + it('refuses to release used duplicate losers by default', async () => { + const { db, collections } = createDb({ + codes: [ + { + _id: 'code-1', + matchId: 'match-1', + gameNumber: 1, + state: 'used', + usedAt: new Date('2026-06-01T00:00:00.000Z'), + }, + { + _id: 'code-2', + matchId: 'match-1', + gameNumber: 1, + state: 'used', + usedAt: new Date('2026-06-02T00:00:00.000Z'), + }, + ], + matches: [{ _id: 'match-1', tournamentCodeIds: ['code-1', 'code-2'] }], + }); + + await expect( + migrateTournamentCodeUniqueIndexes(db, { dryRun: false }), + ).rejects.toThrow('Refusing to release 1 used duplicate tournament codes'); + expect(collections.tournamentCode[0]).toMatchObject({ + matchId: 'match-1', + state: 'used', + }); + }); + + it('keeps the match-referenced slot code before state priority', async () => { + const { db, collections } = createDb({ + codes: [ + { + _id: 'code-1', + code: 'USED-NOT-REFERENCED', + matchId: 'match-1', + gameNumber: 1, + state: 'used', + usedAt: new Date('2026-06-02T00:00:00.000Z'), + }, + { + _id: 'code-2', + code: 'RESERVED-REFERENCED', + matchId: 'match-1', + gameNumber: 1, + state: 'reserved', + reservedAt: new Date('2026-06-01T00:00:00.000Z'), + }, + ], + matches: [{ _id: 'match-1', tournamentCodeIds: ['code-2'] }], + }); + + const beforeCodes = JSON.stringify(collections.tournamentCode); + const beforeMatches = JSON.stringify(collections.match); + + await expect( + migrateTournamentCodeUniqueIndexes(db, { dryRun: false }), + ).rejects.toThrow('Refusing to release 1 used duplicate tournament codes'); + expect(JSON.stringify(collections.tournamentCode)).toBe(beforeCodes); + expect(JSON.stringify(collections.match)).toBe(beforeMatches); + }); + + it('unsets blank riot match ids without treating them as duplicate releases', async () => { + const { db, collections } = createDb({ + codes: [ + { _id: 'code-1', riotMatchId: '', state: 'reserved' }, + { _id: 'code-2', riotMatchId: ' ', state: 'available' }, + ], + }); + + const result = await migrateTournamentCodeUniqueIndexes(db, { + dryRun: false, + createIndexes: false, + }); + + expect(result.stats).toMatchObject({ + blankRiotMatchIdsToUnset: 2, + blankRiotMatchIdsUnset: 2, + duplicateCodesReleased: 0, + duplicateCodesToRelease: 0, + duplicateRiotMatchGroups: 0, + }); + expect(collections.tournamentCode[0]).toMatchObject({ + _id: 'code-1', + state: 'reserved', + }); + expect(collections.tournamentCode[0]).not.toHaveProperty('riotMatchId'); + expect(collections.tournamentCode[1]).not.toHaveProperty('riotMatchId'); + }); + + it('allows used duplicate cleanup only when explicitly requested', async () => { + const now = new Date('2026-06-27T00:00:00.000Z'); + const { db, collections } = createDb({ + codes: [ + { + _id: 'code-1', + matchId: 'match-1', + gameNumber: 1, + state: 'used', + usedAt: new Date('2026-06-01T00:00:00.000Z'), + }, + { + _id: 'code-2', + matchId: 'match-1', + gameNumber: 1, + state: 'used', + usedAt: new Date('2026-06-02T00:00:00.000Z'), + }, + ], + matches: [{ _id: 'match-1', tournamentCodeIds: ['code-1', 'code-2'] }], + }); + + const result = await migrateTournamentCodeUniqueIndexes(db, { + allowUsedDuplicateRelease: true, + dryRun: false, + now, + }); + + expect(result.stats).toMatchObject({ + duplicateCodesReleased: 1, + unsafeUsedDuplicateCodes: 1, + unsafeUsedDuplicateGroups: 1, + }); + expect(collections.tournamentCode[0]).toMatchObject({ + releasedAt: now, + state: 'released', + }); + expect(collections.tournamentCode[0]).not.toHaveProperty('usedAt'); + expect(collections.match[0].tournamentCodeIds).toEqual(['code-2']); + }); +}); diff --git a/apps/api/src/modules/global/database/migrations/20260627-tournament-code-unique-indexes.ts b/apps/api/src/modules/global/database/migrations/20260627-tournament-code-unique-indexes.ts new file mode 100644 index 00000000..ecb37865 --- /dev/null +++ b/apps/api/src/modules/global/database/migrations/20260627-tournament-code-unique-indexes.ts @@ -0,0 +1,757 @@ +type TournamentCodeState = + | 'available' + | 'reserved' + | 'used' + | 'released' + | 'expired' + | 'voided' + | string; + +type AnyDoc = Record & { _id: unknown }; +type IndexKey = Record; +type PartialFilterExpression = Record; +type Projection = Record; + +interface MigrationCursor { + limit(limit: number): AsyncIterable; +} + +interface MigrationIndex { + key: IndexKey; + name: string; + partialFilterExpression?: PartialFilterExpression; + unique?: boolean; +} + +interface MigrationCollection { + find( + filter: Record, + options?: { projection?: Projection }, + ): MigrationCursor; + updateMany( + filter: Record, + update: { + $pull?: Record; + $set?: Record; + $unset?: Record; + }, + options?: { ordered?: boolean }, + ): Promise<{ matchedCount?: number; modifiedCount?: number } | unknown>; + indexes?(): Promise; + dropIndex?(name: string): Promise; + createIndex?( + key: IndexKey, + options: { + commitQuorum?: number | string; + maxTimeMS?: number; + name: string; + partialFilterExpression?: PartialFilterExpression; + unique?: boolean; + }, + ): Promise; +} + +interface MigrationDb { + collection(name: string): MigrationCollection; +} + +interface TournamentCodeDoc extends AnyDoc { + bracketMatchId?: unknown; + code?: unknown; + createdAt?: unknown; + gameNumber?: unknown; + matchId?: unknown; + reservedAt?: unknown; + riotMatchId?: unknown; + state?: TournamentCodeState; + updatedAt?: unknown; + uploadedAt?: unknown; + usedAt?: unknown; +} + +interface MatchDoc extends AnyDoc { + tournamentCodeIds?: unknown; +} + +interface DesiredIndex { + key: IndexKey; + options: { + name: string; + partialFilterExpression?: PartialFilterExpression; + unique?: boolean; + }; +} + +interface DuplicateGroup { + codeDocs: TournamentCodeDoc[]; + key: string; + kind: TournamentCodeDuplicateKind; +} + +interface ReleasePlanEntry { + code: TournamentCodeDoc; + reasons: string[]; +} + +export type TournamentCodeDuplicateKind = + | 'gameNumber' + | 'bracketMatchId' + | 'riotMatchId'; + +export interface TournamentCodeDuplicateSummary { + bracketMatchId?: string; + code?: string; + gameNumber?: string; + id: string; + matchId?: string; + riotMatchId?: string; + state?: string; +} + +export interface TournamentCodeDuplicateGroupSummary { + keeper: TournamentCodeDuplicateSummary; + key: string; + kind: TournamentCodeDuplicateKind; + losers: TournamentCodeDuplicateSummary[]; +} + +export interface TournamentCodeUniqueIndexMigrationOptions { + allowUsedDuplicateRelease?: boolean; + createIndexes?: boolean; + dryRun?: boolean; + duplicateSampleLimit?: number; + indexCommitQuorum?: number | string; + indexMaxTimeMS?: number; + now?: Date; + onProgress?: (progress: TournamentCodeUniqueIndexMigrationProgress) => void; + progressInterval?: number; +} + +export interface TournamentCodeUniqueIndexMigrationStats { + blankRiotMatchIdsToUnset: number; + blankRiotMatchIdsUnset: number; + duplicateBracketMatchGroups: number; + duplicateCodesReleased: number; + duplicateCodesToRelease: number; + duplicateGameNumberGroups: number; + duplicateRiotMatchGroups: number; + indexesAlreadyCorrect: number; + indexesCreated: number; + indexesDropped: number; + matchesUpdated: number; + scannedCodes: number; + scannedMatches: number; + unsafeUsedDuplicateCodes: number; + unsafeUsedDuplicateGroups: number; +} + +export interface TournamentCodeUniqueIndexMigrationResult { + sampleDuplicateGroups: TournamentCodeDuplicateGroupSummary[]; + stats: TournamentCodeUniqueIndexMigrationStats; +} + +export interface TournamentCodeUniqueIndexMigrationProgress { + action?: 'alreadyCorrect' | 'create' | 'created' | 'drop' | 'dropped' | 'list'; + phase: + | 'scanMatches' + | 'scanTournamentCodes' + | 'cleanupBlankRiotMatchIds' + | 'cleanupDuplicateCodes' + | 'ensureIndexes'; + scanned?: number; + indexName?: string; + total?: number; +} + +const ACTIVE_CODE_STATES = new Set(['reserved', 'used']); + +const MATCH_SCAN_FILTER = { 'tournamentCodeIds.0': { $exists: true } }; +const MATCH_SCAN_PROJECTION: Projection = { _id: 1, tournamentCodeIds: 1 }; +const TOURNAMENT_CODE_SCAN_FILTER = { + $or: [ + { state: { $in: ['reserved', 'used'] }, matchId: { $exists: true } }, + { riotMatchId: { $type: 'string' } }, + ], +}; +const TOURNAMENT_CODE_SCAN_PROJECTION: Projection = { + _id: 1, + bracketMatchId: 1, + code: 1, + createdAt: 1, + gameNumber: 1, + matchId: 1, + reservedAt: 1, + riotMatchId: 1, + state: 1, + updatedAt: 1, + uploadedAt: 1, + usedAt: 1, +}; + +const DESIRED_TOURNAMENT_CODE_INDEXES: DesiredIndex[] = [ + { + key: { matchId: 1, gameNumber: 1 }, + options: { + name: 'matchId_1_gameNumber_1', + unique: true, + partialFilterExpression: { + matchId: { $exists: true }, + gameNumber: { $exists: true }, + state: { $in: ['reserved', 'used'] }, + }, + }, + }, + { + key: { matchId: 1, bracketMatchId: 1 }, + options: { + name: 'matchId_1_bracketMatchId_1', + unique: true, + partialFilterExpression: { + matchId: { $exists: true }, + bracketMatchId: { $exists: true }, + state: { $in: ['reserved', 'used'] }, + }, + }, + }, + { + key: { riotMatchId: 1 }, + options: { + name: 'riotMatchId_1', + unique: true, + partialFilterExpression: { riotMatchId: { $type: 'string' } }, + }, + }, +]; + +export async function migrateTournamentCodeUniqueIndexes( + db: MigrationDb, + options: TournamentCodeUniqueIndexMigrationOptions = {}, +): Promise { + const dryRun = options.dryRun ?? true; + const now = options.now ?? new Date(); + const tournamentCodeCollection = db.collection('tournamentCode'); + const matchCollection = db.collection('match'); + const matchScan = await readReferencedTournamentCodeIds( + matchCollection, + options, + ); + const codeDocs = await readTournamentCodes(tournamentCodeCollection, options); + const groups = findDuplicateGroups(codeDocs, matchScan.referencedCodeIds); + const releasePlan = buildReleasePlan(groups, matchScan.referencedCodeIds); + const blankRiotMatchCodeIds = codeDocs + .filter(hasBlankRiotMatchId) + .map((code) => code._id); + const unsafePlanEntries = releasePlan.filter( + (entry) => entry.code.state === 'used', + ); + const unsafeGroupCount = groups.filter((group) => { + const keeper = chooseKeeper(group, matchScan.referencedCodeIds); + const keeperId = stringifyId(keeper._id); + return group.codeDocs.some( + (code) => stringifyId(code._id) !== keeperId && code.state === 'used', + ); + }).length; + + const stats: TournamentCodeUniqueIndexMigrationStats = { + blankRiotMatchIdsToUnset: blankRiotMatchCodeIds.length, + blankRiotMatchIdsUnset: 0, + duplicateBracketMatchGroups: groups.filter( + (group) => group.kind === 'bracketMatchId', + ).length, + duplicateCodesReleased: 0, + duplicateCodesToRelease: releasePlan.length, + duplicateGameNumberGroups: groups.filter( + (group) => group.kind === 'gameNumber', + ).length, + duplicateRiotMatchGroups: groups.filter( + (group) => group.kind === 'riotMatchId', + ).length, + indexesAlreadyCorrect: 0, + indexesCreated: 0, + indexesDropped: 0, + matchesUpdated: 0, + scannedCodes: codeDocs.length, + scannedMatches: matchScan.scannedMatches, + unsafeUsedDuplicateCodes: unsafePlanEntries.length, + unsafeUsedDuplicateGroups: unsafeGroupCount, + }; + + if ( + !dryRun && + unsafePlanEntries.length > 0 && + options.allowUsedDuplicateRelease !== true + ) { + throw new Error( + `Refusing to release ${unsafePlanEntries.length} used duplicate tournament codes. ` + + 'Review the duplicate groups manually or re-run with --allow-used-duplicate-release.', + ); + } + + if (!dryRun) { + if (blankRiotMatchCodeIds.length > 0) { + emitProgress(options, 'cleanupBlankRiotMatchIds', { + total: blankRiotMatchCodeIds.length, + }); + const result = await tournamentCodeCollection.updateMany( + { _id: { $in: blankRiotMatchCodeIds } }, + { $unset: { riotMatchId: '' } }, + ); + stats.blankRiotMatchIdsUnset = readModifiedCount(result); + } + + if (releasePlan.length > 0) { + const releaseIds = releasePlan.map((entry) => entry.code._id); + emitProgress(options, 'cleanupDuplicateCodes', { + total: releaseIds.length, + }); + const releaseResult = await tournamentCodeCollection.updateMany( + { _id: { $in: releaseIds } }, + { + $set: { releasedAt: now, state: 'released' }, + $unset: { + bracketMatchId: '', + gameNumber: '', + matchId: '', + reservedAt: '', + riotMatchId: '', + usedAt: '', + }, + }, + ); + stats.duplicateCodesReleased = readModifiedCount(releaseResult); + + const matchResult = await matchCollection.updateMany( + { tournamentCodeIds: { $in: releaseIds } }, + { $pull: { tournamentCodeIds: { $in: releaseIds } } }, + ); + stats.matchesUpdated = readModifiedCount(matchResult); + } + + if (options.createIndexes ?? true) { + emitProgress(options, 'ensureIndexes'); + const indexStats = await ensureTournamentCodeUniqueIndexes( + tournamentCodeCollection, + options, + ); + stats.indexesAlreadyCorrect = indexStats.indexesAlreadyCorrect; + stats.indexesCreated = indexStats.indexesCreated; + stats.indexesDropped = indexStats.indexesDropped; + } + } + + return { + sampleDuplicateGroups: summarizeGroups( + groups, + matchScan.referencedCodeIds, + options.duplicateSampleLimit ?? 20, + ), + stats, + }; +} + +async function readTournamentCodes( + collection: MigrationCollection, + options: TournamentCodeUniqueIndexMigrationOptions, +): Promise { + const docs: TournamentCodeDoc[] = []; + for await (const doc of collection + .find(TOURNAMENT_CODE_SCAN_FILTER, { + projection: TOURNAMENT_CODE_SCAN_PROJECTION, + }) + .limit(0)) { + docs.push(doc as TournamentCodeDoc); + emitScanProgress(options, 'scanTournamentCodes', docs.length); + } + emitProgress(options, 'scanTournamentCodes', { scanned: docs.length }); + return docs; +} + +async function readReferencedTournamentCodeIds( + collection: MigrationCollection, + options: TournamentCodeUniqueIndexMigrationOptions, +): Promise<{ referencedCodeIds: Set; scannedMatches: number }> { + const referencedCodeIds = new Set(); + let scannedMatches = 0; + + for await (const doc of collection + .find(MATCH_SCAN_FILTER, { projection: MATCH_SCAN_PROJECTION }) + .limit(0)) { + scannedMatches += 1; + emitScanProgress(options, 'scanMatches', scannedMatches); + const ids = (doc as MatchDoc).tournamentCodeIds; + if (!Array.isArray(ids)) continue; + for (const id of ids) { + const stringId = stringifyId(id); + if (stringId) referencedCodeIds.add(stringId); + } + } + + emitProgress(options, 'scanMatches', { scanned: scannedMatches }); + return { referencedCodeIds, scannedMatches }; +} + +function emitScanProgress( + options: TournamentCodeUniqueIndexMigrationOptions, + phase: TournamentCodeUniqueIndexMigrationProgress['phase'], + scanned: number, +) { + const interval = options.progressInterval ?? 5_000; + if (interval < 1 || scanned % interval !== 0) return; + emitProgress(options, phase, { scanned }); +} + +function emitProgress( + options: TournamentCodeUniqueIndexMigrationOptions, + phase: TournamentCodeUniqueIndexMigrationProgress['phase'], + values: Omit = {}, +) { + options.onProgress?.({ phase, ...values }); +} + +function findDuplicateGroups( + docs: TournamentCodeDoc[], + referencedCodeIds: Set, +): DuplicateGroup[] { + return [ + ...duplicateGroupsFor(docs, referencedCodeIds, 'gameNumber'), + ...duplicateGroupsFor(docs, referencedCodeIds, 'bracketMatchId'), + ...duplicateGroupsFor(docs, referencedCodeIds, 'riotMatchId'), + ]; +} + +function duplicateGroupsFor( + docs: TournamentCodeDoc[], + referencedCodeIds: Set, + kind: TournamentCodeDuplicateKind, +): DuplicateGroup[] { + const groups = new Map(); + + for (const doc of docs) { + const key = duplicateKey(doc, kind); + if (!key) continue; + const group = groups.get(key) ?? []; + group.push(doc); + groups.set(key, group); + } + + return [...groups.entries()] + .filter(([, groupDocs]) => groupDocs.length > 1) + .map(([key, groupDocs]) => ({ + codeDocs: sortCodesByKeeperPriority(groupDocs, referencedCodeIds, kind), + key, + kind, + })); +} + +function duplicateKey( + doc: TournamentCodeDoc, + kind: TournamentCodeDuplicateKind, +): string | null { + if (kind === 'riotMatchId') { + if (typeof doc.riotMatchId !== 'string') return null; + if (doc.riotMatchId.trim() === '') return null; + return `riotMatchId:${doc.riotMatchId}`; + } + + if (!ACTIVE_CODE_STATES.has(String(doc.state))) return null; + if (!hasField(doc, 'matchId')) return null; + + if (kind === 'gameNumber') { + if (!hasField(doc, 'gameNumber')) return null; + return `matchId:${stringifyKeyPart(doc.matchId)}:gameNumber:${stringifyKeyPart( + doc.gameNumber, + )}`; + } + + if (!hasField(doc, 'bracketMatchId')) return null; + return `matchId:${stringifyKeyPart( + doc.matchId, + )}:bracketMatchId:${stringifyKeyPart(doc.bracketMatchId)}`; +} + +function buildReleasePlan( + groups: DuplicateGroup[], + referencedCodeIds: Set, +): ReleasePlanEntry[] { + const byCodeId = new Map(); + + for (const group of groups) { + const keeper = chooseKeeper(group, referencedCodeIds); + const keeperId = stringifyId(keeper._id); + for (const code of group.codeDocs) { + const codeId = stringifyId(code._id); + if (!codeId || codeId === keeperId) continue; + const entry = byCodeId.get(codeId) ?? { code, reasons: [] }; + entry.reasons.push(`${group.kind}:${group.key}`); + byCodeId.set(codeId, entry); + } + } + + return [...byCodeId.values()]; +} + +function summarizeGroups( + groups: DuplicateGroup[], + referencedCodeIds: Set, + limit: number, +): TournamentCodeDuplicateGroupSummary[] { + return groups.slice(0, limit).map((group) => { + const keeper = chooseKeeper(group, referencedCodeIds); + const keeperId = stringifyId(keeper._id); + return { + keeper: summarizeCode(keeper), + key: group.key, + kind: group.kind, + losers: group.codeDocs + .filter((code) => stringifyId(code._id) !== keeperId) + .map(summarizeCode), + }; + }); +} + +function summarizeCode(doc: TournamentCodeDoc): TournamentCodeDuplicateSummary { + return { + bracketMatchId: + doc.bracketMatchId === undefined ? undefined : String(doc.bracketMatchId), + code: doc.code === undefined ? undefined : String(doc.code), + gameNumber: doc.gameNumber === undefined ? undefined : String(doc.gameNumber), + id: stringifyId(doc._id) ?? '', + matchId: doc.matchId === undefined ? undefined : stringifyId(doc.matchId), + riotMatchId: + doc.riotMatchId === undefined ? undefined : String(doc.riotMatchId), + state: doc.state === undefined ? undefined : String(doc.state), + }; +} + +function chooseKeeper( + group: DuplicateGroup, + referencedCodeIds: Set, +): TournamentCodeDoc { + return sortCodesByKeeperPriority( + group.codeDocs, + referencedCodeIds, + group.kind, + )[0]; +} + +function sortCodesByKeeperPriority( + docs: TournamentCodeDoc[], + referencedCodeIds: Set, + kind: TournamentCodeDuplicateKind, +): TournamentCodeDoc[] { + return [...docs].sort((left, right) => + compareCodePriority(left, right, referencedCodeIds, kind), + ); +} + +function compareCodePriority( + left: TournamentCodeDoc, + right: TournamentCodeDoc, + referencedCodeIds: Set, + kind: TournamentCodeDuplicateKind, +): number { + if (kind !== 'riotMatchId') { + const leftReferenced = referencedCodeIds.has(stringifyId(left._id) ?? ''); + const rightReferenced = referencedCodeIds.has(stringifyId(right._id) ?? ''); + if (leftReferenced !== rightReferenced) return leftReferenced ? -1 : 1; + } + + const leftStateRank = stateRank(left.state); + const rightStateRank = stateRank(right.state); + if (leftStateRank !== rightStateRank) return leftStateRank - rightStateRank; + + if (kind === 'riotMatchId') { + const leftReferenced = referencedCodeIds.has(stringifyId(left._id) ?? ''); + const rightReferenced = referencedCodeIds.has(stringifyId(right._id) ?? ''); + if (leftReferenced !== rightReferenced) return leftReferenced ? -1 : 1; + } + + const leftHasRiotMatch = hasNonBlankRiotMatchId(left); + const rightHasRiotMatch = hasNonBlankRiotMatchId(right); + if (leftHasRiotMatch !== rightHasRiotMatch) { + return leftHasRiotMatch ? -1 : 1; + } + + const leftDate = latestTimestamp(left); + const rightDate = latestTimestamp(right); + if (leftDate !== rightDate) return rightDate - leftDate; + + return (stringifyId(left._id) ?? '').localeCompare(stringifyId(right._id) ?? ''); +} + +function stateRank(state: unknown): number { + if (state === 'used') return 0; + if (state === 'reserved') return 1; + if (state === 'available') return 2; + if (state === 'released') return 3; + if (state === 'expired') return 4; + if (state === 'voided') return 5; + return 10; +} + +function latestTimestamp(doc: TournamentCodeDoc): number { + return Math.max( + toTimestamp(doc.usedAt), + toTimestamp(doc.reservedAt), + toTimestamp(doc.updatedAt), + toTimestamp(doc.createdAt), + toTimestamp(doc.uploadedAt), + 0, + ); +} + +function toTimestamp(value: unknown): number { + if (value instanceof Date) return value.getTime(); + if (typeof value === 'number') return value; + if (typeof value !== 'string') return 0; + const parsed = Date.parse(value); + return Number.isNaN(parsed) ? 0 : parsed; +} + +function hasBlankRiotMatchId(doc: TournamentCodeDoc): boolean { + return typeof doc.riotMatchId === 'string' && doc.riotMatchId.trim() === ''; +} + +function hasNonBlankRiotMatchId(doc: TournamentCodeDoc): boolean { + return typeof doc.riotMatchId === 'string' && doc.riotMatchId.trim() !== ''; +} + +async function ensureTournamentCodeUniqueIndexes( + collection: MigrationCollection, + options: TournamentCodeUniqueIndexMigrationOptions, +): Promise<{ + indexesAlreadyCorrect: number; + indexesCreated: number; + indexesDropped: number; +}> { + if (!collection.indexes || !collection.dropIndex || !collection.createIndex) { + throw new Error('Migration collection does not support index operations.'); + } + + emitProgress(options, 'ensureIndexes', { action: 'list' }); + let indexes = await listCollectionIndexes(collection); + let indexesAlreadyCorrect = 0; + let indexesCreated = 0; + let indexesDropped = 0; + + for (const desired of DESIRED_TOURNAMENT_CODE_INDEXES) { + const matchingIndexes = indexes.filter( + (index) => + index.name === desired.options.name || + sameObject(index.key, desired.key), + ); + const correctIndex = matchingIndexes.find((index) => + sameIndex(index, desired), + ); + + if (correctIndex) { + emitProgress(options, 'ensureIndexes', { + action: 'alreadyCorrect', + indexName: desired.options.name, + }); + indexesAlreadyCorrect += 1; + continue; + } + + for (const index of matchingIndexes) { + if (index.name === '_id_') continue; + emitProgress(options, 'ensureIndexes', { + action: 'drop', + indexName: index.name, + }); + await collection.dropIndex(index.name); + emitProgress(options, 'ensureIndexes', { + action: 'dropped', + indexName: index.name, + }); + indexesDropped += 1; + } + + emitProgress(options, 'ensureIndexes', { + action: 'create', + indexName: desired.options.name, + }); + await collection.createIndex(desired.key, { + ...desired.options, + ...(options.indexCommitQuorum !== undefined + ? { commitQuorum: options.indexCommitQuorum } + : {}), + ...(options.indexMaxTimeMS !== undefined + ? { maxTimeMS: options.indexMaxTimeMS } + : {}), + }); + emitProgress(options, 'ensureIndexes', { + action: 'created', + indexName: desired.options.name, + }); + indexesCreated += 1; + emitProgress(options, 'ensureIndexes', { action: 'list' }); + indexes = await listCollectionIndexes(collection); + } + + return { indexesAlreadyCorrect, indexesCreated, indexesDropped }; +} + +async function listCollectionIndexes( + collection: MigrationCollection, +): Promise { + if (!collection.indexes) { + throw new Error('Migration collection does not support index operations.'); + } + + try { + return await collection.indexes(); + } catch (error) { + if (isNamespaceNotFoundError(error)) return []; + throw error; + } +} + +function isNamespaceNotFoundError(error: unknown): boolean { + const candidate = error as { + code?: number; + codeName?: string; + message?: string; + }; + return Boolean( + candidate?.code === 26 || + candidate?.codeName === 'NamespaceNotFound' || + candidate?.message?.includes('ns does not exist'), + ); +} + +function sameIndex(index: MigrationIndex, desired: DesiredIndex): boolean { + return ( + sameObject(index.key, desired.key) && + index.unique === desired.options.unique && + sameObject( + index.partialFilterExpression ?? {}, + desired.options.partialFilterExpression ?? {}, + ) + ); +} + +function sameObject(left: unknown, right: unknown): boolean { + return JSON.stringify(left) === JSON.stringify(right); +} + +function readModifiedCount(result: unknown): number { + if (!result || typeof result !== 'object') return 0; + const modifiedCount = (result as { modifiedCount?: unknown }).modifiedCount; + return typeof modifiedCount === 'number' ? modifiedCount : 0; +} + +function hasField(doc: Record, key: string): boolean { + return Object.prototype.hasOwnProperty.call(doc, key); +} + +function stringifyKeyPart(value: unknown): string { + if (value === null) return ''; + if (value === undefined) return ''; + return stringifyId(value) ?? String(value); +} + +function stringifyId(value: unknown): string | undefined { + if (value === undefined || value === null) return undefined; + return typeof value === 'string' ? value : String(value); +} diff --git a/apps/api/src/modules/global/database/migrations/run-tournament-code-unique-indexes.ts b/apps/api/src/modules/global/database/migrations/run-tournament-code-unique-indexes.ts new file mode 100644 index 00000000..48754a6d --- /dev/null +++ b/apps/api/src/modules/global/database/migrations/run-tournament-code-unique-indexes.ts @@ -0,0 +1,318 @@ +import 'reflect-metadata'; +import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { NestFactory } from '@nestjs/core'; +import mongoose from 'mongoose'; +import { readFile } from 'node:fs/promises'; +import { resolve } from 'node:path'; +import { ConnectionService, resolveMongoDbName } from '../connection.service'; +import { SecretModule } from '../../secret/secret.module'; +import { + migrateTournamentCodeUniqueIndexes, + TournamentCodeDuplicateGroupSummary, + TournamentCodeUniqueIndexMigrationProgress, +} from './20260627-tournament-code-unique-indexes'; + +interface CliOptions { + allowUsedDuplicateRelease: boolean; + createIndexes: boolean; + dryRun: boolean; + duplicateSampleLimit: number; + indexCommitQuorum?: number | string; + indexMaxTimeMS?: number; + listDuplicates: boolean; + nodeEnv: string; +} + +@Module({ + imports: [ + ConfigModule.forRoot({ + isGlobal: true, + envFilePath: [ + resolve(__dirname, '../../../../../../project.env'), + resolve(process.cwd(), '../project.env'), + resolve(process.cwd(), 'apps/project.env'), + resolve(process.cwd(), 'project.env'), + ], + }), + SecretModule, + ], + providers: [ConnectionService], +}) +class MigrationModule {} + +async function loadMachineEnvFile() { + const homeDir = process.env.HOME ?? process.env.USERPROFILE; + if (!homeDir) return; + + const machineEnvFile = resolve(homeDir, 'source/global-env-vars.txt'); + + try { + const file = await readFile(machineEnvFile, 'utf8'); + for (const rawLine of file.split(/\r?\n/)) { + const line = rawLine.trim(); + if (!line || line.startsWith('#')) continue; + + const matched = line.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/); + if (!matched) continue; + + const [, key, rawValue] = matched; + process.env[key] = rawValue.replace(/^["']|["']$/g, ''); + } + } catch { + // Local machine env is optional for one-off migrations. + } +} + +function parseArgs(argv: string[]): CliOptions { + let allowUsedDuplicateRelease = false; + let createIndexes = true; + let dryRun = true; + let duplicateSampleLimit = 20; + let indexCommitQuorum: number | string | undefined; + let indexMaxTimeMS: number | undefined; + let listDuplicates = false; + let nodeEnv = process.env.NODE_ENV ?? 'dev'; + + for (let index = 0; index < argv.length; index += 1) { + const arg = argv[index]; + if (arg === '--allow-used-duplicate-release') { + allowUsedDuplicateRelease = true; + continue; + } + if (arg === '--apply') { + dryRun = false; + continue; + } + if (arg === '--dry-run') { + dryRun = true; + continue; + } + if (arg === '--duplicate-sample-limit') { + duplicateSampleLimit = parsePositiveInteger( + argv[index + 1], + '--duplicate-sample-limit', + ); + index += 1; + continue; + } + if (arg.startsWith('--duplicate-sample-limit=')) { + duplicateSampleLimit = parsePositiveInteger( + arg.slice('--duplicate-sample-limit='.length), + '--duplicate-sample-limit', + ); + continue; + } + if (arg === '--env') { + const value = argv[index + 1]; + if (!value) throw new Error('--env requires dev, staging, or prod.'); + nodeEnv = value; + index += 1; + continue; + } + if (arg.startsWith('--env=')) { + nodeEnv = arg.slice('--env='.length); + continue; + } + if (arg === '--index-commit-quorum') { + indexCommitQuorum = parseCommitQuorum( + argv[index + 1], + '--index-commit-quorum', + ); + index += 1; + continue; + } + if (arg.startsWith('--index-commit-quorum=')) { + indexCommitQuorum = parseCommitQuorum( + arg.slice('--index-commit-quorum='.length), + '--index-commit-quorum', + ); + continue; + } + if (arg === '--index-max-time-ms') { + indexMaxTimeMS = parsePositiveInteger( + argv[index + 1], + '--index-max-time-ms', + ); + index += 1; + continue; + } + if (arg.startsWith('--index-max-time-ms=')) { + indexMaxTimeMS = parsePositiveInteger( + arg.slice('--index-max-time-ms='.length), + '--index-max-time-ms', + ); + continue; + } + if (arg === '--list-duplicates') { + listDuplicates = true; + continue; + } + if (arg === '--skip-indexes') { + createIndexes = false; + continue; + } + if (arg === '--help' || arg === '-h') { + printHelp(); + process.exit(0); + } + throw new Error(`Unknown argument: ${arg}`); + } + + if (!['dev', 'staging', 'prod'].includes(nodeEnv)) { + throw new Error('--env must be dev, staging, or prod.'); + } + + return { + allowUsedDuplicateRelease, + createIndexes, + dryRun, + duplicateSampleLimit, + indexCommitQuorum, + indexMaxTimeMS, + listDuplicates, + nodeEnv, + }; +} + +function parseCommitQuorum(value: string | undefined, label: string) { + if (!value) { + throw new Error(`${label} requires a positive integer, majority, or votingMembers.`); + } + if (value === 'majority' || value === 'votingMembers') return value; + return parsePositiveInteger(value, label); +} + +function parsePositiveInteger(value: string | undefined, label: string) { + const parsed = Number(value); + if (!Number.isInteger(parsed) || parsed < 1) { + throw new Error(`${label} requires a positive integer.`); + } + return parsed; +} + +function printHelp() { + console.log(`Usage: + pnpm --filter api exec ts-node src/modules/global/database/migrations/run-tournament-code-unique-indexes.ts [--dry-run] + pnpm --filter api exec ts-node src/modules/global/database/migrations/run-tournament-code-unique-indexes.ts --env staging --apply + +Options: + --dry-run Print affected counts without writing. Default. + --apply Release duplicate losers, pull them from matches, and create unique indexes. + --env dev | staging | prod. Default: NODE_ENV or dev. + --list-duplicates + Print duplicate tournamentCode group samples. + --duplicate-sample-limit + Max duplicate groups to print. Default: 20. + --index-commit-quorum + Override MongoDB index build commitQuorum. Use 1 only after checking replica health. + --index-max-time-ms + Abort an individual index command after n milliseconds. + --skip-indexes Apply document cleanup without creating indexes. + --allow-used-duplicate-release + Allow releasing duplicate codes whose state is used. Default refuses this. +`); +} + +function printDuplicateGroups(groups: TournamentCodeDuplicateGroupSummary[]) { + if (groups.length === 0) { + console.log('No duplicate tournamentCode groups found.'); + return; + } + + console.log('Duplicate tournamentCode groups:'); + console.table( + groups.map((group) => ({ + keeperCode: group.keeper.code ?? '', + keeperId: group.keeper.id, + keeperState: group.keeper.state ?? '', + key: group.key, + kind: group.kind, + loserCodes: group.losers.map((loser) => loser.code ?? '').join(', '), + loserIds: group.losers.map((loser) => loser.id).join(', '), + loserStates: group.losers + .map((loser) => loser.state ?? '') + .join(', '), + })), + ); +} + +function printProgress(progress: TournamentCodeUniqueIndexMigrationProgress) { + const count = + progress.scanned !== undefined + ? ` scanned=${progress.scanned}` + : progress.total !== undefined + ? ` total=${progress.total}` + : ''; + const index = progress.indexName ? ` index=${progress.indexName}` : ''; + const action = progress.action ? ` action=${progress.action}` : ''; + console.log( + `[tournament-code unique index migration] ${progress.phase}${action}${index}${count}`, + ); +} + +async function main() { + const options = parseArgs(process.argv.slice(2)); + process.env.NODE_ENV = options.nodeEnv; + process.env.SECRET_SOURCE = process.env.SECRET_SOURCE || 'infisical'; + await loadMachineEnvFile(); + + const app = await NestFactory.createApplicationContext(MigrationModule, { + logger: ['log', 'warn', 'error'], + }); + + try { + const connectionService = app.get(ConnectionService); + const connection = await connectionService.getConnection(); + if (!connection.db) { + throw new Error('MongoDB connection has no db handle.'); + } + + const dbName = resolveMongoDbName(options.nodeEnv); + console.log( + `[tournament-code unique index migration] mode=${ + options.dryRun ? 'dry-run' : 'apply' + } env=${options.nodeEnv} db=${dbName}`, + ); + + const result = await migrateTournamentCodeUniqueIndexes( + connection.db as Parameters[0], + { + allowUsedDuplicateRelease: options.allowUsedDuplicateRelease, + createIndexes: options.createIndexes, + dryRun: options.dryRun, + duplicateSampleLimit: options.duplicateSampleLimit, + indexCommitQuorum: options.indexCommitQuorum, + indexMaxTimeMS: options.indexMaxTimeMS, + onProgress: printProgress, + }, + ); + + console.table(result.stats); + if (options.listDuplicates) { + printDuplicateGroups(result.sampleDuplicateGroups); + } + if (result.stats.unsafeUsedDuplicateCodes > 0 && options.dryRun) { + console.log( + 'Apply will refuse used duplicate cleanup unless --allow-used-duplicate-release is provided.', + ); + } + if (options.dryRun) { + console.log( + 'No writes were made. Re-run with --apply to clean documents and create indexes.', + ); + } else if (options.createIndexes) { + console.log('Migration applied and indexes ensured.'); + } else { + console.log('Migration applied without index creation.'); + } + } finally { + await app.close(); + await mongoose.disconnect(); + } +} + +void main().catch((error: unknown) => { + console.error(error instanceof Error ? error.message : error); + process.exitCode = 1; +}); diff --git a/apps/api/src/modules/global/database/schema.ts b/apps/api/src/modules/global/database/schema.ts index ead05193..77405a87 100644 --- a/apps/api/src/modules/global/database/schema.ts +++ b/apps/api/src/modules/global/database/schema.ts @@ -1492,10 +1492,36 @@ export const TournamentCodeSchema = new Schema( TournamentCodeSchema.index({ code: 1 }, { unique: true }); TournamentCodeSchema.index({ state: 1, expiresAt: 1 }); -TournamentCodeSchema.index({ matchId: 1, gameNumber: 1 }); -TournamentCodeSchema.index({ matchId: 1, bracketMatchId: 1 }, { sparse: true }); +TournamentCodeSchema.index( + { matchId: 1, gameNumber: 1 }, + { + unique: true, + partialFilterExpression: { + matchId: { $exists: true }, + gameNumber: { $exists: true }, + state: { $in: ['reserved', 'used'] }, + }, + }, +); +TournamentCodeSchema.index( + { matchId: 1, bracketMatchId: 1 }, + { + unique: true, + partialFilterExpression: { + matchId: { $exists: true }, + bracketMatchId: { $exists: true }, + state: { $in: ['reserved', 'used'] }, + }, + }, +); TournamentCodeSchema.index({ state: 1, uploadedAt: -1 }); -TournamentCodeSchema.index({ riotMatchId: 1 }, { sparse: true }); +TournamentCodeSchema.index( + { riotMatchId: 1 }, + { + unique: true, + partialFilterExpression: { riotMatchId: { $type: 'string' } }, + }, +); const MatchParticipantSubSchema = new Schema( { diff --git a/apps/api/src/modules/match/match.service.spec.ts b/apps/api/src/modules/match/match.service.spec.ts index c4619ff1..6d67d28d 100644 --- a/apps/api/src/modules/match/match.service.spec.ts +++ b/apps/api/src/modules/match/match.service.spec.ts @@ -51,9 +51,13 @@ describe('MatchService', () => { }; } - function createService(match: Record) { + function createService( + match: Record, + updatedMatch: Record | null = match, + ) { const MatchModel = { findById: jest.fn().mockResolvedValue(match), + findOneAndUpdate: jest.fn().mockResolvedValue(updatedMatch), }; const connectionService = { getModel: jest.fn().mockImplementation(async (name: string) => { @@ -80,6 +84,7 @@ describe('MatchService', () => { communityStatsService as never, matchStatsService as never, ), + MatchModel, tournamentCodeService, matchStatsService, }; @@ -132,12 +137,16 @@ describe('MatchService', () => { function createServiceWithJoinModels(params: { user: Record | null; match: Record | null; + updatedMatch?: Record | null; }) { const UserModel = { findById: jest.fn().mockResolvedValue(params.user), }; const MatchModel = { findById: jest.fn().mockResolvedValue(params.match), + findOneAndUpdate: jest + .fn() + .mockResolvedValue(params.updatedMatch ?? params.match), }; const connectionService = { getModel: jest.fn().mockImplementation(async (name: string) => { @@ -282,25 +291,185 @@ describe('MatchService', () => { participants: [], save: jest.fn().mockResolvedValue(undefined), }; - const { service } = createServiceWithJoinModels({ user, match }); + const updatedMatch = { + ...match, + participants: [ + { + userId, + puuid: 'puuid-1', + gameName: 'Dahyeon', + tagLine: 'KR1', + profileIconId: 7, + preferredPositions: [{ position: 'MID', priority: 1 }], + excluded: false, + }, + ], + }; + const { service, MatchModel } = createServiceWithJoinModels({ + user, + match, + updatedMatch, + }); - await service.join({ + const joined = await service.join({ matchId, userId, }); - expect(match.participants).toEqual([ + expect(joined).toBe(updatedMatch); + expect(MatchModel.findOneAndUpdate).toHaveBeenCalledWith( expect.objectContaining({ - userId, - puuid: 'puuid-1', - gameName: 'Dahyeon', - tagLine: 'KR1', - profileIconId: 7, - preferredPositions: [{ position: 'MID', priority: 1 }], - excluded: false, + _id: matchId, + communityId, + status: 'recruiting', + 'participants.userId': { $ne: userId }, + $expr: expect.any(Object), }), - ]); - expect(match.save).toHaveBeenCalledTimes(1); + { + $push: { + participants: expect.objectContaining({ + userId, + puuid: 'puuid-1', + gameName: 'Dahyeon', + tagLine: 'KR1', + profileIconId: 7, + preferredPositions: [{ position: 'MID', priority: 1 }], + excluded: false, + }), + }, + }, + expect.objectContaining({ returnDocument: 'after', runValidators: true }), + ); + expect(match.participants).toEqual([]); + expect(match.save).not.toHaveBeenCalled(); + }); + + it('cancels a join with an atomic participant pull', async () => { + const communityId = new mongoose.Types.ObjectId(); + const userId = new mongoose.Types.ObjectId(); + const matchId = new mongoose.Types.ObjectId(); + const user = { + _id: userId, + communityId, + riotAccounts: [], + preferredPositions: [], + }; + const match = { + _id: matchId, + communityId, + status: 'recruiting', + participants: [{ userId }], + save: jest.fn(), + }; + const updatedMatch = { ...match, participants: [] }; + const { service, MatchModel } = createServiceWithJoinModels({ + user, + match, + updatedMatch, + }); + + await expect(service.cancelJoin({ matchId, userId })).resolves.toBe( + updatedMatch, + ); + expect(MatchModel.findOneAndUpdate).toHaveBeenCalledWith( + { + _id: matchId, + communityId, + status: 'recruiting', + 'participants.userId': userId, + }, + { $pull: { participants: { userId } } }, + expect.objectContaining({ returnDocument: 'after', runValidators: true }), + ); + expect(match.save).not.toHaveBeenCalled(); + }); + + it('removes a participant with an atomic participant pull', async () => { + const communityId = new mongoose.Types.ObjectId(); + const targetUserId = new mongoose.Types.ObjectId(); + const matchId = new mongoose.Types.ObjectId(); + const match = { + _id: matchId, + communityId, + status: 'recruiting', + participants: [{ userId: targetUserId }], + save: jest.fn(), + }; + const updatedMatch = { ...match, participants: [] }; + const { service, MatchModel } = createService(match, updatedMatch); + + await expect( + service.removeParticipant({ matchId, communityId, targetUserId }), + ).resolves.toBe(updatedMatch); + expect(MatchModel.findOneAndUpdate).toHaveBeenCalledWith( + { + _id: matchId, + communityId, + status: 'recruiting', + 'participants.userId': targetUserId, + }, + { $pull: { participants: { userId: targetUserId } } }, + expect.objectContaining({ returnDocument: 'after', runValidators: true }), + ); + expect(match.save).not.toHaveBeenCalled(); + }); + + it('updates participant positions with an array-filtered atomic update', async () => { + const communityId = new mongoose.Types.ObjectId(); + const targetUserId = new mongoose.Types.ObjectId(); + const matchId = new mongoose.Types.ObjectId(); + const match = { + _id: matchId, + communityId, + status: 'recruiting', + participants: [ + { + userId: targetUserId, + preferredPositions: [ + { position: 'TOP', priority: 1 }, + { position: 'JUNGLE', priority: 2 }, + { position: 'MID', priority: 3 }, + ], + }, + ], + save: jest.fn(), + }; + const updatedMatch = { ...match }; + const { service, MatchModel } = createService(match, updatedMatch); + + await expect( + service.updateParticipantPositions({ + matchId, + communityId, + targetUserId, + mainPosition: 'MID', + subPosition: 'SUPPORT', + }), + ).resolves.toBe(updatedMatch); + expect(MatchModel.findOneAndUpdate).toHaveBeenCalledWith( + { + _id: matchId, + communityId, + status: 'recruiting', + 'participants.userId': targetUserId, + }, + { + $set: { + 'participants.$[participant].preferredPositions': [ + { position: 'MID', priority: 1 }, + { position: 'SUPPORT', priority: 2 }, + { position: 'TOP', priority: 3 }, + { position: 'JUNGLE', priority: 4 }, + ], + }, + }, + expect.objectContaining({ + arrayFilters: [{ 'participant.userId': targetUserId }], + returnDocument: 'after', + runValidators: true, + }), + ); + expect(match.save).not.toHaveBeenCalled(); }); it('normalizes double-elimination match creation', async () => { diff --git a/apps/api/src/modules/match/match.service.ts b/apps/api/src/modules/match/match.service.ts index 12442880..d139c641 100644 --- a/apps/api/src/modules/match/match.service.ts +++ b/apps/api/src/modules/match/match.service.ts @@ -494,29 +494,11 @@ export class MatchService { const user = await UserModel.findById(params.userId); if (!user) throw new NotFoundException('User not found.'); - const match = await MatchModel.findById(params.matchId); - if (!match) throw new NotFoundException('Match not found.'); - if (!match.communityId.equals(user.communityId)) { - throw new BadRequestException( - 'User and match belong to different communities.', - ); - } - if (match.status !== 'recruiting') { - throw new BadRequestException('Match is not recruiting.'); - } - if (match.participants.length >= match.maxParticipants) { - throw new BadRequestException('Match is full.'); - } - const alreadyIn = match.participants.some((p) => p.userId.equals(user._id)); - if (alreadyIn) { - throw new BadRequestException('User already joined this match.'); - } - const selectedRiot = params.riotPuuid ? user.riotAccounts.find((a) => a.puuid === params.riotPuuid) : user.riotAccounts.find((a) => a.isPrimary); const selectedTier = this.selectParticipantTier(selectedRiot?.tiers ?? []); - match.participants.push({ + const participant = { userId: user._id, puuid: selectedRiot?.puuid, gameName: selectedRiot?.gameName, @@ -529,9 +511,22 @@ export class MatchService { ), joinedAt: new Date(), excluded: false, - }); - await match.save(); - return match; + }; + + const updatedMatch = await MatchModel.findOneAndUpdate( + { + _id: params.matchId, + communityId: user.communityId, + status: 'recruiting', + 'participants.userId': { $ne: user._id }, + $expr: { $lt: [{ $size: '$participants' }, '$maxParticipants'] }, + }, + { $push: { participants: participant } }, + { returnDocument: 'after', runValidators: true }, + ); + if (updatedMatch) return updatedMatch; + + return await this.throwJoinFailure(MatchModel, params.matchId, user); } private normalizeMatchConfig & DoubleEliminationConfigInput>( @@ -729,52 +724,57 @@ export class MatchService { const user = await UserModel.findById(params.userId); if (!user) throw new NotFoundException('User not found.'); - const match = await MatchModel.findById(params.matchId); - if (!match) throw new NotFoundException('Match not found.'); - if (!match.communityId.equals(user.communityId)) { - throw new BadRequestException( - 'User and match belong to different communities.', - ); - } - if (match.status !== 'recruiting') { - throw new BadRequestException('Match is not recruiting.'); - } - - const participantIndex = match.participants.findIndex((participant) => - participant.userId.equals(user._id), + const updatedMatch = await MatchModel.findOneAndUpdate( + { + _id: params.matchId, + communityId: user.communityId, + status: 'recruiting', + 'participants.userId': user._id, + }, + { $pull: { participants: { userId: user._id } } }, + { returnDocument: 'after', runValidators: true }, ); - if (participantIndex === -1) { - throw new BadRequestException('User has not joined this match.'); - } + if (updatedMatch) return updatedMatch; - match.participants.splice(participantIndex, 1); - await match.save(); - return match; + return await this.throwCancelJoinFailure(MatchModel, params.matchId, user); } // 관리자 전용 — 특정 참가자를 강제로 제외한다. public async removeParticipant( params: RemoveParticipantParams, ): Promise { + const MatchModel = await this.connectionService.getModel('match'); + const updatedMatch = await MatchModel.findOneAndUpdate( + { + _id: params.matchId, + communityId: params.communityId, + status: 'recruiting', + 'participants.userId': params.targetUserId, + }, + { $pull: { participants: { userId: params.targetUserId } } }, + { returnDocument: 'after', runValidators: true }, + ); + if (updatedMatch) return updatedMatch; + const match = await this.loadRecruitingMatch( params.matchId, params.communityId, ); - const participantIndex = match.participants.findIndex((participant) => - participant.userId.equals(params.targetUserId), - ); - if (participantIndex === -1) { + if ( + !match.participants.some((participant) => + this.sameObjectId(participant.userId, params.targetUserId), + ) + ) { throw new NotFoundException('Participant not found.'); } - match.participants.splice(participantIndex, 1); - await match.save(); - return match; + throw new BadRequestException('Participant could not be removed.'); } // 관리자 전용 — 참가자의 주/부 포지션(선호 우선순위 1·2)을 교체한다. public async updateParticipantPositions( params: UpdateParticipantPositionsParams, ): Promise { + const MatchModel = await this.connectionService.getModel('match'); const match = await this.loadRecruitingMatch( params.matchId, params.communityId, @@ -785,14 +785,36 @@ export class MatchService { if (!participant) { throw new NotFoundException('Participant not found.'); } - participant.preferredPositions = this.reorderPreferredPositions( + const preferredPositions = this.reorderPreferredPositions( params.mainPosition, params.subPosition, participant.preferredPositions, ); - match.markModified('participants'); - await match.save(); - return match; + + const updatedMatch = await MatchModel.findOneAndUpdate( + { + _id: params.matchId, + communityId: params.communityId, + status: 'recruiting', + 'participants.userId': params.targetUserId, + }, + { + $set: { + 'participants.$[participant].preferredPositions': preferredPositions, + }, + }, + { + arrayFilters: [{ 'participant.userId': params.targetUserId }], + returnDocument: 'after', + runValidators: true, + }, + ); + if (!updatedMatch) { + throw new BadRequestException( + 'Participant positions could not be updated.', + ); + } + return updatedMatch; } private async loadRecruitingMatch( @@ -811,6 +833,69 @@ export class MatchService { return match; } + private async throwJoinFailure( + MatchModel: mongoose.Model, + matchId: mongoose.Types.ObjectId, + user: { _id: mongoose.Types.ObjectId; communityId: mongoose.Types.ObjectId }, + ): Promise { + const match = await MatchModel.findById(matchId); + if (!match) throw new NotFoundException('Match not found.'); + if (!this.sameObjectId(match.communityId, user.communityId)) { + throw new BadRequestException( + 'User and match belong to different communities.', + ); + } + if (match.status !== 'recruiting') { + throw new BadRequestException('Match is not recruiting.'); + } + if ( + match.participants.some((participant) => + this.sameObjectId(participant.userId, user._id), + ) + ) { + throw new BadRequestException('User already joined this match.'); + } + if (match.participants.length >= match.maxParticipants) { + throw new BadRequestException('Match is full.'); + } + throw new BadRequestException('Unable to join this match.'); + } + + private async throwCancelJoinFailure( + MatchModel: mongoose.Model, + matchId: mongoose.Types.ObjectId, + user: { _id: mongoose.Types.ObjectId; communityId: mongoose.Types.ObjectId }, + ): Promise { + const match = await MatchModel.findById(matchId); + if (!match) throw new NotFoundException('Match not found.'); + if (!this.sameObjectId(match.communityId, user.communityId)) { + throw new BadRequestException( + 'User and match belong to different communities.', + ); + } + if (match.status !== 'recruiting') { + throw new BadRequestException('Match is not recruiting.'); + } + if ( + !match.participants.some((participant) => + this.sameObjectId(participant.userId, user._id), + ) + ) { + throw new BadRequestException('User has not joined this match.'); + } + throw new BadRequestException('Unable to cancel match join.'); + } + + private sameObjectId( + left: mongoose.Types.ObjectId | { toString(): string }, + right: mongoose.Types.ObjectId | { toString(): string }, + ): boolean { + if (left instanceof mongoose.Types.ObjectId) { + return left.equals(right.toString()); + } + return left.toString() === right.toString(); + } + // 주/부 포지션을 priority 1·2로 올리고 기존 나머지 선호는 뒤로 보존한다. private reorderPreferredPositions( mainPosition: Position, diff --git a/apps/api/src/modules/tournament-code/tournament-code.module.ts b/apps/api/src/modules/tournament-code/tournament-code.module.ts index 4fe9838f..a908133d 100644 --- a/apps/api/src/modules/tournament-code/tournament-code.module.ts +++ b/apps/api/src/modules/tournament-code/tournament-code.module.ts @@ -1,4 +1,5 @@ import { Module } from '@nestjs/common'; +import { AuctionRedisModule } from '../auction/redis/auction-redis.module'; import { TournamentResultModule } from '../tournament-result/tournament-result.module'; import { TournamentCodeController } from './tournament-code.controller'; import { TournamentCodeAutoProvisionService } from './tournament-code-auto-provision.service'; @@ -8,7 +9,7 @@ import { TournamentCodeService } from './tournament-code.service'; import { TournamentCodeVendorService } from './tournament-code-vendor.service'; @Module({ - imports: [TournamentResultModule], + imports: [TournamentResultModule, AuctionRedisModule], controllers: [TournamentCodeController, TournamentCodeReplacementController], providers: [ TournamentCodeService, diff --git a/apps/api/src/modules/tournament-code/tournament-code.service.spec.ts b/apps/api/src/modules/tournament-code/tournament-code.service.spec.ts index 839612a4..d7560052 100644 --- a/apps/api/src/modules/tournament-code/tournament-code.service.spec.ts +++ b/apps/api/src/modules/tournament-code/tournament-code.service.spec.ts @@ -20,9 +20,26 @@ type TournamentCodeServiceInternals = TournamentCodeService & { attachPendingMatches(communityId?: mongoose.Types.ObjectId): Promise; }; +function createAuctionRedisService() { + return { + acquireTokenLock: jest.fn().mockResolvedValue(true), + releaseTokenLock: jest.fn().mockResolvedValue(true), + }; +} + +function createSession() { + return { + startTransaction: jest.fn(), + commitTransaction: jest.fn().mockResolvedValue(undefined), + abortTransaction: jest.fn().mockResolvedValue(undefined), + endSession: jest.fn().mockResolvedValue(undefined), + }; +} + function createService() { return new TournamentCodeService( {} as never, + createAuctionRedisService() as never, ) as unknown as TournamentCodeServiceInternals; } @@ -297,6 +314,7 @@ describe('TournamentCodeService round-robin scheduling', () => { }; const service = new TournamentCodeService( connectionService as never, + createAuctionRedisService() as never, ) as unknown as TournamentCodeServiceInternals; const slots = await service.buildSlots( @@ -356,6 +374,40 @@ describe('TournamentCodeService round-robin scheduling', () => { }); describe('TournamentCodeService bulk upload', () => { + it('defines unique slot and riot match indexes for multi-instance safety', () => { + const indexes = TournamentCodeSchema.indexes(); + + expect(indexes).toContainEqual([ + { matchId: 1, gameNumber: 1 }, + expect.objectContaining({ + unique: true, + partialFilterExpression: { + matchId: { $exists: true }, + gameNumber: { $exists: true }, + state: { $in: ['reserved', 'used'] }, + }, + }), + ]); + expect(indexes).toContainEqual([ + { matchId: 1, bracketMatchId: 1 }, + expect.objectContaining({ + unique: true, + partialFilterExpression: { + matchId: { $exists: true }, + bracketMatchId: { $exists: true }, + state: { $in: ['reserved', 'used'] }, + }, + }), + ]); + expect(indexes).toContainEqual([ + { riotMatchId: 1 }, + expect.objectContaining({ + unique: true, + partialFilterExpression: { riotMatchId: { $type: 'string' } }, + }), + ]); + }); + it('preserves tournament code casing at the schema layer', async () => { const connection = mongoose.createConnection(); try { @@ -393,6 +445,7 @@ describe('TournamentCodeService bulk upload', () => { }; const service = new TournamentCodeService( connectionService as never, + createAuctionRedisService() as never, ) as unknown as TournamentCodeServiceInternals; service.attachPendingMatches = jest.fn().mockResolvedValue(0); @@ -426,7 +479,10 @@ describe('TournamentCodeService pool stats', () => { const connectionService = { getModel: jest.fn().mockResolvedValue({ countDocuments }), }; - const service = new TournamentCodeService(connectionService as never); + const service = new TournamentCodeService( + connectionService as never, + createAuctionRedisService() as never, + ); await expect(service.countUsableAvailable()).resolves.toBe(12); expect(countDocuments).toHaveBeenCalledWith({ @@ -477,7 +533,9 @@ describe('TournamentCodeService pending match attachment', () => { exec: jest.fn().mockResolvedValue({ _id: codeId }), }), }; + const session = createSession(); const connectionService = { + createSession: jest.fn().mockResolvedValue(session), getModel: jest.fn().mockImplementation((modelName: string) => { if (modelName === 'match') return Promise.resolve(MatchModel); if (modelName === 'tournamentCode') { @@ -486,7 +544,11 @@ describe('TournamentCodeService pending match attachment', () => { throw new Error(`Unexpected model: ${modelName}`); }), }; - const service = new TournamentCodeService(connectionService as never); + const auctionRedisService = createAuctionRedisService(); + const service = new TournamentCodeService( + connectionService as never, + auctionRedisService as never, + ); const matches = await service.attachPendingMatchDocuments(); @@ -494,6 +556,17 @@ describe('TournamentCodeService pending match attachment', () => { expect(MatchModel.findById).toHaveBeenCalledTimes(1); expect(MatchModel.findById).toHaveBeenCalledWith(validMatch._id); expect(TournamentCodeModel.findOneAndUpdate).toHaveBeenCalledTimes(1); + expect(auctionRedisService.acquireTokenLock).toHaveBeenCalledWith( + `tournament-code:match:${validMatch._id.toString()}:lock`, + expect.any(String), + expect.any(Number), + ); + expect(session.startTransaction).toHaveBeenCalledTimes(1); + expect(session.commitTransaction).toHaveBeenCalledTimes(1); + expect(auctionRedisService.releaseTokenLock).toHaveBeenCalledWith( + `tournament-code:match:${validMatch._id.toString()}:lock`, + expect.any(String), + ); expect(validMatch.tournamentCodeIds.map((id) => id.toString())).toEqual([ codeId.toString(), ]); @@ -569,8 +642,11 @@ describe('TournamentCodeService code replacement', () => { const TournamentCodeModel = { findOne: jest.fn().mockReturnValue(queryResult(currentCode)), findOneAndUpdate: jest.fn().mockReturnValue(queryResult(replacementCode)), + updateOne: jest.fn().mockReturnValue(queryResult({ modifiedCount: 1 })), }; + const session = createSession(); const connectionService = { + createSession: jest.fn().mockResolvedValue(session), getModel: jest.fn().mockImplementation((modelName: string) => { if (modelName === 'match') return Promise.resolve(MatchModel); if (modelName === 'tournamentCode') { @@ -579,8 +655,10 @@ describe('TournamentCodeService code replacement', () => { throw new Error(`Unexpected model: ${modelName}`); }), }; + const auctionRedisService = createAuctionRedisService(); const service = new TournamentCodeService( connectionService as never, + auctionRedisService as never, ) as unknown as TournamentCodeServiceInternals; return { @@ -591,6 +669,8 @@ describe('TournamentCodeService code replacement', () => { replacementCode, MatchModel, TournamentCodeModel, + auctionRedisService, + session, service, }; } @@ -604,6 +684,8 @@ describe('TournamentCodeService code replacement', () => { replacementCode, MatchModel, TournamentCodeModel, + auctionRedisService, + session, service, } = createReplacementFixture(); @@ -642,10 +724,30 @@ describe('TournamentCodeService code replacement', () => { }), expect.objectContaining({ returnDocument: 'after' }), ); - expect(currentCode?.state).toBe('released'); - expect(currentCode?.matchId).toBeUndefined(); - expect(currentCode?.gameNumber).toBeUndefined(); - expect(currentCode?.save).toHaveBeenCalled(); + expect(auctionRedisService.acquireTokenLock).toHaveBeenCalledWith( + `tournament-code:match:${matchId.toString()}:lock`, + expect.any(String), + expect.any(Number), + ); + expect(session.startTransaction).toHaveBeenCalledTimes(1); + expect(session.commitTransaction).toHaveBeenCalledTimes(1); + expect(auctionRedisService.releaseTokenLock).toHaveBeenCalledWith( + `tournament-code:match:${matchId.toString()}:lock`, + expect.any(String), + ); + expect(TournamentCodeModel.updateOne).toHaveBeenCalledWith( + { _id: currentCode?._id }, + expect.objectContaining({ + $set: expect.objectContaining({ state: 'released' }), + $unset: expect.objectContaining({ + matchId: 1, + gameNumber: 1, + bracketMatchId: 1, + reservedAt: 1, + }), + }), + ); + expect(currentCode?.save).not.toHaveBeenCalled(); expect(match.tournamentCodeIds.map((id) => id.toString())).toEqual([ replacementCode?._id.toString(), ]); @@ -683,8 +785,8 @@ describe('TournamentCodeService code replacement', () => { expect(fixture.TournamentCodeModel.findOneAndUpdate).not.toHaveBeenCalled(); }); - it('leaves the current code untouched when no replacement code is available', async () => { - const { match, communityId, matchId, currentCode, service } = + it('aborts the transaction when no replacement code is available', async () => { + const { match, communityId, matchId, currentCode, session, service } = createReplacementFixture({ replacementCode: null, }); @@ -694,5 +796,6 @@ describe('TournamentCodeService code replacement', () => { ).rejects.toThrow('No available tournament code'); expect(currentCode?.save).not.toHaveBeenCalled(); expect(match.save).not.toHaveBeenCalled(); + expect(session.abortTransaction).toHaveBeenCalledTimes(1); }); }); diff --git a/apps/api/src/modules/tournament-code/tournament-code.service.ts b/apps/api/src/modules/tournament-code/tournament-code.service.ts index c6ce3252..0ae2e95e 100644 --- a/apps/api/src/modules/tournament-code/tournament-code.service.ts +++ b/apps/api/src/modules/tournament-code/tournament-code.service.ts @@ -1,13 +1,17 @@ import { BadRequestException, + ConflictException, Injectable, + InternalServerErrorException, NotFoundException, } from '@nestjs/common'; +import { randomUUID } from 'crypto'; import mongoose from 'mongoose'; import { buildDoubleEliminationBracket, findNextReadyDoubleEliminationBracketMatches, } from '../double-elimination/double-elimination.util'; +import { AuctionRedisService } from '../auction/redis/auction-redis.service'; import { ConnectionService } from '../global/database/connection.service'; import type { DoubleEliminationBracketMatch, @@ -36,10 +40,15 @@ import { const DEFAULT_REGION = 'kr'; const DEFAULT_SPECTATOR_TYPE: TournamentSpectatorType = 'ALL'; +const TOURNAMENT_CODE_MATCH_LOCK_TTL_MS = 30_000; +const TOURNAMENT_CODE_TRANSACTION_ATTEMPTS = 3; @Injectable() export class TournamentCodeService { - constructor(private readonly connectionService: ConnectionService) {} + constructor( + private readonly connectionService: ConnectionService, + private readonly auctionRedisService: AuctionRedisService, + ) {} public async bulkUpload(params: { communityId?: mongoose.Types.ObjectId; @@ -231,21 +240,34 @@ export class TournamentCodeService { public async reserveForMatch( matchOrId: MatchDocument | mongoose.Types.ObjectId, + ): Promise { + const matchId = this.resolveMatchId(matchOrId); + return this.withLockedMatchTransaction(matchId, (session) => + this.reserveForMatchInTransaction(matchId, session), + ); + } + + private async reserveForMatchInTransaction( + matchId: mongoose.Types.ObjectId, + session: mongoose.ClientSession, ): Promise { const MatchModel = await this.connectionService.getModel('match'); const TournamentCodeModel = await this.connectionService.getModel('tournamentCode'); - const match = - matchOrId instanceof mongoose.Types.ObjectId - ? await MatchModel.findById(matchOrId) - : await MatchModel.findById(matchOrId._id); + const match = await this.execQuery( + this.withQuerySession(MatchModel.findById(matchId), session), + ); if (!match) throw new NotFoundException('Match not found.'); match.gameCount = match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT; match.tournamentCodeIds = match.tournamentCodeIds ?? []; this.ensureGameSchedule(match); if ((match.gameMode ?? 'pair') === 'double-elimination') { - return this.reserveForDoubleEliminationMatch(match, TournamentCodeModel); + return this.reserveForDoubleEliminationMatch( + match, + TournamentCodeModel, + session, + ); } const shouldSaveMetadata = this.isMatchModified(match, 'gameCount') || @@ -281,7 +303,7 @@ export class TournamentCodeService { }, $unset: { releasedAt: 1 }, }, - { sort: { expiresAt: 1 }, returnDocument: 'after' }, + { sort: { expiresAt: 1 }, returnDocument: 'after', session }, ).exec(); if (!code) break; reservedIds.push(code._id); @@ -291,7 +313,7 @@ export class TournamentCodeService { match.tournamentCodeIds.push(...reservedIds); } if (reservedIds.length > 0 || shouldSaveMetadata) { - await match.save(); + await match.save({ session }); } return match; } @@ -301,13 +323,31 @@ export class TournamentCodeService { matchId: mongoose.Types.ObjectId; gameNumber: number; }): Promise { + return this.withLockedMatchTransaction(params.matchId, (session) => + this.replaceForMatchGameInTransaction(params, session), + ); + } + + private async replaceForMatchGameInTransaction( + params: { + communityId: mongoose.Types.ObjectId; + matchId: mongoose.Types.ObjectId; + gameNumber: number; + }, + session: mongoose.ClientSession, + ): Promise { const MatchModel = await this.connectionService.getModel('match'); const TournamentCodeModel = await this.connectionService.getModel('tournamentCode'); - const match = await MatchModel.findOne({ - _id: params.matchId, - communityId: params.communityId, - }).exec(); + const match = await this.execQuery( + this.withQuerySession( + MatchModel.findOne({ + _id: params.matchId, + communityId: params.communityId, + }), + session, + ), + ); if (!match) throw new NotFoundException('Match not found.'); if (match.status !== 'team-built' && match.status !== 'in-progress') { throw new BadRequestException( @@ -348,13 +388,18 @@ export class TournamentCodeService { ); } - const currentCode = await TournamentCodeModel.findOne({ - _id: { $in: match.tournamentCodeIds }, - matchId: match._id, - ...(doubleEliminationBracketMatch - ? { bracketMatchId: doubleEliminationBracketMatch.bracketMatchId } - : { gameNumber: params.gameNumber }), - }).exec(); + const currentCode = await this.execQuery( + this.withQuerySession( + TournamentCodeModel.findOne({ + _id: { $in: match.tournamentCodeIds }, + matchId: match._id, + ...(doubleEliminationBracketMatch + ? { bracketMatchId: doubleEliminationBracketMatch.bracketMatchId } + : { gameNumber: params.gameNumber }), + }), + session, + ), + ); if (!currentCode) { throw new NotFoundException('Assigned tournament code not found.'); } @@ -367,6 +412,25 @@ export class TournamentCodeService { const expiresAfter = new Date( Date.now() + TOURNAMENT_CODE_EXPIRY_MARGIN_MS, ); + await this.execQuery( + this.withQuerySession( + TournamentCodeModel.updateOne( + { _id: currentCode._id }, + { + $set: { state: 'released', releasedAt: new Date() }, + $unset: { + matchId: 1, + gameNumber: 1, + bracketMatchId: 1, + reservedAt: 1, + riotMatchId: 1, + }, + }, + ), + session, + ), + ); + const replacement = await TournamentCodeModel.findOneAndUpdate( { state: 'available', @@ -389,7 +453,7 @@ export class TournamentCodeService { }, $unset: { releasedAt: 1 }, }, - { sort: { expiresAt: 1 }, returnDocument: 'after' }, + { sort: { expiresAt: 1 }, returnDocument: 'after', session }, ).exec(); if (!replacement) { throw new BadRequestException( @@ -397,20 +461,12 @@ export class TournamentCodeService { ); } - currentCode.state = 'released'; - currentCode.matchId = undefined; - currentCode.gameNumber = undefined; - currentCode.bracketMatchId = undefined; - currentCode.reservedAt = undefined; - currentCode.releasedAt = new Date(); - currentCode.riotMatchId = undefined; match.tournamentCodeIds = this.replaceTournamentCodeId( match.tournamentCodeIds, currentCode._id, replacement._id, ); - await currentCode.save(); - await match.save(); + await match.save({ session }); return { gameNumber: params.gameNumber, @@ -431,32 +487,46 @@ export class TournamentCodeService { public async releaseFromMatch( matchOrId: MatchDocument | mongoose.Types.ObjectId, + ): Promise { + const matchId = this.resolveMatchId(matchOrId); + await this.withLockedMatchTransaction(matchId, (session) => + this.releaseFromMatchInTransaction(matchId, session), + ); + } + + private async releaseFromMatchInTransaction( + matchId: mongoose.Types.ObjectId, + session: mongoose.ClientSession, ): Promise { const MatchModel = await this.connectionService.getModel('match'); const TournamentCodeModel = await this.connectionService.getModel('tournamentCode'); - const match = - matchOrId instanceof mongoose.Types.ObjectId - ? await MatchModel.findById(matchOrId) - : await MatchModel.findById(matchOrId._id); + const match = await this.execQuery( + this.withQuerySession(MatchModel.findById(matchId), session), + ); if (!match) return; if (match.tournamentCodeIds.length === 0) return; - await TournamentCodeModel.updateMany( - { _id: { $in: match.tournamentCodeIds }, state: 'reserved' }, - { - $set: { state: 'released', releasedAt: new Date() }, - $unset: { - matchId: 1, - gameNumber: 1, - bracketMatchId: 1, - reservedAt: 1, - }, - }, - ).exec(); + await this.execQuery( + this.withQuerySession( + TournamentCodeModel.updateMany( + { _id: { $in: match.tournamentCodeIds }, state: 'reserved' }, + { + $set: { state: 'released', releasedAt: new Date() }, + $unset: { + matchId: 1, + gameNumber: 1, + bracketMatchId: 1, + reservedAt: 1, + }, + }, + ), + session, + ), + ); match.tournamentCodeIds = []; - await match.save(); + await match.save({ session }); } public async markExpired(): Promise { @@ -589,18 +659,128 @@ export class TournamentCodeService { return next; } + private resolveMatchId( + matchOrId: MatchDocument | mongoose.Types.ObjectId, + ): mongoose.Types.ObjectId { + return matchOrId instanceof mongoose.Types.ObjectId + ? matchOrId + : matchOrId._id; + } + + private async withLockedMatchTransaction( + matchId: mongoose.Types.ObjectId, + operation: (session: mongoose.ClientSession) => Promise, + ): Promise { + const lockKey = this.matchCodeLockKey(matchId); + const token = randomUUID(); + const acquired = await this.auctionRedisService.acquireTokenLock( + lockKey, + token, + TOURNAMENT_CODE_MATCH_LOCK_TTL_MS, + ); + if (!acquired) { + throw new ConflictException( + 'Tournament code operation is already running for this match.', + ); + } + + try { + return await this.runTransactionWithRetry(operation); + } finally { + await this.auctionRedisService.releaseTokenLock(lockKey, token); + } + } + + private async runTransactionWithRetry( + operation: (session: mongoose.ClientSession) => Promise, + ): Promise { + let lastError: unknown; + for ( + let attempt = 1; + attempt <= TOURNAMENT_CODE_TRANSACTION_ATTEMPTS; + attempt += 1 + ) { + const session = await this.connectionService.createSession(); + if (!session) { + throw new InternalServerErrorException('Failed to start DB session.'); + } + + try { + session.startTransaction(); + const result = await operation(session); + await session.commitTransaction(); + return result; + } catch (error) { + lastError = error; + await session.abortTransaction().catch(() => undefined); + if ( + attempt >= TOURNAMENT_CODE_TRANSACTION_ATTEMPTS || + !this.isRetryableTransactionError(error) + ) { + throw error; + } + } finally { + await session.endSession().catch(() => undefined); + } + } + + throw lastError; + } + + private isRetryableTransactionError(error: unknown): boolean { + const candidate = error as { + code?: number; + codeName?: string; + hasErrorLabel?: (label: string) => boolean; + }; + return Boolean( + candidate?.hasErrorLabel?.('TransientTransactionError') || + candidate?.hasErrorLabel?.('UnknownTransactionCommitResult') || + candidate?.code === 112 || + candidate?.codeName === 'WriteConflict', + ); + } + + private matchCodeLockKey(matchId: mongoose.Types.ObjectId): string { + return `tournament-code:match:${matchId.toString()}:lock`; + } + + private withQuerySession(query: T, session: mongoose.ClientSession): T { + const maybeQuery = query as { + session?: (session: mongoose.ClientSession) => T; + }; + if (maybeQuery && typeof maybeQuery.session === 'function') { + return maybeQuery.session(session); + } + return query; + } + + private async execQuery(query: unknown): Promise { + const maybeQuery = query as { exec?: () => Promise }; + if (maybeQuery && typeof maybeQuery.exec === 'function') { + return maybeQuery.exec(); + } + return query as Promise; + } + private async reserveForDoubleEliminationMatch( match: MatchDocument, TournamentCodeModel: mongoose.Model, + session: mongoose.ClientSession, ): Promise { const readyBracketMatches = findNextReadyDoubleEliminationBracketMatches( match.doubleElimination, ); const existingCodes = match.tournamentCodeIds.length > 0 - ? await TournamentCodeModel.find({ - _id: { $in: match.tournamentCodeIds }, - }).exec() + ? await this.execQuery( + this.withQuerySession( + TournamentCodeModel.find({ + _id: { $in: match.tournamentCodeIds }, + }), + session, + ), + ) : []; const byBracketMatchId = new Map( existingCodes @@ -636,7 +816,7 @@ export class TournamentCodeService { }, $unset: { releasedAt: 1 }, }, - { sort: { expiresAt: 1 }, returnDocument: 'after' }, + { sort: { expiresAt: 1 }, returnDocument: 'after', session }, ).exec(); if (!code) break; reservedIds.push(code._id); @@ -650,7 +830,7 @@ export class TournamentCodeService { reservedIds.length > 0 || this.isMatchModified(match, 'doubleElimination') ) { - await match.save(); + await match.save({ session }); } return match; }