refactor use cases
This commit is contained in:
@@ -3,6 +3,7 @@ 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';
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
|
||||
export interface CompleteDriverOnboardingInput {
|
||||
userId: string;
|
||||
@@ -17,28 +18,43 @@ export type CompleteDriverOnboardingResult = {
|
||||
driver: Driver;
|
||||
};
|
||||
|
||||
export type CompleteDriverOnboardingErrorCode =
|
||||
| 'DRIVER_ALREADY_EXISTS'
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
export type CompleteDriverOnboardingApplicationError = ApplicationErrorCode<
|
||||
CompleteDriverOnboardingErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
/**
|
||||
* Use Case for completing driver onboarding.
|
||||
*/
|
||||
export class CompleteDriverOnboardingUseCase {
|
||||
constructor(
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<CompleteDriverOnboardingResult>,
|
||||
) {}
|
||||
|
||||
async execute(command: CompleteDriverOnboardingInput): Promise<Result<void, ApplicationErrorCode<'DRIVER_ALREADY_EXISTS' | 'REPOSITORY_ERROR'>>> {
|
||||
async execute(
|
||||
input: CompleteDriverOnboardingInput,
|
||||
): Promise<Result<void, CompleteDriverOnboardingApplicationError>> {
|
||||
try {
|
||||
const existing = await this.driverRepository.findById(command.userId);
|
||||
const existing = await this.driverRepository.findById(input.userId);
|
||||
if (existing) {
|
||||
return Result.err({ code: 'DRIVER_ALREADY_EXISTS' });
|
||||
return Result.err({
|
||||
code: 'DRIVER_ALREADY_EXISTS',
|
||||
details: { message: '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 } : {}),
|
||||
id: input.userId,
|
||||
iracingId: input.userId,
|
||||
name: input.displayName,
|
||||
country: input.country,
|
||||
...(input.bio !== undefined ? { bio: input.bio } : {}),
|
||||
});
|
||||
|
||||
await this.driverRepository.create(driver);
|
||||
@@ -47,10 +63,16 @@ export class CompleteDriverOnboardingUseCase {
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error('Unknown error');
|
||||
|
||||
this.logger.error('CompleteDriverOnboardingUseCase.execute failed', err, {
|
||||
input,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message: error instanceof Error ? error.message : 'Unknown error',
|
||||
message: err.message ?? 'Unexpected repository error',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user