refactor league module (wip)

This commit is contained in:
2025-12-22 15:47:47 +01:00
parent 03dc81b0ba
commit f59e1b13e7
10 changed files with 444 additions and 819 deletions

View File

@@ -1,6 +1,4 @@
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 { CompleteOnboardingOutputDTO } from './dtos/CompleteOnboardingOutputDTO';
import { DriverRegistrationStatusDTO } from './dtos/DriverRegistrationStatusDTO';
@@ -42,13 +40,6 @@ import {
@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,
@@ -66,29 +57,26 @@ export class DriverService {
private readonly driverRepository: IDriverRepository, // TODO must be removed from service
@Inject(LOGGER_TOKEN)
private readonly logger: Logger,
// Injected presenters
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> {
this.logger.debug('[DriverService] Fetching drivers leaderboard.');
const result = await this.getDriversLeaderboardUseCase.execute({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.driversLeaderboardPresenter.present(result as Result<any, any>);
await this.getDriversLeaderboardUseCase.execute({});
return this.driversLeaderboardPresenter.getResponseModel();
}
async getTotalDrivers(): Promise<DriverStatsDTO> {
this.logger.debug('[DriverService] Fetching total drivers count.');
const result = await this.getTotalDriversUseCase.execute({});
if (result.isErr()) {
const error = result.unwrapErr();
throw new Error(error.details?.message ?? 'Failed to load driver stats');
}
this.driverStatsPresenter.present(result.unwrap());
await this.getTotalDriversUseCase.execute({});
return this.driverStatsPresenter.getResponseModel();
}
@@ -98,7 +86,7 @@ export class DriverService {
): Promise<CompleteOnboardingOutputDTO> {
this.logger.debug('Completing onboarding for user:', userId);
const result = await this.completeDriverOnboardingUseCase.execute({
await this.completeDriverOnboardingUseCase.execute({
userId,
firstName: input.firstName,
lastName: input.lastName,
@@ -107,7 +95,6 @@ export class DriverService {
...(input.bio !== undefined ? { bio: input.bio } : {}),
});
this.completeOnboardingPresenter.present(result);
return this.completeOnboardingPresenter.getResponseModel();
}
@@ -116,28 +103,18 @@ export class DriverService {
): Promise<DriverRegistrationStatusDTO> {
this.logger.debug('Checking driver registration status:', query);
const result = await this.isDriverRegisteredForRaceUseCase.execute({
await this.isDriverRegisteredForRaceUseCase.execute({
raceId: query.raceId,
driverId: query.driverId,
});
if (result.isErr()) {
const error = result.unwrapErr();
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 result = Result.ok(await this.driverRepository.findById(userId));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.driverPresenter.present(result as Result<Driver | null, any>);
await this.driverRepository.findById(userId);
return this.driverPresenter.getResponseModel();
}
@@ -152,39 +129,21 @@ export class DriverService {
if (bio !== undefined) input.bio = bio;
if (country !== undefined) input.country = country;
const result = await this.updateDriverProfileUseCase.execute(input);
if (result.isErr()) {
this.logger.error(`Failed to update driver profile: ${result.unwrapErr().code}`);
this.driverPresenter.present(Result.ok(null));
return this.driverPresenter.getResponseModel();
}
this.driverPresenter.present(Result.ok(result.unwrap()));
await this.updateDriverProfileUseCase.execute(input);
return this.driverPresenter.getResponseModel();
}
async getDriver(driverId: string): Promise<GetDriverOutputDTO | null> {
this.logger.debug(`[DriverService] Fetching driver for driverId: ${driverId}`);
const driver = await this.driverRepository.findById(driverId);
this.driverPresenter.present(Result.ok(driver));
await this.driverRepository.findById(driverId);
return this.driverPresenter.getResponseModel();
}
async getDriverProfile(driverId: string): Promise<GetDriverProfileOutputDTO> {
this.logger.debug(`[DriverService] Fetching driver profile for driverId: ${driverId}`);
const result = await this.getProfileOverviewUseCase.execute({ driverId });
if (result.isErr()) {
const error = result.unwrapErr();
throw new Error(error.details?.message ?? 'Failed to load driver profile');
}
this.driverProfilePresenter.present(result.unwrap());
await this.getProfileOverviewUseCase.execute({ driverId });
return this.driverProfilePresenter.getResponseModel();
}
}
}