46 lines
2.0 KiB
TypeScript
46 lines
2.0 KiB
TypeScript
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
|
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
|
import type { GetAllRacesOutputPort } from '../ports/output/GetAllRacesOutputPort';
|
|
import type { AsyncUseCase, Logger } from '@core/shared/application';
|
|
import { Result } from '@core/shared/application/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
|
|
export class GetAllRacesUseCase implements AsyncUseCase<void, GetAllRacesOutputPort, 'REPOSITORY_ERROR'> {
|
|
constructor(
|
|
private readonly raceRepository: IRaceRepository,
|
|
private readonly leagueRepository: ILeagueRepository,
|
|
private readonly logger: Logger,
|
|
) {}
|
|
|
|
async execute(): Promise<Result<GetAllRacesOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
|
|
this.logger.debug('Executing GetAllRacesUseCase');
|
|
try {
|
|
const races = await this.raceRepository.findAll();
|
|
const leagues = await this.leagueRepository.findAll();
|
|
const leagueMap = new Map(leagues.map(league => [league.id, league.name]));
|
|
|
|
const output: GetAllRacesOutputPort = {
|
|
races: races.map(race => ({
|
|
id: race.id,
|
|
leagueId: race.leagueId,
|
|
track: race.track,
|
|
car: race.car,
|
|
status: race.status as 'scheduled' | 'running' | 'completed' | 'cancelled',
|
|
scheduledAt: race.scheduledAt.toISOString(),
|
|
strengthOfField: race.strengthOfField || null,
|
|
leagueName: (leagueMap.get(race.leagueId) || 'Unknown League').toString(),
|
|
})),
|
|
totalCount: races.length,
|
|
};
|
|
|
|
this.logger.debug('Successfully retrieved all races.');
|
|
return Result.ok(output);
|
|
} catch (error) {
|
|
this.logger.error('Error executing GetAllRacesUseCase', error instanceof Error ? error : new Error(String(error)));
|
|
return Result.err({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: { message: error instanceof Error ? error.message : 'Unknown error occurred' },
|
|
});
|
|
}
|
|
}
|
|
} |