40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
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/result/Result';
|
|
import { RacingDomainValidationError } from '../../domain/errors/RacingDomainError';
|
|
import type { CompleteDriverOnboardingCommand } from './CompleteDriverOnboardingCommand';
|
|
|
|
/**
|
|
* Use Case for completing driver onboarding.
|
|
*/
|
|
export class CompleteDriverOnboardingUseCase
|
|
implements AsyncUseCase<CompleteDriverOnboardingCommand, Result<{ driverId: string }, RacingDomainValidationError>>
|
|
{
|
|
constructor(private readonly driverRepository: IDriverRepository) {}
|
|
|
|
async execute(command: CompleteDriverOnboardingCommand): Promise<Result<{ driverId: string }, RacingDomainValidationError>> {
|
|
try {
|
|
// Check if driver already exists
|
|
const existing = await this.driverRepository.findById(command.userId);
|
|
if (existing) {
|
|
return Result.err(new RacingDomainValidationError('Driver already exists'));
|
|
}
|
|
|
|
// Create new driver
|
|
const driver = Driver.create({
|
|
id: command.userId,
|
|
iracingId: command.userId, // Assuming userId is iracingId for now
|
|
name: command.displayName,
|
|
country: command.country,
|
|
...(command.bio !== undefined ? { bio: command.bio } : {}),
|
|
});
|
|
|
|
await this.driverRepository.create(driver);
|
|
|
|
return Result.ok({ driverId: driver.id });
|
|
} catch (error) {
|
|
return Result.err(new RacingDomainValidationError(error instanceof Error ? error.message : 'Unknown error'));
|
|
}
|
|
}
|
|
} |