import type { IAvatarGenerationRepository, } from '@gridpilot/media'; import { AvatarGenerationRequest, type AvatarGenerationRequestProps, } from '@gridpilot/media'; import { ILogger } from '@gridpilot/shared/logging/ILogger'; /** * In-memory implementation of IAvatarGenerationRepository. * * For demo/development purposes. In production, this would use a database. */ export class InMemoryAvatarGenerationRepository implements IAvatarGenerationRepository { private readonly requests = new Map(); private readonly logger: ILogger; constructor(logger: ILogger) { this.logger = logger; this.logger.info('InMemoryAvatarGenerationRepository initialized.'); } async save(request: AvatarGenerationRequest): Promise { this.logger.debug(`Saving avatar generation request with ID: ${request.id}`); this.requests.set(request.id, request.toProps()); this.logger.info(`Avatar generation request with ID: ${request.id} saved successfully.`); } async findById(id: string): Promise { this.logger.debug(`Finding avatar generation request by ID: ${id}`); const props = this.requests.get(id); if (!props) { this.logger.info(`Avatar generation request with ID: ${id} not found.`); return null; } this.logger.info(`Avatar generation request with ID: ${id} found.`); return AvatarGenerationRequest.reconstitute(props); } async findByUserId(userId: string): Promise { this.logger.debug(`Finding avatar generation requests by user ID: ${userId}`); const results: AvatarGenerationRequest[] = []; for (const props of Array.from(this.requests.values())) { if (props.userId === userId) { results.push(AvatarGenerationRequest.reconstitute(props)); } } this.logger.info(`${results.length} avatar generation requests found for user ID: ${userId}.`); return results.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()); } async findLatestByUserId(userId: string): Promise { this.logger.debug(`Finding latest avatar generation request for user ID: ${userId}`); const userRequests = await this.findByUserId(userId); if (userRequests.length === 0) { this.logger.info(`No avatar generation requests found for user ID: ${userId}.`); return null; } const latest = userRequests[0]; if (!latest) { this.logger.info(`No latest avatar generation request found for user ID: ${userId}.`); return null; } this.logger.info(`Latest avatar generation request found for user ID: ${userId}, ID: ${latest.id}.`); return latest; } async delete(id: string): Promise { this.logger.debug(`Deleting avatar generation request with ID: ${id}`); const deleted = this.requests.delete(id); if (deleted) { this.logger.info(`Avatar generation request with ID: ${id} deleted successfully.`); } else { this.logger.warn(`Attempted to delete non-existent avatar generation request with ID: ${id}.`); } } }