Some checks failed
CI / lint-typecheck (pull_request) Failing after 1m29s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
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<void> {
|
|
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(),
|
|
},
|
|
]);
|
|
}
|
|
}
|
|
}
|