refactor use cases

This commit is contained in:
2025-12-21 01:20:27 +01:00
parent c12656d671
commit 8ecd638396
39 changed files with 2523 additions and 686 deletions

View File

@@ -4,11 +4,12 @@
* Handles the business logic for updating a driver's avatar.
*/
import type { IAvatarRepository } from '../../domain/repositories/IAvatarRepository';
import type { Logger } from '@core/shared/application';
import { Avatar } from '../../domain/entities/Avatar';
import type { IUpdateAvatarPresenter } from '../presenters/IUpdateAvatarPresenter';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { v4 as uuidv4 } from 'uuid';
import { Avatar } from '../../domain/entities/Avatar';
import type { IAvatarRepository } from '../../domain/repositories/IAvatarRepository';
export interface UpdateAvatarInput {
driverId: string;
@@ -16,39 +17,38 @@ export interface UpdateAvatarInput {
}
export interface UpdateAvatarResult {
success: boolean;
errorMessage?: string;
avatarId: string;
driverId: string;
}
export interface IUpdateAvatarPresenter {
present(result: UpdateAvatarResult): void;
}
export type UpdateAvatarErrorCode = 'REPOSITORY_ERROR';
export type UpdateAvatarApplicationError = ApplicationErrorCode<
UpdateAvatarErrorCode,
{ message: string }
>;
export class UpdateAvatarUseCase {
constructor(
private readonly avatarRepo: IAvatarRepository,
private readonly output: UseCaseOutputPort<UpdateAvatarResult>,
private readonly logger: Logger,
) {}
async execute(
input: UpdateAvatarInput,
presenter: IUpdateAvatarPresenter,
): Promise<void> {
try {
this.logger.info('[UpdateAvatarUseCase] Updating avatar', {
driverId: input.driverId,
mediaUrl: input.mediaUrl,
});
async execute(input: UpdateAvatarInput): Promise<Result<void, UpdateAvatarApplicationError>> {
this.logger.info('[UpdateAvatarUseCase] Updating avatar', {
driverId: input.driverId,
mediaUrl: input.mediaUrl,
});
// Deactivate current active avatar
try {
const currentAvatar = await this.avatarRepo.findActiveByDriverId(input.driverId);
if (currentAvatar) {
currentAvatar.deactivate();
await this.avatarRepo.save(currentAvatar);
}
// Create new avatar
const avatarId = uuidv4();
const avatarId = uuidv4(); // TODO this ID should be a value object
const newAvatar = Avatar.create({
id: avatarId,
driverId: input.driverId,
@@ -57,8 +57,9 @@ export class UpdateAvatarUseCase {
await this.avatarRepo.save(newAvatar);
presenter.present({
success: true,
this.output.present({
avatarId,
driverId: input.driverId,
});
this.logger.info('[UpdateAvatarUseCase] Avatar updated successfully', {
@@ -66,15 +67,17 @@ export class UpdateAvatarUseCase {
avatarId,
});
return Result.ok(undefined);
} catch (error) {
this.logger.error('[UpdateAvatarUseCase] Error updating avatar', {
error: error instanceof Error ? error.message : 'Unknown error',
const err = error instanceof Error ? error : new Error(String(error));
this.logger.error('[UpdateAvatarUseCase] Error updating avatar', err, {
driverId: input.driverId,
});
presenter.present({
success: false,
errorMessage: 'Internal error occurred while updating avatar',
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message ?? 'Internal error occurred while updating avatar' },
});
}
}