Files
gridpilot.gg/packages/racing/application/use-cases/GetRacePenaltiesQuery.ts
2025-12-09 10:32:59 +01:00

74 lines
2.1 KiB
TypeScript

/**
* Application Query: GetRacePenaltiesQuery
*
* Returns all penalties applied for a specific race, with driver details.
*/
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { PenaltyType, PenaltyStatus } from '../../domain/entities/Penalty';
export interface RacePenaltyDTO {
id: string;
raceId: string;
driverId: string;
driverName: string;
type: PenaltyType;
value?: number;
reason: string;
protestId?: string;
issuedBy: string;
issuedByName: string;
status: PenaltyStatus;
description: string;
issuedAt: string;
appliedAt?: string;
notes?: string;
}
export class GetRacePenaltiesQuery {
constructor(
private readonly penaltyRepository: IPenaltyRepository,
private readonly driverRepository: IDriverRepository,
) {}
async execute(raceId: string): Promise<RacePenaltyDTO[]> {
const penalties = await this.penaltyRepository.findByRaceId(raceId);
// Load all driver details in parallel
const driverIds = new Set<string>();
penalties.forEach(penalty => {
driverIds.add(penalty.driverId);
driverIds.add(penalty.issuedBy);
});
const drivers = await Promise.all(
Array.from(driverIds).map(id => this.driverRepository.findById(id))
);
const driverMap = new Map<string, string>();
drivers.forEach(driver => {
if (driver) {
driverMap.set(driver.id, driver.name);
}
});
return penalties.map(penalty => ({
id: penalty.id,
raceId: penalty.raceId,
driverId: penalty.driverId,
driverName: driverMap.get(penalty.driverId) || 'Unknown',
type: penalty.type,
value: penalty.value,
reason: penalty.reason,
protestId: penalty.protestId,
issuedBy: penalty.issuedBy,
issuedByName: driverMap.get(penalty.issuedBy) || 'Unknown',
status: penalty.status,
description: penalty.getDescription(),
issuedAt: penalty.issuedAt.toISOString(),
appliedAt: penalty.appliedAt?.toISOString(),
notes: penalty.notes,
}));
}
}