diff --git a/apps/api/src/modules/legacy-import/legacy-import.controller.ts b/apps/api/src/modules/legacy-import/legacy-import.controller.ts new file mode 100644 index 00000000..6df3f88b --- /dev/null +++ b/apps/api/src/modules/legacy-import/legacy-import.controller.ts @@ -0,0 +1,150 @@ +import { + BadRequestException, + Body, + Controller, + Get, + Param, + Post, + Query, + UseGuards, +} from '@nestjs/common'; +import mongoose from 'mongoose'; +import { CurrentUser } from '../auth/current-user.decorator'; +import { Roles } from '../auth/roles.decorator'; +import { RolesGuard } from '../auth/roles.guard'; +import { ParseObjectIdPipe } from '../common/parse-object-id.pipe'; +import type { UserDocument } from '../global/database/schema'; +import type { + BulkDeleteLegacyMatchesRequest, + StartLegacyImportRequest, +} from './legacy-import.types'; +import { LegacyImportService } from './legacy-import.service'; + +@Controller('communities/:communityId') +@Roles('owner', 'admin') +@UseGuards(RolesGuard) +export class LegacyImportController { + constructor(private readonly legacyImportService: LegacyImportService) {} + + @Get('legacy-import/members') + public previewMembers( + @CurrentUser() currentUser: UserDocument, + @Param('communityId', ParseObjectIdPipe) + communityId: mongoose.Types.ObjectId, + @Query('windowStart') windowStart?: string, + @Query('windowEnd') windowEnd?: string, + ) { + this.assertSameCommunity(currentUser, communityId); + return this.legacyImportService.previewMembers({ + communityId, + windowStart, + windowEnd, + }); + } + + @Post('legacy-import') + public start( + @CurrentUser() currentUser: UserDocument, + @Param('communityId', ParseObjectIdPipe) + communityId: mongoose.Types.ObjectId, + @Body() body: StartLegacyImportRequest, + ) { + this.assertSameCommunity(currentUser, communityId); + return this.legacyImportService.start({ + communityId, + triggeredBy: currentUser._id, + body, + }); + } + + @Get('legacy-import-jobs') + public listJobs( + @CurrentUser() currentUser: UserDocument, + @Param('communityId', ParseObjectIdPipe) + communityId: mongoose.Types.ObjectId, + @Query('limit') limit?: string, + ) { + this.assertSameCommunity(currentUser, communityId); + return this.legacyImportService.listJobs({ + communityId, + limit: this.toNumber(limit), + }); + } + + @Get('legacy-import-jobs/:jobId') + public getJob( + @CurrentUser() currentUser: UserDocument, + @Param('communityId', ParseObjectIdPipe) + communityId: mongoose.Types.ObjectId, + @Param('jobId') jobId: string, + ) { + this.assertSameCommunity(currentUser, communityId); + return this.legacyImportService.getJob({ communityId, jobId }); + } + + @Post('legacy-import-jobs/:jobId/cancel') + public cancel( + @CurrentUser() currentUser: UserDocument, + @Param('communityId', ParseObjectIdPipe) + communityId: mongoose.Types.ObjectId, + @Param('jobId') jobId: string, + ) { + this.assertSameCommunity(currentUser, communityId); + return this.legacyImportService.cancel({ communityId, jobId }); + } + + @Get('legacy-matches') + public listLegacyMatches( + @CurrentUser() currentUser: UserDocument, + @Param('communityId', ParseObjectIdPipe) + communityId: mongoose.Types.ObjectId, + @Query('cursor') cursor?: string, + @Query('limit') limit?: string, + ) { + this.assertSameCommunity(currentUser, communityId); + return this.legacyImportService.listLegacyMatches({ + communityId, + cursor: this.toNumber(cursor), + limit: this.toNumber(limit), + }); + } + + @Post('legacy-matches/bulk-delete') + public bulkDeleteLegacyMatches( + @CurrentUser() currentUser: UserDocument, + @Param('communityId', ParseObjectIdPipe) + communityId: mongoose.Types.ObjectId, + @Body() body: BulkDeleteLegacyMatchesRequest, + ) { + this.assertSameCommunity(currentUser, communityId); + return this.legacyImportService.bulkDeleteLegacyMatches({ + communityId, + body, + }); + } + + @Post('legacy-matches/rebuild') + public rebuildLegacyMatches( + @CurrentUser() currentUser: UserDocument, + @Param('communityId', ParseObjectIdPipe) + communityId: mongoose.Types.ObjectId, + ) { + this.assertSameCommunity(currentUser, communityId); + return this.legacyImportService.rebuildLegacyMatches({ communityId }); + } + + private assertSameCommunity( + user: UserDocument, + communityId: mongoose.Types.ObjectId, + ): void { + if (!user.communityId.equals(communityId)) { + throw new BadRequestException('Different community.'); + } + } + + private toNumber(value: string | undefined): number | undefined { + if (!value) return undefined; + const parsed = Number(value); + return Number.isFinite(parsed) ? parsed : undefined; + } +} diff --git a/apps/api/src/modules/legacy-import/legacy-import.module.ts b/apps/api/src/modules/legacy-import/legacy-import.module.ts new file mode 100644 index 00000000..9bd9ad80 --- /dev/null +++ b/apps/api/src/modules/legacy-import/legacy-import.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { CommunityStatsModule } from '../community-stats/community-stats.module'; +import { RiotModule } from '../riot/riot.module'; +import { LegacyImportController } from './legacy-import.controller'; +import { LegacyImportService } from './legacy-import.service'; + +@Module({ + imports: [RiotModule, CommunityStatsModule], + controllers: [LegacyImportController], + providers: [LegacyImportService], + exports: [LegacyImportService], +}) +export class LegacyImportModule {} diff --git a/apps/api/src/modules/legacy-import/legacy-import.service.ts b/apps/api/src/modules/legacy-import/legacy-import.service.ts new file mode 100644 index 00000000..a56c3210 --- /dev/null +++ b/apps/api/src/modules/legacy-import/legacy-import.service.ts @@ -0,0 +1,775 @@ +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, + ); +} diff --git a/apps/api/src/modules/legacy-import/legacy-import.types.ts b/apps/api/src/modules/legacy-import/legacy-import.types.ts new file mode 100644 index 00000000..e1559b30 --- /dev/null +++ b/apps/api/src/modules/legacy-import/legacy-import.types.ts @@ -0,0 +1,75 @@ +import type { RiotRegionalRoute } from '../riot/riot.types'; + +export const LEGACY_IMPORT_DEFAULT_START_ISO = '2024-05-01T00:00:00.000Z'; +export const LEGACY_IMPORT_LOCK_TTL_SECONDS = 6 * 60 * 60; +export const LEGACY_IMPORT_MEMBER_AVG_SECONDS = 60; + +export interface LegacyImportMemberJob { + kind: 'legacy-import-member'; + jobId: string; + parentJobId: string; + communityId: string; + userId: string; + puuid: string; + startTime: number; + endTime: number; + triggeredBy: string; + region: RiotRegionalRoute; +} + +export interface LegacyImportMemberOverrideRequest { + userId: string; + startTime?: string; + endTime?: string; +} + +export interface StartLegacyImportRequest { + windowStart?: string; + windowEnd?: string; + memberOverrides?: LegacyImportMemberOverrideRequest[]; +} + +export interface LegacyImportMemberWindowView { + userId: string; + puuid: string; + displayName: string; + gameName: string; + tagLine: string; + profileIconId: number; + isActivated: boolean; + startTime: string; + endTime: string; +} + +export interface StartLegacyImportResponse { + jobId: string; + totalMembers: number; + etaSeconds: number; +} + +export interface LegacyMatchListResponse { + matches: Array<{ + matchId: string; + title: string; + scheduledAt: string; + riotMatchId: string | null; + memberCount: number; + externalCount: number; + blueScore: number; + redScore: number; + winnerSide?: 'blue' | 'red'; + }>; + nextCursor: number | null; +} + +export interface BulkDeleteLegacyMatchesRequest { + matchIds?: string[]; +} + +export interface RebuildLegacyMatchesResponse { + scanned: number; + rebuilt: number; + normalizedTitles: number; + skippedMissingRaw: number; + skippedNoKnownParticipants: number; +} diff --git a/apps/api/src/modules/legacy-import/match-builder.util.ts b/apps/api/src/modules/legacy-import/match-builder.util.ts new file mode 100644 index 00000000..e60d67d6 --- /dev/null +++ b/apps/api/src/modules/legacy-import/match-builder.util.ts @@ -0,0 +1,189 @@ +import mongoose from 'mongoose'; +import type { + MatchGameResult, + MatchParticipant, + MatchTeam, + Position, + RiotAccount, + TeamSide, + UserDocument, +} from '../global/database/schema'; +import type { RiotMatchDto, RiotMatchParticipantDto } from '../riot/riot.types'; + +const RIOT_BLUE_TEAM_ID = 100; +const RIOT_RED_TEAM_ID = 200; + +export interface LegacyUserSnapshot { + userId: mongoose.Types.ObjectId; + account: RiotAccount; + displayName: string; + preferredPositions: UserDocument['preferredPositions']; +} + +export type UserByPuuidMap = Map; + +export function buildUserByPuuidMap(users: UserDocument[]): UserByPuuidMap { + const entries = new Map(); + for (const user of users) { + for (const account of user.riotAccounts) { + entries.set(account.puuid, { + userId: user._id, + account, + displayName: + user.kakaoTalkNickname ?? + user.discordNickname ?? + account.gameName ?? + user._id.toString(), + preferredPositions: user.preferredPositions ?? [], + }); + } + } + return entries; +} + +export function countKnownParticipants( + detail: RiotMatchDto, + userByPuuid: UserByPuuidMap, +): { team100: number; team200: number; total: number } { + const team100 = detail.info.participants.filter( + (participant) => + participant.teamId === RIOT_BLUE_TEAM_ID && userByPuuid.has(participant.puuid), + ).length; + const team200 = detail.info.participants.filter( + (participant) => + participant.teamId === RIOT_RED_TEAM_ID && userByPuuid.has(participant.puuid), + ).length; + return { team100, team200, total: team100 + team200 }; +} + +export function resolveTeam100IsBlue( + detail: RiotMatchDto, + userByPuuid: UserByPuuidMap, +): boolean { + const counts = countKnownParticipants(detail, userByPuuid); + return counts.team100 >= counts.team200; +} + +export function buildParticipantsSnapshot( + detail: RiotMatchDto, + userByPuuid: UserByPuuidMap, +): MatchParticipant[] { + return detail.info.participants + .map((participant): MatchParticipant | null => { + const user = userByPuuid.get(participant.puuid); + if (!user) return null; + return { + userId: user.userId, + puuid: user.account.puuid, + gameName: user.account.gameName || participant.riotIdGameName, + tagLine: user.account.tagLine || participant.riotIdTagLine, + profileIconId: user.account.profileIconId, + tier: + user.account.tiers.find((tier) => tier.queueType === 'RANKED_SOLO_5x5') ?? + user.account.tiers[0], + preferredPositions: user.preferredPositions, + joinedAt: new Date(detail.info.gameCreation), + }; + }) + .filter((participant): participant is MatchParticipant => Boolean(participant)); +} + +export function buildTeams( + detail: RiotMatchDto, + userByPuuid: UserByPuuidMap, + team100IsBlue: boolean, +): MatchTeam[] { + return (['blue', 'red'] as TeamSide[]).map((side) => { + const teamId = resolveRiotTeamId(side, team100IsBlue); + const members = detail.info.participants + .filter((participant) => participant.teamId === teamId) + .map((participant) => { + const user = userByPuuid.get(participant.puuid); + if (!user) return null; + return { + userId: user.userId, + assignedPosition: resolveAssignedPosition(participant, user), + }; + }) + .filter((member): member is NonNullable => Boolean(member)); + return { + side, + name: side === 'blue' ? '블루팀' : '레드팀', + members, + }; + }); +} + +export function buildGameResult( + detail: RiotMatchDto, + userByPuuid: UserByPuuidMap, + team100IsBlue: boolean, + gameNumber = 1, +): MatchGameResult | null { + const winningTeam = detail.info.teams.find((team) => team.win); + if (!winningTeam) return null; + const winnerSide = resolveSide(winningTeam.teamId, team100IsBlue); + if (!winnerSide) return null; + const participants = detail.info.participants + .map((participant) => { + const user = userByPuuid.get(participant.puuid); + const side = resolveSide(participant.teamId, team100IsBlue); + if (!user || !side) return null; + return { + userId: user.userId, + side, + championId: participant.championId, + kills: participant.kills, + deaths: participant.deaths, + assists: participant.assists, + win: participant.win, + }; + }) + .filter((participant): participant is NonNullable => + Boolean(participant), + ); + + return { + gameNumber, + riotMatchId: detail.metadata.matchId, + winnerSide, + gameCreation: detail.info.gameCreation, + gameDuration: detail.info.gameDuration, + participants, + }; +} + +export function resolveExternalCount( + detail: RiotMatchDto, + userByPuuid: UserByPuuidMap, +): number { + return detail.info.participants.filter( + (participant) => !userByPuuid.has(participant.puuid), + ).length; +} + +function resolveRiotTeamId(side: TeamSide, team100IsBlue: boolean): number { + if (side === 'blue') { + return team100IsBlue ? RIOT_BLUE_TEAM_ID : RIOT_RED_TEAM_ID; + } + return team100IsBlue ? RIOT_RED_TEAM_ID : RIOT_BLUE_TEAM_ID; +} + +function resolveSide(teamId: number, team100IsBlue: boolean): TeamSide | null { + if (teamId === RIOT_BLUE_TEAM_ID) return team100IsBlue ? 'blue' : 'red'; + if (teamId === RIOT_RED_TEAM_ID) return team100IsBlue ? 'red' : 'blue'; + return null; +} + +function resolveAssignedPosition( + participant: RiotMatchParticipantDto, + user: LegacyUserSnapshot, +): Position { + const lane = participant.teamPosition || participant.individualPosition; + if (lane === 'TOP') return 'TOP'; + if (lane === 'JUNGLE') return 'JGL'; + if (lane === 'MIDDLE') return 'MID'; + if (lane === 'BOTTOM') return 'ADC'; + if (lane === 'UTILITY') return 'SUP'; + return user.preferredPositions[0]?.position ?? 'TOP'; +}