refactor driver module (wip)

This commit is contained in:
2025-12-22 01:43:34 +01:00
parent b445d6dd37
commit e7dbec4a85
31 changed files with 379 additions and 395 deletions

View File

@@ -0,0 +1,6 @@
export interface IImageServicePort {
getDriverAvatar(driverId: string): string;
getTeamLogo(teamId: string): string;
getLeagueCover(leagueId: string): string;
getLeagueLogo(leagueId: string): string;
}

View File

@@ -2,7 +2,7 @@ import type { IDriverRepository } from '../../domain/repositories/IDriverReposit
import { Driver } from '../../domain/entities/Driver';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort, UseCase } from '@core/shared/application';
import type { UseCase, UseCaseOutputPort } from '@core/shared/application';
import type { Logger } from '@core/shared/application/Logger';
export interface CompleteDriverOnboardingInput {
@@ -30,16 +30,15 @@ export type CompleteDriverOnboardingApplicationError = ApplicationErrorCode<
/**
* Use Case for completing driver onboarding.
*/
export class CompleteDriverOnboardingUseCase implements UseCase<CompleteDriverOnboardingInput, void, CompleteDriverOnboardingErrorCode> {
export class CompleteDriverOnboardingUseCase implements UseCase<CompleteDriverOnboardingInput, CompleteDriverOnboardingResult, CompleteDriverOnboardingErrorCode> {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<CompleteDriverOnboardingResult>,
) {}
async execute(
input: CompleteDriverOnboardingInput,
): Promise<Result<void, CompleteDriverOnboardingApplicationError>> {
): Promise<Result<CompleteDriverOnboardingResult, CompleteDriverOnboardingApplicationError>> {
try {
const existing = await this.driverRepository.findById(input.userId);
if (existing) {
@@ -59,9 +58,8 @@ export class CompleteDriverOnboardingUseCase implements UseCase<CompleteDriverOn
await this.driverRepository.create(driver);
this.output.present({ driver });
return Result.ok(undefined);
const result: CompleteDriverOnboardingResult = { driver };
return Result.ok(result);
} catch (error) {
const err = error instanceof Error ? error : new Error('Unknown error');

View File

@@ -1,4 +1,4 @@
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { Logger, UseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Driver } from '../../domain/entities/Driver';
@@ -42,21 +42,20 @@ export type GetDriversLeaderboardErrorCode =
* Use Case for retrieving driver leaderboard data.
* Returns a Result containing the domain leaderboard model.
*/
export class GetDriversLeaderboardUseCase {
export class GetDriversLeaderboardUseCase implements UseCase<GetDriversLeaderboardInput, GetDriversLeaderboardResult, GetDriversLeaderboardErrorCode> {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly rankingService: IRankingService,
private readonly driverStatsService: IDriverStatsService,
private readonly getDriverAvatar: (driverId: string) => Promise<string | undefined>,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<GetDriversLeaderboardResult>,
) {}
async execute(
input: GetDriversLeaderboardInput,
): Promise<
Result<
void,
GetDriversLeaderboardResult,
ApplicationErrorCode<GetDriversLeaderboardErrorCode, { message: string }>
>
> {
@@ -108,9 +107,7 @@ export class GetDriversLeaderboardUseCase {
this.logger.debug('Successfully computed drivers leaderboard');
this.output.present(result);
return Result.ok(undefined);
return Result.ok(result);
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));

View File

@@ -92,7 +92,7 @@ export type GetProfileOverviewErrorCode =
| 'DRIVER_NOT_FOUND'
| 'REPOSITORY_ERROR';
export class GetProfileOverviewUseCase implements UseCase<GetProfileOverviewInput, void, GetProfileOverviewErrorCode> {
export class GetProfileOverviewUseCase implements UseCase<GetProfileOverviewInput, GetProfileOverviewResult, GetProfileOverviewErrorCode> {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly teamRepository: ITeamRepository,
@@ -102,13 +102,12 @@ export class GetProfileOverviewUseCase implements UseCase<GetProfileOverviewInpu
private readonly driverExtendedProfileProvider: DriverExtendedProfileProvider,
private readonly getDriverStats: (driverId: string) => ProfileDriverStatsAdapter | null,
private readonly getAllDriverRankings: () => DriverRankingEntry[],
private readonly output: UseCaseOutputPort<GetProfileOverviewResult>,
) {}
async execute(
input: GetProfileOverviewInput,
): Promise<
Result<void, ApplicationErrorCode<GetProfileOverviewErrorCode, { message: string }>>
Result<GetProfileOverviewResult, ApplicationErrorCode<GetProfileOverviewErrorCode, { message: string }>>
> {
try {
const { driverId } = input;
@@ -144,9 +143,7 @@ export class GetProfileOverviewUseCase implements UseCase<GetProfileOverviewInpu
extendedProfile,
};
this.output.present(result);
return Result.ok(undefined);
return Result.ok(result);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',

View File

@@ -1,7 +1,7 @@
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { UseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort, UseCase } from '@core/shared/application';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
/**
* Input type for retrieving total number of drivers.
@@ -17,22 +17,19 @@ export type GetTotalDriversResult = {
export type GetTotalDriversErrorCode = 'REPOSITORY_ERROR';
export class GetTotalDriversUseCase implements UseCase<GetTotalDriversInput, void, GetTotalDriversErrorCode> {
export class GetTotalDriversUseCase implements UseCase<GetTotalDriversInput, GetTotalDriversResult, GetTotalDriversErrorCode> {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly output: UseCaseOutputPort<GetTotalDriversResult>,
) {}
async execute(
_input: GetTotalDriversInput,
): Promise<Result<void, ApplicationErrorCode<GetTotalDriversErrorCode, { message: string }>>> {
): Promise<Result<GetTotalDriversResult, ApplicationErrorCode<GetTotalDriversErrorCode, { message: string }>>> {
try {
const drivers = await this.driverRepository.findAll();
const result: GetTotalDriversResult = { totalDrivers: drivers.length };
this.output.present(result);
return Result.ok(undefined);
return Result.ok(result);
} catch (error) {
const message = (error as Error | undefined)?.message ?? 'Failed to compute total drivers';

View File

@@ -1,5 +1,5 @@
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
import type { Logger, UseCaseOutputPort, UseCase } from '@core/shared/application';
import type { Logger, UseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
@@ -26,23 +26,20 @@ export type IsDriverRegisteredForRaceResult = {
*
* Checks if a driver is registered for a specific race.
*/
export class IsDriverRegisteredForRaceUseCase implements UseCase<IsDriverRegisteredForRaceInput, void, IsDriverRegisteredForRaceErrorCode> {
export class IsDriverRegisteredForRaceUseCase implements UseCase<IsDriverRegisteredForRaceInput, IsDriverRegisteredForRaceResult, IsDriverRegisteredForRaceErrorCode> {
constructor(
private readonly registrationRepository: IRaceRegistrationRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<IsDriverRegisteredForRaceResult>,
) {}
async execute(params: IsDriverRegisteredForRaceInput): Promise<Result<void, IsDriverRegisteredForRaceApplicationError>> {
async execute(params: IsDriverRegisteredForRaceInput): Promise<Result<IsDriverRegisteredForRaceResult, IsDriverRegisteredForRaceApplicationError>> {
this.logger.debug('IsDriverRegisteredForRaceUseCase:execute', { params });
const { raceId, driverId } = params;
try {
const isRegistered = await this.registrationRepository.isRegistered(raceId, driverId);
this.output.present({ isRegistered, raceId, driverId });
return Result.ok(undefined);
return Result.ok({ isRegistered, raceId, driverId });
} catch (error) {
this.logger.error(
'IsDriverRegisteredForRaceUseCase:execution error',

View File

@@ -1,5 +1,5 @@
import { Result } from '@core/shared/application/Result';
import type { UseCaseOutputPort, UseCase } from '@core/shared/application';
import type { UseCase } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Logger } from '@core/shared/application/Logger';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
@@ -23,16 +23,15 @@ export type UpdateDriverProfileErrorCode =
* Encapsulates domain entity mutation. Mapping to DTOs is handled by presenters
* in the presentation layer through the output port.
*/
export class UpdateDriverProfileUseCase implements UseCase<UpdateDriverProfileInput, void, UpdateDriverProfileErrorCode> {
export class UpdateDriverProfileUseCase implements UseCase<UpdateDriverProfileInput, UpdateDriverProfileResult, UpdateDriverProfileErrorCode> {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly output: UseCaseOutputPort<UpdateDriverProfileResult>,
private readonly logger: Logger,
) {}
async execute(
input: UpdateDriverProfileInput,
): Promise<Result<void, ApplicationErrorCode<UpdateDriverProfileErrorCode, { message: string }>>> {
): Promise<Result<UpdateDriverProfileResult, ApplicationErrorCode<UpdateDriverProfileErrorCode, { message: string }>>> {
const { driverId, bio, country } = input;
if ((bio !== undefined && bio.trim().length === 0) || (country !== undefined && country.trim().length === 0)) {
@@ -62,9 +61,7 @@ export class UpdateDriverProfileUseCase implements UseCase<UpdateDriverProfileIn
await this.driverRepository.update(updated);
this.output.present(updated);
return Result.ok(undefined);
return Result.ok(updated);
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to update driver profile';