43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
|
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
|
import type { RacesPageOutputPort } from '../ports/output/RacesPageOutputPort';
|
|
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
|
|
import { Result } from '@core/shared/application/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
|
|
export class GetRacesPageDataUseCase implements AsyncUseCase<void, RacesPageOutputPort, 'NO_ERROR'> {
|
|
constructor(
|
|
private readonly raceRepository: IRaceRepository,
|
|
private readonly leagueRepository: ILeagueRepository,
|
|
) {}
|
|
|
|
async execute(): Promise<Result<RacesPageOutputPort, ApplicationErrorCode<'NO_ERROR'>>> {
|
|
const [allRaces, allLeagues] = await Promise.all([
|
|
this.raceRepository.findAll(),
|
|
this.leagueRepository.findAll(),
|
|
]);
|
|
|
|
const leagueMap = new Map(allLeagues.map(l => [l.id, l.name]));
|
|
|
|
const races = allRaces
|
|
.sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime())
|
|
.map(race => ({
|
|
id: race.id,
|
|
track: race.track,
|
|
car: race.car,
|
|
scheduledAt: race.scheduledAt,
|
|
status: race.status,
|
|
leagueId: race.leagueId,
|
|
strengthOfField: race.strengthOfField,
|
|
}));
|
|
|
|
const outputPort: RacesPageOutputPort = {
|
|
page: 1,
|
|
pageSize: races.length,
|
|
totalCount: races.length,
|
|
races,
|
|
};
|
|
|
|
return Result.ok(outputPort);
|
|
}
|
|
} |