This commit is contained in:
2025-12-16 21:05:01 +01:00
parent f61e3a4e5a
commit 7532c7ed6d
207 changed files with 7861 additions and 2606 deletions

View File

@@ -1,10 +1,12 @@
import type { AsyncUseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import type { IRaceEventRepository } from '../../domain/repositories/IRaceEventRepository';
import type { IDomainEventPublisher } from '@core/shared/domain/IDomainEvent';
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { DomainEventPublisher } from '@/shared/domain/DomainEvent';
import { RaceEventStewardingClosedEvent } from '../../domain/events/RaceEventStewardingClosed';
import { Result } from '@core/shared/result/Result';
import { RacingDomainValidationError } from '../../domain/errors/RacingDomainError';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { CloseRaceEventStewardingCommand } from './CloseRaceEventStewardingCommand';
import type { RaceEvent } from '../../domain/entities/RaceEvent';
@@ -18,17 +20,19 @@ import type { RaceEvent } from '../../domain/entities/RaceEvent';
* to automatically close stewarding windows based on league configuration.
*/
export class CloseRaceEventStewardingUseCase
implements AsyncUseCase<CloseRaceEventStewardingCommand, Result<void, RacingDomainValidationError>>
implements AsyncUseCase<CloseRaceEventStewardingCommand, void, string>
{
constructor(
private readonly logger: Logger,
private readonly raceEventRepository: IRaceEventRepository,
private readonly domainEventPublisher: IDomainEventPublisher,
private readonly raceRegistrationRepository: IRaceRegistrationRepository,
private readonly penaltyRepository: IPenaltyRepository,
private readonly domainEventPublisher: DomainEventPublisher,
) {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async execute(_command: CloseRaceEventStewardingCommand): Promise<Result<void, RacingDomainValidationError>> {
async execute(_command: CloseRaceEventStewardingCommand): Promise<Result<void, ApplicationErrorCode<string>>> {
try {
// Find all race events awaiting stewarding that have expired windows
const expiredEvents = await this.raceEventRepository.findAwaitingStewardingClose();
@@ -40,7 +44,7 @@ export class CloseRaceEventStewardingUseCase
return Result.ok(undefined);
} catch (error) {
this.logger.error('Failed to close race event stewarding', error instanceof Error ? error : new Error(String(error)));
return Result.err(new RacingDomainValidationError('Failed to close stewarding for race events'));
return Result.err({ code: 'FAILED_TO_CLOSE_STEWARDING' });
}
}
@@ -50,11 +54,11 @@ export class CloseRaceEventStewardingUseCase
const closedRaceEvent = raceEvent.closeStewarding();
await this.raceEventRepository.update(closedRaceEvent);
// Get list of participating drivers (would need to be implemented)
const driverIds = await this.getParticipatingDriverIds();
// Get list of participating drivers
const driverIds = await this.getParticipatingDriverIds(raceEvent);
// Check if any penalties were applied during stewarding
const hadPenaltiesApplied = await this.checkForAppliedPenalties();
const hadPenaltiesApplied = await this.checkForAppliedPenalties(raceEvent);
// Publish domain event to trigger final results notifications
const event = new RaceEventStewardingClosedEvent({
@@ -70,19 +74,17 @@ export class CloseRaceEventStewardingUseCase
} catch (error) {
this.logger.error(`Failed to close stewarding for race event ${raceEvent.id}`, error instanceof Error ? error : new Error(String(error)));
// TODO: In production, this would trigger alerts/monitoring
// In production, this would trigger alerts/monitoring
throw error;
}
}
private async getParticipatingDriverIds(): Promise<string[]> {
// TODO: Implement query for participating driver IDs from race event registrations
// This would typically involve querying race registrations for the event
return [];
private async getParticipatingDriverIds(raceEvent: RaceEvent): Promise<string[]> {
return await this.raceRegistrationRepository.getRegisteredDrivers(raceEvent.id);
}
private async checkForAppliedPenalties(): Promise<boolean> {
// TODO: Implement check for applied penalties during stewarding window
// This would query the penalty repository for penalties related to this race event
return false;
private async checkForAppliedPenalties(raceEvent: RaceEvent): Promise<boolean> {
const penalties = await this.penaltyRepository.findByRaceId(raceEvent.id);
return penalties.length > 0;
}
}