refactor
This commit is contained in:
@@ -1,60 +1,40 @@
|
||||
import type { AsyncUseCase } from '@core/shared/application';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { ICompleteDriverOnboardingPresenter, CompleteDriverOnboardingResultDTO } from '../presenters/ICompleteDriverOnboardingPresenter';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
import { Driver } from '../../domain/entities/Driver';
|
||||
|
||||
export interface CompleteDriverOnboardingInput {
|
||||
userId: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
displayName: string;
|
||||
country: string;
|
||||
timezone?: string;
|
||||
bio?: string;
|
||||
}
|
||||
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 UseCase<CompleteDriverOnboardingInput, CompleteDriverOnboardingResultDTO, any, ICompleteDriverOnboardingPresenter>
|
||||
implements AsyncUseCase<CompleteDriverOnboardingCommand, Result<{ driverId: string }, RacingDomainValidationError>>
|
||||
{
|
||||
constructor(private readonly driverRepository: IDriverRepository) {}
|
||||
|
||||
async execute(input: CompleteDriverOnboardingInput, presenter: ICompleteDriverOnboardingPresenter): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
async execute(command: CompleteDriverOnboardingCommand): Promise<Result<{ driverId: string }, RacingDomainValidationError>> {
|
||||
try {
|
||||
// Check if driver already exists
|
||||
const existing = await this.driverRepository.findById(input.userId);
|
||||
const existing = await this.driverRepository.findById(command.userId);
|
||||
if (existing) {
|
||||
presenter.present({
|
||||
success: false,
|
||||
errorMessage: 'Driver already exists',
|
||||
});
|
||||
return;
|
||||
return Result.err(new RacingDomainValidationError('Driver already exists'));
|
||||
}
|
||||
|
||||
// Create new driver
|
||||
const driver = Driver.create({
|
||||
id: input.userId,
|
||||
iracingId: input.userId, // Assuming userId is iracingId for now
|
||||
name: input.displayName,
|
||||
country: input.country,
|
||||
bio: input.bio,
|
||||
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.save(driver);
|
||||
await this.driverRepository.create(driver);
|
||||
|
||||
presenter.present({
|
||||
success: true,
|
||||
driverId: driver.id,
|
||||
});
|
||||
return Result.ok({ driverId: driver.id });
|
||||
} catch (error) {
|
||||
presenter.present({
|
||||
success: false,
|
||||
errorMessage: error instanceof Error ? error.message : 'Unknown error',
|
||||
});
|
||||
return Result.err(new RacingDomainValidationError(error instanceof Error ? error.message : 'Unknown error'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user