rename to core
This commit is contained in:
35
core/media/application/ports/AvatarGenerationPort.ts
Normal file
35
core/media/application/ports/AvatarGenerationPort.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Port: AvatarGenerationPort
|
||||
*
|
||||
* Defines the contract for AI-powered avatar generation.
|
||||
*/
|
||||
|
||||
import type { RacingSuitColor, AvatarStyle } from '../../domain/types/AvatarGenerationRequest';
|
||||
|
||||
export interface AvatarGenerationOptions {
|
||||
facePhotoUrl: string;
|
||||
prompt: string;
|
||||
suitColor: RacingSuitColor;
|
||||
style: AvatarStyle;
|
||||
count: number;
|
||||
}
|
||||
|
||||
export interface GeneratedAvatar {
|
||||
url: string;
|
||||
thumbnailUrl?: string;
|
||||
}
|
||||
|
||||
export interface AvatarGenerationResult {
|
||||
success: boolean;
|
||||
avatars: GeneratedAvatar[];
|
||||
errorMessage?: string;
|
||||
}
|
||||
|
||||
export interface AvatarGenerationPort {
|
||||
/**
|
||||
* Generate racing avatars from a face photo
|
||||
* @param options Generation options including face photo and styling preferences
|
||||
* @returns Generated avatar URLs
|
||||
*/
|
||||
generateAvatars(options: AvatarGenerationOptions): Promise<AvatarGenerationResult>;
|
||||
}
|
||||
20
core/media/application/ports/FaceValidationPort.ts
Normal file
20
core/media/application/ports/FaceValidationPort.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Port: FaceValidationPort
|
||||
*
|
||||
* Defines the contract for validating face photos.
|
||||
*/
|
||||
|
||||
export interface FaceValidationResult {
|
||||
isValid: boolean;
|
||||
hasFace: boolean;
|
||||
faceCount: number;
|
||||
confidence: number;
|
||||
errorMessage?: string;
|
||||
}
|
||||
|
||||
export interface FaceValidationPort {
|
||||
/**
|
||||
* Validate that an image contains exactly one valid face
|
||||
*/
|
||||
validateFacePhoto(imageData: string | Buffer): Promise<FaceValidationResult>;
|
||||
}
|
||||
6
core/media/application/ports/ImageServicePort.ts
Normal file
6
core/media/application/ports/ImageServicePort.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface ImageServicePort {
|
||||
getDriverAvatar(driverId: string): string;
|
||||
getTeamLogo(teamId: string): string;
|
||||
getLeagueCover(leagueId: string): string;
|
||||
getLeagueLogo(leagueId: string): string;
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
import type { AsyncUseCase, ILogger } from '@gridpilot/shared/application';
|
||||
import type { IAvatarGenerationRepository } from '../../domain/repositories/IAvatarGenerationRepository';
|
||||
import type { FaceValidationPort } from '../ports/FaceValidationPort';
|
||||
import type { AvatarGenerationPort } from '../ports/AvatarGenerationPort';
|
||||
import { AvatarGenerationRequest } from '../../domain/entities/AvatarGenerationRequest';
|
||||
import type { RacingSuitColor, AvatarStyle } from '../../domain/types/AvatarGenerationRequest';
|
||||
|
||||
export interface RequestAvatarGenerationCommand {
|
||||
userId: string;
|
||||
facePhotoData: string; // Base64 encoded image data
|
||||
suitColor: RacingSuitColor;
|
||||
style?: AvatarStyle;
|
||||
}
|
||||
|
||||
export interface RequestAvatarGenerationResult {
|
||||
requestId: string;
|
||||
status: 'validating' | 'generating' | 'completed' | 'failed';
|
||||
avatarUrls?: string[];
|
||||
errorMessage?: string;
|
||||
}
|
||||
|
||||
export class RequestAvatarGenerationUseCase
|
||||
implements AsyncUseCase<RequestAvatarGenerationCommand, RequestAvatarGenerationResult> {
|
||||
constructor(
|
||||
private readonly avatarRepository: IAvatarGenerationRepository,
|
||||
private readonly faceValidation: FaceValidationPort,
|
||||
private readonly avatarGeneration: AvatarGenerationPort,
|
||||
private readonly logger: ILogger,
|
||||
) {}
|
||||
|
||||
async execute(command: RequestAvatarGenerationCommand): Promise<RequestAvatarGenerationResult> {
|
||||
this.logger.debug(
|
||||
`Executing RequestAvatarGenerationUseCase for userId: ${command.userId}`,
|
||||
command,
|
||||
);
|
||||
|
||||
try {
|
||||
// Create the generation request
|
||||
const requestId = this.generateId();
|
||||
const request = AvatarGenerationRequest.create({
|
||||
id: requestId,
|
||||
userId: command.userId,
|
||||
facePhotoUrl: `data:image/jpeg;base64,${command.facePhotoData}`,
|
||||
suitColor: command.suitColor,
|
||||
...(command.style ? { style: command.style } : {}),
|
||||
});
|
||||
|
||||
this.logger.info(`Avatar generation request created with ID: ${requestId}`);
|
||||
|
||||
// Mark as validating
|
||||
request.markAsValidating();
|
||||
await this.avatarRepository.save(request);
|
||||
this.logger.debug(`Request ${requestId} marked as validating.`);
|
||||
|
||||
// Validate the face photo
|
||||
const validationResult = await this.faceValidation.validateFacePhoto(command.facePhotoData);
|
||||
this.logger.debug(
|
||||
`Face validation result for request ${requestId}:`,
|
||||
validationResult,
|
||||
);
|
||||
|
||||
if (!validationResult.isValid) {
|
||||
const errorMessage = validationResult.errorMessage || 'Face validation failed';
|
||||
request.fail(errorMessage);
|
||||
await this.avatarRepository.save(request);
|
||||
this.logger.error(`Face validation failed for request ${requestId}: ${errorMessage}`);
|
||||
return {
|
||||
requestId,
|
||||
status: 'failed',
|
||||
errorMessage: validationResult.errorMessage || 'Please upload a clear photo of your face',
|
||||
};
|
||||
}
|
||||
|
||||
if (!validationResult.hasFace) {
|
||||
const errorMessage = 'No face detected in the image';
|
||||
request.fail(errorMessage);
|
||||
await this.avatarRepository.save(request);
|
||||
this.logger.error(`No face detected for request ${requestId}: ${errorMessage}`);
|
||||
return {
|
||||
requestId,
|
||||
status: 'failed',
|
||||
errorMessage: 'No face detected. Please upload a photo that clearly shows your face.',
|
||||
};
|
||||
}
|
||||
|
||||
if (validationResult.faceCount > 1) {
|
||||
const errorMessage = 'Multiple faces detected';
|
||||
request.fail(errorMessage);
|
||||
await this.avatarRepository.save(request);
|
||||
this.logger.error(`Multiple faces detected for request ${requestId}: ${errorMessage}`);
|
||||
return {
|
||||
requestId,
|
||||
status: 'failed',
|
||||
errorMessage: 'Multiple faces detected. Please upload a photo with only your face.',
|
||||
};
|
||||
}
|
||||
this.logger.info(`Face validation successful for request ${requestId}.`);
|
||||
|
||||
// Mark as generating
|
||||
request.markAsGenerating();
|
||||
await this.avatarRepository.save(request);
|
||||
this.logger.debug(`Request ${requestId} marked as generating.`);
|
||||
|
||||
// Generate avatars
|
||||
const generationResult = await this.avatarGeneration.generateAvatars({
|
||||
facePhotoUrl: request.facePhotoUrl.value,
|
||||
prompt: request.buildPrompt(),
|
||||
suitColor: request.suitColor,
|
||||
style: request.style,
|
||||
count: 3, // Generate 3 options
|
||||
});
|
||||
this.logger.debug(
|
||||
`Avatar generation service result for request ${requestId}:`,
|
||||
generationResult,
|
||||
);
|
||||
|
||||
if (!generationResult.success) {
|
||||
const errorMessage = generationResult.errorMessage || 'Avatar generation failed';
|
||||
request.fail(errorMessage);
|
||||
await this.avatarRepository.save(request);
|
||||
this.logger.error(`Avatar generation failed for request ${requestId}: ${errorMessage}`);
|
||||
return {
|
||||
requestId,
|
||||
status: 'failed',
|
||||
errorMessage: generationResult.errorMessage || 'Failed to generate avatars. Please try again.',
|
||||
};
|
||||
}
|
||||
|
||||
// Complete with generated avatars
|
||||
const avatarUrls = generationResult.avatars.map(a => a.url);
|
||||
request.completeWithAvatars(avatarUrls);
|
||||
await this.avatarRepository.save(request);
|
||||
this.logger.info(`Avatar generation completed successfully for request ${requestId}.`);
|
||||
|
||||
return {
|
||||
requestId,
|
||||
status: 'completed',
|
||||
avatarUrls,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`An unexpected error occurred during avatar generation for userId: ${command.userId}`,
|
||||
error,
|
||||
);
|
||||
// Re-throw or return a generic error, depending on desired error handling strategy
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private generateId(): string {
|
||||
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
return 'avatar-' + Date.now().toString(36) + Math.random().toString(36).substr(2, 9);
|
||||
}
|
||||
}
|
||||
78
core/media/application/use-cases/SelectAvatarUseCase.ts
Normal file
78
core/media/application/use-cases/SelectAvatarUseCase.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Use Case: SelectAvatarUseCase
|
||||
*
|
||||
* Allows a user to select one of the generated avatars as their profile avatar.
|
||||
*/
|
||||
|
||||
import type { AsyncUseCase, ILogger } from '@gridpilot/shared/application';
|
||||
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
|
||||
implements AsyncUseCase<SelectAvatarCommand, SelectAvatarResult> {
|
||||
constructor(
|
||||
private readonly avatarRepository: IAvatarGenerationRepository,
|
||||
private readonly logger: ILogger,
|
||||
) {}
|
||||
|
||||
async execute(command: SelectAvatarCommand): Promise<SelectAvatarResult> {
|
||||
this.logger.debug(`Executing SelectAvatarUseCase for userId: ${command.userId}, requestId: ${command.requestId}, avatarIndex: ${command.avatarIndex}`);
|
||||
|
||||
const request = await this.avatarRepository.findById(command.requestId);
|
||||
|
||||
if (!request) {
|
||||
this.logger.info(`Avatar generation request not found for requestId: ${command.requestId}`);
|
||||
return {
|
||||
success: false,
|
||||
errorMessage: 'Avatar generation request not found',
|
||||
};
|
||||
}
|
||||
|
||||
if (request.userId !== command.userId) {
|
||||
this.logger.info(`Permission denied for userId: ${command.userId} to select avatar for requestId: ${command.requestId}`);
|
||||
return {
|
||||
success: false,
|
||||
errorMessage: 'You do not have permission to select this avatar',
|
||||
};
|
||||
}
|
||||
|
||||
if (request.status !== 'completed') {
|
||||
this.logger.info(`Avatar generation not completed for requestId: ${command.requestId}, current status: ${request.status}`);
|
||||
return {
|
||||
success: false,
|
||||
errorMessage: 'Avatar generation is not yet complete',
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
request.selectAvatar(command.avatarIndex);
|
||||
await this.avatarRepository.save(request);
|
||||
|
||||
const selectedAvatarUrl = request.selectedAvatarUrl;
|
||||
const result: SelectAvatarResult =
|
||||
selectedAvatarUrl !== undefined
|
||||
? { success: true, selectedAvatarUrl }
|
||||
: { success: true };
|
||||
|
||||
this.logger.info(`Avatar selected successfully for userId: ${command.userId}, requestId: ${command.requestId}, selectedAvatarUrl: ${selectedAvatarUrl}`);
|
||||
return result;
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to select avatar for userId: ${command.userId}, requestId: ${command.requestId}: ${error instanceof Error ? error.message : 'Unknown error'}`, error);
|
||||
return {
|
||||
success: false,
|
||||
errorMessage: error instanceof Error ? error.message : 'Failed to select avatar',
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user