/** * InMemory implementation of ISponsorshipPricingRepository */ import type { ISponsorshipPricingRepository } from '@core/racing/domain/repositories/ISponsorshipPricingRepository'; import { SponsorshipPricing } from '@core/racing/domain/value-objects/SponsorshipPricing'; import type { SponsorableEntityType } from '@core/racing/domain/entities/SponsorshipRequest'; import type { Logger } from '@core/shared/application'; interface StorageKey { entityType: SponsorableEntityType; entityId: string; } export class InMemorySponsorshipPricingRepository implements ISponsorshipPricingRepository { private pricings: Map = new Map(); private readonly logger: Logger; constructor(logger: Logger, seedData?: Array<{ entityType: SponsorableEntityType; entityId: string; pricing: SponsorshipPricing }>) { this.logger = logger; this.logger.info('InMemorySponsorshipPricingRepository initialized.'); if (seedData) { this.seed(seedData); } } private makeKey(entityType: SponsorableEntityType, entityId: string): string { return `${entityType}:${entityId}`; } async findByEntity(entityType: SponsorableEntityType, entityId: string): Promise { this.logger.debug(`Finding sponsorship pricing for entity: ${entityType}, ${entityId}`); try { const key = this.makeKey(entityType, entityId); const entry = this.pricings.get(key); const pricing = entry?.pricing ?? null; if (pricing) { this.logger.info(`Found sponsorship pricing for entity: ${entityType}, ${entityId}.`); } else { this.logger.warn(`Sponsorship pricing for entity ${entityType}, ${entityId} not found.`); } return pricing; } catch (error) { this.logger.error(`Error finding sponsorship pricing for entity ${entityType}, ${entityId}:`, error); throw error; } } async save(entityType: SponsorableEntityType, entityId: string, pricing: SponsorshipPricing): Promise { this.logger.debug(`Saving sponsorship pricing for entity: ${entityType}, ${entityId}`); try { const key = this.makeKey(entityType, entityId); if (this.pricings.has(key)) { this.logger.info(`Updating existing sponsorship pricing for entity: ${entityType}, ${entityId}.`); } else { this.logger.info(`Creating new sponsorship pricing for entity: ${entityType}, ${entityId}.`); } this.pricings.set(key, { entityType, entityId, pricing }); this.logger.info(`Sponsorship pricing saved for entity: ${entityType}, ${entityId}.`); } catch (error) { this.logger.error(`Error saving sponsorship pricing for entity ${entityType}, ${entityId}:`, error); throw error; } } async delete(entityType: SponsorableEntityType, entityId: string): Promise { this.logger.debug(`Deleting sponsorship pricing for entity: ${entityType}, ${entityId}`); try { const key = this.makeKey(entityType, entityId); if (this.pricings.delete(key)) { this.logger.info(`Sponsorship pricing deleted for entity: ${entityType}, ${entityId}.`); } else { this.logger.warn(`Sponsorship pricing for entity ${entityType}, ${entityId} not found for deletion.`); } } catch (error) { this.logger.error(`Error deleting sponsorship pricing for entity ${entityType}, ${entityId}:`, error); throw error; } } async exists(entityType: SponsorableEntityType, entityId: string): Promise { this.logger.debug(`Checking existence of sponsorship pricing for entity: ${entityType}, ${entityId}`); try { const key = this.makeKey(entityType, entityId); const exists = this.pricings.has(key); this.logger.debug(`Sponsorship pricing for entity ${entityType}, ${entityId} exists: ${exists}.`); return exists; } catch (error) { this.logger.error(`Error checking existence of sponsorship pricing for entity ${entityType}, ${entityId}:`, error); throw error; } } async findAcceptingApplications(entityType: SponsorableEntityType): Promise> { this.logger.debug(`Finding entities accepting applications for type: ${entityType}`); try { const accepting = Array.from(this.pricings.values()) .filter(entry => entry.entityType === entityType && entry.pricing.acceptingApplications) .map(entry => ({ entityId: entry.entityId, pricing: entry.pricing })); this.logger.info(`Found ${accepting.length} entities accepting applications for type: ${entityType}.`); return accepting; } catch (error) { this.logger.error(`Error finding accepting applications for entity type ${entityType}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } /** * Seed initial data */ seed(data: Array<{ entityType: SponsorableEntityType; entityId: string; pricing: SponsorshipPricing }>): void { this.logger.debug(`Seeding ${data.length} sponsorship pricing entries.`); try { for (const entry of data) { const key = this.makeKey(entry.entityType, entry.entityId); this.pricings.set(key, entry); this.logger.debug(`Seeded pricing for entity ${entry.entityType}, ${entry.entityId}.`); } this.logger.info(`Successfully seeded ${data.length} sponsorship pricing entries.`); } catch (error) { this.logger.error(`Error seeding sponsorship pricing data:`, error instanceof Error ? error : new Error(String(error))); throw error; } } /** * Clear all data (for testing) */ clear(): void { this.logger.debug('Clearing all sponsorship pricing data.'); this.pricings.clear(); this.logger.info('All sponsorship pricing data cleared.'); } }