refactor: whitelist public match responses

This commit is contained in:
dahyeon committed 2026-07-12 19:28:04 +00:00
1 parent 874d785381
commit 28e9ac1529
4 files changed
+248 -40

No files matched your search

@@ -0,0 +1,130 @@
import type {
MatchGameMode,
MatchStatus,
Position,
} from '../global/database/schema';
type IdLike = string | { toString(): string };
interface PublicMatchSource {
_id: IdLike;
communityId: IdLike;
competitionId?: IdLike;
shortCode?: string;
title: string;
description: string;
mapId: number;
status: MatchStatus;
scheduledAt: Date | string;
maxParticipants: number;
gameCount: number;
gameMode: MatchGameMode;
roundRobinMaxRounds?: number;
hostUserId?: IdLike;
participants: Array<{
userId: IdLike;
gameName?: string;
tagLine?: string;
profileIconId?: number;
tier?: unknown;
preferredPositions: Array<{ position: Position; priority: number }>;
joinedAt: Date | string;
excluded?: boolean;
[key: string]: unknown;
}>;
teams: Array<{
teamIndex: number;
name: string;
captainUserId?: IdLike;
members: Array<{
userId: IdLike;
assignedPosition: Position;
cost?: number;
[key: string]: unknown;
}>;
[key: string]: unknown;
}>;
prizes: string[];
notices: string[];
schedule: Array<{ time: string; label: string }>;
createdAt: Date | string;
updatedAt: Date | string;
[key: string]: unknown;
}
/** Public Match DTO. Competitive results and Tournament Codes live elsewhere. */
export function presentPublicMatch(
source: PublicMatchSource,
options: { joined?: boolean } = {},
) {
return {
_id: source._id.toString(),
communityId: source.communityId.toString(),
...(source.competitionId
? { competitionId: source.competitionId.toString() }
: {}),
...(source.shortCode ? { shortCode: source.shortCode } : {}),
title: source.title,
description: source.description,
mapId: source.mapId,
status: source.status,
scheduledAt: asIso(source.scheduledAt),
maxParticipants: source.maxParticipants,
gameCount: source.gameCount,
gameMode: source.gameMode,
...(source.roundRobinMaxRounds === undefined
? {}
: { roundRobinMaxRounds: source.roundRobinMaxRounds }),
...(source.hostUserId ? { hostUserId: source.hostUserId.toString() } : {}),
participants: source.participants.map((participant) => ({
userId: participant.userId.toString(),
...(participant.gameName ? { gameName: participant.gameName } : {}),
...(participant.tagLine ? { tagLine: participant.tagLine } : {}),
...(participant.profileIconId === undefined
? {}
: { profileIconId: participant.profileIconId }),
...(participant.tier === undefined
? {}
: { tier: toWireValue(participant.tier) }),
preferredPositions: participant.preferredPositions.map((preference) => ({
position: preference.position,
priority: preference.priority,
})),
joinedAt: asIso(participant.joinedAt),
excluded: participant.excluded ?? false,
})),
teams: source.teams.map((team) => ({
teamIndex: team.teamIndex,
name: team.name,
...(team.captainUserId
? { captainUserId: team.captainUserId.toString() }
: {}),
members: team.members.map((member) => ({
userId: member.userId.toString(),
assignedPosition: member.assignedPosition,
...(member.cost === undefined ? {} : { cost: member.cost }),
})),
})),
prizes: [...source.prizes],
notices: [...source.notices],
schedule: source.schedule.map(({ time, label }) => ({ time, label })),
...(options.joined === undefined ? {} : { joined: options.joined }),
createdAt: asIso(source.createdAt),
updatedAt: asIso(source.updatedAt),
};
}
function asIso(value: Date | string): string {
return value instanceof Date ? value.toISOString() : value;
}
function toWireValue(value: unknown): unknown {
if (value instanceof Date) return value.toISOString();
if (Array.isArray(value)) return value.map(toWireValue);
if (typeof value === 'object' && value !== null) {
return Object.fromEntries(
Object.entries(value).map(([key, child]) => [key, toWireValue(child)]),
);
}
return value;
}