diff --git a/apps/api/src/modules/auction/auction.service.spec.ts b/apps/api/src/modules/auction/auction.service.spec.ts index 3cfdb577..9e53a110 100644 --- a/apps/api/src/modules/auction/auction.service.spec.ts +++ b/apps/api/src/modules/auction/auction.service.spec.ts @@ -183,6 +183,77 @@ describe('AuctionService', () => { expect(auctionRedisCommandService.placeBid).not.toHaveBeenCalled(); }); + it('lists auction history with a summary projection and short local cache', async () => { + const nowSpy = jest.spyOn(Date, 'now').mockReturnValue(1000); + const matchId = new mongoose.Types.ObjectId(); + const communityId = new mongoose.Types.ObjectId(); + const match = { + communityId: { + equals: jest.fn((value) => value.toString() === communityId.toString()), + }, + }; + const historyRows = [ + { + action: 'sold', + sequence: 4, + takenAt: new Date('2026-06-26T00:00:00.000Z'), + }, + { + action: 'open-bidding', + sequence: 3, + takenAt: new Date('2026-06-26T00:00:01.000Z'), + }, + ]; + const historyExec = jest.fn().mockResolvedValue(historyRows); + const historyLean = jest.fn().mockReturnValue({ exec: historyExec }); + const historyLimit = jest.fn().mockReturnValue({ lean: historyLean }); + const historySort = jest.fn().mockReturnValue({ limit: historyLimit }); + const historySelect = jest.fn().mockReturnValue({ sort: historySort }); + const historyFind = jest.fn().mockReturnValue({ select: historySelect }); + const { service } = createService({ + auctionHistory: { + find: historyFind, + }, + match: { + findById: jest.fn().mockReturnValue({ + select: jest.fn().mockResolvedValue(match), + }), + }, + }); + + await expect( + service.listHistory({ matchId, communityId }), + ).resolves.toEqual([ + { + action: 'sold', + actionLabel: '낙찰 처리', + sequence: 4, + takenAt: historyRows[0].takenAt, + }, + { + action: 'open-bidding', + actionLabel: '입찰 열기', + sequence: 3, + takenAt: historyRows[1].takenAt, + }, + ]); + await service.listHistory({ matchId, communityId }); + + expect(historyFind).toHaveBeenCalledTimes(1); + expect(historySelect).toHaveBeenCalledWith({ + _id: 0, + action: 1, + sequence: 1, + takenAt: 1, + }); + expect(historySort).toHaveBeenCalledWith({ sequence: -1 }); + expect(historyLimit).toHaveBeenCalledWith(100); + + nowSpy.mockReturnValue(3101); + await service.listHistory({ matchId, communityId }); + expect(historyFind).toHaveBeenCalledTimes(2); + }); + it('starts an auction without reserving tournament codes before teams are complete', async () => { const matchId = new mongoose.Types.ObjectId(); const communityId = new mongoose.Types.ObjectId(); diff --git a/apps/api/src/modules/auction/auction.service.ts b/apps/api/src/modules/auction/auction.service.ts index 9c7f3b47..6c92e9a6 100644 --- a/apps/api/src/modules/auction/auction.service.ts +++ b/apps/api/src/modules/auction/auction.service.ts @@ -58,6 +58,9 @@ const AUCTION_ACTION_REQUEST_REPLAY_WINDOW_MS = 5 * 60 * 1000; const AUCTION_ACTION_REQUEST_LEASE_DURATION_MS = 15 * 1000; const AUCTION_REHYDRATE_LOCK_TTL_MS = 5 * 1000; const AUCTION_REHYDRATE_LOCK_WAIT_MS = 50; +const AUCTION_HISTORY_LIST_LIMIT = 100; +const AUCTION_HISTORY_LIST_CACHE_TTL_MS = 2 * 1000; +const AUCTION_HISTORY_LIST_CACHE_MAX_ENTRIES = 256; interface StartAuctionParams { matchId: mongoose.Types.ObjectId; @@ -141,6 +144,17 @@ export interface AuctionHistoryView { takenAt: Date; } +interface AuctionHistorySummaryRecord { + sequence: number; + action: AuctionActionType; + takenAt: Date; +} + +interface AuctionHistoryListCacheEntry { + entries: AuctionHistoryView[]; + expiresAtTimestampMS: number; +} + export type AuctionStateResponse = AuctionDocument | Record; export interface AuctionBidDeltaResponse { @@ -183,6 +197,10 @@ function isMongoDuplicateKeyError(error: unknown): boolean { @Injectable() export class AuctionService { private readonly logger = new Logger(AuctionService.name); + private readonly auctionHistoryListCache = new Map< + string, + AuctionHistoryListCacheEntry + >(); constructor( private readonly connectionService: ConnectionService, @@ -551,18 +569,64 @@ export class AuctionService { throw new BadRequestException('Match does not belong to this community.'); } - const entries = await AuctionHistoryModel.find({ matchId: params.matchId }) - .sort({ sequence: -1 }) - .limit(100) - .lean() - .exec(); + const cacheKey = this.buildAuctionHistoryListCacheKey(params); + const cachedHistory = this.getCachedAuctionHistoryList(cacheKey); + if (cachedHistory) return cachedHistory; - return entries.map((entry) => ({ + const entries = (await AuctionHistoryModel.find({ matchId: params.matchId }) + .select({ _id: 0, action: 1, sequence: 1, takenAt: 1 }) + .sort({ sequence: -1 }) + .limit(AUCTION_HISTORY_LIST_LIMIT) + .lean() + .exec()) as AuctionHistorySummaryRecord[]; + + const history = entries.map((entry) => ({ sequence: entry.sequence, action: entry.action, actionLabel: this.formatHistoryActionLabel(entry.action), takenAt: entry.takenAt, })); + this.setCachedAuctionHistoryList(cacheKey, history); + return history; + } + + private buildAuctionHistoryListCacheKey(params: { + matchId: mongoose.Types.ObjectId; + communityId: mongoose.Types.ObjectId; + }): string { + return `${params.communityId.toString()}:${params.matchId.toString()}`; + } + + private getCachedAuctionHistoryList( + cacheKey: string, + ): AuctionHistoryView[] | null { + const cached = this.auctionHistoryListCache.get(cacheKey); + if (!cached) return null; + if (cached.expiresAtTimestampMS <= Date.now()) { + this.auctionHistoryListCache.delete(cacheKey); + return null; + } + return cached.entries; + } + + private setCachedAuctionHistoryList( + cacheKey: string, + entries: AuctionHistoryView[], + ): void { + if ( + !this.auctionHistoryListCache.has(cacheKey) && + this.auctionHistoryListCache.size >= + AUCTION_HISTORY_LIST_CACHE_MAX_ENTRIES + ) { + const oldestKey = this.auctionHistoryListCache.keys().next().value as + | string + | undefined; + if (oldestKey) this.auctionHistoryListCache.delete(oldestKey); + } + this.auctionHistoryListCache.set(cacheKey, { + entries, + expiresAtTimestampMS: Date.now() + AUCTION_HISTORY_LIST_CACHE_TTL_MS, + }); } public async openBidding(