65 lines
1.5 KiB
TypeScript
65 lines
1.5 KiB
TypeScript
/**
|
|
* Use Case: SelectAvatarUseCase
|
|
*
|
|
* Allows a user to select one of the generated avatars as their profile avatar.
|
|
*/
|
|
|
|
import type { IAvatarGenerationRepository } from '../../domain/repositories/IAvatarGenerationRepository';
|
|
|
|
export interface SelectAvatarCommand {
|
|
requestId: string;
|
|
userId: string;
|
|
avatarIndex: number;
|
|
}
|
|
|
|
export interface SelectAvatarResult {
|
|
success: boolean;
|
|
selectedAvatarUrl?: string;
|
|
errorMessage?: string;
|
|
}
|
|
|
|
export class SelectAvatarUseCase {
|
|
constructor(
|
|
private readonly avatarRepository: IAvatarGenerationRepository,
|
|
) {}
|
|
|
|
async execute(command: SelectAvatarCommand): Promise<SelectAvatarResult> {
|
|
const request = await this.avatarRepository.findById(command.requestId);
|
|
|
|
if (!request) {
|
|
return {
|
|
success: false,
|
|
errorMessage: 'Avatar generation request not found',
|
|
};
|
|
}
|
|
|
|
if (request.userId !== command.userId) {
|
|
return {
|
|
success: false,
|
|
errorMessage: 'You do not have permission to select this avatar',
|
|
};
|
|
}
|
|
|
|
if (request.status !== 'completed') {
|
|
return {
|
|
success: false,
|
|
errorMessage: 'Avatar generation is not yet complete',
|
|
};
|
|
}
|
|
|
|
try {
|
|
request.selectAvatar(command.avatarIndex);
|
|
await this.avatarRepository.save(request);
|
|
|
|
return {
|
|
success: true,
|
|
selectedAvatarUrl: request.selectedAvatarUrl,
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
errorMessage: error instanceof Error ? error.message : 'Failed to select avatar',
|
|
};
|
|
}
|
|
}
|
|
} |