This commit is contained in:
dahyeon committed 2026-04-27 19:48:42 -07:00
1 parent b38cc03a71
commit 1fd37f992b
6 files changed
+184 -17

No files matched your search

@@ -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,
@@ -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<RebuildLegacyMatchesResponse> {
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<string, RiotMatchDto>();
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<Match> = {};
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<Set<string>> {
const matchIds = new Set<string>();
let start = 0;
@@ -386,7 +483,7 @@ export class LegacyImportService {
const winnerSide = gameResult.winnerSide;
const matchPayload: Partial<Match> = {
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',
@@ -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;
}