From 1fd37f992bd14133d70be7d4aabe12a980d78efe Mon Sep 17 00:00:00 2001 From: dahyeon Date: Mon, 27 Apr 2026 19:48:42 -0700 Subject: [PATCH] . --- .../legacy-import/legacy-import.controller.ts | 10 ++ .../legacy-import/legacy-import.service.ts | 104 +++++++++++++++++- .../legacy-import/legacy-import.types.ts | 8 ++ apps/web/src/api/hooks.ts | 11 ++ apps/web/src/api/types.ts | 8 ++ .../web/src/pages/admin/LegacyMatchesPage.tsx | 60 +++++++--- 6 files changed, 184 insertions(+), 17 deletions(-) diff --git a/apps/api/src/modules/legacy-import/legacy-import.controller.ts b/apps/api/src/modules/legacy-import/legacy-import.controller.ts index 3a63b05d..6df3f88b 100644 --- a/apps/api/src/modules/legacy-import/legacy-import.controller.ts +++ b/apps/api/src/modules/legacy-import/legacy-import.controller.ts @@ -123,6 +123,16 @@ export class LegacyImportController { }); } + @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, diff --git a/apps/api/src/modules/legacy-import/legacy-import.service.ts b/apps/api/src/modules/legacy-import/legacy-import.service.ts index 2f46721a..a56c3210 100644 --- a/apps/api/src/modules/legacy-import/legacy-import.service.ts +++ b/apps/api/src/modules/legacy-import/legacy-import.service.ts @@ -37,6 +37,7 @@ import { type LegacyImportMemberJob, type LegacyImportMemberWindowView, type LegacyMatchListResponse, + type RebuildLegacyMatchesResponse, type StartLegacyImportRequest, type StartLegacyImportResponse, } from './legacy-import.types'; @@ -335,6 +336,102 @@ export class LegacyImportService { 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; @@ -386,7 +483,7 @@ export class LegacyImportService { const winnerSide = gameResult.winnerSide; const matchPayload: Partial = { communityId: new mongoose.Types.ObjectId(job.communityId), - title: `[Legacy] ${this.formatLegacyDate(detail.info.gameCreation)}`, + title: this.formatLegacyDate(detail.info.gameCreation), description: 'Legacy import (자동 생성)', mapId: detail.info.mapId ?? 11, status: 'finished', @@ -647,6 +744,11 @@ export class LegacyImportService { 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', diff --git a/apps/api/src/modules/legacy-import/legacy-import.types.ts b/apps/api/src/modules/legacy-import/legacy-import.types.ts index 559e2237..e1559b30 100644 --- a/apps/api/src/modules/legacy-import/legacy-import.types.ts +++ b/apps/api/src/modules/legacy-import/legacy-import.types.ts @@ -65,3 +65,11 @@ export interface LegacyMatchListResponse { export interface BulkDeleteLegacyMatchesRequest { matchIds?: string[]; } + +export interface RebuildLegacyMatchesResponse { + scanned: number; + rebuilt: number; + normalizedTitles: number; + skippedMissingRaw: number; + skippedNoKnownParticipants: number; +} diff --git a/apps/web/src/api/hooks.ts b/apps/web/src/api/hooks.ts index e350e01b..5853d8e6 100644 --- a/apps/web/src/api/hooks.ts +++ b/apps/web/src/api/hooks.ts @@ -34,6 +34,7 @@ import type { MeResponse, RiotQueueGroup, RiotRankFilter, + RebuildLegacyMatchesResponse, TournamentCodeBulkUploadResponse, TournamentCodeListResponse, TournamentCodeState, @@ -619,6 +620,16 @@ export function useBulkDeleteLegacyMatches(communityId = DEFAULT_COMMUNITY_ID) { }); } +export function useRebuildLegacyMatches(communityId = DEFAULT_COMMUNITY_ID) { + return useMutation({ + mutationFn: () => + apiFetch( + `/communities/${communityId}/legacy-matches/rebuild`, + { method: 'POST' }, + ), + }); +} + export function useMe() { const authUserId = useAuthUserId(); const { accounts, inProgress, instance } = useMsal(); diff --git a/apps/web/src/api/types.ts b/apps/web/src/api/types.ts index 18dfb9be..0a431f5b 100644 --- a/apps/web/src/api/types.ts +++ b/apps/web/src/api/types.ts @@ -704,6 +704,14 @@ export interface LegacyMatchListResponse { nextCursor: number | null; } +export interface RebuildLegacyMatchesResponse { + scanned: number; + rebuilt: number; + normalizedTitles: number; + skippedMissingRaw: number; + skippedNoKnownParticipants: number; +} + export type ApiRiotJobPriority = 'high' | 'low'; export type ApiRiotJobState = 'queued' | 'processing' | 'completed' | 'failed'; diff --git a/apps/web/src/pages/admin/LegacyMatchesPage.tsx b/apps/web/src/pages/admin/LegacyMatchesPage.tsx index e04d5c18..adabd578 100644 --- a/apps/web/src/pages/admin/LegacyMatchesPage.tsx +++ b/apps/web/src/pages/admin/LegacyMatchesPage.tsx @@ -6,6 +6,7 @@ import { useDeleteMatch, useLegacyMatches, useMe, + useRebuildLegacyMatches, } from '../../api/hooks'; import type { LegacyMatchListItem } from '../../api/types'; import { DEFAULT_COMMUNITY_ID } from '../../config'; @@ -41,6 +42,7 @@ export default function LegacyMatchesPage() { const matchesQuery = useLegacyMatches(communityId, canManage); const deleteMutation = useDeleteMatch(communityId); const bulkDeleteMutation = useBulkDeleteLegacyMatches(communityId); + const rebuildMutation = useRebuildLegacyMatches(communityId); const matches = matchesQuery.data?.pages.flatMap((page) => page.matches) ?? []; const selectedIds = useMemo( () => Object.entries(selected).filter(([, value]) => value).map(([id]) => id), @@ -57,6 +59,9 @@ export default function LegacyMatchesPage() { queryClient.invalidateQueries({ queryKey: ['community-internal-stats-leaderboard', communityId], }), + queryClient.invalidateQueries({ + queryKey: ['community-internal-stats-team-insights', communityId], + }), ]); } @@ -104,24 +109,47 @@ export default function LegacyMatchesPage() { 백필로 들어온 매치를 확인하고 외부 토너먼트 기록을 삭제할 수 있습니다.

- +
+ + +
+ {rebuildMutation.data && ( +
+ 재구성 완료: {rebuildMutation.data.scanned}개 확인 ·{' '} + {rebuildMutation.data.rebuilt}개 갱신 · 제목 정리{' '} + {rebuildMutation.data.normalizedTitles}개 +
+ )} +