diff --git a/apps/api/src/modules/match/match.controller.ts b/apps/api/src/modules/match/match.controller.ts index 74b448a2..81e2a985 100644 --- a/apps/api/src/modules/match/match.controller.ts +++ b/apps/api/src/modules/match/match.controller.ts @@ -72,16 +72,27 @@ export class MatchController { communityId: mongoose.Types.ObjectId, @Query('status') status?: MatchStatus, @Query('limit') limit?: string, + @Query('page') page?: string, + @Query('offset') offset?: string, + @Query('scheduledFrom') scheduledFrom?: string, + @Query('scheduledTo') scheduledTo?: string, + @Query('sort') sort?: 'asc' | 'desc', ) { - const matches = await this.matchService.list({ + const result = await this.matchService.listPage({ communityId, status, - limit: limit ? Number(limit) : undefined, + limit: this.toOptionalNumber(limit), + page: this.toOptionalNumber(page), + offset: this.toOptionalNumber(offset), + scheduledFrom: this.toOptionalDate(scheduledFrom), + scheduledTo: this.toOptionalDate(scheduledTo), + sort, }); - if (!currentUser) return { matches }; + if (!currentUser) return result; const myId = currentUser._id; return { - matches: matches.map((m) => this.annotateJoined(m, myId)), + ...result, + matches: result.matches.map((m) => this.annotateJoined(m, myId)), }; } @@ -211,4 +222,15 @@ export class MatchController { throw new BadRequestException('Different community.'); } } + + private toOptionalNumber(value?: string): number | undefined { + const numberValue = Number(value); + return Number.isFinite(numberValue) ? numberValue : undefined; + } + + private toOptionalDate(value?: string): Date | undefined { + if (!value) return undefined; + const date = new Date(value); + return Number.isNaN(date.getTime()) ? undefined : date; + } } diff --git a/apps/api/src/modules/match/match.service.spec.ts b/apps/api/src/modules/match/match.service.spec.ts index a846a29f..337f8599 100644 --- a/apps/api/src/modules/match/match.service.spec.ts +++ b/apps/api/src/modules/match/match.service.spec.ts @@ -36,6 +36,135 @@ describe('MatchService', () => { }; } + function createServiceWithListModel() { + const exec = jest.fn().mockResolvedValue([{ title: 'second page' }]); + const limit = jest.fn().mockReturnValue({ exec }); + const skip = jest.fn().mockReturnValue({ limit }); + const sort = jest.fn().mockReturnValue({ skip }); + const find = jest.fn().mockReturnValue({ sort }); + const countExec = jest.fn().mockResolvedValue(12); + const countDocuments = jest.fn().mockReturnValue({ exec: countExec }); + const MatchModel = { + find, + countDocuments, + }; + const connectionService = { + getModel: jest.fn().mockImplementation(async (name: string) => { + if (name === 'match') return MatchModel; + return {}; + }), + }; + const tournamentCodeService = { + buildSlots: jest.fn().mockResolvedValue([]), + releaseFromMatch: jest.fn().mockResolvedValue(undefined), + }; + const communityStatsService = { + invalidateCommunity: jest.fn().mockResolvedValue(undefined), + }; + const matchStatsService = { + buildAndUpsert: jest.fn().mockResolvedValue(undefined), + removeByMatch: jest.fn().mockResolvedValue(undefined), + }; + + return { + service: new MatchService( + connectionService as never, + tournamentCodeService as never, + communityStatsService as never, + matchStatsService as never, + ), + MatchModel, + sort, + skip, + limit, + }; + } + + it('returns a paginated match list with total count', async () => { + const communityId = new mongoose.Types.ObjectId(); + const { service, MatchModel, sort, skip, limit } = + createServiceWithListModel(); + + const page = await service.listPage({ + communityId, + page: 2, + limit: 5, + }); + + expect(MatchModel.find).toHaveBeenCalledWith({ communityId }); + expect(MatchModel.countDocuments).toHaveBeenCalledWith({ communityId }); + expect(sort).toHaveBeenCalledWith({ scheduledAt: -1 }); + expect(skip).toHaveBeenCalledWith(5); + expect(limit).toHaveBeenCalledWith(5); + expect(page).toEqual({ + matches: [{ title: 'second page' }], + total: 12, + page: 2, + limit: 5, + }); + }); + + it('applies offset before page-based pagination', async () => { + const communityId = new mongoose.Types.ObjectId(); + const { service, skip } = createServiceWithListModel(); + + const page = await service.listPage({ + communityId, + offset: 4, + page: 2, + limit: 5, + }); + + expect(skip).toHaveBeenCalledWith(9); + expect(page.total).toBe(8); + }); + + it('filters future matches and sorts oldest first for upcoming sections', async () => { + const communityId = new mongoose.Types.ObjectId(); + const scheduledFrom = new Date('2026-06-08T12:00:00.000Z'); + const { service, MatchModel, sort } = createServiceWithListModel(); + + await service.listPage({ + communityId, + scheduledFrom, + sort: 'asc', + limit: 4, + }); + + expect(MatchModel.find).toHaveBeenCalledWith({ + communityId, + scheduledAt: { $gte: scheduledFrom }, + }); + expect(MatchModel.countDocuments).toHaveBeenCalledWith({ + communityId, + scheduledAt: { $gte: scheduledFrom }, + }); + expect(sort).toHaveBeenCalledWith({ scheduledAt: 1 }); + }); + + it('filters past matches and keeps newest first for history sections', async () => { + const communityId = new mongoose.Types.ObjectId(); + const scheduledTo = new Date('2026-06-08T12:00:00.000Z'); + const { service, MatchModel, sort } = createServiceWithListModel(); + + await service.listPage({ + communityId, + scheduledTo, + sort: 'desc', + limit: 10, + }); + + expect(MatchModel.find).toHaveBeenCalledWith({ + communityId, + scheduledAt: { $lt: scheduledTo }, + }); + expect(MatchModel.countDocuments).toHaveBeenCalledWith({ + communityId, + scheduledAt: { $lt: scheduledTo }, + }); + expect(sort).toHaveBeenCalledWith({ scheduledAt: -1 }); + }); + it('clears game schedule when a built match returns to recruiting', async () => { const communityId = new mongoose.Types.ObjectId(); const match = { diff --git a/apps/api/src/modules/match/match.service.ts b/apps/api/src/modules/match/match.service.ts index d1a1f184..a3db4a51 100644 --- a/apps/api/src/modules/match/match.service.ts +++ b/apps/api/src/modules/match/match.service.ts @@ -23,6 +23,8 @@ import { TournamentCodeService } from '../tournament-code/tournament-code.servic const MATCH_SHORT_CODE_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; const MATCH_SHORT_CODE_LENGTH = 4; const MATCH_SHORT_CODE_GENERATION_ATTEMPTS = 20; +const DEFAULT_MATCH_LIST_LIMIT = 50; +const MAX_MATCH_LIST_LIMIT = 200; interface CreateMatchParams { communityId: mongoose.Types.ObjectId; @@ -40,6 +42,23 @@ interface ListMatchesQuery { communityId: mongoose.Types.ObjectId; status?: MatchStatus; limit?: number; + scheduledFrom?: Date; + scheduledTo?: Date; + sort?: 'asc' | 'desc'; +} + +interface ListMatchesPageQuery extends ListMatchesQuery { + page?: number; + offset?: number; +} + +interface MatchListFilter { + communityId: mongoose.Types.ObjectId; + status?: MatchStatus; + scheduledAt?: { + $gte?: Date; + $lt?: Date; + }; } interface JoinMatchParams { @@ -101,19 +120,42 @@ export class MatchService { public async list(query: ListMatchesQuery): Promise { const MatchModel = await this.connectionService.getModel('match'); - const filter: { - communityId: mongoose.Types.ObjectId; - status?: MatchStatus; - } = { - communityId: query.communityId, - }; - if (query.status) filter.status = query.status; + const filter = this.buildListFilter(query); return MatchModel.find(filter) - .sort({ scheduledAt: -1 }) - .limit(Math.min(query.limit ?? 50, 200)) + .sort(this.buildListSort(query.sort)) + .limit(this.normalizeListLimit(query.limit)) .exec(); } + public async listPage(query: ListMatchesPageQuery): Promise<{ + matches: MatchDocument[]; + total: number; + page: number; + limit: number; + }> { + const MatchModel = await this.connectionService.getModel('match'); + const filter = this.buildListFilter(query); + const page = this.normalizeListPage(query.page); + const limit = this.normalizeListLimit(query.limit); + const offset = this.normalizeListOffset(query.offset); + const skip = offset + (page - 1) * limit; + const [matches, rawTotal] = await Promise.all([ + MatchModel.find(filter) + .sort(this.buildListSort(query.sort)) + .skip(skip) + .limit(limit) + .exec(), + MatchModel.countDocuments(filter).exec(), + ]); + + return { + matches, + total: Math.max(rawTotal - offset, 0), + page, + limit, + }; + } + public async ensureShortCode(match: MatchDocument): Promise { if (match.shortCode) return match; @@ -394,6 +436,44 @@ export class MatchService { }; } + private buildListFilter(query: ListMatchesQuery): MatchListFilter { + const filter: MatchListFilter = { + communityId: query.communityId, + }; + if (query.status) filter.status = query.status; + if (query.scheduledFrom || query.scheduledTo) { + filter.scheduledAt = {}; + if (query.scheduledFrom) filter.scheduledAt.$gte = query.scheduledFrom; + if (query.scheduledTo) filter.scheduledAt.$lt = query.scheduledTo; + } + return filter; + } + + private buildListSort(sort?: 'asc' | 'desc'): { scheduledAt: 1 | -1 } { + return { scheduledAt: sort === 'asc' ? 1 : -1 }; + } + + private normalizeListLimit(limit?: number): number { + if (!Number.isFinite(limit) || !limit || limit < 1) { + return DEFAULT_MATCH_LIST_LIMIT; + } + return Math.min(Math.floor(limit), MAX_MATCH_LIST_LIMIT); + } + + private normalizeListPage(page?: number): number { + if (!Number.isFinite(page) || !page || page < 1) { + return 1; + } + return Math.floor(page); + } + + private normalizeListOffset(offset?: number): number { + if (!Number.isFinite(offset) || !offset || offset < 1) { + return 0; + } + return Math.floor(offset); + } + private async generateUniqueShortCode( MatchModel: mongoose.Model, communityId: mongoose.Types.ObjectId, diff --git a/apps/web/src/api/hooks.ts b/apps/web/src/api/hooks.ts index b619b68c..f7b096d3 100644 --- a/apps/web/src/api/hooks.ts +++ b/apps/web/src/api/hooks.ts @@ -50,6 +50,11 @@ const DDRAGON_MANIFEST_STALE_MS = 24 * 60 * 60 * 1000; interface MatchesQueryOptions { status?: MatchStatus; limit?: number; + page?: number; + offset?: number; + scheduledFrom?: string; + scheduledTo?: string; + sort?: 'asc' | 'desc'; } export interface UserHistoryFilters { @@ -74,6 +79,11 @@ function createMatchesPath( const searchParams = new URLSearchParams(); if (options.status) searchParams.set('status', options.status); if (options.limit) searchParams.set('limit', String(options.limit)); + if (options.page) searchParams.set('page', String(options.page)); + if (options.offset) searchParams.set('offset', String(options.offset)); + if (options.scheduledFrom) searchParams.set('scheduledFrom', options.scheduledFrom); + if (options.scheduledTo) searchParams.set('scheduledTo', options.scheduledTo); + if (options.sort) searchParams.set('sort', options.sort); const queryString = searchParams.toString(); return `/communities/${communityId}/matches${queryString ? `?${queryString}` : ''}`; @@ -93,7 +103,17 @@ export function useMatches( communityId = DEFAULT_COMMUNITY_ID, ) { return useQuery({ - queryKey: ['matches', communityId, options.status ?? null, options.limit ?? null], + queryKey: [ + 'matches', + communityId, + options.status ?? null, + options.limit ?? null, + options.page ?? null, + options.offset ?? null, + options.scheduledFrom ?? null, + options.scheduledTo ?? null, + options.sort ?? null, + ], queryFn: async () => { const response = await apiFetch( createMatchesPath(communityId, options), @@ -105,6 +125,30 @@ export function useMatches( }); } +export function useMatchesPage( + options: MatchesQueryOptions, + communityId = DEFAULT_COMMUNITY_ID, +) { + return useQuery({ + queryKey: [ + 'matches-page', + communityId, + options.status ?? null, + options.limit ?? null, + options.page ?? null, + options.offset ?? null, + options.scheduledFrom ?? null, + options.scheduledTo ?? null, + options.sort ?? null, + ], + queryFn: () => + apiFetch(createMatchesPath(communityId, options), { + auth: 'none', + }), + enabled: Boolean(communityId), + }); +} + export function useMatch(matchId: string | undefined, communityId = DEFAULT_COMMUNITY_ID) { return useQuery({ queryKey: ['match', communityId, matchId ?? null], diff --git a/apps/web/src/api/types.ts b/apps/web/src/api/types.ts index 5adde77a..d65f0211 100644 --- a/apps/web/src/api/types.ts +++ b/apps/web/src/api/types.ts @@ -340,6 +340,9 @@ export interface MeResponse { export interface MatchesResponse { matches: ApiMatch[]; + total: number; + page: number; + limit: number; } export interface MatchMutationRequest { diff --git a/apps/web/src/pages/HomePage.tsx b/apps/web/src/pages/HomePage.tsx index bb2c45a2..cd7fd43d 100644 --- a/apps/web/src/pages/HomePage.tsx +++ b/apps/web/src/pages/HomePage.tsx @@ -1,10 +1,134 @@ +import { useState } from 'react'; +import { ChevronLeft, ChevronRight } from 'lucide-react'; import { Link } from 'react-router'; import { mapApiMatchToMatchDetail } from '../api/mappers'; -import { useMatches, useMe } from '../api/hooks'; +import { useMatches, useMatchesPage, useMe } from '../api/hooks'; import { DEFAULT_COMMUNITY_ID } from '../config'; const UPCOMING_GRID_MATCH_LIMIT = 3; const UPCOMING_MATCH_LIST_LIMIT = UPCOMING_GRID_MATCH_LIMIT + 1; +const HISTORY_MATCH_PAGE_SIZE = 10; +const EMPTY_SCHEDULE_TEXT = '예정된 매치가 없습니다.'; + +function SkeletonBlock({ className }: { className: string }) { + return ( +