import { LeagueRepository } from '../ports/LeagueRepository'; import { DriverRepository } from '../../../racing/domain/repositories/DriverRepository'; import { EventPublisher } from '../../../shared/ports/EventPublisher'; import { JoinLeagueCommand } from '../ports/JoinLeagueCommand'; export class JoinLeagueUseCase { constructor( private readonly leagueRepository: LeagueRepository, private readonly driverRepository: DriverRepository, private readonly eventPublisher: EventPublisher, ) {} async execute(command: JoinLeagueCommand): Promise { const league = await this.leagueRepository.findById(command.leagueId); if (!league) { throw new Error('League not found'); } const driver = await this.driverRepository.findById(command.driverId); if (!driver) { throw new Error('Driver not found'); } if (league.approvalRequired) { await this.leagueRepository.addPendingRequests(command.leagueId, [ { id: `request-${Date.now()}`, driverId: command.driverId, name: driver.name.toString(), requestDate: new Date(), }, ]); } else { await this.leagueRepository.addLeagueMembers(command.leagueId, [ { driverId: command.driverId, name: driver.name.toString(), role: 'member', joinDate: new Date(), }, ]); } } }