This commit is contained in:
2025-12-21 19:53:22 +01:00
parent f2d8a23583
commit 3c64f328e2
105 changed files with 3191 additions and 1706 deletions

View File

@@ -1,6 +1,12 @@
import { Injectable, Inject } from '@nestjs/common';
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 { GetDriverOutputDTO } from './dtos/GetDriverOutputDTO';
import { GetDriverProfileOutputDTO } from './dtos/GetDriverProfileOutputDTO';
// Use cases
import { GetDriversLeaderboardUseCase } from '@core/racing/application/use-cases/GetDriversLeaderboardUseCase';
@@ -51,37 +57,42 @@ export class DriverService {
private readonly driverRepository: IDriverRepository,
@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<DriversLeaderboardPresenter> {
async getDriversLeaderboard(): Promise<DriversLeaderboardDTO> {
this.logger.debug('[DriverService] Fetching drivers leaderboard.');
const result = await this.getDriversLeaderboardUseCase.execute();
if (result.isErr()) {
throw new Error(`Failed to fetch drivers leaderboard: ${result.unwrapErr().details.message}`);
}
const result = await this.getDriversLeaderboardUseCase.execute({});
const presenter = new DriversLeaderboardPresenter();
presenter.reset();
presenter.present(result.unwrap());
return presenter;
presenter.present(result);
return presenter.getResponseModel();
}
async getTotalDrivers(): Promise<DriverStatsPresenter> {
async getTotalDrivers(): Promise<DriverStatsDTO> {
this.logger.debug('[DriverService] Fetching total drivers count.');
const result = await this.getTotalDriversUseCase.execute();
const result = await this.getTotalDriversUseCase.execute({});
if (result.isErr()) {
throw new Error(result.unwrapErr().code);
const error = result.unwrapErr();
throw new Error(error.details?.message ?? 'Failed to load driver stats');
}
const presenter = new DriverStatsPresenter();
presenter.reset();
presenter.present(result.unwrap());
return presenter;
return this.driverStatsPresenter.getResponseModel();
}
async completeOnboarding(userId: string, input: CompleteOnboardingInputDTO): Promise<CompleteOnboardingPresenter> {
async completeOnboarding(
userId: string,
input: CompleteOnboardingInputDTO,
): Promise<CompleteOnboardingOutputDTO> {
this.logger.debug('Completing onboarding for user:', userId);
const result = await this.completeDriverOnboardingUseCase.execute({
@@ -95,20 +106,14 @@ export class DriverService {
});
const presenter = new CompleteOnboardingPresenter();
presenter.reset();
presenter.present(result);
if (result.isOk()) {
presenter.present(result.value);
} else {
presenter.presentError(result.error.code);
}
return presenter;
return presenter.responseModel;
}
async getDriverRegistrationStatus(
query: GetDriverRegistrationStatusQueryDTO,
): Promise<DriverRegistrationStatusPresenter> {
): Promise<DriverRegistrationStatusDTO> {
this.logger.debug('Checking driver registration status:', query);
const result = await this.isDriverRegisteredForRaceUseCase.execute({
@@ -116,77 +121,64 @@ export class DriverService {
driverId: query.driverId,
});
if (result.isErr()) {
throw new Error(`Failed to check registration status: ${result.unwrapErr().code}`);
}
const presenter = new DriverRegistrationStatusPresenter();
presenter.reset();
presenter.present(result);
const output = result.unwrap();
presenter.present(output.isRegistered, output.raceId, output.driverId);
return presenter;
return presenter.responseModel;
}
async getCurrentDriver(userId: string): Promise<DriverPresenter> {
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 presenter = new DriverPresenter();
presenter.reset();
presenter.present(driver ?? null);
return presenter;
return presenter.responseModel;
}
async updateDriverProfile(
driverId: string,
bio?: string,
country?: string,
): Promise<DriverPresenter> {
): Promise<GetDriverOutputDTO | null> {
this.logger.debug(`[DriverService] Updating driver profile for driverId: ${driverId}`);
const result = await this.updateDriverProfileUseCase.execute({ driverId, bio, country });
const presenter = new DriverPresenter();
presenter.reset();
if (result.isErr()) {
this.logger.error(`Failed to update driver profile: ${result.error.code}`);
presenter.present(null);
return presenter;
return presenter.responseModel;
}
presenter.present(result.value);
return presenter;
const updatedDriver = await this.driverRepository.findById(driverId);
presenter.present(updatedDriver ?? null);
return presenter.responseModel;
}
async getDriver(driverId: string): Promise<DriverPresenter> {
async getDriver(driverId: string): Promise<GetDriverOutputDTO | null> {
this.logger.debug(`[DriverService] Fetching driver for driverId: ${driverId}`);
const driver = await this.driverRepository.findById(driverId);
const presenter = new DriverPresenter();
presenter.reset();
presenter.present(driver ?? null);
return presenter;
return presenter.responseModel;
}
async getDriverProfile(driverId: string): Promise<DriverProfilePresenter> {
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()) {
throw new Error(`Failed to fetch driver profile: ${result.error.code}`);
}
const presenter = new DriverProfilePresenter();
presenter.reset();
presenter.present(result.value);
presenter.present(result);
return presenter;
return presenter.responseModel;
}
}