58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
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 { 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 {
|
|
constructor(
|
|
private readonly driverRepository: IDriverRepository,
|
|
private readonly output: UseCaseOutputPort<CompleteDriverOnboardingResult>,
|
|
) {}
|
|
|
|
async execute(command: CompleteDriverOnboardingInput): Promise<Result<void, ApplicationErrorCode<'DRIVER_ALREADY_EXISTS' | 'REPOSITORY_ERROR'>>> {
|
|
try {
|
|
const existing = await this.driverRepository.findById(command.userId);
|
|
if (existing) {
|
|
return Result.err({ code: 'DRIVER_ALREADY_EXISTS' });
|
|
}
|
|
|
|
const driver = Driver.create({
|
|
id: command.userId,
|
|
iracingId: command.userId,
|
|
name: command.displayName,
|
|
country: command.country,
|
|
...(command.bio !== undefined ? { bio: command.bio } : {}),
|
|
});
|
|
|
|
await this.driverRepository.create(driver);
|
|
|
|
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',
|
|
},
|
|
});
|
|
}
|
|
}
|
|
} |