From 79d2a46a0518560d1fb0f49049c6d07ae1b31115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EB=8B=A4=ED=98=84=20=28Dahyeon=20Park=29?= Date: Thu, 9 Apr 2026 01:08:00 +0900 Subject: [PATCH] feat(api): add database modules --- .../database/connection.service.spec.ts | 14 + .../global/database/connection.service.ts | 100 ++ .../global/database/database.module.ts | 9 + .../dev-seeds/backfill-match-stats.ts | 102 ++ .../dev-seeds/create-temp-finished-match.ts | 574 ++++++ .../20260502-auction-pass-history.ts | 100 ++ .../migrations/20260502-team-index.spec.ts | 160 ++ .../migrations/20260502-team-index.ts | 335 ++++ .../database/migrations/run-team-index.ts | 167 ++ .../api/src/modules/global/database/schema.ts | 1559 +++++++++++++++++ .../src/modules/global/database/types.d.ts | 5 + .../modules/global/garnet/garnet.constants.ts | 1 + .../modules/global/garnet/garnet.module.ts | 57 + .../modules/global/garnet/garnet.service.ts | 213 +++ apps/api/src/modules/global/global.module.ts | 13 + .../src/modules/global/nats/nats.constants.ts | 1 + .../src/modules/global/nats/nats.module.ts | 45 + .../src/modules/global/nats/nats.service.ts | 117 ++ .../modules/global/secret/secret.module.ts | 9 + .../modules/global/secret/secret.service.ts | 243 +++ garnet.ts | 389 ++++ 21 files changed, 4213 insertions(+) create mode 100644 apps/api/src/modules/global/database/connection.service.spec.ts create mode 100644 apps/api/src/modules/global/database/connection.service.ts create mode 100644 apps/api/src/modules/global/database/database.module.ts create mode 100644 apps/api/src/modules/global/database/dev-seeds/backfill-match-stats.ts create mode 100644 apps/api/src/modules/global/database/dev-seeds/create-temp-finished-match.ts create mode 100644 apps/api/src/modules/global/database/migrations/20260502-auction-pass-history.ts create mode 100644 apps/api/src/modules/global/database/migrations/20260502-team-index.spec.ts create mode 100644 apps/api/src/modules/global/database/migrations/20260502-team-index.ts create mode 100644 apps/api/src/modules/global/database/migrations/run-team-index.ts create mode 100644 apps/api/src/modules/global/database/schema.ts create mode 100644 apps/api/src/modules/global/database/types.d.ts create mode 100644 apps/api/src/modules/global/garnet/garnet.constants.ts create mode 100644 apps/api/src/modules/global/garnet/garnet.module.ts create mode 100644 apps/api/src/modules/global/garnet/garnet.service.ts create mode 100644 apps/api/src/modules/global/global.module.ts create mode 100644 apps/api/src/modules/global/nats/nats.constants.ts create mode 100644 apps/api/src/modules/global/nats/nats.module.ts create mode 100644 apps/api/src/modules/global/nats/nats.service.ts create mode 100644 apps/api/src/modules/global/secret/secret.module.ts create mode 100644 apps/api/src/modules/global/secret/secret.service.ts create mode 100644 garnet.ts diff --git a/apps/api/src/modules/global/database/connection.service.spec.ts b/apps/api/src/modules/global/database/connection.service.spec.ts new file mode 100644 index 00000000..ae75d66b --- /dev/null +++ b/apps/api/src/modules/global/database/connection.service.spec.ts @@ -0,0 +1,14 @@ +import { resolveMongoDbName } from './connection.service'; + +describe('resolveMongoDbName', () => { + it('uses production database only for prod', () => { + expect(resolveMongoDbName('prod')).toBe('lolympus'); + }); + + it('uses isolated databases for non-production environments', () => { + expect(resolveMongoDbName('dev')).toBe('lolympus-dev'); + expect(resolveMongoDbName('test')).toBe('lolympus-test'); + expect(resolveMongoDbName('')).toBe('lolympus-dev'); + expect(resolveMongoDbName('local')).toBe('lolympus-dev'); + }); +}); diff --git a/apps/api/src/modules/global/database/connection.service.ts b/apps/api/src/modules/global/database/connection.service.ts new file mode 100644 index 00000000..2b51a437 --- /dev/null +++ b/apps/api/src/modules/global/database/connection.service.ts @@ -0,0 +1,100 @@ +import { Injectable, Scope, OnModuleInit } from '@nestjs/common'; +import mongoose from 'mongoose'; +import { SecretService } from '../secret/secret.service'; +import { tournamentModelDefinitions, TournamentModelTypes } from './schema'; + +export function resolveMongoDbName(nodeEnv = process.env.NODE_ENV): string { + if (nodeEnv === 'prod') return 'lolympus'; + if (nodeEnv === 'test') return 'lolympus-test'; + return 'lolympus-dev'; +} + +@Injectable({ scope: Scope.DEFAULT }) +export class ConnectionService implements OnModuleInit { + private connection: mongoose.Connection | null = null; + constructor(private readonly secretService: SecretService) {} + + private tournamentModels: Map> | null = null; + + public async getConnection(): Promise { + if (this.connection !== null && this.connection.readyState === 1) { + return this.connection; + } + + const connectionString = await this.secretService.get( + 'MONGODB_CONNECTION_STRING', + ); + const dbName = resolveMongoDbName(); + const defaultConnection = await mongoose + .createConnection(connectionString, { + dbName, + minPoolSize: 10, + maxPoolSize: 20, + }) + .asPromise(); + this.connection = defaultConnection; + console.log(`Default Connection이 새로 생성됨. dbName=${dbName}`); + + return this.connection; + } + + public async getModel( + modelKey: K, + ): Promise> { + if (this.tournamentModels) + return this.tournamentModels.get(modelKey) as mongoose.Model< + TournamentModelTypes[K] + >; + + const models = new Map>(); + const connection = await this.getConnection(); + + tournamentModelDefinitions.forEach((modelDefinition) => { + // 이미 생성된 경우 컴파일 스킵. + if ( + Object.prototype.hasOwnProperty.call( + connection.models, + modelDefinition.name, + ) + ) { + models.set( + modelDefinition.name as K, + connection.models[modelDefinition.name], + ); + return; + } else { + models.set( + modelDefinition.name as K, + connection.model( + modelDefinition.name, + modelDefinition.schema, + modelDefinition.schemaName, + ), + ); + } + }); + + this.tournamentModels = models; + return models.get(modelKey) as mongoose.Model; + } + + public async createSession( + options?: mongoose.ClientSessionOptions, + ): Promise { + try { + const connection = await this.getConnection(); + const session = await connection.startSession(options); + return session as unknown as mongoose.ClientSession; + } catch (error) { + console.log(error); + } + } + + public async onModuleInit() { + await this.getConnection().catch(console.error); + } + + public async onModuleDestroy() { + this.connection?.close(); + } +} diff --git a/apps/api/src/modules/global/database/database.module.ts b/apps/api/src/modules/global/database/database.module.ts new file mode 100644 index 00000000..a4b5e918 --- /dev/null +++ b/apps/api/src/modules/global/database/database.module.ts @@ -0,0 +1,9 @@ +import { DynamicModule, Module } from '@nestjs/common'; +import { ConnectionService } from './connection.service'; + +@Module({ + imports: [], + providers: [ConnectionService], + exports: [ConnectionService], +}) +export class DatabaseModule {} diff --git a/apps/api/src/modules/global/database/dev-seeds/backfill-match-stats.ts b/apps/api/src/modules/global/database/dev-seeds/backfill-match-stats.ts new file mode 100644 index 00000000..f186f2fa --- /dev/null +++ b/apps/api/src/modules/global/database/dev-seeds/backfill-match-stats.ts @@ -0,0 +1,102 @@ +import { NestFactory } from '@nestjs/core'; +import mongoose from 'mongoose'; +import { AppModule } from '../../../../app.module'; +import { ConnectionService } from '../connection.service'; +import { MatchStatsService } from '../../../community-stats/match-stats.service'; + +interface Options { + communityId?: mongoose.Types.ObjectId; + dryRun: boolean; + force: boolean; +} + +async function main() { + const options = parseArgs(process.argv.slice(2)); + const app = await NestFactory.createApplicationContext(AppModule, { + logger: ['log', 'warn', 'error'], + }); + + try { + const connectionService = app.get(ConnectionService); + const matchStatsService = app.get(MatchStatsService); + const MatchModel = await connectionService.getModel('match'); + const MatchStatsModel = await connectionService.getModel('matchStats'); + + const filter: Record = { + status: { $in: ['finished', 'in-progress'] }, + 'result.games.0': { $exists: true }, + }; + if (options.communityId) filter.communityId = options.communityId; + + const matches = await MatchModel.find(filter) + .sort({ scheduledAt: -1 }) + .select({ _id: 1 }) + .exec(); + console.log(`match_stats backfill target=${matches.length}`); + + let processed = 0; + let skipped = 0; + let failed = 0; + + for (const match of matches) { + if (!options.force) { + const existing = await MatchStatsModel.exists({ matchId: match._id }); + if (existing) { + skipped += 1; + continue; + } + } + if (options.dryRun) { + processed += 1; + continue; + } + + try { + await matchStatsService.buildAndUpsert(match._id); + processed += 1; + } catch (error) { + failed += 1; + console.warn( + `failed ${match._id.toString()}: ${ + error instanceof Error ? error.message : error + }`, + ); + } + + if ((processed + skipped + failed) % 50 === 0) { + console.log( + `progress processed=${processed} skipped=${skipped} failed=${failed}`, + ); + } + } + + console.log( + `done processed=${processed} skipped=${skipped} failed=${failed}`, + ); + } finally { + await app.close(); + } +} + +function parseArgs(args: string[]): Options { + const options: Options = { dryRun: false, force: false }; + for (let index = 0; index < args.length; index += 1) { + const arg = args[index]; + if (arg === '--dry-run') options.dryRun = true; + if (arg === '--force') options.force = true; + if (arg === '--community-id') { + const value = args[index + 1]; + if (!value || !mongoose.isValidObjectId(value)) { + throw new Error('--community-id requires a valid ObjectId'); + } + options.communityId = new mongoose.Types.ObjectId(value); + index += 1; + } + } + return options; +} + +void main().catch((error) => { + console.error(error); + process.exit(1); +}); diff --git a/apps/api/src/modules/global/database/dev-seeds/create-temp-finished-match.ts b/apps/api/src/modules/global/database/dev-seeds/create-temp-finished-match.ts new file mode 100644 index 00000000..79f9dc54 --- /dev/null +++ b/apps/api/src/modules/global/database/dev-seeds/create-temp-finished-match.ts @@ -0,0 +1,574 @@ +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 type { + MatchGameResult, + MatchParticipant, + MatchSeriesResult, + MatchTeam, + Position, + PositionPreference, + TeamSide, + TierInfo, +} from '../schema'; +import { SecretModule } from '../../secret/secret.module'; + +const TEMP_JOB_ID = 'temp-finished-30-players-6-teams'; +const TEAM_COUNT = 6; +const TEAM_SIZE = 5; +const TOTAL_PLAYERS = TEAM_COUNT * TEAM_SIZE; +const GAME_COUNT_PER_SERIES_GROUP = 5; +const POSITIONS: Position[] = ['TOP', 'JGL', 'MID', 'ADC', 'SUP']; +const CHAMPION_IDS = [ + 266, 64, 103, 222, 412, 24, 421, 134, 145, 497, 58, 121, 777, 202, 111, 39, + 120, 7, 81, 89, 114, 104, 157, 51, 432, 92, 234, 61, 21, 53, +]; + +interface CliOptions { + communityId?: string; + dryRun: boolean; + nodeEnv: string; + replace: boolean; + scheduledAt: Date; + title: string; +} + +interface TempPlayer { + userId: mongoose.Types.ObjectId; + puuid: string; + gameName: string; + tagLine: string; + profileIconId: number; + tier: TierInfo; + preferredPositions: PositionPreference[]; +} + +interface CommunityLean { + _id: mongoose.Types.ObjectId; + name?: 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 TempSeedModule {} + +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 { + // 로컬 machine env 파일이 없으면 그냥 지나간다. + } +} + +function parseArgs(argv: string[]): CliOptions { + let communityId = + process.env.VITE_DEFAULT_COMMUNITY_ID ?? process.env.DEFAULT_COMMUNITY_ID; + let dryRun = false; + let nodeEnv = process.env.NODE_ENV ?? 'dev'; + let replace = false; + let scheduledAt = new Date(Date.now() - 24 * 60 * 60 * 1000); + let title = `결과 검증 임시 내전 30인 6팀 ${formatStamp(new Date())}`; + + for (let index = 0; index < argv.length; index += 1) { + const arg = argv[index]; + if (arg === '--community-id') { + const value = argv[index + 1]; + if (!value) throw new Error('--community-id requires an ObjectId.'); + communityId = value; + index += 1; + continue; + } + if (arg.startsWith('--community-id=')) { + communityId = arg.slice('--community-id='.length); + continue; + } + if (arg === '--dry-run') { + dryRun = true; + continue; + } + if (arg === '--env') { + const value = argv[index + 1]; + if (!value) throw new Error('--env requires dev, test, or prod.'); + nodeEnv = value; + index += 1; + continue; + } + if (arg.startsWith('--env=')) { + nodeEnv = arg.slice('--env='.length); + continue; + } + if (arg === '--replace') { + replace = true; + continue; + } + if (arg === '--scheduled-at') { + scheduledAt = parseDateArg(argv[index + 1], '--scheduled-at'); + index += 1; + continue; + } + if (arg.startsWith('--scheduled-at=')) { + scheduledAt = parseDateArg( + arg.slice('--scheduled-at='.length), + '--scheduled-at', + ); + continue; + } + if (arg === '--title') { + const value = argv[index + 1]; + if (!value) throw new Error('--title requires a value.'); + title = value; + index += 1; + continue; + } + if (arg.startsWith('--title=')) { + title = arg.slice('--title='.length); + continue; + } + if (arg === '--help' || arg === '-h') { + printHelp(); + process.exit(0); + } + throw new Error(`Unknown argument: ${arg}`); + } + + if (!['dev', 'test', 'prod'].includes(nodeEnv)) { + throw new Error('--env must be dev, test, or prod.'); + } + if (communityId && !mongoose.isValidObjectId(communityId)) { + throw new Error('--community-id must be a valid ObjectId.'); + } + + return { + communityId: communityId?.trim() || undefined, + dryRun, + nodeEnv, + replace, + scheduledAt, + title, + }; +} + +function parseDateArg(value: string | undefined, label: string): Date { + if (!value) throw new Error(`${label} requires an ISO date value.`); + const parsed = new Date(value); + if (Number.isNaN(parsed.getTime())) { + throw new Error(`${label} must be a valid ISO date value.`); + } + return parsed; +} + +function printHelp() { + console.log(`Usage: + pnpm --filter api exec ts-node src/modules/global/database/dev-seeds/create-temp-finished-match.ts + pnpm --filter api exec ts-node src/modules/global/database/dev-seeds/create-temp-finished-match.ts --replace + +Options: + --community-id Community ObjectId. Defaults to env or first community. + --dry-run Print the payload summary without writing. + --env dev | test | prod. Default: NODE_ENV or dev. + --replace Delete previous temp matches created by this script first. + --scheduled-at Scheduled date. Default: yesterday. + --title Match title. Default: timestamped temp title. +`); +} + +async function main() { + const options = parseArgs(process.argv.slice(2)); + process.env.NODE_ENV = options.nodeEnv; + await loadMachineEnvFile(); + + const app = await NestFactory.createApplicationContext(TempSeedModule, { + logger: ['log', 'warn', 'error'], + }); + + try { + const connectionService = app.get(ConnectionService); + const CommunityModel = await connectionService.getModel('community'); + const MatchModel = await connectionService.getModel('match'); + const community = await resolveCommunity( + CommunityModel, + options.communityId, + ); + + const payload = buildMatchPayload(community._id, options); + + console.log( + `[temp result seed] mode=${options.dryRun ? 'dry-run' : 'apply'} env=${options.nodeEnv} db=${resolveMongoDbName(options.nodeEnv)}`, + ); + console.log( + `[temp result seed] community=${community.name ?? '(unnamed)'} (${community._id.toString()})`, + ); + console.log( + `[temp result seed] title="${options.title}" participants=${payload.participants.length} teams=${payload.teams.length} games=${payload.result.games.length}`, + ); + + if (options.dryRun) { + console.log('[temp result seed] No writes were made.'); + return; + } + + if (options.replace) { + const deleteResult = await MatchModel.deleteMany({ + communityId: community._id, + 'legacyImportSource.jobId': TEMP_JOB_ID, + }).exec(); + console.log( + `[temp result seed] deleted=${deleteResult.deletedCount ?? 0}`, + ); + } + + const match = await MatchModel.create(payload); + console.log(`[temp result seed] created=${match._id.toString()}`); + console.log(`[temp result seed] route=/match/${match._id.toString()}`); + } finally { + await app.close(); + } +} + +async function resolveCommunity( + CommunityModel: mongoose.Model<unknown>, + communityId?: string, +): Promise<CommunityLean> { + const community = communityId + ? await CommunityModel.findById(communityId) + .select({ name: 1 }) + .lean<CommunityLean>() + .exec() + : await CommunityModel.findOne() + .sort({ createdAt: 1 }) + .select({ name: 1 }) + .lean<CommunityLean>() + .exec(); + + if (!community) { + throw new Error( + communityId + ? `Community not found: ${communityId}` + : 'No community found. Pass --community-id.', + ); + } + + return community; +} + +function buildMatchPayload( + communityId: mongoose.Types.ObjectId, + options: CliOptions, +) { + const now = new Date(); + const players = buildPlayers(now); + const participants = players.map( + (player, index): MatchParticipant => ({ + userId: player.userId, + puuid: player.puuid, + gameName: player.gameName, + tagLine: player.tagLine, + profileIconId: player.profileIconId, + tier: player.tier, + preferredPositions: player.preferredPositions, + joinedAt: new Date( + options.scheduledAt.getTime() - (TOTAL_PLAYERS - index) * 60000, + ), + excluded: false, + }), + ); + const teams = buildTeams(players); + const games = buildGames(players, options.scheduledAt); + const seriesResults = buildSeriesResults(games); + const winnerTeamIndex = resolveOverallWinner(seriesResults); + + return { + communityId, + title: options.title, + description: + '결과 페이지 검증용으로 생성된 30명, 6팀, 15경기 완료 임시 데이터입니다.', + mapId: 11, + status: 'finished', + scheduledAt: options.scheduledAt, + maxParticipants: TOTAL_PLAYERS, + gameCount: GAME_COUNT_PER_SERIES_GROUP, + gameMode: 'round-robin', + tournamentCodeIds: [], + teamBuiltAt: new Date(options.scheduledAt.getTime() - 30 * 60000), + participants, + teams, + result: { + winnerTeamIndex, + mvpUserId: players[0].userId, + riotMatchIds: games.map((game) => game.riotMatchId), + games, + seriesResults, + }, + resultPollState: { + startedAt: new Date(options.scheduledAt.getTime() + 2 * 60 * 60 * 1000), + lastPolledAt: now, + lastPollOutcome: 'matched', + matchedCount: games.length, + expandedToAllPuuids: true, + state: 'complete', + }, + legacyImported: true, + legacyImportSource: { + importedAt: now, + importedBy: players[0].userId, + triggeringPuuid: players[0].puuid, + jobId: TEMP_JOB_ID, + }, + prizes: ['1등 팀: 결과 화면 검증용 임시 상품', 'MVP: Temp01'], + notices: ['이 매치는 UI 검증용 임시 데이터입니다.'], + schedule: [ + { time: '20:00', label: '로비 오픈' }, + { time: '20:10', label: '1경기 시작' }, + { time: '23:30', label: '풀리그 종료' }, + ], + }; +} + +function buildPlayers(now: Date): TempPlayer[] { + return Array.from({ length: TOTAL_PLAYERS }, (_, index) => { + const playerNumber = index + 1; + const primaryPosition = POSITIONS[index % POSITIONS.length]; + + return { + userId: new mongoose.Types.ObjectId(), + puuid: `temp-result-puuid-${playerNumber.toString().padStart(2, '0')}`, + gameName: `Temp${playerNumber.toString().padStart(2, '0')}`, + tagLine: `T${playerNumber.toString().padStart(3, '0')}`, + profileIconId: 20 + playerNumber, + tier: buildTier(playerNumber, now), + preferredPositions: buildPreferredPositions(primaryPosition), + }; + }); +} + +function buildPreferredPositions( + primaryPosition: Position, +): PositionPreference[] { + const ordered = [ + primaryPosition, + ...POSITIONS.filter((position) => position !== primaryPosition), + ]; + return ordered.map((position, index) => ({ + position, + priority: index + 1, + })); +} + +function buildTier(playerNumber: number, fetchedAt: Date): TierInfo { + const tiers = [ + ['CHALLENGER', 'I'], + ['GRANDMASTER', 'I'], + ['MASTER', 'I'], + ['DIAMOND', 'I'], + ['DIAMOND', 'II'], + ['EMERALD', 'I'], + ['EMERALD', 'II'], + ['PLATINUM', 'I'], + ['PLATINUM', 'II'], + ['GOLD', 'I'], + ] as const; + const [tier, rank] = tiers[(playerNumber - 1) % tiers.length]; + + return { + queueType: 'RANKED_SOLO_5x5', + tier, + rank, + leaguePoints: (playerNumber * 17) % 100, + wins: 40 + playerNumber, + losses: 22 + (playerNumber % 13), + fetchedAt, + }; +} + +function buildTeams(players: TempPlayer[]): MatchTeam[] { + return Array.from({ length: TEAM_COUNT }, (_, teamOffset) => { + const teamIndex = teamOffset + 1; + const members = players + .slice(teamOffset * TEAM_SIZE, (teamOffset + 1) * TEAM_SIZE) + .map((player, memberOffset) => ({ + userId: player.userId, + assignedPosition: POSITIONS[memberOffset], + cost: memberOffset === 0 ? 0 : 80 + teamIndex * 15 + memberOffset * 20, + })); + + return { + teamIndex, + name: `${teamIndex}팀`, + captainUserId: members[0]?.userId, + members, + }; + }); +} + +function buildGames( + players: TempPlayer[], + scheduledAt: Date, +): MatchGameResult[] { + const games: MatchGameResult[] = []; + const playersByTeam = new Map<number, TempPlayer[]>(); + + for (let teamIndex = 1; teamIndex <= TEAM_COUNT; teamIndex += 1) { + playersByTeam.set( + teamIndex, + players.slice((teamIndex - 1) * TEAM_SIZE, teamIndex * TEAM_SIZE), + ); + } + + for (let teamAIndex = 1; teamAIndex <= TEAM_COUNT; teamAIndex += 1) { + for ( + let teamBIndex = teamAIndex + 1; + teamBIndex <= TEAM_COUNT; + teamBIndex += 1 + ) { + const gameNumber = games.length + 1; + const lowerTeamStartsBlue = gameNumber % 2 === 1; + const blueTeamIndex = lowerTeamStartsBlue ? teamAIndex : teamBIndex; + const redTeamIndex = lowerTeamStartsBlue ? teamBIndex : teamAIndex; + const winnerTeamIndex = teamAIndex; + const winnerSide: TeamSide = + winnerTeamIndex === blueTeamIndex ? 'blue' : 'red'; + const gameCreation = scheduledAt.getTime() + gameNumber * 20 * 60 * 1000; + const riotMatchId = `KR_TEMP_30X6_${formatStamp(scheduledAt)}_${gameNumber + .toString() + .padStart(2, '0')}`; + + games.push({ + gameNumber, + riotMatchId, + blueTeamIndex, + redTeamIndex, + winnerTeamIndex, + winnerSide, + gameCreation, + gameDuration: 1650 + gameNumber * 23, + participants: [ + ...buildGameParticipants({ + players: playersByTeam.get(blueTeamIndex) ?? [], + teamIndex: blueTeamIndex, + side: 'blue', + winnerTeamIndex, + gameNumber, + }), + ...buildGameParticipants({ + players: playersByTeam.get(redTeamIndex) ?? [], + teamIndex: redTeamIndex, + side: 'red', + winnerTeamIndex, + gameNumber, + }), + ], + }); + } + } + + return games; +} + +function buildGameParticipants(params: { + players: TempPlayer[]; + teamIndex: number; + side: TeamSide; + winnerTeamIndex: number; + gameNumber: number; +}): MatchGameResult['participants'] { + const didWin = params.teamIndex === params.winnerTeamIndex; + + return params.players.map((player, index) => { + const championIndex = + (params.gameNumber + params.teamIndex + index) % CHAMPION_IDS.length; + return { + userId: player.userId, + teamIndex: params.teamIndex, + side: params.side, + championId: CHAMPION_IDS[championIndex], + kills: didWin + ? 4 + ((params.gameNumber + index) % 8) + : 1 + ((params.gameNumber + index) % 5), + deaths: didWin ? 1 + (index % 4) : 4 + ((params.gameNumber + index) % 7), + assists: didWin + ? 7 + ((params.gameNumber + index) % 12) + : 3 + ((params.gameNumber + index) % 8), + win: didWin, + }; + }); +} + +function buildSeriesResults(games: MatchGameResult[]): MatchSeriesResult[] { + return games.map((game) => { + const teamAIndex = Math.min(game.blueTeamIndex, game.redTeamIndex); + const teamBIndex = Math.max(game.blueTeamIndex, game.redTeamIndex); + + return { + teamAIndex, + teamBIndex, + teamAWins: game.winnerTeamIndex === teamAIndex ? 1 : 0, + teamBWins: game.winnerTeamIndex === teamBIndex ? 1 : 0, + winnerTeamIndex: game.winnerTeamIndex, + }; + }); +} + +function resolveOverallWinner( + seriesResults: MatchSeriesResult[], +): number | undefined { + const winsByTeam = new Map<number, number>(); + for (const series of seriesResults) { + if (!series.winnerTeamIndex) continue; + winsByTeam.set( + series.winnerTeamIndex, + (winsByTeam.get(series.winnerTeamIndex) ?? 0) + 1, + ); + } + + return [...winsByTeam.entries()].sort( + (left, right) => right[1] - left[1], + )[0]?.[0]; +} + +function formatStamp(date: Date): string { + const year = date.getFullYear(); + const month = `${date.getMonth() + 1}`.padStart(2, '0'); + const day = `${date.getDate()}`.padStart(2, '0'); + const hours = `${date.getHours()}`.padStart(2, '0'); + const minutes = `${date.getMinutes()}`.padStart(2, '0'); + const seconds = `${date.getSeconds()}`.padStart(2, '0'); + return `${year}${month}${day}-${hours}${minutes}${seconds}`; +} + +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/migrations/20260502-auction-pass-history.ts b/apps/api/src/modules/global/database/migrations/20260502-auction-pass-history.ts new file mode 100644 index 00000000..95f0bbce --- /dev/null +++ b/apps/api/src/modules/global/database/migrations/20260502-auction-pass-history.ts @@ -0,0 +1,100 @@ +interface MigrationCollection { + find(filter: Record<string, unknown>): { + limit(limit: number): AsyncIterable<AnyDoc>; + }; + bulkWrite( + writes: Array<{ + updateOne: { + filter: { _id: unknown }; + update: { $set: Record<string, unknown> }; + }; + }>, + options: { ordered: boolean }, + ): Promise<unknown>; +} + +interface MigrationDb { + collection(name: string): MigrationCollection; +} + +interface MigrationOptions { + dryRun?: boolean; + limit?: number; +} + +interface MigrationStats { + scannedAuctions: number; + changedAuctions: number; +} + +type AnyDoc = Record<string, unknown> & { _id: unknown }; + +export async function migrateAuctionPassHistory( + db: MigrationDb, + options: MigrationOptions = {}, +): Promise<MigrationStats> { + const dryRun = options.dryRun ?? true; + const stats: MigrationStats = { + scannedAuctions: 0, + changedAuctions: 0, + }; + const writes: Array<{ + updateOne: { + filter: { _id: unknown }; + update: { $set: Record<string, unknown> }; + }; + }> = []; + + for await (const doc of db.collection('auction').find({}).limit(options.limit ?? 0)) { + stats.scannedAuctions += 1; + const set: Record<string, unknown> = {}; + let changed = false; + + if (!Array.isArray(doc.passedQueue)) { + set.passedQueue = []; + changed = true; + } + if (typeof doc.auctionRound !== 'number') { + set.auctionRound = 1; + changed = true; + } + if (typeof doc.topTierThreshold !== 'string') { + set.topTierThreshold = 'NONE'; + changed = true; + } + if (!Array.isArray(doc.manualTopTierUserIds)) { + set.manualTopTierUserIds = []; + changed = true; + } + + const order = Array.isArray(doc.order) + ? doc.order.map((player) => { + if (!isRecord(player) || typeof player.passCount === 'number') { + return player; + } + changed = true; + return { ...player, passCount: 0 }; + }) + : []; + if (changed && order.length > 0) set.order = order; + if (!changed) continue; + + stats.changedAuctions += 1; + writes.push({ + updateOne: { + filter: { _id: doc._id }, + update: { $set: set }, + }, + }); + } + + if (!dryRun && writes.length > 0) { + await db.collection('auction').bulkWrite(writes, { ordered: false }); + } + + return stats; +} + +function isRecord(value: unknown): value is Record<string, unknown> { + return Boolean(value && typeof value === 'object' && !Array.isArray(value)); +} diff --git a/apps/api/src/modules/global/database/migrations/20260502-team-index.spec.ts b/apps/api/src/modules/global/database/migrations/20260502-team-index.spec.ts new file mode 100644 index 00000000..deb3168f --- /dev/null +++ b/apps/api/src/modules/global/database/migrations/20260502-team-index.spec.ts @@ -0,0 +1,160 @@ +import { migrateTeamIndex } from './20260502-team-index'; + +function createDb(params: { + auctions?: Array<Record<string, unknown>>; + matches?: Array<Record<string, unknown>>; +}) { + const collections = { + auction: params.auctions ?? [], + match: params.matches ?? [], + }; + + return { + collections, + db: { + collection(name: string) { + const key = name === 'auction' ? 'auction' : 'match'; + const docs = collections[key]; + return { + find() { + return { + limit() { + return { + async *[Symbol.asyncIterator]() { + for (const doc of docs) yield doc; + }, + }; + }, + }; + }, + async bulkWrite(writes: Array<{ + updateOne: { + filter: { _id: unknown }; + update: { + $set: Record<string, unknown>; + $unset: Record<string, ''>; + }; + }; + }>) { + for (const write of writes) { + const doc = docs.find((item) => item._id === write.updateOne.filter._id); + if (!doc) continue; + Object.assign(doc, write.updateOne.update.$set); + for (const key of Object.keys(write.updateOne.update.$unset)) { + delete doc[key]; + } + } + }, + }; + }, + }, + }; +} + +describe('migrateTeamIndex', () => { + it('migrates auction and match team identity fields', async () => { + const { db, collections } = createDb({ + auctions: [ + { + _id: 'auction-1', + teams: [{ side: 'blue', groupIndex: 1 }, { side: 'red', groupIndex: 1 }], + bids: [{ side: 'red', groupIndex: 1, amount: 100 }], + currentBidder: 'red', + currentBidderGroupIndex: 1, + }, + ], + matches: [ + { + _id: 'match-1', + teams: [{ side: 'blue', groupIndex: 1 }, { side: 'red', groupIndex: 1 }], + result: { + winnerSide: 'blue', + blueScore: 1, + redScore: 0, + games: [ + { + gameNumber: 1, + riotMatchId: 'KR_1', + winnerSide: 'blue', + blueGroupIndex: 1, + redGroupIndex: 1, + participants: [ + { userId: 'u1', side: 'blue' }, + { userId: 'u2', side: 'red' }, + ], + }, + ], + seriesResults: [ + { + blueGroupIndex: 1, + redGroupIndex: 1, + blueScore: 1, + redScore: 0, + winnerSide: 'blue', + }, + ], + }, + }, + ], + }); + + const stats = await migrateTeamIndex(db, { dryRun: false }); + + expect(stats).toEqual({ + scannedAuctions: 1, + changedAuctions: 1, + scannedMatches: 1, + changedMatches: 1, + }); + expect(collections.auction[0]).toMatchObject({ + teams: [{ teamIndex: 1 }, { teamIndex: 2 }], + bids: [{ teamIndex: 2, amount: 100 }], + currentBidderTeamIndex: 2, + }); + expect(collections.match[0]).toMatchObject({ + teams: [{ teamIndex: 1, name: '1팀' }, { teamIndex: 2, name: '2팀' }], + result: { + winnerTeamIndex: 1, + games: [ + { + blueTeamIndex: 1, + redTeamIndex: 2, + winnerTeamIndex: 1, + participants: [ + { teamIndex: 1, side: 'blue' }, + { teamIndex: 2, side: 'red' }, + ], + }, + ], + seriesResults: [ + { + teamAIndex: 1, + teamBIndex: 2, + teamAWins: 1, + teamBWins: 0, + winnerTeamIndex: 1, + }, + ], + }, + }); + expect(collections.auction[0]).not.toHaveProperty('currentBidder'); + expect(collections.match[0].result).not.toHaveProperty('blueScore'); + }); + + it('is idempotent after applying updates', async () => { + const { db } = createDb({ + matches: [ + { + _id: 'match-1', + teams: [{ side: 'blue', groupIndex: 1 }], + }, + ], + }); + + await migrateTeamIndex(db, { dryRun: false }); + const stats = await migrateTeamIndex(db, { dryRun: false }); + + expect(stats.changedMatches).toBe(0); + expect(stats.changedAuctions).toBe(0); + }); +}); diff --git a/apps/api/src/modules/global/database/migrations/20260502-team-index.ts b/apps/api/src/modules/global/database/migrations/20260502-team-index.ts new file mode 100644 index 00000000..cbbf21c2 --- /dev/null +++ b/apps/api/src/modules/global/database/migrations/20260502-team-index.ts @@ -0,0 +1,335 @@ +type LegacySide = 'blue' | 'red'; + +interface MigrationCollection { + find(filter: Record<string, unknown>): { + limit(limit: number): AsyncIterable<AnyDoc>; + }; + bulkWrite( + writes: Array<{ + updateOne: { + filter: { _id: unknown }; + update: { $set: Record<string, unknown>; $unset: Record<string, ''> }; + }; + }>, + options: { ordered: boolean }, + ): Promise<unknown>; +} + +interface MigrationDb { + collection(name: string): MigrationCollection; +} + +interface MigrationOptions { + dryRun?: boolean; + limit?: number; +} + +interface MigrationStats { + scannedAuctions: number; + changedAuctions: number; + scannedMatches: number; + changedMatches: number; +} + +type AnyDoc = Record<string, unknown> & { _id: unknown }; + +export async function migrateTeamIndex( + db: MigrationDb, + options: MigrationOptions = {}, +): Promise<MigrationStats> { + const dryRun = options.dryRun ?? true; + const stats: MigrationStats = { + scannedAuctions: 0, + changedAuctions: 0, + scannedMatches: 0, + changedMatches: 0, + }; + + await migrateCollection(db.collection('auction'), options.limit, dryRun, stats, 'auction'); + await migrateCollection(db.collection('match'), options.limit, dryRun, stats, 'match'); + + return stats; +} + +async function migrateCollection( + collection: MigrationCollection, + limit: number | undefined, + dryRun: boolean, + stats: MigrationStats, + kind: 'auction' | 'match', +) { + const cursor = collection.find({}).limit(limit ?? 0); + const writes: Array<{ + updateOne: { + filter: { _id: unknown }; + update: { $set: Record<string, unknown>; $unset: Record<string, ''> }; + }; + }> = []; + + for await (const doc of cursor) { + if (kind === 'auction') stats.scannedAuctions += 1; + if (kind === 'match') stats.scannedMatches += 1; + + const update = kind === 'auction' ? migrateAuction(doc) : migrateMatch(doc); + if (!update) continue; + + if (kind === 'auction') stats.changedAuctions += 1; + if (kind === 'match') stats.changedMatches += 1; + writes.push({ + updateOne: { + filter: { _id: doc._id }, + update, + }, + }); + } + + if (!dryRun && writes.length > 0) { + await collection.bulkWrite(writes, { ordered: false }); + } +} + +function migrateAuction(doc: AnyDoc) { + const set: Record<string, unknown> = {}; + const unset: Record<string, ''> = {}; + let changed = false; + + const teams = asArray(doc.teams).map((team) => { + const migrated = { ...team }; + const teamIndex = readTeamIndex(migrated); + if (teamIndex !== undefined) migrated.teamIndex = teamIndex; + if ('side' in migrated || 'groupIndex' in migrated) changed = true; + delete migrated.side; + delete migrated.groupIndex; + return migrated; + }); + if (changed) set.teams = teams; + + const bids = asArray(doc.bids).map((bid) => { + const migrated = { ...bid }; + const teamIndex = readTeamIndex(migrated); + if (teamIndex !== undefined) migrated.teamIndex = teamIndex; + if ('side' in migrated || 'groupIndex' in migrated) changed = true; + delete migrated.side; + delete migrated.groupIndex; + return migrated; + }); + if (changed) set.bids = bids; + + if ( + typeof doc.currentBidderTeamIndex !== 'number' && + isSide(doc.currentBidder) + ) { + set.currentBidderTeamIndex = sideGroupToTeamIndex( + doc.currentBidder, + numberOrDefault(doc.currentBidderGroupIndex, 1), + ); + changed = true; + } + if ('currentBidder' in doc || 'currentBidderGroupIndex' in doc) { + unset.currentBidder = ''; + unset.currentBidderGroupIndex = ''; + changed = true; + } + + return changed ? { $set: set, $unset: unset } : null; +} + +function migrateMatch(doc: AnyDoc) { + const set: Record<string, unknown> = {}; + const unset: Record<string, ''> = {}; + let changed = false; + + const teams = asArray(doc.teams).map((team) => { + const migrated = { ...team }; + const teamIndex = readTeamIndex(migrated); + if (teamIndex !== undefined) migrated.teamIndex = teamIndex; + if (!migrated.name && typeof teamIndex === 'number') migrated.name = `${teamIndex}팀`; + if ('side' in migrated || 'groupIndex' in migrated) changed = true; + delete migrated.side; + delete migrated.groupIndex; + return migrated; + }); + if (changed) set.teams = teams; + + const result = isRecord(doc.result) ? migrateResult(doc.result) : null; + if (result) { + set.result = result; + changed = true; + } + + return changed ? { $set: set, $unset: unset } : null; +} + +function migrateResult(result: Record<string, unknown>) { + let changed = false; + const migrated = { ...result }; + + const games = asArray(result.games).map((game) => { + const next = { ...game }; + const blueTeamIndex = + numberOrUndefined(next.blueTeamIndex) ?? + sideGroupToTeamIndex('blue', numberOrDefault(next.blueGroupIndex, 1)); + const redTeamIndex = + numberOrUndefined(next.redTeamIndex) ?? + sideGroupToTeamIndex('red', numberOrDefault(next.redGroupIndex, 1)); + const winnerSide = isSide(next.winnerSide) ? next.winnerSide : undefined; + const winnerTeamIndex = + numberOrUndefined(next.winnerTeamIndex) ?? + (winnerSide === 'blue' + ? blueTeamIndex + : winnerSide === 'red' + ? redTeamIndex + : undefined); + + next.blueTeamIndex = blueTeamIndex; + next.redTeamIndex = redTeamIndex; + if (winnerTeamIndex !== undefined) next.winnerTeamIndex = winnerTeamIndex; + next.participants = asArray(next.participants).map((participant) => { + const migratedParticipant = { ...participant }; + if (typeof migratedParticipant.teamIndex !== 'number') { + migratedParticipant.teamIndex = + migratedParticipant.side === 'blue' ? blueTeamIndex : redTeamIndex; + } + return migratedParticipant; + }); + if ('blueGroupIndex' in next || 'redGroupIndex' in next) changed = true; + delete next.blueGroupIndex; + delete next.redGroupIndex; + return next; + }); + migrated.games = games; + + const seriesResults = asArray(result.seriesResults).map((series) => { + if (typeof series.teamAIndex === 'number' && typeof series.teamBIndex === 'number') { + return series; + } + changed = true; + const legacyBlueTeamIndex = sideGroupToTeamIndex( + 'blue', + numberOrDefault(series.blueGroupIndex, 1), + ); + const legacyRedTeamIndex = sideGroupToTeamIndex( + 'red', + numberOrDefault(series.redGroupIndex, 1), + ); + const teamAIndex = Math.min(legacyBlueTeamIndex, legacyRedTeamIndex); + const teamBIndex = Math.max(legacyBlueTeamIndex, legacyRedTeamIndex); + const blueScore = numberOrDefault(series.blueScore, 0); + const redScore = numberOrDefault(series.redScore, 0); + const winnerSide = isSide(series.winnerSide) ? series.winnerSide : undefined; + const winnerTeamIndex = + winnerSide === 'blue' + ? legacyBlueTeamIndex + : winnerSide === 'red' + ? legacyRedTeamIndex + : undefined; + return { + teamAIndex, + teamBIndex, + teamAWins: teamAIndex === legacyBlueTeamIndex ? blueScore : redScore, + teamBWins: teamBIndex === legacyBlueTeamIndex ? blueScore : redScore, + winnerTeamIndex, + }; + }); + migrated.seriesResults = seriesResults.length > 0 ? seriesResults : buildSeries(games); + + if (typeof migrated.winnerTeamIndex !== 'number') { + const migratedSeriesResults = Array.isArray(migrated.seriesResults) + ? migrated.seriesResults + : []; + migrated.winnerTeamIndex = + resolveWinnerTeamIndex(migratedSeriesResults) ?? + (isSide(migrated.winnerSide) + ? sideGroupToTeamIndex(migrated.winnerSide, 1) + : undefined); + } + + if ('winnerSide' in migrated || 'blueScore' in migrated || 'redScore' in migrated) { + changed = true; + } + delete migrated.winnerSide; + delete migrated.blueScore; + delete migrated.redScore; + + return changed ? migrated : null; +} + +function buildSeries(games: Record<string, unknown>[]) { + const bySeries = new Map< + string, + { + teamAIndex: number; + teamBIndex: number; + teamAWins: number; + teamBWins: number; + winnerTeamIndex?: number; + } + >(); + for (const game of games) { + const blueTeamIndex = numberOrDefault(game.blueTeamIndex, 1); + const redTeamIndex = numberOrDefault(game.redTeamIndex, 2); + const winnerTeamIndex = numberOrUndefined(game.winnerTeamIndex); + const teamAIndex = Math.min(blueTeamIndex, redTeamIndex); + const teamBIndex = Math.max(blueTeamIndex, redTeamIndex); + const key = `${teamAIndex}:${teamBIndex}`; + const current = + bySeries.get(key) ?? { teamAIndex, teamBIndex, teamAWins: 0, teamBWins: 0 }; + if (winnerTeamIndex === teamAIndex) current.teamAWins += 1; + if (winnerTeamIndex === teamBIndex) current.teamBWins += 1; + current.winnerTeamIndex = + current.teamAWins > current.teamBWins + ? teamAIndex + : current.teamBWins > current.teamAWins + ? teamBIndex + : undefined; + bySeries.set(key, current); + } + return [...bySeries.values()]; +} + +function resolveWinnerTeamIndex( + seriesResults: Array<Record<string, unknown>>, +): number | undefined { + const wins = new Map<number, number>(); + for (const series of seriesResults) { + const winnerTeamIndex = numberOrUndefined(series.winnerTeamIndex); + if (!winnerTeamIndex) continue; + wins.set(winnerTeamIndex, (wins.get(winnerTeamIndex) ?? 0) + 1); + } + const sorted = [...wins.entries()].sort((left, right) => right[1] - left[1]); + if (sorted.length === 0) return undefined; + if (sorted.length > 1 && sorted[0][1] === sorted[1][1]) return undefined; + return sorted[0][0]; +} + +function readTeamIndex(value: Record<string, unknown>): number | undefined { + if (typeof value.teamIndex === 'number') return value.teamIndex; + if (!isSide(value.side)) return undefined; + return sideGroupToTeamIndex(value.side, numberOrDefault(value.groupIndex, 1)); +} + +function sideGroupToTeamIndex(side: LegacySide, groupIndex: number) { + return (Math.max(1, groupIndex) - 1) * 2 + (side === 'blue' ? 1 : 2); +} + +function asArray(value: unknown): Record<string, unknown>[] { + return Array.isArray(value) + ? value.filter(isRecord).map((item) => ({ ...item })) + : []; +} + +function isRecord(value: unknown): value is Record<string, unknown> { + return Boolean(value && typeof value === 'object' && !Array.isArray(value)); +} + +function isSide(value: unknown): value is LegacySide { + return value === 'blue' || value === 'red'; +} + +function numberOrDefault(value: unknown, fallback: number) { + return typeof value === 'number' && Number.isFinite(value) ? value : fallback; +} + +function numberOrUndefined(value: unknown) { + return typeof value === 'number' && Number.isFinite(value) ? value : undefined; +} diff --git a/apps/api/src/modules/global/database/migrations/run-team-index.ts b/apps/api/src/modules/global/database/migrations/run-team-index.ts new file mode 100644 index 00000000..1b871ce8 --- /dev/null +++ b/apps/api/src/modules/global/database/migrations/run-team-index.ts @@ -0,0 +1,167 @@ +import 'reflect-metadata'; +import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { NestFactory } from '@nestjs/core'; +import { readFile } from 'node:fs/promises'; +import { resolve } from 'node:path'; +import { ConnectionService, resolveMongoDbName } from '../connection.service'; +import { SecretModule } from '../../secret/secret.module'; +import { migrateTeamIndex } from './20260502-team-index'; + +interface CliOptions { + dryRun: boolean; + limit?: number; + 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 { + // 로컬 machine env 파일이 없으면 그냥 지나간다. + } +} + +function parseArgs(argv: string[]): CliOptions { + let dryRun = true; + let limit: number | undefined; + let nodeEnv = process.env.NODE_ENV ?? 'dev'; + + for (let index = 0; index < argv.length; index += 1) { + const arg = argv[index]; + if (arg === '--apply') { + dryRun = false; + continue; + } + if (arg === '--dry-run') { + dryRun = true; + continue; + } + if (arg === '--env') { + const value = argv[index + 1]; + if (!value) throw new Error('--env requires dev, test, or prod.'); + nodeEnv = value; + index += 1; + continue; + } + if (arg.startsWith('--env=')) { + nodeEnv = arg.slice('--env='.length); + continue; + } + if (arg === '--limit') { + const value = Number(argv[index + 1]); + if (!Number.isInteger(value) || value < 1) { + throw new Error('--limit requires a positive integer.'); + } + limit = value; + index += 1; + continue; + } + if (arg.startsWith('--limit=')) { + const value = Number(arg.slice('--limit='.length)); + if (!Number.isInteger(value) || value < 1) { + throw new Error('--limit requires a positive integer.'); + } + limit = value; + continue; + } + if (arg === '--help' || arg === '-h') { + printHelp(); + process.exit(0); + } + throw new Error(`Unknown argument: ${arg}`); + } + + if (!['dev', 'test', 'prod'].includes(nodeEnv)) { + throw new Error('--env must be dev, test, or prod.'); + } + + return { dryRun, limit, nodeEnv }; +} + +function printHelp() { + console.log(`Usage: + pnpm --filter api exec ts-node src/modules/global/database/migrations/run-team-index.ts [--dry-run] + pnpm --filter api exec ts-node src/modules/global/database/migrations/run-team-index.ts --apply + +Options: + --dry-run Print affected counts without writing. Default. + --apply Apply updates to MongoDB. + --env <env> dev | test | prod. Default: NODE_ENV or dev. + --limit <n> Only scan first n documents per collection. +`); +} + +async function main() { + const options = parseArgs(process.argv.slice(2)); + process.env.NODE_ENV = options.nodeEnv; + await loadMachineEnvFile(); + + const app = await NestFactory.createApplicationContext(MigrationModule, { + logger: options.dryRun ? ['log', 'warn', 'error'] : ['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( + `[team-index migration] mode=${options.dryRun ? 'dry-run' : 'apply'} env=${options.nodeEnv} db=${dbName}`, + ); + + const stats = await migrateTeamIndex(connection.db as Parameters<typeof migrateTeamIndex>[0], { + dryRun: options.dryRun, + limit: options.limit, + }); + + console.table(stats); + if (options.dryRun) { + console.log('No writes were made. Re-run with --apply to update documents.'); + } else { + console.log('Migration applied.'); + } + } finally { + await app.close(); + } +} + +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 new file mode 100644 index 00000000..560778dc --- /dev/null +++ b/apps/api/src/modules/global/database/schema.ts @@ -0,0 +1,1559 @@ +import mongoose, { Schema, HydratedDocument } from 'mongoose'; +import type { + RiotPlatformRoute, + RiotRegionalRoute, +} from '../../riot/riot.types'; +import { ModelDefinition } from './types'; + +export type Position = 'TOP' | 'JGL' | 'MID' | 'ADC' | 'SUP'; +export const POSITIONS: Position[] = ['TOP', 'JGL', 'MID', 'ADC', 'SUP']; + +export type RiotLane = 'TOP' | 'JUNGLE' | 'MIDDLE' | 'BOTTOM' | 'UTILITY'; +export const RIOT_LANES: RiotLane[] = [ + 'TOP', + 'JUNGLE', + 'MIDDLE', + 'BOTTOM', + 'UTILITY', +]; + +export type MatchStatus = + | 'recruiting' + | 'team-built' + | 'in-progress' + | 'finished'; +export const MATCH_STATUSES: MatchStatus[] = [ + 'recruiting', + 'team-built', + 'in-progress', + 'finished', +]; + +export type TeamSide = 'blue' | 'red'; +export const TEAM_SIDES: TeamSide[] = ['blue', 'red']; + +export type MatchGameMode = 'pair' | 'bracket' | 'round-robin'; +export const MATCH_GAME_MODES: MatchGameMode[] = [ + 'pair', + 'bracket', + 'round-robin', +]; + +export type CommunityRole = 'owner' | 'admin' | 'member'; +export const COMMUNITY_ROLES: CommunityRole[] = ['owner', 'admin', 'member']; + +export type TournamentCodeState = + | 'available' + | 'reserved' + | 'used' + | 'expired' + | 'released'; +export const TOURNAMENT_CODE_STATES: TournamentCodeState[] = [ + 'available', + 'reserved', + 'used', + 'expired', + 'released', +]; +export type TournamentMapType = 'SUMMONERS_RIFT'; +export const TOURNAMENT_MAP_TYPES: TournamentMapType[] = ['SUMMONERS_RIFT']; +export type TournamentSpectatorType = 'NONE' | 'LOBBYONLY' | 'ALL'; +export const TOURNAMENT_SPECTATOR_TYPES: TournamentSpectatorType[] = [ + 'NONE', + 'LOBBYONLY', + 'ALL', +]; + +export type MatchResultPollOutcome = 'matched' | 'not-found' | 'failed'; +export type MatchResultPollStateValue = + | 'polling' + | 'complete' + | 'expired' + | 'failed'; +export const MATCH_RESULT_POLL_STATES: MatchResultPollStateValue[] = [ + 'polling', + 'complete', + 'expired', + 'failed', +]; + +export type LegacyImportJobState = + | 'pending' + | 'running' + | 'completed' + | 'failed' + | 'cancelled'; +export const LEGACY_IMPORT_JOB_STATES: LegacyImportJobState[] = [ + 'pending', + 'running', + 'completed', + 'failed', + 'cancelled', +]; + +export interface PositionPreference { + position: Position; + priority: number; // 1 = 최우선, 숫자가 작을수록 높은 우선순위 +} + +export type AuctionPhase = + | 'waiting' + | 'presenting' + | 'bidding' + | 'sold' + | 'passed' + | 'complete'; +export const AUCTION_PHASES: AuctionPhase[] = [ + 'waiting', + 'presenting', + 'bidding', + 'sold', + 'passed', + 'complete', +]; +export const AUCTION_TOP_TIER_THRESHOLDS = [ + 'NONE', + 'EMERALD', + 'DIAMOND', + 'MASTER', + 'MANUAL', +] as const; +export type AuctionTopTierThreshold = + (typeof AUCTION_TOP_TIER_THRESHOLDS)[number]; +export type AuctionActionType = + | 'start' + | 'open-bidding' + | 'bid' + | 'sold' + | 'passed' + | 'next' + | 'complete'; + +export interface MongooseTimestamps { + createdAt: Date; + updatedAt: Date; +} + +// ===== Identity ===== +export interface Identity extends MongooseTimestamps { + entraOid: string; + tenantId?: string; + email?: string; + displayName?: string; + lastSignInAt?: Date; +} + +// ===== Tier ===== +export interface TierInfo { + leagueId?: string; + queueType: string; // RANKED_SOLO_5x5, RANKED_FLEX_SR 등 + tier: string; // IRON, BRONZE, ..., CHALLENGER + rank: string; // I | II | III | IV + puuid?: string; + leaguePoints: number; + wins: number; + losses: number; + veteran?: boolean; + inactive?: boolean; + freshBlood?: boolean; + hotStreak?: boolean; + miniSeries?: Record<string, unknown>; + fetchedAt: Date; // Riot API로부터 조회된 시각 +} + +export interface TierHistoryEntry { + queueType: string; + tier: string; + rank?: string; + leaguePoints?: number; + wins?: number; + losses?: number; + recordedAt: Date; + season?: string; // e.g., 'S13', 'S14' + peakTier?: string; + peakRank?: string; +} + +// ===== RiotAccount (User 하위에 임베딩) ===== +/** + * 챔피언 단위 집계 기록. 필드 추가가 잦을 것으로 예상되어 Mixed로 저장. + * key: championId (string), value: championId별 누적/평균 지표 자유형 JSON + */ +export type ChampionRecordMap = Record<string, Record<string, unknown>>; + +export interface ChampionMasteryEntry { + puuid: string; + championId: number; + championLevel: number; + championPoints: number; + lastPlayTime?: number; + championPointsSinceLastLevel?: number; + championPointsUntilNextLevel?: number; + markRequiredForNextLevel?: number; + tokensEarned?: number; + championSeasonMilestone?: number; + milestoneGrades?: string[]; + nextSeasonMilestone?: Record<string, unknown>; +} + +export interface RiotLaneStats { + games: number; + wins: number; + losses: number; + avgKills: number; + avgDeaths: number; + avgAssists: number; + avgCs: number; + avgCsPerMin: number; + mostPlayedChampionIds: number[]; +} + +export interface RiotRecentStatsSummary { + overall: RiotLaneStats; + byLane: Partial<Record<RiotLane, RiotLaneStats>>; +} + +export function createEmptyRiotLaneStats(): RiotLaneStats { + return { + games: 0, + wins: 0, + losses: 0, + avgKills: 0, + avgDeaths: 0, + avgAssists: 0, + avgCs: 0, + avgCsPerMin: 0, + mostPlayedChampionIds: [], + }; +} + +export function createEmptyRiotRecentStatsSummary(): RiotRecentStatsSummary { + return { + overall: createEmptyRiotLaneStats(), + byLane: {}, + }; +} + +export interface RiotAccount extends MongooseTimestamps { + puuid: string; + gameName: string; + tagLine: string; + profileIconId: number; + revisionDate: number; + summonerLevel: number; + isPrimary: boolean; + + // 현재 티어들(솔랭/자랭 각각) + tiers: TierInfo[]; + // 과거 티어 기록 + tierHistory: TierHistoryEntry[]; + + // Riot matchId 참조 (원본은 RiotMatch 컬렉션에 저장) + matchIds: string[]; + matchIdsFetchedAt?: Date; + matchIdsByQueueFetchedAt?: Map<string, Date>; + lastRefreshRequestedAt?: Date; + + // 최근 경기 요약 + recentStatsSummary?: RiotRecentStatsSummary; + recentStatsFetchedAt?: Date; + + // 챔피언 숙련도 + championMasteries: ChampionMasteryEntry[]; + championMasteriesFetchedAt?: Date; + + // 챔피언별 기록 (JSON, 필드 추가 자유로움) + championRecords: ChampionRecordMap; + championRecordsFetchedAt?: Date; +} + +// ===== User ===== +export interface User extends MongooseTimestamps { + communityId: mongoose.Types.ObjectId; + identityId?: mongoose.Types.ObjectId; + role: CommunityRole; + kakaoTalkId?: string; + kakaoTalkNickname?: string; + discordId?: string; + discordNickname?: string; + riotAccounts: RiotAccount[]; + preferredPositions: PositionPreference[]; + isActivated: boolean; +} + +export interface Community extends MongooseTimestamps { + name: string; + description: string; + ownerUserId: mongoose.Types.ObjectId; + dakaoProjectId?: mongoose.Types.ObjectId; + riotPlatform?: RiotPlatformRoute; + riotRegion?: RiotRegionalRoute; + seasonStartedAt?: Date; + seasons?: CommunitySeason[]; + matchGameCount?: number; +} + +export interface CommunitySeason { + id: string; + label: string; + startedAt: Date; + endedAt?: Date; +} + +export type CommunityDocument = HydratedDocument<Community>; +export type IdentityDocument = HydratedDocument<Identity>; +export type UserDocument = HydratedDocument<User>; + +// ===== RiotMatch (Riot 원본 매치 아카이브) ===== +export interface RiotMatch extends MongooseTimestamps { + matchId: string; // Riot matchId (예: KR_7123456789) + gameCreation: number; + gameDuration: number; + gameVersion?: string; + queueId?: number; + participantPuuids: string[]; + raw: Record<string, unknown>; // Riot API Match-V5 원본 응답 전체 + fetchedAt: Date; +} + +export type RiotMatchDocument = HydratedDocument<RiotMatch>; + +// ===== TournamentCode (운영자가 미리 업로드한 토너먼트 코드 풀) ===== +export interface TournamentCode extends MongooseTimestamps { + code: string; + region: RiotPlatformRoute; + mapType: TournamentMapType; + teamSize: number; + spectatorType: TournamentSpectatorType; + metadata?: Record<string, unknown>; + expiresAt: Date; + state: TournamentCodeState; + matchId?: mongoose.Types.ObjectId; + gameNumber?: number; + reservedAt?: Date; + usedAt?: Date; + releasedAt?: Date; + riotMatchId?: string; + uploadedBy: mongoose.Types.ObjectId; + uploadedAt: Date; + source?: string; + notes?: string; +} + +export type TournamentCodeDocument = HydratedDocument<TournamentCode>; + +// ===== Match (내전) ===== +export interface MatchParticipant { + userId: mongoose.Types.ObjectId; + // 표시용 스냅샷 (대표 RiotAccount 기준) + puuid?: string; + gameName?: string; + tagLine?: string; + profileIconId?: number; + tier?: TierInfo; + preferredPositions: PositionPreference[]; + joinedAt: Date; + excluded?: boolean; +} + +export interface MatchTeamMember { + userId: mongoose.Types.ObjectId; + assignedPosition: Position; + cost?: number; // 경매로 영입된 경우의 입찰가 +} + +export interface MatchTeam { + teamIndex: number; + name: string; + captainUserId?: mongoose.Types.ObjectId; + members: MatchTeamMember[]; +} + +export interface MatchGameResultParticipant { + userId: mongoose.Types.ObjectId; + teamIndex: number; + side: TeamSide; + championId: number; + kills: number; + deaths: number; + assists: number; + win: boolean; +} + +export interface MatchGameResult { + gameNumber: number; + riotMatchId: string; + blueTeamIndex: number; + redTeamIndex: number; + winnerTeamIndex: number; + winnerSide: TeamSide; + gameCreation: number; + gameDuration: number; + participants: MatchGameResultParticipant[]; +} + +export interface MatchSeriesResult { + teamAIndex: number; + teamBIndex: number; + teamAWins: number; + teamBWins: number; + winnerTeamIndex?: number; +} + +export interface MatchGameAssignment { + gameNumber: number; + teamAIndex: number; + teamBIndex: number; +} + +export interface MatchResult { + winnerTeamIndex?: number; + mvpUserId?: mongoose.Types.ObjectId; + riotMatchIds: string[]; // 내전에서 플레이된 Riot matchId들 + games: MatchGameResult[]; + seriesResults?: MatchSeriesResult[]; +} + +export interface MatchResultPollState { + startedAt: Date; + lastPolledAt?: Date; + lastPollOutcome?: MatchResultPollOutcome; + matchedCount: number; + expandedToAllPuuids: boolean; + state: MatchResultPollStateValue; +} + +export interface MatchScheduleItem { + time: string; + label: string; +} + +export interface Match extends MongooseTimestamps { + communityId: mongoose.Types.ObjectId; + shortCode?: string; + title: string; + description: string; + mapId: number; // Riot Match-V5 mapId (11=소환사의 협곡, 12=ARAM 등) + status: MatchStatus; + scheduledAt: Date; + maxParticipants: number; + gameCount: number; + gameMode: MatchGameMode; + tournamentCodeIds: mongoose.Types.ObjectId[]; + gameSchedule: MatchGameAssignment[]; + teamBuiltAt?: Date; + hostUserId?: mongoose.Types.ObjectId; + participants: MatchParticipant[]; + teams: MatchTeam[]; + result?: MatchResult; + resultPollState?: MatchResultPollState; + legacyImported?: boolean; + legacyImportSource?: { + importedAt: Date; + importedBy: mongoose.Types.ObjectId; + triggeringPuuid: string; + jobId?: string; + }; + prizes: string[]; + notices: string[]; + schedule: MatchScheduleItem[]; +} + +export type MatchDocument = HydratedDocument<Match>; + +// ===== Legacy Import Job ===== +export interface LegacyImportJobMemberOverride { + userId: mongoose.Types.ObjectId; + puuid: string; + startTime: Date; + endTime: Date; +} + +export interface LegacyImportJobError { + puuid?: string; + matchId?: string; + reason: string; + at: Date; +} + +export interface LegacyImportJob extends MongooseTimestamps { + jobId: string; + communityId: mongoose.Types.ObjectId; + triggeredBy: mongoose.Types.ObjectId; + startedAt: Date; + completedAt?: Date; + state: LegacyImportJobState; + windowStart: Date; + windowEnd: Date; + memberOverrides: LegacyImportJobMemberOverride[]; + totalMembers: number; + processedMembers: number; + totalDiscoveredMatchIds: number; + totalImportedMatches: number; + totalSkippedDuplicate: number; + totalErrors: number; + averageMemberSeconds?: number; + errors?: LegacyImportJobError[]; +} + +export type LegacyImportJobDocument = HydratedDocument<LegacyImportJob>; + +// ===== Auction (경매) ===== +export interface AuctionPlayerSnapshot { + userId: mongoose.Types.ObjectId; + // 표시용 스냅샷(대표 RiotAccount 기준) — RiotAccount에서 복사 + puuid: string; + gameName: string; + tagLine: string; + profileIconId: number; + preferredPositions: PositionPreference[]; + tiers: TierInfo[]; + masteries: ChampionMasteryEntry[]; + recentStatsSummary?: RiotRecentStatsSummary; + passCount: number; +} + +export interface AuctionTeamMember { + userId: mongoose.Types.ObjectId; + assignedPosition: Position; + cost: number; +} + +export interface AuctionTeam { + teamIndex: number; + captainUserId: mongoose.Types.ObjectId; + points: number; + members: AuctionTeamMember[]; +} + +export interface AuctionBid { + teamIndex: number; + amount: number; + bidderUserId: mongoose.Types.ObjectId; + timestamp: Date; +} + +export interface Auction extends MongooseTimestamps { + matchId: mongoose.Types.ObjectId; + phase: AuctionPhase; + teams: AuctionTeam[]; + order: AuctionPlayerSnapshot[]; + passedQueue: AuctionPlayerSnapshot[]; + auctionRound: number; + currentIndex: number; + bids: AuctionBid[]; + currentBid: number; + currentBidderTeamIndex?: number; + maxTimerSeconds: number; + bidExtensionSeconds: number; + currentDeadline?: Date; + initialPoints: number; + topTierThreshold: AuctionTopTierThreshold; + manualTopTierUserIds: mongoose.Types.ObjectId[]; + startedAt?: Date; + completedAt?: Date; +} + +export type AuctionDocument = HydratedDocument<Auction>; + +export interface AuctionHistoryEntry extends MongooseTimestamps { + auctionId: mongoose.Types.ObjectId; + matchId: mongoose.Types.ObjectId; + sequence: number; + action: AuctionActionType; + actorUserId?: mongoose.Types.ObjectId; + snapshotBefore: Record<string, unknown>; + takenAt: Date; +} + +export type AuctionHistoryEntryDocument = HydratedDocument<AuctionHistoryEntry>; + +// ===== MatchStats (precomputed internal stats fragment) ===== +export interface MatchStatsRecord { + matchId: string; + title: string; + scheduledAt: Date; + gameNumber: number; + riotMatchId: string; + gameCreation: number; + gameDuration: number; + userId: string; + summonerName: string; + profileIconId: number; + teamIndex: number; + side: TeamSide; + championId: number; + championLevel: number; + teamPosition: string; + kills: number; + deaths: number; + assists: number; + win: boolean; + cs: number; + csPerMin: number; + gold: number; + gpm: number; + damageDealtToChampions: number; + damageTaken: number; + damageShare: number; + killParticipation: number; + visionScore: number; + controlWardsBought: number; + wardsPlaced: number; + wardsKilled: number; + items: number[]; + trinket: number; + summonerSpells: [number, number]; + perks: { primaryStyle?: number; subStyle?: number; keystone?: number }; + rating: number; +} + +export interface MatchStatsGameDetail { + gameNumber: number; + riotMatchId: string; + gameCreation: number; + gameDuration: number; + winnerSide: TeamSide; + teams: Array<{ + teamIndex: number; + side: TeamSide; + win: boolean; + objectives: { baron: number; dragon: number; tower: number }; + participants: MatchStatsRecord[]; + }>; +} + +export interface MatchStats extends MongooseTimestamps { + matchId: mongoose.Types.ObjectId; + communityId: mongoose.Types.ObjectId; + scheduledAt: Date; + gameCreationMin: number; + gameCreationMax: number; + userIds: mongoose.Types.ObjectId[]; + championIds: number[]; + matchCard: Record<string, unknown>; + records: MatchStatsRecord[]; + gameDetails: MatchStatsGameDetail[]; + builtAt: Date; + builderVersion: number; +} + +export type MatchStatsDocument = HydratedDocument<MatchStats>; + +// ===== Sub-schemas ===== +const PositionPreferenceSubSchema = new Schema<PositionPreference>( + { + position: { type: String, enum: POSITIONS, required: true }, + priority: { type: Number, required: true, min: 1 }, + }, + { _id: false }, +); + +const TierInfoSubSchema = new Schema<TierInfo>( + { + leagueId: { type: String }, + queueType: { type: String, required: true }, + tier: { type: String, required: true }, + rank: { type: String, required: true, default: '' }, + puuid: { type: String }, + leaguePoints: { type: Number, required: true, default: 0 }, + wins: { type: Number, required: true, default: 0 }, + losses: { type: Number, required: true, default: 0 }, + veteran: { type: Boolean }, + inactive: { type: Boolean }, + freshBlood: { type: Boolean }, + hotStreak: { type: Boolean }, + miniSeries: { type: Schema.Types.Mixed }, + fetchedAt: { type: Date, required: true, default: Date.now }, + }, + { _id: false }, +); + +const TierHistoryEntrySubSchema = new Schema<TierHistoryEntry>( + { + queueType: { type: String, required: true }, + tier: { type: String, required: true }, + rank: { type: String }, + leaguePoints: { type: Number }, + wins: { type: Number }, + losses: { type: Number }, + recordedAt: { type: Date, required: true }, + season: { type: String }, + peakTier: { type: String }, + peakRank: { type: String }, + }, + { _id: false }, +); + +const ChampionMasteryEntrySubSchema = new Schema<ChampionMasteryEntry>( + { + puuid: { type: String, required: true }, + championId: { type: Number, required: true }, + championLevel: { type: Number, required: true, default: 0 }, + championPoints: { type: Number, required: true, default: 0 }, + lastPlayTime: { type: Number }, + championPointsSinceLastLevel: { type: Number }, + championPointsUntilNextLevel: { type: Number }, + markRequiredForNextLevel: { type: Number }, + tokensEarned: { type: Number }, + championSeasonMilestone: { type: Number }, + milestoneGrades: { type: [String] }, + nextSeasonMilestone: { type: Schema.Types.Mixed }, + }, + { _id: false }, +); + +const RiotLaneStatsSubSchema = new Schema<RiotLaneStats>( + { + games: { type: Number, required: true, default: 0 }, + wins: { type: Number, required: true, default: 0 }, + losses: { type: Number, required: true, default: 0 }, + avgKills: { type: Number, required: true, default: 0 }, + avgDeaths: { type: Number, required: true, default: 0 }, + avgAssists: { type: Number, required: true, default: 0 }, + avgCs: { type: Number, required: true, default: 0 }, + avgCsPerMin: { type: Number, required: true, default: 0 }, + mostPlayedChampionIds: { type: [Number], required: true, default: [] }, + }, + { _id: false }, +); + +const RiotRecentStatsByLaneSubSchema = new Schema< + Partial<Record<RiotLane, RiotLaneStats>> +>( + { + TOP: { type: RiotLaneStatsSubSchema, required: false }, + JUNGLE: { type: RiotLaneStatsSubSchema, required: false }, + MIDDLE: { type: RiotLaneStatsSubSchema, required: false }, + BOTTOM: { type: RiotLaneStatsSubSchema, required: false }, + UTILITY: { type: RiotLaneStatsSubSchema, required: false }, + }, + { _id: false }, +); + +const RiotRecentStatsSummarySubSchema = new Schema<RiotRecentStatsSummary>( + { + overall: { + type: RiotLaneStatsSubSchema, + required: true, + default: createEmptyRiotLaneStats, + }, + byLane: { + type: RiotRecentStatsByLaneSubSchema, + required: true, + default: () => ({}), + }, + }, + { _id: false }, +); + +const RiotAccountSubSchema = new Schema<RiotAccount>( + { + puuid: { type: String, required: true }, + gameName: { type: String, required: true }, + tagLine: { type: String, required: true }, + profileIconId: { type: Number, required: true, default: 0 }, + revisionDate: { type: Number, required: true, default: 0 }, + summonerLevel: { type: Number, required: true, default: 0 }, + isPrimary: { type: Boolean, required: true, default: false }, + + tiers: { type: [TierInfoSubSchema], required: true, default: [] }, + tierHistory: { + type: [TierHistoryEntrySubSchema], + required: true, + default: [], + }, + + matchIds: { type: [String], required: true, default: [] }, + matchIdsFetchedAt: { type: Date }, + matchIdsByQueueFetchedAt: { type: Map, of: Date }, + lastRefreshRequestedAt: { type: Date }, + + recentStatsSummary: { type: RiotRecentStatsSummarySubSchema }, + recentStatsFetchedAt: { type: Date }, + + championMasteries: { + type: [ChampionMasteryEntrySubSchema], + required: true, + default: [], + }, + championMasteriesFetchedAt: { type: Date }, + + championRecords: { + type: Schema.Types.Mixed, + required: true, + default: () => ({}), + }, + championRecordsFetchedAt: { type: Date }, + }, + { + _id: false, + timestamps: true, + }, +); + +export const IdentitySchema = new Schema<Identity>( + { + entraOid: { type: String, required: true }, + tenantId: { type: String }, + email: { type: String }, + displayName: { type: String }, + lastSignInAt: { type: Date }, + }, + { + timestamps: true, + collection: 'identity', + }, +); + +IdentitySchema.index({ entraOid: 1 }, { unique: true }); +IdentitySchema.index({ email: 1 }); + +export const CommunitySchema = new Schema<Community>( + { + name: { type: String, required: true }, + description: { type: String, required: true, default: '' }, + ownerUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + dakaoProjectId: { type: Schema.Types.ObjectId }, + riotPlatform: { type: String }, + riotRegion: { type: String }, + seasonStartedAt: { type: Date }, + matchGameCount: { type: Number, required: false, default: 3 }, + seasons: { + type: [ + new Schema<CommunitySeason>( + { + id: { type: String, required: true }, + label: { type: String, required: true }, + startedAt: { type: Date, required: true }, + endedAt: { type: Date }, + }, + { _id: false }, + ), + ], + required: false, + }, + }, + { + timestamps: true, + collection: 'community', + }, +); + +CommunitySchema.index({ name: 1 }, { unique: true }); +CommunitySchema.index({ ownerUserId: 1 }); +CommunitySchema.index({ dakaoProjectId: 1 }, { unique: true, sparse: true }); + +export const UserSchema = new Schema<User>( + { + communityId: { + type: Schema.Types.ObjectId, + ref: 'community', + required: true, + }, + identityId: { type: Schema.Types.ObjectId, ref: 'identity' }, + role: { + type: String, + enum: COMMUNITY_ROLES, + required: true, + default: 'member', + }, + kakaoTalkId: { type: String }, + kakaoTalkNickname: { type: String }, + discordId: { type: String }, + discordNickname: { type: String }, + riotAccounts: { + type: [RiotAccountSubSchema], + required: true, + default: [], + validate: [ + { + validator: function (accounts: RiotAccount[]) { + const puuids = accounts.map((a) => a.puuid); + return puuids.length === new Set(puuids).size; + }, + message: 'Duplicate Riot puuid within a user is not allowed.', + }, + { + validator: function (accounts: RiotAccount[]) { + if (accounts.length === 0) return true; + const primaryCount = accounts.filter((a) => a.isPrimary).length; + return primaryCount === 1; + }, + message: + 'Exactly one Riot account must be marked as primary when accounts exist.', + }, + ], + }, + preferredPositions: { + type: [PositionPreferenceSubSchema], + required: true, + default: [], + }, + isActivated: { type: Boolean, required: true, default: true }, + }, + { + timestamps: true, + collection: 'user', + }, +); + +UserSchema.index({ communityId: 1 }); +UserSchema.index({ communityId: 1, role: 1 }); +UserSchema.index( + { communityId: 1, identityId: 1 }, + { + unique: true, + partialFilterExpression: { identityId: { $type: 'objectId' } }, + }, +); +UserSchema.index( + { identityId: 1 }, + { partialFilterExpression: { identityId: { $type: 'objectId' } } }, +); +UserSchema.index( + { communityId: 1, kakaoTalkId: 1 }, + { + unique: true, + partialFilterExpression: { kakaoTalkId: { $type: 'string' } }, + }, +); +UserSchema.index( + { communityId: 1, discordId: 1 }, + { + unique: true, + partialFilterExpression: { discordId: { $type: 'string' } }, + }, +); +UserSchema.index( + { communityId: 1, 'riotAccounts.puuid': 1 }, + { + unique: true, + partialFilterExpression: { 'riotAccounts.puuid': { $type: 'string' } }, + }, +); + +export const RiotMatchSchema = new Schema<RiotMatch>( + { + matchId: { type: String, required: true, unique: true }, + gameCreation: { type: Number, required: true, default: 0 }, + gameDuration: { type: Number, required: true, default: 0 }, + gameVersion: { type: String }, + queueId: { type: Number }, + participantPuuids: { type: [String], required: true, default: [] }, + raw: { type: Schema.Types.Mixed, required: true, default: () => ({}) }, + fetchedAt: { type: Date, required: true, default: Date.now }, + }, + { + timestamps: true, + collection: 'riot_match', + }, +); + +RiotMatchSchema.index({ participantPuuids: 1, gameCreation: -1 }); +RiotMatchSchema.index({ participantPuuids: 1, queueId: 1, gameCreation: -1 }); +RiotMatchSchema.index({ 'raw.info.tournamentCode': 1 }, { sparse: true }); + +export const TournamentCodeSchema = new Schema<TournamentCode>( + { + code: { + type: String, + required: true, + trim: true, + uppercase: true, + }, + region: { type: String, required: true, default: 'kr' }, + mapType: { + type: String, + enum: TOURNAMENT_MAP_TYPES, + required: true, + default: 'SUMMONERS_RIFT', + }, + teamSize: { type: Number, required: true, default: 5 }, + spectatorType: { + type: String, + enum: TOURNAMENT_SPECTATOR_TYPES, + required: true, + default: 'ALL', + }, + metadata: { type: Schema.Types.Mixed }, + expiresAt: { type: Date, required: true }, + state: { + type: String, + enum: TOURNAMENT_CODE_STATES, + required: true, + default: 'available', + }, + matchId: { type: Schema.Types.ObjectId, ref: 'match' }, + gameNumber: { type: Number }, + reservedAt: { type: Date }, + usedAt: { type: Date }, + releasedAt: { type: Date }, + riotMatchId: { type: String }, + uploadedBy: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + uploadedAt: { type: Date, required: true, default: Date.now }, + source: { type: String }, + notes: { type: String }, + }, + { + timestamps: true, + collection: 'tournament_code', + }, +); + +TournamentCodeSchema.index({ code: 1 }, { unique: true }); +TournamentCodeSchema.index({ state: 1, expiresAt: 1 }); +TournamentCodeSchema.index({ matchId: 1, gameNumber: 1 }); +TournamentCodeSchema.index({ state: 1, uploadedAt: -1 }); +TournamentCodeSchema.index({ riotMatchId: 1 }, { sparse: true }); + +const MatchParticipantSubSchema = new Schema<MatchParticipant>( + { + userId: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + puuid: { type: String }, + gameName: { type: String }, + tagLine: { type: String }, + profileIconId: { type: Number }, + tier: { type: TierInfoSubSchema }, + preferredPositions: { + type: [PositionPreferenceSubSchema], + required: true, + default: [], + }, + joinedAt: { type: Date, required: true, default: Date.now }, + excluded: { type: Boolean, required: true, default: false }, + }, + { _id: false }, +); + +const MatchTeamMemberSubSchema = new Schema<MatchTeamMember>( + { + userId: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + assignedPosition: { type: String, enum: POSITIONS, required: true }, + cost: { type: Number, required: false }, + }, + { _id: false }, +); + +const MatchTeamSubSchema = new Schema<MatchTeam>( + { + teamIndex: { type: Number, required: true, min: 1 }, + name: { type: String, required: true }, + captainUserId: { type: Schema.Types.ObjectId, ref: 'user' }, + members: { type: [MatchTeamMemberSubSchema], required: true, default: [] }, + }, + { _id: false }, +); + +const MatchGameResultParticipantSubSchema = + new Schema<MatchGameResultParticipant>( + { + userId: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + teamIndex: { type: Number, required: true, min: 1 }, + side: { type: String, enum: TEAM_SIDES, required: true }, + championId: { type: Number, required: true }, + kills: { type: Number, required: true, default: 0 }, + deaths: { type: Number, required: true, default: 0 }, + assists: { type: Number, required: true, default: 0 }, + win: { type: Boolean, required: true, default: false }, + }, + { _id: false }, + ); + +const MatchGameResultSubSchema = new Schema<MatchGameResult>( + { + gameNumber: { type: Number, required: true }, + riotMatchId: { type: String, required: true }, + blueTeamIndex: { type: Number, required: true, min: 1 }, + redTeamIndex: { type: Number, required: true, min: 1 }, + winnerTeamIndex: { type: Number, required: true, min: 1 }, + winnerSide: { type: String, enum: TEAM_SIDES, required: true }, + gameCreation: { type: Number, required: true }, + gameDuration: { type: Number, required: true }, + participants: { + type: [MatchGameResultParticipantSubSchema], + required: true, + default: [], + }, + }, + { _id: false }, +); + +const MatchSeriesResultSubSchema = new Schema<MatchSeriesResult>( + { + teamAIndex: { type: Number, required: true, min: 1 }, + teamBIndex: { type: Number, required: true, min: 1 }, + teamAWins: { type: Number, required: true, default: 0 }, + teamBWins: { type: Number, required: true, default: 0 }, + winnerTeamIndex: { type: Number, required: false, min: 1 }, + }, + { _id: false }, +); + +const MatchGameAssignmentSubSchema = new Schema<MatchGameAssignment>( + { + gameNumber: { type: Number, required: true, min: 1 }, + teamAIndex: { type: Number, required: true, min: 1 }, + teamBIndex: { type: Number, required: true, min: 1 }, + }, + { _id: false }, +); + +const MatchResultSubSchema = new Schema<MatchResult>( + { + winnerTeamIndex: { type: Number, required: false, min: 1 }, + mvpUserId: { type: Schema.Types.ObjectId, ref: 'user' }, + riotMatchIds: { type: [String], required: true, default: [] }, + games: { type: [MatchGameResultSubSchema], required: true, default: [] }, + seriesResults: { + type: [MatchSeriesResultSubSchema], + required: true, + default: [], + }, + }, + { _id: false }, +); + +const MatchResultPollStateSubSchema = new Schema<MatchResultPollState>( + { + startedAt: { type: Date, required: true }, + lastPolledAt: { type: Date }, + lastPollOutcome: { + type: String, + enum: ['matched', 'not-found', 'failed'], + required: false, + }, + matchedCount: { type: Number, required: true, default: 0 }, + expandedToAllPuuids: { type: Boolean, required: true, default: false }, + state: { + type: String, + enum: MATCH_RESULT_POLL_STATES, + required: true, + default: 'polling', + }, + }, + { _id: false }, +); + +const MatchScheduleItemSubSchema = new Schema<MatchScheduleItem>( + { + time: { type: String, required: true }, + label: { type: String, required: true }, + }, + { _id: false }, +); + +const MatchLegacyImportSourceSubSchema = new Schema< + NonNullable<Match['legacyImportSource']> +>( + { + importedAt: { type: Date, required: true }, + importedBy: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + triggeringPuuid: { type: String, required: true }, + jobId: { type: String }, + }, + { _id: false }, +); + +export const MatchSchema = new Schema<Match>( + { + communityId: { + type: Schema.Types.ObjectId, + ref: 'community', + required: true, + }, + shortCode: { + type: String, + required: false, + trim: true, + uppercase: true, + minlength: 4, + maxlength: 6, + }, + title: { type: String, required: true }, + description: { type: String, required: false, default: '' }, + mapId: { type: Number, required: true, default: 11 }, + status: { + type: String, + enum: MATCH_STATUSES, + required: true, + default: 'recruiting', + }, + scheduledAt: { type: Date, required: true }, + maxParticipants: { + type: Number, + required: true, + default: 10, + min: 10, + validate: { + validator: (value: number) => + Number.isInteger(value) && value % 10 === 0, + message: 'maxParticipants must be a multiple of 10.', + }, + }, + gameCount: { type: Number, required: true, default: 3 }, + gameMode: { + type: String, + enum: MATCH_GAME_MODES, + required: true, + default: 'pair', + }, + tournamentCodeIds: { + type: [{ type: Schema.Types.ObjectId, ref: 'tournamentCode' }], + required: true, + default: [], + }, + gameSchedule: { + type: [MatchGameAssignmentSubSchema], + required: true, + default: [], + }, + teamBuiltAt: { type: Date }, + hostUserId: { type: Schema.Types.ObjectId, ref: 'user' }, + participants: { + type: [MatchParticipantSubSchema], + required: true, + default: [], + validate: { + validator: function (parts: MatchParticipant[]) { + const ids = parts.map((p) => p.userId.toString()); + return ids.length === new Set(ids).size; + }, + message: 'Duplicate participants are not allowed.', + }, + }, + teams: { type: [MatchTeamSubSchema], required: true, default: [] }, + result: { type: MatchResultSubSchema, required: false }, + resultPollState: { + type: MatchResultPollStateSubSchema, + required: false, + }, + legacyImported: { type: Boolean, required: false, default: false }, + legacyImportSource: { + type: MatchLegacyImportSourceSubSchema, + required: false, + }, + prizes: { type: [String], required: true, default: [] }, + notices: { type: [String], required: true, default: [] }, + schedule: { + type: [MatchScheduleItemSubSchema], + required: true, + default: [], + }, + }, + { + timestamps: true, + collection: 'match', + }, +); + +MatchSchema.index({ communityId: 1, scheduledAt: -1 }); +MatchSchema.index({ communityId: 1, status: 1, scheduledAt: -1 }); +MatchSchema.index( + { communityId: 1, shortCode: 1 }, + { + unique: true, + partialFilterExpression: { shortCode: { $type: 'string' } }, + }, +); +MatchSchema.index({ legacyImported: 1, scheduledAt: -1 }); +MatchSchema.index({ 'result.games.riotMatchId': 1 }, { sparse: true }); +MatchSchema.index({ title: 'text', description: 'text' }); + +const LegacyImportJobMemberOverrideSubSchema = + new Schema<LegacyImportJobMemberOverride>( + { + userId: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + puuid: { type: String, required: true }, + startTime: { type: Date, required: true }, + endTime: { type: Date, required: true }, + }, + { _id: false }, + ); + +const LegacyImportJobErrorSubSchema = new Schema<LegacyImportJobError>( + { + puuid: { type: String }, + matchId: { type: String }, + reason: { type: String, required: true }, + at: { type: Date, required: true, default: Date.now }, + }, + { _id: false }, +); + +export const LegacyImportJobSchema = new Schema<LegacyImportJob>( + { + jobId: { type: String, required: true, unique: true }, + communityId: { + type: Schema.Types.ObjectId, + ref: 'community', + required: true, + }, + triggeredBy: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + startedAt: { type: Date, required: true, default: Date.now }, + completedAt: { type: Date }, + state: { + type: String, + enum: LEGACY_IMPORT_JOB_STATES, + required: true, + default: 'pending', + }, + windowStart: { type: Date, required: true }, + windowEnd: { type: Date, required: true }, + memberOverrides: { + type: [LegacyImportJobMemberOverrideSubSchema], + required: true, + default: [], + }, + totalMembers: { type: Number, required: true, default: 0 }, + processedMembers: { type: Number, required: true, default: 0 }, + totalDiscoveredMatchIds: { type: Number, required: true, default: 0 }, + totalImportedMatches: { type: Number, required: true, default: 0 }, + totalSkippedDuplicate: { type: Number, required: true, default: 0 }, + totalErrors: { type: Number, required: true, default: 0 }, + averageMemberSeconds: { type: Number }, + errors: { + type: [LegacyImportJobErrorSubSchema], + required: true, + default: [], + }, + }, + { + timestamps: true, + collection: 'legacy_import_job', + suppressReservedKeysWarning: true, + }, +); + +LegacyImportJobSchema.index({ communityId: 1, startedAt: -1 }); +LegacyImportJobSchema.index({ communityId: 1, state: 1 }); + +const AuctionPlayerSnapshotSubSchema = new Schema<AuctionPlayerSnapshot>( + { + userId: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + puuid: { type: String, required: true }, + gameName: { type: String, required: true }, + tagLine: { type: String, required: true }, + profileIconId: { type: Number, required: true, default: 0 }, + preferredPositions: { + type: [PositionPreferenceSubSchema], + required: true, + default: [], + }, + tiers: { type: [TierInfoSubSchema], required: true, default: [] }, + masteries: { + type: [ChampionMasteryEntrySubSchema], + required: true, + default: [], + }, + recentStatsSummary: { type: RiotRecentStatsSummarySubSchema }, + passCount: { type: Number, required: true, default: 0 }, + }, + { _id: false }, +); + +const AuctionTeamMemberSubSchema = new Schema<AuctionTeamMember>( + { + userId: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + assignedPosition: { type: String, enum: POSITIONS, required: true }, + cost: { type: Number, required: true, default: 0 }, + }, + { _id: false }, +); + +const AuctionTeamSubSchema = new Schema<AuctionTeam>( + { + teamIndex: { type: Number, required: true, min: 1 }, + captainUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + points: { type: Number, required: true, default: 0 }, + members: { + type: [AuctionTeamMemberSubSchema], + required: true, + default: [], + }, + }, + { _id: false }, +); + +const AuctionBidSubSchema = new Schema<AuctionBid>( + { + teamIndex: { type: Number, required: true, min: 1 }, + amount: { type: Number, required: true }, + bidderUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true }, + timestamp: { type: Date, required: true, default: Date.now }, + }, + { _id: false }, +); + +export const AuctionSchema = new Schema<Auction>( + { + matchId: { + type: Schema.Types.ObjectId, + ref: 'match', + required: true, + unique: true, + }, + phase: { + type: String, + enum: AUCTION_PHASES, + required: true, + default: 'waiting', + }, + teams: { type: [AuctionTeamSubSchema], required: true, default: [] }, + order: { + type: [AuctionPlayerSnapshotSubSchema], + required: true, + default: [], + }, + passedQueue: { + type: [AuctionPlayerSnapshotSubSchema], + required: true, + default: [], + }, + auctionRound: { type: Number, required: true, default: 1, min: 1 }, + currentIndex: { type: Number, required: true, default: 0 }, + bids: { type: [AuctionBidSubSchema], required: true, default: [] }, + currentBid: { type: Number, required: true, default: 0 }, + currentBidderTeamIndex: { type: Number, required: false, min: 1 }, + maxTimerSeconds: { type: Number, required: true, default: 30 }, + bidExtensionSeconds: { type: Number, required: true, default: 5, min: 0 }, + currentDeadline: { type: Date, required: false }, + initialPoints: { type: Number, required: true, default: 1000 }, + topTierThreshold: { + type: String, + enum: AUCTION_TOP_TIER_THRESHOLDS, + required: true, + default: 'NONE', + }, + manualTopTierUserIds: { + type: [Schema.Types.ObjectId], + ref: 'user', + required: true, + default: [], + }, + startedAt: { type: Date, required: false }, + completedAt: { type: Date, required: false }, + }, + { + timestamps: true, + collection: 'auction', + }, +); + +const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>( + { + auctionId: { + type: Schema.Types.ObjectId, + ref: 'auction', + required: true, + index: true, + }, + matchId: { + type: Schema.Types.ObjectId, + ref: 'match', + required: true, + index: true, + }, + sequence: { type: Number, required: true, min: 1 }, + action: { + type: String, + enum: [ + 'start', + 'open-bidding', + 'bid', + 'sold', + 'passed', + 'next', + 'complete', + ], + required: true, + }, + actorUserId: { type: Schema.Types.ObjectId, ref: 'user', required: false }, + snapshotBefore: { type: Schema.Types.Mixed, required: true }, + takenAt: { type: Date, required: true, default: Date.now }, + }, + { + timestamps: true, + collection: 'auctionHistory', + }, +); + +AuctionHistoryEntrySchema.index({ auctionId: 1, sequence: -1 }); + +export const MatchStatsSchema: mongoose.Schema<MatchStats> = new Schema( + { + matchId: { + type: Schema.Types.ObjectId, + ref: 'match', + required: true, + unique: true, + }, + communityId: { + type: Schema.Types.ObjectId, + ref: 'community', + required: true, + }, + scheduledAt: { type: Date, required: true }, + gameCreationMin: { type: Number, required: true }, + gameCreationMax: { type: Number, required: true }, + userIds: { type: [Schema.Types.ObjectId], required: true, default: [] }, + championIds: { type: [Number], required: true, default: [] }, + matchCard: { type: Schema.Types.Mixed, required: true }, + records: { type: [Schema.Types.Mixed], required: true, default: [] }, + gameDetails: { type: [Schema.Types.Mixed], required: true, default: [] }, + builtAt: { type: Date, required: true, default: Date.now }, + builderVersion: { type: Number, required: true, default: 1 }, + } as mongoose.SchemaDefinition, + { timestamps: true, collection: 'match_stats' }, +) as mongoose.Schema<MatchStats>; + +MatchStatsSchema.index({ communityId: 1, scheduledAt: -1 }); +MatchStatsSchema.index({ + communityId: 1, + gameCreationMin: 1, + gameCreationMax: 1, +}); +MatchStatsSchema.index({ communityId: 1, userIds: 1 }); +MatchStatsSchema.index({ communityId: 1, championIds: 1 }); + +export const tournamentModelDefinitions: ModelDefinition[] = [ + { name: 'identity', schema: IdentitySchema, schemaName: 'identity' }, + { name: 'community', schema: CommunitySchema, schemaName: 'community' }, + { name: 'user', schema: UserSchema, schemaName: 'user' }, + { name: 'riotMatch', schema: RiotMatchSchema, schemaName: 'riotMatch' }, + { + name: 'tournamentCode', + schema: TournamentCodeSchema, + schemaName: 'tournamentCode', + }, + { + name: 'legacyImportJob', + schema: LegacyImportJobSchema, + schemaName: 'legacyImportJob', + }, + { name: 'match', schema: MatchSchema, schemaName: 'match' }, + { name: 'auction', schema: AuctionSchema, schemaName: 'auction' }, + { + name: 'auctionHistory', + schema: AuctionHistoryEntrySchema, + schemaName: 'auctionHistory', + }, + { + name: 'matchStats', + schema: MatchStatsSchema, + schemaName: 'matchStats', + }, +]; + +export interface TournamentModelTypes { + identity: Identity; + community: Community; + user: User; + riotMatch: RiotMatch; + tournamentCode: TournamentCode; + legacyImportJob: LegacyImportJob; + match: Match; + auction: Auction; + auctionHistory: AuctionHistoryEntry; + matchStats: MatchStats; +} diff --git a/apps/api/src/modules/global/database/types.d.ts b/apps/api/src/modules/global/database/types.d.ts new file mode 100644 index 00000000..748932f3 --- /dev/null +++ b/apps/api/src/modules/global/database/types.d.ts @@ -0,0 +1,5 @@ +export interface ModelDefinition { + name: string; + schema: mongoose.Schema<any>; + schemaName?: string; +} diff --git a/apps/api/src/modules/global/garnet/garnet.constants.ts b/apps/api/src/modules/global/garnet/garnet.constants.ts new file mode 100644 index 00000000..842616cb --- /dev/null +++ b/apps/api/src/modules/global/garnet/garnet.constants.ts @@ -0,0 +1 @@ +export const GARNET_CLIENT = Symbol('GARNET_CLIENT'); diff --git a/apps/api/src/modules/global/garnet/garnet.module.ts b/apps/api/src/modules/global/garnet/garnet.module.ts new file mode 100644 index 00000000..8bc9ec66 --- /dev/null +++ b/apps/api/src/modules/global/garnet/garnet.module.ts @@ -0,0 +1,57 @@ +import { Logger, Module } from '@nestjs/common'; +import { createClient } from 'redis'; +import { SecretModule } from '../secret/secret.module'; +import { SecretService } from '../secret/secret.service'; +import { GARNET_CLIENT } from './garnet.constants'; +import { GarnetService, type GarnetRedisClient } from './garnet.service'; + +function normalizeGarnetUrl(endpoint: string): string { + const trimmed = endpoint.trim(); + if (!trimmed) { + throw new Error('GARNET_ENDPOINT cannot be empty'); + } + if (/^redis(s)?:\/\//iu.test(trimmed)) { + return trimmed; + } + return `redis://${trimmed}`; +} + +@Module({ + imports: [SecretModule], + providers: [ + { + provide: GARNET_CLIENT, + inject: [SecretService], + useFactory: async ( + secretService: SecretService, + ): Promise<GarnetRedisClient> => { + await secretService.ready(); + + const [endpoint, username, password] = await Promise.all([ + secretService.get<string>('GARNET_ENDPOINT'), + secretService.get<string>('GARNET_USERID'), + secretService.get<string>('GARNET_PASSWORD'), + ]); + + const logger = new Logger('GarnetClient'); + const client = createClient({ + url: normalizeGarnetUrl(endpoint), + username, + password, + }); + + client.on('error', (error) => { + logger.error(`Garnet client error: ${error?.message ?? error}`); + }); + + await client.connect(); + logger.log('Garnet connected'); + + return client; + }, + }, + GarnetService, + ], + exports: [GarnetService, GARNET_CLIENT], +}) +export class GarnetModule {} diff --git a/apps/api/src/modules/global/garnet/garnet.service.ts b/apps/api/src/modules/global/garnet/garnet.service.ts new file mode 100644 index 00000000..c79b30d9 --- /dev/null +++ b/apps/api/src/modules/global/garnet/garnet.service.ts @@ -0,0 +1,213 @@ +import { + Inject, + Injectable, + Logger, + OnModuleDestroy, +} from '@nestjs/common'; +import type { createClient } from 'redis'; +import { GARNET_CLIENT } from './garnet.constants'; + +export type GarnetRedisClient = ReturnType<typeof createClient>; + +export interface StreamEntry { + deliveriesCounter?: number; + id: string; + message: Record<string, string>; + millisElapsedFromDelivery?: number; +} + +export interface StreamReadResult { + messages: StreamEntry[]; + name: string; +} + +interface RawStreamMessage { + deliveriesCounter?: number; + id: string; + message: unknown; + millisElapsedFromDelivery?: number; +} + +interface RawStreamReadResult { + messages: RawStreamMessage[]; + name: string; +} + +function normalizeStreamFields(fields: unknown): Record<string, string> { + if (fields instanceof Map) { + return Object.fromEntries( + [...fields.entries()].map(([key, value]) => [String(key), String(value)]), + ); + } + + if (typeof fields === 'object' && fields !== null) { + return Object.fromEntries( + Object.entries(fields as Record<string, unknown>).map(([key, value]) => [ + key, + String(value), + ]), + ); + } + + return {}; +} + +@Injectable() +export class GarnetService implements OnModuleDestroy { + private readonly logger = new Logger(GarnetService.name); + + constructor( + @Inject(GARNET_CLIENT) private readonly client: GarnetRedisClient, + ) {} + + async onModuleDestroy() { + try { + await this.client.quit(); + } catch { + this.client.destroy(); + } + } + + public raw(): GarnetRedisClient { + return this.client; + } + + public async getJson<T>(key: string): Promise<T | null> { + const raw = await this.client.get(key); + if (!raw) return null; + try { + return JSON.parse(raw) as T; + } catch (err) { + this.logger.warn( + `Failed to JSON.parse cached value for key="${key}": ${ + (err as Error)?.message ?? err + }`, + ); + return null; + } + } + + public async setJson( + key: string, + value: unknown, + ttlSeconds?: number, + ): Promise<void> { + const serialized = JSON.stringify(value); + if (ttlSeconds && ttlSeconds > 0) { + await this.client.set(key, serialized, { EX: ttlSeconds }); + return; + } + await this.client.set(key, serialized); + } + + public async getString(key: string): Promise<string | null> { + return this.client.get(key); + } + + public async setString( + key: string, + value: string, + ttlSeconds?: number, + ): Promise<void> { + if (ttlSeconds && ttlSeconds > 0) { + await this.client.set(key, value, { EX: ttlSeconds }); + return; + } + await this.client.set(key, value); + } + + public async setStringIfAbsent( + key: string, + value: string, + ttlSeconds?: number, + ): Promise<boolean> { + const options: { EX?: number; NX: true } = { NX: true }; + if (ttlSeconds && ttlSeconds > 0) options.EX = ttlSeconds; + const result = await this.client.set(key, value, options); + return result === 'OK'; + } + + public async delete(key: string): Promise<number> { + return this.client.del(key); + } + + public async appendStream( + key: string, + fields: Record<string, string>, + options?: { maxLen?: number }, + ): Promise<string> { + const trimOptions = + options?.maxLen && options.maxLen > 0 + ? { + TRIM: { + strategy: 'MAXLEN' as const, + strategyModifier: '~' as const, + threshold: options.maxLen, + }, + } + : undefined; + + return this.client.xAdd(key, '*', fields, trimOptions); + } + + public async createConsumerGroup( + key: string, + group: string, + startId = '$', + ): Promise<void> { + await this.client.xGroupCreate(key, group, startId, { MKSTREAM: true }); + } + + public async readGroup( + key: string, + group: string, + consumer: string, + startId: string, + options?: { blockMs?: number; count?: number }, + ): Promise<StreamReadResult[] | null> { + const readOptions: { BLOCK?: number; COUNT?: number } = {}; + if (options?.blockMs !== undefined) readOptions.BLOCK = options.blockMs; + if (options?.count !== undefined) readOptions.COUNT = options.count; + + const reply = await this.client.xReadGroup( + group, + consumer, + { key, id: startId }, + readOptions, + ); + + if (!reply) return null; + + return (reply as RawStreamReadResult[]).map((stream) => ({ + name: String(stream.name), + messages: stream.messages.map((message) => ({ + deliveriesCounter: + typeof message.deliveriesCounter === 'number' + ? message.deliveriesCounter + : undefined, + id: String(message.id), + message: normalizeStreamFields(message.message), + millisElapsedFromDelivery: + typeof message.millisElapsedFromDelivery === 'number' + ? message.millisElapsedFromDelivery + : undefined, + })), + })); + } + + public async acknowledgeStream( + key: string, + group: string, + ids: string | string[], + ): Promise<number> { + return this.client.xAck(key, group, ids); + } + + public async streamLength(key: string): Promise<number> { + return this.client.xLen(key); + } + + public async publish(channel: string, message: string): Promise<number> { + return this.client.publish(channel, message); + } +} diff --git a/apps/api/src/modules/global/global.module.ts b/apps/api/src/modules/global/global.module.ts new file mode 100644 index 00000000..ba3ea479 --- /dev/null +++ b/apps/api/src/modules/global/global.module.ts @@ -0,0 +1,13 @@ +import { Module, Global } from '@nestjs/common'; +import { SecretModule } from './secret/secret.module'; +import { NatsModule } from './nats/nats.module'; +import { DatabaseModule } from './database/database.module'; +import { GarnetModule } from './garnet/garnet.module'; + +@Global() +@Module({ + imports: [SecretModule, NatsModule, DatabaseModule, GarnetModule], + providers: [], + exports: [SecretModule, NatsModule, DatabaseModule, GarnetModule], +}) +export class GlobalModule {} diff --git a/apps/api/src/modules/global/nats/nats.constants.ts b/apps/api/src/modules/global/nats/nats.constants.ts new file mode 100644 index 00000000..dd4fc9b9 --- /dev/null +++ b/apps/api/src/modules/global/nats/nats.constants.ts @@ -0,0 +1 @@ +export const NATS_CLIENT = Symbol('NATS_CLIENT'); diff --git a/apps/api/src/modules/global/nats/nats.module.ts b/apps/api/src/modules/global/nats/nats.module.ts new file mode 100644 index 00000000..79d5a32b --- /dev/null +++ b/apps/api/src/modules/global/nats/nats.module.ts @@ -0,0 +1,45 @@ +import { Module } from '@nestjs/common'; +import fs from 'fs/promises'; +import { connect } from 'nats'; +import type { NatsConnection } from 'nats'; +import { SecretModule } from '../secret/secret.module'; +import { SecretService } from '../secret/secret.service'; +import { NATS_CLIENT } from './nats.constants'; +import { NatsService } from './nats.service'; + +@Module({ + imports: [SecretModule], + providers: [ + { + provide: NATS_CLIENT, + inject: [SecretService], + useFactory: async ( + secretService: SecretService, + ): Promise<NatsConnection> => { + await secretService.ready(); + + const [endpoint, user, password, serviceName] = await Promise.all([ + secretService.get<string>('NATS_ENDPOINT'), + secretService.get<string>('NATS_USER'), + secretService.get<string>('NATS_PASS'), + secretService.get<string>('SERVICE_NAME'), + ]); + + return connect({ + name: serviceName, + servers: [endpoint], + maxReconnectAttempts: -1, + reconnectTimeWait: 500, + reconnect: true, + pingInterval: 20000, + timeout: 5000, + user, + pass: password, + }); + }, + }, + NatsService, + ], + exports: [NatsService, NATS_CLIENT], +}) +export class NatsModule {} diff --git a/apps/api/src/modules/global/nats/nats.service.ts b/apps/api/src/modules/global/nats/nats.service.ts new file mode 100644 index 00000000..eb456d00 --- /dev/null +++ b/apps/api/src/modules/global/nats/nats.service.ts @@ -0,0 +1,117 @@ +import { + Inject, + Injectable, + Logger, + OnModuleDestroy, +} from '@nestjs/common'; +import { StringCodec } from 'nats'; +import type { Msg, NatsConnection, Subscription } from 'nats'; +import { NATS_CLIENT } from './nats.constants'; + +export type NatsMessageHandler<T = unknown> = ( + data: T, + msg: Msg, +) => Promise<void> | void; + +@Injectable() +export class NatsService implements OnModuleDestroy { + private readonly logger = new Logger(NatsService.name); + private readonly stringCodec = StringCodec(); + private readonly subscriptions = new Set<Subscription>(); + + constructor( + @Inject(NATS_CLIENT) private readonly natsClient: NatsConnection, + ) {} + + async onModuleDestroy() { + for (const sub of this.subscriptions) { + try { + sub.unsubscribe(); + } catch { + // ignore + } + } + this.subscriptions.clear(); + + try { + const closed = this.natsClient.closed(); + await this.natsClient.drain(); + const err = await closed; + if (err) this.logger.error(`NATS closed with error: ${err}`); + } catch { + try { + await this.natsClient.close(); + } catch { + // ignore + } + } + } + + public async publish<T = unknown>(subject: string, data: T): Promise<void> { + this.natsClient.publish( + subject, + this.stringCodec.encode(JSON.stringify(data)), + ); + await this.natsClient.flush(); + } + + public subscribe<T = unknown>( + subject: string, + handler: NatsMessageHandler<T>, + options?: { queue?: string }, + ): Subscription { + const sub = this.natsClient.subscribe(subject, { + queue: options?.queue, + }); + this.subscriptions.add(sub); + void this.consumeSubscription(sub, handler); + return sub; + } + + private async consumeSubscription<T>( + sub: Subscription, + handler: NatsMessageHandler<T>, + ): Promise<void> { + try { + for await (const msg of sub) { + await this.dispatch(msg, handler); + } + } catch (err) { + this.logger.error( + `NATS subscription on subject="${sub.getSubject()}" terminated: ${ + (err as Error)?.message ?? err + }`, + ); + } finally { + this.subscriptions.delete(sub); + } + } + + private async dispatch<T>( + msg: Msg, + handler: NatsMessageHandler<T>, + ): Promise<void> { + let data: T; + try { + const decoded = this.stringCodec.decode(msg.data); + data = JSON.parse(decoded) as T; + } catch (err) { + this.logger.warn( + `Failed to decode NATS message on subject="${msg.subject}": ${ + (err as Error)?.message ?? err + }`, + ); + return; + } + + try { + await handler(data, msg); + } catch (err) { + this.logger.error( + `Handler error on subject="${msg.subject}": ${ + (err as Error)?.stack ?? err + }`, + ); + } + } +} diff --git a/apps/api/src/modules/global/secret/secret.module.ts b/apps/api/src/modules/global/secret/secret.module.ts new file mode 100644 index 00000000..3412bc84 --- /dev/null +++ b/apps/api/src/modules/global/secret/secret.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { SecretService } from './secret.service'; + +@Module({ + imports: [], + providers: [SecretService], + exports: [SecretService], +}) +export class SecretModule {} diff --git a/apps/api/src/modules/global/secret/secret.service.ts b/apps/api/src/modules/global/secret/secret.service.ts new file mode 100644 index 00000000..a6c28034 --- /dev/null +++ b/apps/api/src/modules/global/secret/secret.service.ts @@ -0,0 +1,243 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { InfisicalSDK } from '@infisical/sdk'; +import { ConfigService } from '@nestjs/config'; + +type SecretMap = Record<string, string | undefined>; + +const DEFAULT_INFISICAL_SITE_URL = 'https://secret-manager.dahyeon.us/'; +const IMDS_TOKEN_TTL_SECONDS = 60; +const IMDS_REQUEST_TIMEOUT_MS = 1500; +const FALLBACK_IMDS_BASE_URLS = [ + 'http://169.254.169.254', + 'http://host.docker.internal:8675', +]; + +interface InfisicalBootstrapConfig { + siteUrl: string; + clientId: string; + clientSecret: string; + projectId: string; +} + +@Injectable() +export class SecretService { + private logger = new Logger(SecretService.name); + + private secrets: SecretMap | null = null; // null = 아직 로드 전 + private loading?: Promise<void>; // 진행 중인 로드(중복 호출 방지) + + constructor(private readonly config: ConfigService) {} + + /** 한 번만 로그인/페치. 동시에 여러 번 호출돼도 같은 Promise를 공유 */ + private async fetchSecrets(): Promise<void> { + if (this.secrets) return; // 이미 로드됨 + if (this.loading) return this.loading; // 로드 중이면 그거 기다리기 + + this.loading = (async () => { + const bootstrapConfig = await this.resolveInfisicalBootstrapConfig(); + const infisicalClient = new InfisicalSDK({ + siteUrl: bootstrapConfig.siteUrl, + }); + + await infisicalClient.auth().universalAuth.login({ + clientId: bootstrapConfig.clientId, + clientSecret: bootstrapConfig.clientSecret, + }); + + const res = await infisicalClient.secrets().listSecrets({ + environment: + process.env.NODE_ENV === 'prod' + ? 'prod' + : process.env.NODE_ENV === 'test' + ? 'staging' + : 'dev', + projectId: bootstrapConfig.projectId, + }); + + // 임시 객체에 채운 뒤 한 번에 교체(일관성 보장) + const map: SecretMap = {}; + for (const s of res.secrets) { + map[s.secretKey] = s.secretValue; + } + + // 앱 생명주기에서 필요한 env는 모두 SecretService가 process.env에 반영한다. + for (const [k, v] of Object.entries(map)) { + if (typeof v === 'string' && v.length > 0) { + process.env[k] = v; + } + } + + this.secrets = map; + this.logger.log( + `Fetched ${Object.keys(map).length} secrets from Infisical`, + ); + })(); + + try { + await this.loading; + } finally { + this.loading = undefined; // 실패/성공 모두 끝나면 해제(실패 시 재시도 가능) + } + } + + private async resolveInfisicalBootstrapConfig(): Promise<InfisicalBootstrapConfig> { + const [clientId, clientSecret, projectId] = await Promise.all([ + this.resolveBootstrapValue('INFISICAL_CLIENT_ID'), + this.resolveBootstrapValue('INFISICAL_CLIENT_SECRET'), + this.resolveBootstrapValue('INFISICAL_PROJECT_ID'), + ]); + const siteUrl = + (await this.resolveBootstrapValue('INFISICAL_SITE_URL', false)) ?? + DEFAULT_INFISICAL_SITE_URL; + + const missing = [ + !siteUrl && 'INFISICAL_SITE_URL', + !clientId && 'INFISICAL_CLIENT_ID', + !clientSecret && 'INFISICAL_CLIENT_SECRET', + !projectId && 'INFISICAL_PROJECT_ID', + ].filter(Boolean); + if (missing.length > 0) { + throw new Error( + `Missing required Infisical bootstrap configuration: ${missing.join(', ')}`, + ); + } + if (!clientId || !clientSecret || !projectId) { + throw new Error('Missing required Infisical bootstrap configuration.'); + } + + return { + siteUrl, + clientId, + clientSecret, + projectId, + }; + } + + private async resolveBootstrapValue( + key: string, + required = true, + ): Promise<string | undefined> { + const configured = this.config.get<string>(key)?.trim(); + if (configured) return configured; + + const fromImds = await this.getImdsSecret(key); + if (fromImds) { + process.env[key] = fromImds; + return fromImds; + } + + if (required) { + throw new Error(`Missing required bootstrap secret: ${key}`); + } + return undefined; + } + + private async getImdsSecret(key: string): Promise<string | undefined> { + let lastError: Error | undefined; + for (const baseUrl of this.resolveImdsBaseUrls()) { + try { + const token = await this.issueImdsToken(baseUrl); + const secret = await this.fetchImdsText( + `${baseUrl.replace(/\/$/u, '')}/latest/meta-data/secrets/${encodeURIComponent(key)}`, + { + 'X-aws-ec2-metadata-token': token, + }, + ); + return secret.trim() || undefined; + } catch (error) { + lastError = error instanceof Error ? error : new Error(String(error)); + } + } + + if (lastError) { + this.logger.debug( + `IMDS secret lookup failed for ${key}: ${lastError.message}`, + ); + } + return undefined; + } + + private resolveImdsBaseUrls(): string[] { + return [ + this.config.get<string>('IMDS_BASE_URL'), + this.config.get<string>('MACHINE_METADATA_BASE_URL'), + ...FALLBACK_IMDS_BASE_URLS, + ] + .map((value) => value?.trim()) + .filter((value): value is string => Boolean(value)); + } + + private async issueImdsToken(baseUrl: string): Promise<string> { + return this.fetchImdsText( + `${baseUrl.replace(/\/$/u, '')}/latest/api/token`, + { + 'X-aws-ec2-metadata-token-ttl-seconds': String(IMDS_TOKEN_TTL_SECONDS), + }, + 'PUT', + ); + } + + private async fetchImdsText( + url: string, + headers: Record<string, string>, + method = 'GET', + ): Promise<string> { + const controller = new AbortController(); + const timeout = setTimeout( + () => controller.abort(), + IMDS_REQUEST_TIMEOUT_MS, + ); + try { + const response = await fetch(url, { + method, + headers, + signal: controller.signal, + }); + if (!response.ok) { + throw new Error(`IMDS ${method} ${url} failed with ${response.status}`); + } + return response.text(); + } finally { + clearTimeout(timeout); + } + } + + /** lazy get: 필요할 때 처음 한번만 로드 */ + public async get<T = string>(key: string, fallback?: T): Promise<T> { + if (!this.secrets) { + await this.fetchSecrets(); + } + // console.log('Secrets loaded:', this.secrets); + const val = this.secrets![key]; + if (val === undefined) { + const cfg = this.config.get<T>(key); + if (cfg !== undefined) return cfg; + if (fallback !== undefined) return fallback; + throw new Error(`Secret not found for key="${key}"`); + } + return val as T; + } + + /** 필요 시: 동기 접근(초기화 안됐으면 에러). constructor 등에서 강제 사용 금지 */ + public getSync<T = string>(key: string, fallback?: T): T { + if (!this.secrets) { + const cfg = this.config.get<T>(key); + if (cfg !== undefined) return cfg; + if (fallback !== undefined) return fallback; + throw new Error(`SecretService not initialized yet (key="${key}")`); + } + const val = this.secrets[key]; + if (val === undefined) { + const cfg = this.config.get<T>(key); + if (cfg !== undefined) return cfg; + if (fallback !== undefined) return fallback; + throw new Error(`Secret not found for key="${key}"`); + } + return val as T; + } + + /** 선택: 사전에 강제로 로드하고 싶을 때 호출 */ + public async ready(): Promise<void> { + await this.fetchSecrets(); + } +} diff --git a/garnet.ts b/garnet.ts new file mode 100644 index 00000000..66a0a1a9 --- /dev/null +++ b/garnet.ts @@ -0,0 +1,389 @@ +import { createClient } from "redis"; +import { getSecret } from "@common/secret-manager"; +import { getLogger } from "@obs/logger"; + +type GarnetRedisClient = ReturnType<typeof createClient>; + +export interface StreamEntry { + deliveriesCounter?: number | undefined; + id: string; + message: Record<string, string>; + millisElapsedFromDelivery?: number | undefined; +} + +export interface StreamReadResult { + messages: StreamEntry[]; + name: string; +} + +export interface StreamPendingEntry { + consumer: string; + deliveriesCounter: number; + id: string; + millisecondsSinceLastDelivery: number; +} + +interface RawStreamMessage { + deliveriesCounter?: number | undefined; + id: string; + message: unknown; + millisElapsedFromDelivery?: number | undefined; +} + +interface RawStreamReadResult { + messages: RawStreamMessage[]; + name: string; +} + +function normalizeGarnetUrl(endpoint: string): string { + const trimmed = endpoint.trim(); + if (!trimmed) { + throw new Error("GARNET_ENDPOINT cannot be empty"); + } + + if (/^redis(s)?:\/\//iu.test(trimmed)) { + return trimmed; + } + + return `redis://${trimmed}`; +} + +function normalizeStreamFields(fields: unknown): Record<string, string> { + if (fields instanceof Map) { + return Object.fromEntries( + [...fields.entries()].map(([key, value]) => [String(key), String(value)]), + ); + } + + if (typeof fields === "object" && fields !== null) { + return Object.fromEntries( + Object.entries(fields as Record<string, unknown>).map(([key, value]) => [ + key, + String(value), + ]), + ); + } + + return {}; +} + +export class GarnetClient { + private client: GarnetRedisClient | null = null; + private readonly logger = getLogger(); + + private getClient(): GarnetRedisClient { + if (!this.client) { + throw new Error("Garnet client not connected"); + } + + return this.client; + } + + async connect(): Promise<void> { + const endpoint = getSecret("GARNET_ENDPOINT"); + const username = getSecret("GARNET_USERID"); + const password = getSecret("GARNET_PASSWORD"); + + if (!endpoint || !username || !password) { + throw new Error("Missing Garnet credentials in secrets"); + } + + const client = createClient({ + password, + url: normalizeGarnetUrl(endpoint), + username, + }); + + client.on("error", (error) => { + this.logger.error({ err: error }, "[Garnet] Client error"); + }); + + await client.connect(); + this.client = client; + + this.logger.info("✓ Garnet connected"); + } + + async getJson<T>(key: string): Promise<T | null> { + const rawValue = await this.getClient().get(key); + if (!rawValue) { + return null; + } + + return JSON.parse(rawValue) as T; + } + + async setJson( + key: string, + value: unknown, + ttlSeconds?: number, + ): Promise<void> { + const serialized = JSON.stringify(value); + const client = this.getClient(); + + if (ttlSeconds && ttlSeconds > 0) { + await client.set(key, serialized, { EX: ttlSeconds }); + return; + } + + await client.set(key, serialized); + } + + async getString(key: string): Promise<string | null> { + return this.getClient().get(key); + } + + async setString( + key: string, + value: string, + ttlSeconds?: number, + ): Promise<void> { + const client = this.getClient(); + + if (ttlSeconds && ttlSeconds > 0) { + await client.set(key, value, { EX: ttlSeconds }); + return; + } + + await client.set(key, value); + } + + async setStringIfAbsent( + key: string, + value: string, + ttlSeconds?: number, + ): Promise<boolean> { + const client = this.getClient(); + const setOptions: { + EX?: number; + NX: true; + } = { + NX: true, + }; + if (ttlSeconds && ttlSeconds > 0) { + setOptions.EX = ttlSeconds; + } + const result = await client.set(key, value, { + ...setOptions, + }); + return result === "OK"; + } + + async delete(key: string): Promise<number> { + return this.getClient().del(key); + } + + async getHash(key: string): Promise<Record<string, string>> { + return this.getClient().hGetAll(key); + } + + async setHash(key: string, fields: Record<string, string>): Promise<void> { + await this.getClient().hSet(key, fields); + } + + async appendStream( + key: string, + fields: Record<string, string>, + options?: { maxLen?: number | undefined }, + ): Promise<string> { + const trimOptions = + options?.maxLen && options.maxLen > 0 + ? { + TRIM: { + strategy: "MAXLEN" as const, + strategyModifier: "~" as const, + threshold: options.maxLen, + }, + } + : undefined; + + return this.getClient().xAdd(key, "*", fields, trimOptions); + } + + async createConsumerGroup( + key: string, + group: string, + startId = "$", + ): Promise<void> { + await this.getClient().xGroupCreate(key, group, startId, { + MKSTREAM: true, + }); + } + + async readGroup( + key: string, + group: string, + consumer: string, + startId: string, + options?: { + blockMs?: number | undefined; + count?: number | undefined; + }, + ): Promise<StreamReadResult[] | null> { + const readOptions: { + BLOCK?: number; + COUNT?: number; + } = {}; + if (options?.blockMs !== undefined) { + readOptions.BLOCK = options.blockMs; + } + if (options?.count !== undefined) { + readOptions.COUNT = options.count; + } + + const reply = await this.getClient().xReadGroup( + group, + consumer, + { + key, + id: startId, + }, + readOptions, + ); + + if (!reply) { + return null; + } + + return (reply as RawStreamReadResult[]).map((stream) => ({ + name: String(stream.name), + messages: stream.messages.map((message) => ({ + deliveriesCounter: + typeof message.deliveriesCounter === "number" + ? message.deliveriesCounter + : undefined, + id: String(message.id), + message: normalizeStreamFields(message.message), + millisElapsedFromDelivery: + typeof message.millisElapsedFromDelivery === "number" + ? message.millisElapsedFromDelivery + : undefined, + })), + })); + } + + async acknowledgeStream( + key: string, + group: string, + ids: string | string[], + ): Promise<number> { + return this.getClient().xAck(key, group, ids); + } + + async getPendingStreamRange( + key: string, + group: string, + count: number, + options?: { + consumer?: string | undefined; + idleMs?: number | undefined; + start?: string | undefined; + end?: string | undefined; + }, + ): Promise<StreamPendingEntry[]> { + const pendingOptions: { + IDLE?: number; + consumer?: string; + } = {}; + if (options?.idleMs !== undefined) { + pendingOptions.IDLE = options.idleMs; + } + if (options?.consumer !== undefined) { + pendingOptions.consumer = options.consumer; + } + + const reply = await this.getClient().xPendingRange( + key, + group, + options?.start ?? "-", + options?.end ?? "+", + count, + pendingOptions, + ); + + return reply.map((entry) => ({ + consumer: String(entry.consumer), + deliveriesCounter: entry.deliveriesCounter, + id: String(entry.id), + millisecondsSinceLastDelivery: entry.millisecondsSinceLastDelivery, + })); + } + + async claimPendingStream( + key: string, + group: string, + consumer: string, + minIdleTimeMs: number, + ids: string | string[], + options?: { + retryCount?: number | undefined; + }, + ): Promise<StreamEntry[]> { + const claimOptions: { + RETRYCOUNT?: number; + } = {}; + if (options?.retryCount !== undefined) { + claimOptions.RETRYCOUNT = options.retryCount; + } + + const claimed = await this.getClient().xClaim( + key, + group, + consumer, + minIdleTimeMs, + ids, + claimOptions, + ); + + return claimed.flatMap((message) => { + if (!message) { + return []; + } + + return [ + { + deliveriesCounter: + typeof message.deliveriesCounter === "number" + ? message.deliveriesCounter + : undefined, + id: String(message.id), + message: normalizeStreamFields(message.message), + millisElapsedFromDelivery: + typeof message.millisElapsedFromDelivery === "number" + ? message.millisElapsedFromDelivery + : undefined, + }, + ]; + }); + } + + async scanKeys(pattern: string): Promise<string[]> { + const keys: string[] = []; + + for await (const batch of this.getClient().scanIterator({ + COUNT: 100, + MATCH: pattern, + })) { + for (const key of batch) { + keys.push(String(key)); + } + } + + return keys; + } + + async close(): Promise<void> { + if (!this.client) { + return; + } + + const client = this.client; + this.client = null; + + try { + await client.quit(); + } catch { + client.destroy(); + } + } +}