refactor racing use cases
This commit is contained in:
@@ -4,31 +4,35 @@ import type { IDriverRepository } from '../../domain/repositories/IDriverReposit
|
||||
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { RaceDetailOutputPort } from '../ports/output/RaceDetailOutputPort';
|
||||
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/UseCaseOutputPort';
|
||||
import type { League } from '../../domain/entities/League';
|
||||
import type { Race } from '../../domain/entities/Race';
|
||||
import type { RaceRegistration } from '../../domain/entities/RaceRegistration';
|
||||
import type { Result as DomainResult } from '../../domain/entities/Result';
|
||||
|
||||
/**
|
||||
* Use Case: GetRaceDetailUseCase
|
||||
*
|
||||
* Given a race id and current driver id:
|
||||
* - When the race exists, it builds a view model with race, league, entry list, registration flags and user result.
|
||||
* - When the race does not exist, it presents a view model with an error and no race data.
|
||||
*
|
||||
* Given a completed race with a result for the driver:
|
||||
* - When computing rating change, it applies the same position-based formula used in the legacy UI.
|
||||
*/
|
||||
export interface GetRaceDetailQueryParams {
|
||||
export type GetRaceDetailInput = {
|
||||
raceId: string;
|
||||
driverId: string;
|
||||
}
|
||||
};
|
||||
|
||||
type GetRaceDetailErrorCode = 'RACE_NOT_FOUND';
|
||||
// Backwards-compatible alias for older callers
|
||||
export type GetRaceDetailQueryParams = GetRaceDetailInput;
|
||||
|
||||
export class GetRaceDetailUseCase
|
||||
implements AsyncUseCase<GetRaceDetailQueryParams, RaceDetailOutputPort, GetRaceDetailErrorCode>
|
||||
{
|
||||
export type GetRaceDetailErrorCode = 'RACE_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
export type GetRaceDetailResult = {
|
||||
race: Race;
|
||||
league: League | null;
|
||||
registrations: RaceRegistration[];
|
||||
drivers: NonNullable<Awaited<ReturnType<IDriverRepository['findById']>>>[];
|
||||
userResult: DomainResult | null;
|
||||
isUserRegistered: boolean;
|
||||
canRegister: boolean;
|
||||
};
|
||||
|
||||
export class GetRaceDetailUseCase {
|
||||
constructor(
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
@@ -36,50 +40,69 @@ export class GetRaceDetailUseCase
|
||||
private readonly raceRegistrationRepository: IRaceRegistrationRepository,
|
||||
private readonly resultRepository: IResultRepository,
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
private readonly output: UseCaseOutputPort<GetRaceDetailResult>,
|
||||
) {}
|
||||
|
||||
async execute(params: GetRaceDetailQueryParams): Promise<Result<RaceDetailOutputPort, ApplicationErrorCode<GetRaceDetailErrorCode>>> {
|
||||
const { raceId, driverId } = params;
|
||||
async execute(
|
||||
input: GetRaceDetailInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetRaceDetailErrorCode, { message: string }>>> {
|
||||
const { raceId, driverId } = input;
|
||||
|
||||
const race = await this.raceRepository.findById(raceId);
|
||||
if (!race) {
|
||||
return Result.err({ code: 'RACE_NOT_FOUND' });
|
||||
try {
|
||||
const race = await this.raceRepository.findById(raceId);
|
||||
if (!race) {
|
||||
return Result.err({
|
||||
code: 'RACE_NOT_FOUND',
|
||||
details: { message: 'Race not found' },
|
||||
});
|
||||
}
|
||||
|
||||
const [league, registrations, membership] = await Promise.all([
|
||||
this.leagueRepository.findById(race.leagueId),
|
||||
this.raceRegistrationRepository.findByRaceId(race.id),
|
||||
this.leagueMembershipRepository.getMembership(race.leagueId, driverId),
|
||||
]);
|
||||
|
||||
const drivers = await Promise.all(
|
||||
registrations.map(registration => this.driverRepository.findById(registration.driverId.toString())),
|
||||
);
|
||||
|
||||
const validDrivers = drivers.filter((driver): driver is NonNullable<typeof driver> => driver !== null);
|
||||
|
||||
const isUserRegistered = registrations.some(reg => reg.driverId.toString() === driverId);
|
||||
const isUpcoming = race.status === 'scheduled' && race.scheduledAt > new Date();
|
||||
const canRegister = !!membership && membership.status === 'active' && isUpcoming;
|
||||
|
||||
let userResult: DomainResult | null = null;
|
||||
|
||||
if (race.status === 'completed') {
|
||||
const results = await this.resultRepository.findByRaceId(race.id);
|
||||
userResult = results.find(r => r.driverId.toString() === driverId) ?? null;
|
||||
}
|
||||
|
||||
const result: GetRaceDetailResult = {
|
||||
race,
|
||||
league: league ?? null,
|
||||
registrations,
|
||||
drivers: validDrivers,
|
||||
userResult,
|
||||
isUserRegistered,
|
||||
canRegister,
|
||||
};
|
||||
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error: unknown) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
? error.message
|
||||
: 'Failed to load race detail';
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
});
|
||||
}
|
||||
|
||||
const [league, registrations, membership] = await Promise.all([
|
||||
this.leagueRepository.findById(race.leagueId),
|
||||
this.raceRegistrationRepository.findByRaceId(race.id),
|
||||
this.leagueMembershipRepository.getMembership(race.leagueId, driverId),
|
||||
]);
|
||||
|
||||
const drivers = await Promise.all(
|
||||
registrations.map(registration => this.driverRepository.findById(registration.driverId.toString())),
|
||||
);
|
||||
|
||||
const validDrivers = drivers.filter((driver): driver is NonNullable<typeof driver> => driver !== null);
|
||||
|
||||
const isUserRegistered = registrations.some(reg => reg.driverId.toString() === driverId);
|
||||
const isUpcoming = race.status === 'scheduled' && race.scheduledAt > new Date();
|
||||
const canRegister = !!membership && membership.status === 'active' && isUpcoming;
|
||||
|
||||
let userResult: Result | null = null;
|
||||
|
||||
if (race.status === 'completed') {
|
||||
const results = await this.resultRepository.findByRaceId(race.id);
|
||||
userResult = results.find(r => r.driverId.toString() === driverId) ?? null;
|
||||
}
|
||||
|
||||
const outputPort: RaceDetailOutputPort = {
|
||||
race,
|
||||
league,
|
||||
registrations,
|
||||
drivers: validDrivers,
|
||||
userResult,
|
||||
isUserRegistered,
|
||||
canRegister,
|
||||
};
|
||||
|
||||
return Result.ok(outputPort);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user