.
This commit is contained in:
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;
|
||||
}
|
||||
@@ -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<RebuildLegacyMatchesResponse>(
|
||||
`/communities/${communityId}/legacy-matches/rebuild`,
|
||||
{ method: 'POST' },
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function useMe() {
|
||||
const authUserId = useAuthUserId();
|
||||
const { accounts, inProgress, instance } = useMsal();
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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() {
|
||||
백필로 들어온 매치를 확인하고 외부 토너먼트 기록을 삭제할 수 있습니다.
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-ghost btn-sm px-4 text-xs text-red-400"
|
||||
disabled={selectedIds.length === 0 || bulkDeleteMutation.isPending}
|
||||
onClick={() => {
|
||||
if (!window.confirm(`${selectedIds.length}개 매치를 삭제할까요?`)) return;
|
||||
bulkDeleteMutation.mutate(selectedIds, {
|
||||
onSuccess: async () => {
|
||||
setSelected({});
|
||||
await invalidate();
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
선택 삭제 ({selectedIds.length})
|
||||
</button>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary btn-sm px-4 text-xs"
|
||||
disabled={rebuildMutation.isPending}
|
||||
onClick={() => {
|
||||
if (!window.confirm('현재 가입자 기준으로 과거 매치를 재구성할까요?')) {
|
||||
return;
|
||||
}
|
||||
rebuildMutation.mutate(undefined, { onSuccess: invalidate });
|
||||
}}
|
||||
>
|
||||
{rebuildMutation.isPending ? '재구성 중...' : '과거 데이터 재구성'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-ghost btn-sm px-4 text-xs text-red-400"
|
||||
disabled={selectedIds.length === 0 || bulkDeleteMutation.isPending}
|
||||
onClick={() => {
|
||||
if (!window.confirm(`${selectedIds.length}개 매치를 삭제할까요?`)) return;
|
||||
bulkDeleteMutation.mutate(selectedIds, {
|
||||
onSuccess: async () => {
|
||||
setSelected({});
|
||||
await invalidate();
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
선택 삭제 ({selectedIds.length})
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{rebuildMutation.data && (
|
||||
<div className="mb-4 border border-primary/20 bg-primary/5 px-3 py-2 text-xs text-base-content/55">
|
||||
재구성 완료: {rebuildMutation.data.scanned}개 확인 ·{' '}
|
||||
{rebuildMutation.data.rebuilt}개 갱신 · 제목 정리{' '}
|
||||
{rebuildMutation.data.normalizedTitles}개
|
||||
</div>
|
||||
)}
|
||||
|
||||
<section className="border border-base-content/10 bg-base-100 overflow-hidden">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full min-w-[900px] text-left">
|
||||
|
||||
Reference in new issue
Block a user