diff --git a/apps/api/src/modules/auction/auction.service.spec.ts b/apps/api/src/modules/auction/auction.service.spec.ts new file mode 100644 index 00000000..298f361e --- /dev/null +++ b/apps/api/src/modules/auction/auction.service.spec.ts @@ -0,0 +1,186 @@ +import mongoose from 'mongoose'; +import { AuctionService } from './auction.service'; + +describe('AuctionService', () => { + function createService(models: Record) { + const connectionService = { + getModel: jest.fn((name: string) => models[name]), + createSession: jest.fn(), + }; + const enrichmentService = { + enrichRiotAccount: jest.fn(), + }; + + return { + service: new AuctionService( + connectionService as never, + enrichmentService as never, + ), + connectionService, + }; + } + + it('syncs completed auction teams into the match document', async () => { + const matchId = new mongoose.Types.ObjectId(); + const blueCaptainId = new mongoose.Types.ObjectId(); + const redCaptainId = new mongoose.Types.ObjectId(); + const blueMemberId = new mongoose.Types.ObjectId(); + const redMemberId = new mongoose.Types.ObjectId(); + + const auction = { + matchId, + phase: 'sold', + currentIndex: 1, + order: [{ userId: blueMemberId }, { userId: redMemberId }], + teams: [ + { + side: 'blue', + captainUserId: blueCaptainId, + points: 970, + members: [ + { + userId: blueMemberId, + assignedPosition: 'ADC', + cost: 30, + }, + ], + }, + { + side: 'red', + captainUserId: redCaptainId, + points: 940, + members: [ + { + userId: redMemberId, + assignedPosition: 'JGL', + cost: 60, + }, + ], + }, + ], + save: jest.fn().mockResolvedValue(undefined), + }; + const match = { + participants: [ + { + userId: blueCaptainId, + preferredPositions: [{ position: 'TOP', priority: 1 }], + }, + { + userId: redCaptainId, + preferredPositions: [{ position: 'MID', priority: 1 }], + }, + { + userId: blueMemberId, + preferredPositions: [{ position: 'ADC', priority: 1 }], + }, + { + userId: redMemberId, + preferredPositions: [{ position: 'JGL', priority: 1 }], + }, + ], + status: 'team-built', + teams: [], + save: jest.fn().mockResolvedValue(undefined), + }; + const { service } = createService({ + auction: { + findOne: jest.fn().mockResolvedValue(auction), + }, + match: { + findById: jest.fn().mockResolvedValue(match), + }, + }); + + await service.next(matchId); + + expect(auction.phase).toBe('complete'); + expect(auction.completedAt).toBeInstanceOf(Date); + expect(auction.save).toHaveBeenCalledTimes(1); + expect(match.save).toHaveBeenCalledTimes(1); + expect(match.teams).toEqual([ + { + side: 'blue', + name: '블루팀', + captainUserId: blueCaptainId, + members: [ + { + userId: blueCaptainId, + assignedPosition: 'TOP', + cost: 0, + }, + { + userId: blueMemberId, + assignedPosition: 'ADC', + cost: 30, + }, + ], + }, + { + side: 'red', + name: '레드팀', + captainUserId: redCaptainId, + members: [ + { + userId: redCaptainId, + assignedPosition: 'MID', + cost: 0, + }, + { + userId: redMemberId, + assignedPosition: 'JGL', + cost: 60, + }, + ], + }, + ]); + }); + + it('clears match teams when resetting an auction', async () => { + const matchId = new mongoose.Types.ObjectId(); + const communityId = new mongoose.Types.ObjectId(); + const session = { + startTransaction: jest.fn(), + commitTransaction: jest.fn().mockResolvedValue(undefined), + abortTransaction: jest.fn().mockResolvedValue(undefined), + endSession: jest.fn().mockResolvedValue(undefined), + }; + const auction = { + deleteOne: jest.fn().mockResolvedValue(undefined), + }; + const match = { + communityId, + status: 'team-built', + teams: [ + { + side: 'blue', + name: '블루팀', + members: [], + }, + ], + save: jest.fn().mockResolvedValue(undefined), + }; + const auctionFindOneQuery = { + session: jest.fn().mockResolvedValue(auction), + }; + const matchFindByIdQuery = { + session: jest.fn().mockResolvedValue(match), + }; + const { service, connectionService } = createService({ + auction: { + findOne: jest.fn().mockReturnValue(auctionFindOneQuery), + }, + match: { + findById: jest.fn().mockReturnValue(matchFindByIdQuery), + }, + }); + connectionService.createSession.mockResolvedValue(session); + + await service.reset({ matchId, communityId }); + + expect(match.status).toBe('recruiting'); + expect(match.teams).toEqual([]); + expect(match.save).toHaveBeenCalledWith({ session }); + expect(session.commitTransaction).toHaveBeenCalledTimes(1); + }); +}); diff --git a/apps/api/src/modules/auction/auction.service.ts b/apps/api/src/modules/auction/auction.service.ts index 44eb7f37..3968e475 100644 --- a/apps/api/src/modules/auction/auction.service.ts +++ b/apps/api/src/modules/auction/auction.service.ts @@ -9,6 +9,7 @@ import { ConnectionService } from '../global/database/connection.service'; import { AuctionDocument, AuctionPlayerSnapshot, + MatchTeamMember, Position, TeamSide, } from '../global/database/schema'; @@ -266,6 +267,9 @@ export class AuctionService { } auction.currentDeadline = undefined; await auction.save(); + if (auction.phase === 'complete') { + await this.syncMatchTeamsFromAuction(auction); + } return auction; } @@ -296,6 +300,7 @@ export class AuctionService { await auction.deleteOne({ session }); if (match.status === 'team-built') { match.status = 'recruiting'; + match.teams = []; await match.save({ session }); } await session.commitTransaction(); @@ -321,6 +326,52 @@ export class AuctionService { ); return Math.max(5, remainingSeconds); } + + private async syncMatchTeamsFromAuction( + auction: AuctionDocument, + ): Promise { + const MatchModel = await this.connectionService.getModel('match'); + const match = await MatchModel.findById(auction.matchId); + if (!match) throw new NotFoundException('Match not found.'); + + const participantByUserId = new Map( + match.participants.map((participant) => [ + participant.userId.toString(), + participant, + ]), + ); + + match.teams = auction.teams.map((team) => { + const captain = participantByUserId.get(team.captainUserId.toString()); + const members: MatchTeamMember[] = [ + { + userId: team.captainUserId, + assignedPosition: captain?.preferredPositions[0]?.position ?? 'MID', + cost: 0, + }, + ...team.members + .filter((member) => !member.userId.equals(team.captainUserId)) + .map((member) => ({ + userId: member.userId, + assignedPosition: member.assignedPosition, + cost: member.cost, + })), + ]; + + return { + side: team.side, + name: this.resolveTeamName(team.side), + captainUserId: team.captainUserId, + members, + }; + }); + match.status = 'team-built'; + await match.save(); + } + + private resolveTeamName(side: TeamSide): string { + return side === 'blue' ? '블루팀' : '레드팀'; + } } async function runWithConcurrency(