refactor racing use cases
This commit is contained in:
@@ -1,36 +1,89 @@
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { Driver } from '../../domain/entities/Driver';
|
||||
|
||||
export interface UpdateDriverProfileInput {
|
||||
export type UpdateDriverProfileInput = {
|
||||
driverId: string;
|
||||
bio?: string;
|
||||
country?: string;
|
||||
}
|
||||
};
|
||||
|
||||
export type UpdateDriverProfileResult = {
|
||||
driverId: string;
|
||||
};
|
||||
|
||||
export type UpdateDriverProfileErrorCode =
|
||||
| 'DRIVER_NOT_FOUND'
|
||||
| 'INVALID_PROFILE_DATA'
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
/**
|
||||
* Application use case responsible for updating basic driver profile details.
|
||||
* Encapsulates domain entity mutation and returns the updated entity.
|
||||
* Mapping to DTOs is handled by presenters in the presentation layer.
|
||||
* Encapsulates domain entity mutation. Mapping to DTOs is handled by presenters
|
||||
* in the presentation layer through the output port.
|
||||
*/
|
||||
export class UpdateDriverProfileUseCase {
|
||||
constructor(private readonly driverRepository: IDriverRepository) {}
|
||||
constructor(
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
private readonly output: UseCaseOutputPort<UpdateDriverProfileResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(input: UpdateDriverProfileInput): Promise<Result<Driver, ApplicationErrorCode<'DRIVER_NOT_FOUND'>>> {
|
||||
async execute(
|
||||
input: UpdateDriverProfileInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<UpdateDriverProfileErrorCode, { message: string }>>> {
|
||||
const { driverId, bio, country } = input;
|
||||
|
||||
const existing = await this.driverRepository.findById(driverId);
|
||||
if (!existing) {
|
||||
return Result.err({ code: 'DRIVER_NOT_FOUND' });
|
||||
if ((bio !== undefined && bio.trim().length === 0) || (country !== undefined && country.trim().length === 0)) {
|
||||
return Result.err({
|
||||
code: 'INVALID_PROFILE_DATA',
|
||||
details: {
|
||||
message: 'Profile data is invalid',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const updated = existing.update({
|
||||
...(bio !== undefined ? { bio } : {}),
|
||||
...(country !== undefined ? { country } : {}),
|
||||
});
|
||||
try {
|
||||
const existing = await this.driverRepository.findById(driverId);
|
||||
if (!existing) {
|
||||
return Result.err({
|
||||
code: 'DRIVER_NOT_FOUND',
|
||||
details: {
|
||||
message: `Driver with id ${driverId} not found`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const persisted = await this.driverRepository.update(updated);
|
||||
return Result.ok(persisted);
|
||||
const updated: Driver = existing.update({
|
||||
...(bio !== undefined ? { bio } : {}),
|
||||
...(country !== undefined ? { country } : {}),
|
||||
});
|
||||
|
||||
await this.driverRepository.update(updated);
|
||||
|
||||
const result: UpdateDriverProfileResult = {
|
||||
driverId: updated.id,
|
||||
};
|
||||
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to update driver profile';
|
||||
|
||||
this.logger.error('Failed to update driver profile', error instanceof Error ? error : undefined, {
|
||||
driverId,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user