Files
gridpilot.gg/core/media/application/use-cases/UpdateAvatarUseCase.ts
2025-12-19 01:22:45 +01:00

81 lines
2.1 KiB
TypeScript

/**
* Use Case: UpdateAvatarUseCase
*
* 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 { v4 as uuidv4 } from 'uuid';
export interface UpdateAvatarInput {
driverId: string;
mediaUrl: string;
}
export interface UpdateAvatarResult {
success: boolean;
errorMessage?: string;
}
export interface IUpdateAvatarPresenter {
present(result: UpdateAvatarResult): void;
}
export class UpdateAvatarUseCase {
constructor(
private readonly avatarRepo: IAvatarRepository,
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,
});
// Deactivate current active avatar
const currentAvatar = await this.avatarRepo.findActiveByDriverId(input.driverId);
if (currentAvatar) {
currentAvatar.deactivate();
await this.avatarRepo.save(currentAvatar);
}
// Create new avatar
const avatarId = uuidv4();
const newAvatar = Avatar.create({
id: avatarId,
driverId: input.driverId,
mediaUrl: input.mediaUrl,
});
await this.avatarRepo.save(newAvatar);
presenter.present({
success: true,
});
this.logger.info('[UpdateAvatarUseCase] Avatar updated successfully', {
driverId: input.driverId,
avatarId,
});
} catch (error) {
this.logger.error('[UpdateAvatarUseCase] Error updating avatar', {
error: error instanceof Error ? error.message : 'Unknown error',
driverId: input.driverId,
});
presenter.present({
success: false,
errorMessage: 'Internal error occurred while updating avatar',
});
}
}
}