refactor
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
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';
|
||||
import type { RaceEventStewardingClosedEvent } from '../../domain/events/RaceEventStewardingClosed';
|
||||
import type { IDomainEventPublisher } from '@core/shared/domain/IDomainEvent';
|
||||
import { RaceEventStewardingClosedEvent } from '../../domain/events/RaceEventStewardingClosed';
|
||||
import { Result } from '@core/shared/result/Result';
|
||||
import { RacingDomainValidationError } from '../../domain/errors/RacingDomainError';
|
||||
import type { CloseRaceEventStewardingCommand } from './CloseRaceEventStewardingCommand';
|
||||
import type { RaceEvent } from '../../domain/entities/RaceEvent';
|
||||
|
||||
/**
|
||||
* Use Case: CloseRaceEventStewardingUseCase
|
||||
@@ -13,12 +17,8 @@ import type { RaceEventStewardingClosedEvent } from '../../domain/events/RaceEve
|
||||
* This would typically be run by a scheduled job (e.g., every 5 minutes)
|
||||
* to automatically close stewarding windows based on league configuration.
|
||||
*/
|
||||
export interface CloseRaceEventStewardingCommand {
|
||||
// No parameters needed - finds all expired events automatically
|
||||
}
|
||||
|
||||
export class CloseRaceEventStewardingUseCase
|
||||
implements UseCase<CloseRaceEventStewardingCommand, void, void, void>
|
||||
implements AsyncUseCase<CloseRaceEventStewardingCommand, Result<void, RacingDomainValidationError>>
|
||||
{
|
||||
constructor(
|
||||
private readonly logger: Logger,
|
||||
@@ -27,26 +27,34 @@ export class CloseRaceEventStewardingUseCase
|
||||
private readonly domainEventPublisher: IDomainEventPublisher,
|
||||
) {}
|
||||
|
||||
async execute(command: CloseRaceEventStewardingCommand): Promise<void> {
|
||||
// Find all race events awaiting stewarding that have expired windows
|
||||
const expiredEvents = await this.raceEventRepository.findAwaitingStewardingClose();
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async execute(_command: CloseRaceEventStewardingCommand): Promise<Result<void, RacingDomainValidationError>> {
|
||||
try {
|
||||
// Find all race events awaiting stewarding that have expired windows
|
||||
const expiredEvents = await this.raceEventRepository.findAwaitingStewardingClose();
|
||||
|
||||
for (const raceEvent of expiredEvents) {
|
||||
await this.closeStewardingForRaceEvent(raceEvent);
|
||||
for (const raceEvent of expiredEvents) {
|
||||
await this.closeStewardingForRaceEvent(raceEvent);
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
|
||||
private async closeStewardingForRaceEvent(raceEvent: any): Promise<void> {
|
||||
private async closeStewardingForRaceEvent(raceEvent: RaceEvent): Promise<void> {
|
||||
try {
|
||||
// Close the stewarding window
|
||||
const closedRaceEvent = raceEvent.closeStewarding();
|
||||
await this.raceEventRepository.update(closedRaceEvent);
|
||||
|
||||
// Get list of participating drivers (would need to be implemented)
|
||||
const driverIds = await this.getParticipatingDriverIds(raceEvent);
|
||||
const driverIds = await this.getParticipatingDriverIds();
|
||||
|
||||
// Check if any penalties were applied during stewarding
|
||||
const hadPenaltiesApplied = await this.checkForAppliedPenalties(raceEvent);
|
||||
const hadPenaltiesApplied = await this.checkForAppliedPenalties();
|
||||
|
||||
// Publish domain event to trigger final results notifications
|
||||
const event = new RaceEventStewardingClosedEvent({
|
||||
@@ -62,28 +70,19 @@ 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)));
|
||||
// In production, this would trigger alerts/monitoring
|
||||
// TODO: In production, this would trigger alerts/monitoring
|
||||
}
|
||||
}
|
||||
|
||||
private async getParticipatingDriverIds(raceEvent: any): Promise<string[]> {
|
||||
// In a real implementation, this would query race registrations
|
||||
// For the prototype, we'll return a mock list
|
||||
// This would typically involve:
|
||||
// 1. Get all sessions in the race event
|
||||
// 2. For each session, get registered drivers
|
||||
// 3. Return unique driver IDs across all sessions
|
||||
|
||||
// Mock implementation for prototype
|
||||
return ['driver-1', 'driver-2', 'driver-3']; // Would be dynamic in real implementation
|
||||
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 checkForAppliedPenalties(raceEvent: any): Promise<boolean> {
|
||||
// In a real implementation, this would check if any penalties were issued
|
||||
// during the stewarding window for this race event
|
||||
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
|
||||
|
||||
// Mock implementation for prototype - randomly simulate penalties
|
||||
return Math.random() > 0.7; // 30% chance of penalties being applied
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user