refactor racing use cases
This commit is contained in:
@@ -7,40 +7,60 @@
|
||||
|
||||
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { RacePenaltiesOutputPort } from '../ports/output/RacePenaltiesOutputPort';
|
||||
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { Driver } from '../../domain/entities/Driver';
|
||||
|
||||
export interface GetRacePenaltiesInput {
|
||||
export type GetRacePenaltiesInput = {
|
||||
raceId: string;
|
||||
}
|
||||
};
|
||||
|
||||
export class GetRacePenaltiesUseCase implements AsyncUseCase<GetRacePenaltiesInput, RacePenaltiesOutputPort, 'NO_ERROR'> {
|
||||
export type GetRacePenaltiesResult = {
|
||||
penalties: unknown[];
|
||||
drivers: Driver[];
|
||||
};
|
||||
|
||||
export type GetRacePenaltiesErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetRacePenaltiesUseCase {
|
||||
constructor(
|
||||
private readonly penaltyRepository: IPenaltyRepository,
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
private readonly output: UseCaseOutputPort<GetRacePenaltiesResult>,
|
||||
) {}
|
||||
|
||||
async execute(input: GetRacePenaltiesInput): Promise<Result<RacePenaltiesOutputPort, ApplicationErrorCode<'NO_ERROR'>>> {
|
||||
const penalties = await this.penaltyRepository.findByRaceId(input.raceId);
|
||||
async execute(
|
||||
input: GetRacePenaltiesInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetRacePenaltiesErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const penalties = await this.penaltyRepository.findByRaceId(input.raceId);
|
||||
|
||||
const driverIds = new Set<string>();
|
||||
penalties.forEach((penalty) => {
|
||||
driverIds.add(penalty.driverId);
|
||||
driverIds.add(penalty.issuedBy);
|
||||
});
|
||||
const driverIds = new Set<string>();
|
||||
penalties.forEach((penalty: any) => {
|
||||
driverIds.add(penalty.driverId);
|
||||
driverIds.add(penalty.issuedBy);
|
||||
});
|
||||
|
||||
const drivers = await Promise.all(
|
||||
Array.from(driverIds).map((id) => this.driverRepository.findById(id)),
|
||||
);
|
||||
const drivers = await Promise.all(
|
||||
Array.from(driverIds).map((id) => this.driverRepository.findById(id)),
|
||||
);
|
||||
|
||||
const validDrivers = drivers.filter((driver): driver is NonNullable<typeof driver> => driver !== null);
|
||||
const validDrivers = drivers.filter((driver): driver is NonNullable<typeof driver> => driver !== null);
|
||||
|
||||
const outputPort: RacePenaltiesOutputPort = {
|
||||
penalties,
|
||||
drivers: validDrivers,
|
||||
};
|
||||
return Result.ok(outputPort);
|
||||
this.output.present({ penalties, drivers: validDrivers });
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message ? error.message : 'Failed to load race penalties';
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message,
|
||||
},
|
||||
} as ApplicationErrorCode<GetRacePenaltiesErrorCode, { message: string }>);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user