refactor racing use cases

This commit is contained in:
2025-12-21 00:43:42 +01:00
parent e9d6f90bb2
commit c12656d671
308 changed files with 14401 additions and 7419 deletions

View File

@@ -1,31 +1,41 @@
import type { AsyncUseCase } from '@core/shared/application';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import { Driver } from '../../domain/entities/Driver';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { CompleteDriverOnboardingCommand } from '../dto/CompleteDriverOnboardingCommand';
import type { CompleteDriverOnboardingOutputPort } from '../ports/output/CompleteDriverOnboardingOutputPort';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
export interface CompleteDriverOnboardingInput {
userId: string;
firstName: string;
lastName: string;
displayName: string;
country: string;
bio?: string;
}
export type CompleteDriverOnboardingResult = {
driver: Driver;
};
/**
* Use Case for completing driver onboarding.
*/
export class CompleteDriverOnboardingUseCase
implements AsyncUseCase<CompleteDriverOnboardingCommand, CompleteDriverOnboardingOutputPort, string>
{
constructor(private readonly driverRepository: IDriverRepository) {}
export class CompleteDriverOnboardingUseCase {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly output: UseCaseOutputPort<CompleteDriverOnboardingResult>,
) {}
async execute(command: CompleteDriverOnboardingCommand): Promise<Result<CompleteDriverOnboardingOutputPort, ApplicationErrorCode<string>>> {
async execute(command: CompleteDriverOnboardingInput): Promise<Result<void, ApplicationErrorCode<'DRIVER_ALREADY_EXISTS' | 'REPOSITORY_ERROR'>>> {
try {
// Check if driver already exists
const existing = await this.driverRepository.findById(command.userId);
if (existing) {
return Result.err({ code: 'DRIVER_ALREADY_EXISTS' });
}
// Create new driver
const driver = Driver.create({
id: command.userId,
iracingId: command.userId, // Assuming userId is iracingId for now
iracingId: command.userId,
name: command.displayName,
country: command.country,
...(command.bio !== undefined ? { bio: command.bio } : {}),
@@ -33,9 +43,16 @@ export class CompleteDriverOnboardingUseCase
await this.driverRepository.create(driver);
return Result.ok({ driverId: driver.id });
} catch {
return Result.err({ code: 'UNKNOWN_ERROR' });
this.output.present({ driver });
return Result.ok(undefined);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',
details: {
message: error instanceof Error ? error.message : 'Unknown error',
},
});
}
}
}