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

@@ -1,45 +1,54 @@
import { Injectable, Inject } from '@nestjs/common';
import { Result } from '@core/shared/application/Result';
import { Inject, Injectable } from '@nestjs/common';
import type { Driver } from '@core/racing/domain/entities/Driver';
import { CompleteOnboardingInputDTO } from './dtos/CompleteOnboardingInputDTO';
import { GetDriverRegistrationStatusQueryDTO } from './dtos/GetDriverRegistrationStatusQueryDTO';
import { DriversLeaderboardDTO } from './dtos/DriversLeaderboardDTO';
import { DriverStatsDTO } from './dtos/DriverStatsDTO';
import { CompleteOnboardingOutputDTO } from './dtos/CompleteOnboardingOutputDTO';
import { DriverRegistrationStatusDTO } from './dtos/DriverRegistrationStatusDTO';
import { DriversLeaderboardDTO } from './dtos/DriversLeaderboardDTO';
import { DriverStatsDTO } from './dtos/DriverStatsDTO';
import { GetDriverOutputDTO } from './dtos/GetDriverOutputDTO';
import { GetDriverProfileOutputDTO } from './dtos/GetDriverProfileOutputDTO';
import { GetDriverRegistrationStatusQueryDTO } from './dtos/GetDriverRegistrationStatusQueryDTO';
// Use cases
import { GetDriversLeaderboardUseCase } from '@core/racing/application/use-cases/GetDriversLeaderboardUseCase';
import { GetTotalDriversUseCase } from '@core/racing/application/use-cases/GetTotalDriversUseCase';
import { CompleteDriverOnboardingUseCase } from '@core/racing/application/use-cases/CompleteDriverOnboardingUseCase';
import { IsDriverRegisteredForRaceUseCase } from '@core/racing/application/use-cases/IsDriverRegisteredForRaceUseCase';
import { GetDriversLeaderboardUseCase } from '@core/racing/application/use-cases/GetDriversLeaderboardUseCase';
import { GetProfileOverviewUseCase } from '@core/racing/application/use-cases/GetProfileOverviewUseCase';
import { GetTotalDriversUseCase } from '@core/racing/application/use-cases/GetTotalDriversUseCase';
import { IsDriverRegisteredForRaceUseCase } from '@core/racing/application/use-cases/IsDriverRegisteredForRaceUseCase';
import { UpdateDriverProfileUseCase, type UpdateDriverProfileInput } from '@core/racing/application/use-cases/UpdateDriverProfileUseCase';
// Presenters
import { DriverStatsPresenter } from './presenters/DriverStatsPresenter';
import { DriversLeaderboardPresenter } from './presenters/DriversLeaderboardPresenter';
import { CompleteOnboardingPresenter } from './presenters/CompleteOnboardingPresenter';
import { DriverRegistrationStatusPresenter } from './presenters/DriverRegistrationStatusPresenter';
import { DriverPresenter } from './presenters/DriverPresenter';
import { DriverProfilePresenter } from './presenters/DriverProfilePresenter';
import { DriverRegistrationStatusPresenter } from './presenters/DriverRegistrationStatusPresenter';
import { DriversLeaderboardPresenter } from './presenters/DriversLeaderboardPresenter';
import { DriverStatsPresenter } from './presenters/DriverStatsPresenter';
// Tokens
import {
GET_DRIVERS_LEADERBOARD_USE_CASE_TOKEN,
GET_TOTAL_DRIVERS_USE_CASE_TOKEN,
COMPLETE_DRIVER_ONBOARDING_USE_CASE_TOKEN,
IS_DRIVER_REGISTERED_FOR_RACE_USE_CASE_TOKEN,
UPDATE_DRIVER_PROFILE_USE_CASE_TOKEN,
GET_PROFILE_OVERVIEW_USE_CASE_TOKEN,
LOGGER_TOKEN,
DRIVER_REPOSITORY_TOKEN,
} from './DriverProviders';
import type { IDriverRepository } from '@core/racing/domain/repositories/IDriverRepository';
import type { Logger } from '@core/shared/application';
import { IDriverRepository } from '@core/racing/domain/repositories/IDriverRepository';
import {
COMPLETE_DRIVER_ONBOARDING_USE_CASE_TOKEN,
DRIVER_REPOSITORY_TOKEN,
GET_DRIVERS_LEADERBOARD_USE_CASE_TOKEN,
GET_PROFILE_OVERVIEW_USE_CASE_TOKEN,
GET_TOTAL_DRIVERS_USE_CASE_TOKEN,
IS_DRIVER_REGISTERED_FOR_RACE_USE_CASE_TOKEN,
LOGGER_TOKEN,
UPDATE_DRIVER_PROFILE_USE_CASE_TOKEN,
} from './DriverProviders';
@Injectable()
export class DriverService {
private readonly driversLeaderboardPresenter = new DriversLeaderboardPresenter();
private readonly driverStatsPresenter = new DriverStatsPresenter();
private readonly completeOnboardingPresenter = new CompleteOnboardingPresenter();
private readonly driverRegistrationStatusPresenter = new DriverRegistrationStatusPresenter();
private readonly driverPresenter = new DriverPresenter();
private readonly driverProfilePresenter = new DriverProfilePresenter();
constructor(
@Inject(GET_DRIVERS_LEADERBOARD_USE_CASE_TOKEN)
private readonly getDriversLeaderboardUseCase: GetDriversLeaderboardUseCase,
@@ -54,15 +63,9 @@ export class DriverService {
@Inject(GET_PROFILE_OVERVIEW_USE_CASE_TOKEN)
private readonly getProfileOverviewUseCase: GetProfileOverviewUseCase,
@Inject(DRIVER_REPOSITORY_TOKEN)
private readonly driverRepository: IDriverRepository,
private readonly driverRepository: IDriverRepository, // TODO must be removed from service
@Inject(LOGGER_TOKEN)
private readonly logger: Logger,
private readonly driversLeaderboardPresenter: DriversLeaderboardPresenter,
private readonly driverStatsPresenter: DriverStatsPresenter,
private readonly completeOnboardingPresenter: CompleteOnboardingPresenter,
private readonly driverRegistrationStatusPresenter: DriverRegistrationStatusPresenter,
private readonly driverPresenter: DriverPresenter,
private readonly driverProfilePresenter: DriverProfilePresenter,
) {}
async getDriversLeaderboard(): Promise<DriversLeaderboardDTO> {
@@ -70,11 +73,8 @@ export class DriverService {
const result = await this.getDriversLeaderboardUseCase.execute({});
if (result.isErr()) {
const error = result.unwrapErr();
throw new Error(error.details?.message ?? 'Failed to load drivers leaderboard');
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.driversLeaderboardPresenter.present(result as Result<any, any>);
return this.driversLeaderboardPresenter.getResponseModel();
}
@@ -88,6 +88,7 @@ export class DriverService {
throw new Error(error.details?.message ?? 'Failed to load driver stats');
}
this.driverStatsPresenter.present(result.unwrap());
return this.driverStatsPresenter.getResponseModel();
}
@@ -106,11 +107,7 @@ export class DriverService {
...(input.bio !== undefined ? { bio: input.bio } : {}),
});
if (result.isErr()) {
const error = result.unwrapErr();
throw new Error(error.details?.message ?? 'Failed to complete onboarding');
}
this.completeOnboardingPresenter.present(result);
return this.completeOnboardingPresenter.getResponseModel();
}
@@ -129,15 +126,17 @@ export class DriverService {
throw new Error(error.details?.message ?? 'Failed to check registration status');
}
this.driverRegistrationStatusPresenter.present(result.unwrap());
return this.driverRegistrationStatusPresenter.getResponseModel();
}
async getCurrentDriver(userId: string): Promise<GetDriverOutputDTO | null> {
this.logger.debug(`[DriverService] Fetching current driver for userId: ${userId}`);
const driver = await this.driverRepository.findById(userId);
const result = Result.ok(await this.driverRepository.findById(userId));
this.driverPresenter.present(driver ?? null);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.driverPresenter.present(result as Result<Driver | null, any>);
return this.driverPresenter.getResponseModel();
}
@@ -157,12 +156,11 @@ export class DriverService {
if (result.isErr()) {
this.logger.error(`Failed to update driver profile: ${result.unwrapErr().code}`);
this.driverPresenter.present(null);
this.driverPresenter.present(Result.ok(null));
return this.driverPresenter.getResponseModel();
}
const updatedDriver = await this.driverRepository.findById(driverId);
this.driverPresenter.present(updatedDriver ?? null);
this.driverPresenter.present(Result.ok(result.unwrap()));
return this.driverPresenter.getResponseModel();
}
@@ -171,7 +169,7 @@ export class DriverService {
const driver = await this.driverRepository.findById(driverId);
this.driverPresenter.present(driver ?? null);
this.driverPresenter.present(Result.ok(driver));
return this.driverPresenter.getResponseModel();
}
@@ -186,6 +184,7 @@ export class DriverService {
throw new Error(error.details?.message ?? 'Failed to load driver profile');
}
this.driverProfilePresenter.present(result.unwrap());
return this.driverProfilePresenter.getResponseModel();
}
}