import { BadRequestException, ConflictException, Injectable, Logger, NotFoundException, } from '@nestjs/common'; import { randomUUID } from 'crypto'; import mongoose from 'mongoose'; import { CommunityStatsService } from '../community-stats/community-stats.service'; import { ConnectionService } from '../global/database/connection.service'; import type { LegacyImportJobDocument, Match, RiotMatch as StoredRiotMatch, UserDocument, } from '../global/database/schema'; import { GarnetService } from '../global/garnet/garnet.service'; import { RIOT_JOB_LOW_STREAM } from '../riot-job-queue/riot-job-queue.types'; import { RiotMatchService } from '../riot/riot-match.service'; import type { RiotMatchDto, RiotRegionalRoute } from '../riot/riot.types'; import { buildGameResult, buildParticipantsSnapshot, buildTeams, buildUserByPuuidMap, countKnownParticipants, resolveExternalCount, resolveTeam100IsBlue, type UserByPuuidMap, } from './match-builder.util'; import { LEGACY_IMPORT_DEFAULT_START_ISO, LEGACY_IMPORT_LOCK_TTL_SECONDS, LEGACY_IMPORT_MEMBER_AVG_SECONDS, type BulkDeleteLegacyMatchesRequest, type LegacyImportMemberJob, type LegacyImportMemberWindowView, type LegacyMatchListResponse, type RebuildLegacyMatchesResponse, type StartLegacyImportRequest, type StartLegacyImportResponse, } from './legacy-import.types'; const MATCH_IMPORT_LOCK_TTL_SECONDS = 60; const MATCH_ID_PAGE_SIZE = 100; type ImportOutcome = 'imported' | 'duplicate' | 'skipped' | 'error'; @Injectable() export class LegacyImportService { private readonly logger = new Logger(LegacyImportService.name); constructor( private readonly connectionService: ConnectionService, private readonly garnetService: GarnetService, private readonly riotMatchService: RiotMatchService, private readonly communityStatsService: CommunityStatsService, ) {} public async previewMembers(params: { communityId: mongoose.Types.ObjectId; windowStart?: string; windowEnd?: string; }): Promise<{ members: LegacyImportMemberWindowView[] }> { const { windowStart, windowEnd } = this.resolveWindow(params); const users = await this.findImportableUsers(params.communityId); return { members: users.map((user) => this.toMemberWindowView(user, windowStart, windowEnd), ), }; } public async start(params: { communityId: mongoose.Types.ObjectId; triggeredBy: mongoose.Types.ObjectId; body: StartLegacyImportRequest; }): Promise { const lockKey = this.communityLockKey(params.communityId); const jobId = randomUUID(); const acquired = await this.garnetService.setStringIfAbsent( lockKey, jobId, LEGACY_IMPORT_LOCK_TTL_SECONDS, ); if (!acquired) { throw new ConflictException('Legacy import is already running.'); } try { const CommunityModel = await this.connectionService.getModel('community'); const community = await CommunityModel.findById(params.communityId) .select({ riotRegion: 1 }) .lean() .exec(); if (!community) throw new NotFoundException('Community not found.'); const { windowStart, windowEnd } = this.resolveWindow({ windowStart: params.body.windowStart, windowEnd: params.body.windowEnd, }); const users = await this.findImportableUsers(params.communityId); const overrides = new Map( (params.body.memberOverrides ?? []).map((override) => [ override.userId, override, ]), ); const windows = users .map((user) => this.resolveMemberWindow(user, windowStart, windowEnd, overrides)) .filter((window) => window.startTime.getTime() <= window.endTime.getTime()); const LegacyImportJobModel = await this.connectionService.getModel('legacyImportJob'); await LegacyImportJobModel.create({ jobId, communityId: params.communityId, triggeredBy: params.triggeredBy, startedAt: new Date(), state: 'running', windowStart, windowEnd, memberOverrides: windows, totalMembers: windows.length, processedMembers: 0, totalDiscoveredMatchIds: 0, totalImportedMatches: 0, totalSkippedDuplicate: 0, totalErrors: 0, averageMemberSeconds: LEGACY_IMPORT_MEMBER_AVG_SECONDS, }); for (const window of windows) { const job: LegacyImportMemberJob = { kind: 'legacy-import-member', jobId: randomUUID(), parentJobId: jobId, communityId: params.communityId.toString(), userId: window.userId.toString(), puuid: window.puuid, startTime: Math.floor(window.startTime.getTime() / 1000), endTime: Math.floor(window.endTime.getTime() / 1000), triggeredBy: params.triggeredBy.toString(), region: (community.riotRegion ?? 'asia') as RiotRegionalRoute, }; await this.garnetService.appendStream( RIOT_JOB_LOW_STREAM, { job: JSON.stringify(job) }, { maxLen: 10_000 }, ); } if (windows.length === 0) { await LegacyImportJobModel.updateOne( { jobId }, { $set: { state: 'completed', completedAt: new Date() } }, ).exec(); await this.garnetService.delete(lockKey); } return { jobId, totalMembers: windows.length, etaSeconds: windows.length * LEGACY_IMPORT_MEMBER_AVG_SECONDS, }; } catch (error) { await this.garnetService.delete(lockKey); throw error; } } public async listJobs(params: { communityId: mongoose.Types.ObjectId; limit?: number; }): Promise<{ jobs: LegacyImportJobDocument[] }> { const LegacyImportJobModel = await this.connectionService.getModel('legacyImportJob'); const jobs = await LegacyImportJobModel.find({ communityId: params.communityId }) .sort({ startedAt: -1 }) .limit(Math.min(params.limit ?? 10, 50)) .exec(); return { jobs }; } public async getJob(params: { communityId: mongoose.Types.ObjectId; jobId: string; }): Promise { const LegacyImportJobModel = await this.connectionService.getModel('legacyImportJob'); const job = await LegacyImportJobModel.findOne({ communityId: params.communityId, jobId: params.jobId, }).exec(); if (!job) throw new NotFoundException('Legacy import job not found.'); return job; } public async cancel(params: { communityId: mongoose.Types.ObjectId; jobId: string; }): Promise { const LegacyImportJobModel = await this.connectionService.getModel('legacyImportJob'); const job = await LegacyImportJobModel.findOneAndUpdate( { communityId: params.communityId, jobId: params.jobId, state: { $in: ['pending', 'running'] }, }, { $set: { state: 'cancelled', completedAt: new Date() } }, { returnDocument: 'after' }, ).exec(); if (!job) throw new NotFoundException('Running legacy import job not found.'); await this.garnetService.delete(this.communityLockKey(params.communityId)); return job; } public async processMemberJob(job: LegacyImportMemberJob): Promise { const started = Date.now(); const legacyJob = await this.getRunnableJob(job); if (!legacyJob) return; let discovered = 0; let imported = 0; let duplicates = 0; let errors = 0; try { const userByPuuid = await this.buildUserByPuuidMap( new mongoose.Types.ObjectId(job.communityId), ); const matchIds = await this.collectMatchIds(job); discovered = matchIds.size; for (const matchId of matchIds) { if (await this.isCancelled(job.parentJobId)) break; const outcome = await this.importSingleMatch(matchId, job, userByPuuid); if (outcome === 'imported') imported += 1; if (outcome === 'duplicate') duplicates += 1; if (outcome === 'error') errors += 1; } } catch (error) { errors += 1; await this.recordError(job.parentJobId, { puuid: job.puuid, reason: error instanceof Error ? error.message : String(error), }); this.logger.warn( `Legacy import member job failed ${job.jobId}: ${ error instanceof Error ? error.message : error }`, ); } finally { await this.finishMemberJob(job, { discovered, imported, duplicates, errors, elapsedSeconds: Math.max(1, Math.round((Date.now() - started) / 1000)), }); } } public async listLegacyMatches(params: { communityId: mongoose.Types.ObjectId; cursor?: number; limit?: number; }): Promise { const MatchModel = await this.connectionService.getModel('match'); const limit = Math.max(1, Math.min(params.limit ?? 30, 100)); const filter: Record = { communityId: params.communityId, legacyImported: true, }; if (params.cursor) { filter.scheduledAt = { $lt: new Date(params.cursor) }; } const matches = await MatchModel.find(filter) .sort({ scheduledAt: -1 }) .limit(limit + 1) .exec(); const visible = matches.slice(0, limit); const riotMatchIds = visible .map((match) => match.result?.games[0]?.riotMatchId) .filter((matchId): matchId is string => Boolean(matchId)); const RiotMatchModel = await this.connectionService.getModel('riotMatch'); const rawDocs = await RiotMatchModel.find({ matchId: { $in: riotMatchIds } }) .lean() .exec(); const rawById = new Map( rawDocs.map((doc) => [doc.matchId, doc.raw as unknown as RiotMatchDto]), ); const userByPuuid = await this.buildUserByPuuidMap(params.communityId); return { matches: visible.map((match) => { const riotMatchId = match.result?.games[0]?.riotMatchId ?? null; const raw = riotMatchId ? rawById.get(riotMatchId) : undefined; const memberCount = match.result?.games[0]?.participants.length ?? 0; return { matchId: match._id.toString(), title: match.title, scheduledAt: match.scheduledAt.toISOString(), riotMatchId, memberCount, externalCount: raw ? resolveExternalCount(raw, userByPuuid) : Math.max(0, 10 - memberCount), blueScore: match.result?.blueScore ?? 0, redScore: match.result?.redScore ?? 0, winnerSide: match.result?.winnerSide, }; }), nextCursor: matches.length > limit ? visible[visible.length - 1]?.scheduledAt.getTime() ?? null : null, }; } public async bulkDeleteLegacyMatches(params: { communityId: mongoose.Types.ObjectId; body: BulkDeleteLegacyMatchesRequest; }): Promise<{ deleted: number }> { const ids = (params.body.matchIds ?? []) .filter((id) => mongoose.isValidObjectId(id)) .map((id) => new mongoose.Types.ObjectId(id)); if (ids.length === 0) return { deleted: 0 }; const MatchModel = await this.connectionService.getModel('match'); const result = await MatchModel.deleteMany({ _id: { $in: ids }, communityId: params.communityId, legacyImported: true, }).exec(); await this.communityStatsService.invalidateCommunity(params.communityId); return { deleted: result.deletedCount ?? 0 }; } public async rebuildLegacyMatches(params: { communityId: mongoose.Types.ObjectId; }): Promise { const MatchModel = await this.connectionService.getModel('match'); const matches = await MatchModel.find({ communityId: params.communityId, legacyImported: true, }).exec(); const riotMatchIds = matches .map( (match) => match.result?.games[0]?.riotMatchId ?? match.result?.riotMatchIds?.[0], ) .filter((matchId): matchId is string => Boolean(matchId)); const RiotMatchModel = await this.connectionService.getModel('riotMatch'); const rawDocs = await RiotMatchModel.find({ matchId: { $in: riotMatchIds } }) .lean() .exec(); const rawById = new Map(); for (const doc of rawDocs) { if (isRiotMatchDto(doc.raw)) rawById.set(doc.matchId, doc.raw); } const userByPuuid = await this.buildUserByPuuidMap(params.communityId); const result: RebuildLegacyMatchesResponse = { scanned: matches.length, rebuilt: 0, normalizedTitles: 0, skippedMissingRaw: 0, skippedNoKnownParticipants: 0, }; for (const match of matches) { const riotMatchId = match.result?.games[0]?.riotMatchId ?? match.result?.riotMatchIds?.[0]; const raw = riotMatchId ? rawById.get(riotMatchId) : undefined; const title = this.normalizeLegacyTitle( match.title, raw?.info.gameCreation ?? match.scheduledAt.getTime(), ); const update: Partial = {}; if (title !== match.title) { update.title = title; result.normalizedTitles += 1; } if (!raw || !riotMatchId) { result.skippedMissingRaw += 1; if (Object.keys(update).length > 0) { await MatchModel.updateOne({ _id: match._id }, { $set: update }).exec(); } continue; } const team100IsBlue = resolveTeam100IsBlue(raw, userByPuuid); const participants = buildParticipantsSnapshot(raw, userByPuuid); const gameResult = buildGameResult(raw, userByPuuid, team100IsBlue, 1); if (!gameResult || participants.length === 0) { result.skippedNoKnownParticipants += 1; if (Object.keys(update).length > 0) { await MatchModel.updateOne({ _id: match._id }, { $set: update }).exec(); } continue; } const winnerSide = gameResult.winnerSide; update.mapId = raw.info.mapId ?? match.mapId; update.participants = participants; update.teams = buildTeams(raw, userByPuuid, team100IsBlue); update.result = { winnerSide, blueScore: winnerSide === 'blue' ? 1 : 0, redScore: winnerSide === 'red' ? 1 : 0, riotMatchIds: [riotMatchId], games: [gameResult], }; await MatchModel.updateOne( { _id: match._id, communityId: params.communityId, legacyImported: true, }, { $set: update }, ).exec(); result.rebuilt += 1; } if (result.rebuilt > 0 || result.normalizedTitles > 0) { await this.communityStatsService.invalidateCommunity(params.communityId); } return result; } private async collectMatchIds(job: LegacyImportMemberJob): Promise> { const matchIds = new Set(); let start = 0; while (true) { const ids = await this.riotMatchService.getMatchIdsByPuuid(job.puuid, { region: job.region, type: 'tourney', startTime: job.startTime, endTime: job.endTime, start, count: MATCH_ID_PAGE_SIZE, }); ids.forEach((matchId) => matchIds.add(matchId)); if (ids.length < MATCH_ID_PAGE_SIZE) break; start += MATCH_ID_PAGE_SIZE; } return matchIds; } private async importSingleMatch( riotMatchId: string, job: LegacyImportMemberJob, userByPuuid: UserByPuuidMap, ): Promise { const lockKey = `legacy-import:match:${riotMatchId}`; const acquired = await this.garnetService.setStringIfAbsent( lockKey, job.parentJobId, MATCH_IMPORT_LOCK_TTL_SECONDS, ); if (!acquired) return 'duplicate'; try { const MatchModel = await this.connectionService.getModel('match'); const existing = await MatchModel.exists({ 'result.games.riotMatchId': riotMatchId, }); if (existing) return 'duplicate'; const detail = await this.ensureRiotMatchPersisted(riotMatchId, job.region); if (!detail.info.tournamentCode) return 'skipped'; const counts = countKnownParticipants(detail, userByPuuid); if (counts.total === 0) return 'skipped'; const team100IsBlue = resolveTeam100IsBlue(detail, userByPuuid); const gameResult = buildGameResult(detail, userByPuuid, team100IsBlue, 1); if (!gameResult || gameResult.participants.length === 0) return 'skipped'; const winnerSide = gameResult.winnerSide; const matchPayload: Partial = { communityId: new mongoose.Types.ObjectId(job.communityId), title: this.formatLegacyDate(detail.info.gameCreation), description: 'Legacy import (자동 생성)', mapId: detail.info.mapId ?? 11, status: 'finished', scheduledAt: new Date(detail.info.gameCreation), maxParticipants: 10, gameCount: 1, tournamentCodeIds: [], teamBuiltAt: new Date(detail.info.gameCreation), hostUserId: new mongoose.Types.ObjectId(job.triggeredBy), participants: buildParticipantsSnapshot(detail, userByPuuid), teams: buildTeams(detail, userByPuuid, team100IsBlue), result: { winnerSide, blueScore: winnerSide === 'blue' ? 1 : 0, redScore: winnerSide === 'red' ? 1 : 0, riotMatchIds: [riotMatchId], games: [gameResult], }, legacyImported: true, legacyImportSource: { importedAt: new Date(), importedBy: new mongoose.Types.ObjectId(job.triggeredBy), triggeringPuuid: job.puuid, jobId: job.parentJobId, }, prizes: [], notices: [], schedule: [], }; await MatchModel.create(matchPayload); return 'imported'; } catch (error) { await this.recordError(job.parentJobId, { puuid: job.puuid, matchId: riotMatchId, reason: error instanceof Error ? error.message : String(error), }); return 'error'; } finally { await this.garnetService.delete(lockKey); } } private async ensureRiotMatchPersisted( riotMatchId: string, region: RiotRegionalRoute, ): Promise { const RiotMatchModel = await this.connectionService.getModel('riotMatch'); const stored = await RiotMatchModel.findOne({ matchId: riotMatchId }).exec(); if (isRiotMatchDto(stored?.raw)) return stored.raw; const dto = await this.riotMatchService.getMatch(riotMatchId, { region }); const payload: Partial = { matchId: dto.metadata.matchId, gameCreation: dto.info.gameCreation, gameDuration: dto.info.gameDuration, gameVersion: dto.info.gameVersion, queueId: dto.info.queueId, participantPuuids: dto.metadata.participants, raw: dto as unknown as Record, fetchedAt: new Date(), }; await RiotMatchModel.updateOne( { matchId: dto.metadata.matchId }, { $set: payload }, { upsert: true }, ).exec(); return dto; } private async finishMemberJob( job: LegacyImportMemberJob, stats: { discovered: number; imported: number; duplicates: number; errors: number; elapsedSeconds: number; }, ): Promise { const LegacyImportJobModel = await this.connectionService.getModel('legacyImportJob'); const current = await LegacyImportJobModel.findOne({ jobId: job.parentJobId, }).exec(); if (!current || current.state === 'cancelled') return; const average = current.averageMemberSeconds === undefined ? stats.elapsedSeconds : Math.round(current.averageMemberSeconds * 0.7 + stats.elapsedSeconds * 0.3); const nextProcessed = current.processedMembers + 1; const completed = nextProcessed >= current.totalMembers; await LegacyImportJobModel.updateOne( { jobId: job.parentJobId }, { $set: { state: completed ? 'completed' : current.state, completedAt: completed ? new Date() : current.completedAt, averageMemberSeconds: average, }, $inc: { processedMembers: 1, totalDiscoveredMatchIds: stats.discovered, totalImportedMatches: stats.imported, totalSkippedDuplicate: stats.duplicates, totalErrors: stats.errors, }, }, ).exec(); if (completed) { await Promise.all([ this.garnetService.delete( this.communityLockKey(new mongoose.Types.ObjectId(job.communityId)), ), this.communityStatsService.invalidateCommunity( new mongoose.Types.ObjectId(job.communityId), ), ]); } } private async getRunnableJob( job: LegacyImportMemberJob, ): Promise { const LegacyImportJobModel = await this.connectionService.getModel('legacyImportJob'); const legacyJob = await LegacyImportJobModel.findOne({ jobId: job.parentJobId, }).exec(); if (!legacyJob || legacyJob.state === 'cancelled') return null; if (legacyJob.state === 'completed' || legacyJob.state === 'failed') return null; return legacyJob; } private async isCancelled(parentJobId: string): Promise { const LegacyImportJobModel = await this.connectionService.getModel('legacyImportJob'); const job = await LegacyImportJobModel.findOne({ jobId: parentJobId }) .select({ state: 1 }) .lean() .exec(); return job?.state === 'cancelled'; } private async recordError( jobId: string, error: { puuid?: string; matchId?: string; reason: string }, ): Promise { const LegacyImportJobModel = await this.connectionService.getModel('legacyImportJob'); await LegacyImportJobModel.updateOne( { jobId }, { $push: { errors: { ...error, at: new Date(), }, }, }, ).exec(); } private async buildUserByPuuidMap( communityId: mongoose.Types.ObjectId, ): Promise { const users = await this.findImportableUsers(communityId); return buildUserByPuuidMap(users); } private async findImportableUsers( communityId: mongoose.Types.ObjectId, ): Promise { const UserModel = await this.connectionService.getModel('user'); return UserModel.find({ communityId, 'riotAccounts.0': { $exists: true }, }).exec(); } private toMemberWindowView( user: UserDocument, windowStart: Date, windowEnd: Date, ): LegacyImportMemberWindowView { const resolved = this.resolveMemberWindow(user, windowStart, windowEnd, new Map()); const account = user.riotAccounts.find((riotAccount) => riotAccount.isPrimary) ?? user.riotAccounts[0]; return { userId: user._id.toString(), puuid: account.puuid, displayName: user.kakaoTalkNickname ?? user.discordNickname ?? account.gameName ?? user._id.toString(), gameName: account.gameName, tagLine: account.tagLine, profileIconId: account.profileIconId, isActivated: user.isActivated, startTime: resolved.startTime.toISOString(), endTime: resolved.endTime.toISOString(), }; } private resolveMemberWindow( user: UserDocument, windowStart: Date, windowEnd: Date, overrides: Map, ) { const account = user.riotAccounts.find((riotAccount) => riotAccount.isPrimary) ?? user.riotAccounts[0]; if (!account) throw new BadRequestException('Riot account not found.'); const override = overrides.get(user._id.toString()); const overrideStart = this.parseOptionalDate(override?.startTime); const overrideEnd = this.parseOptionalDate(override?.endTime); const activeEnd = user.isActivated ? windowEnd : user.updatedAt; const startTime = overrideStart ?? windowStart; const endTime = this.minDate(windowEnd, activeEnd, overrideEnd); return { userId: user._id, puuid: account.puuid, startTime, endTime, }; } private resolveWindow(params: { windowStart?: string; windowEnd?: string; }) { const windowStart = this.parseOptionalDate(params.windowStart) ?? new Date(LEGACY_IMPORT_DEFAULT_START_ISO); const windowEnd = this.parseOptionalDate(params.windowEnd) ?? new Date(); if (windowStart.getTime() > windowEnd.getTime()) { throw new BadRequestException('windowStart must be before windowEnd.'); } return { windowStart, windowEnd }; } private parseOptionalDate(value: string | undefined): Date | undefined { if (!value) return undefined; const parsed = new Date(value); if (Number.isNaN(parsed.getTime())) { throw new BadRequestException('Invalid date.'); } return parsed; } private minDate(...values: Array): Date { return new Date(Math.min(...values.filter(Boolean).map((date) => date!.getTime()))); } private normalizeLegacyTitle(title: string, gameCreation: number): string { const stripped = title.replace(/^\[(?:legacy|lagacy)\]\s*/i, '').trim(); return stripped || this.formatLegacyDate(gameCreation); } private formatLegacyDate(gameCreation: number): string { return new Intl.DateTimeFormat('ko-KR', { timeZone: 'Asia/Seoul', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', }).format(new Date(gameCreation)); } private communityLockKey(communityId: mongoose.Types.ObjectId): string { return `legacy-import:lock:${communityId.toString()}`; } } function isRiotMatchDto(value: unknown): value is RiotMatchDto { return Boolean( value && typeof value === 'object' && 'metadata' in value && 'info' in value, ); }