/** * Infrastructure: InMemoryEngagementRepository * * In-memory implementation of IEngagementRepository for development/testing. */ import type { IEngagementRepository } from '../../domain/repositories/IEngagementRepository'; import { EngagementEvent, type EngagementAction, type EngagementEntityType } from '../../domain/entities/EngagementEvent'; export class InMemoryEngagementRepository implements IEngagementRepository { private events: Map = new Map(); async save(event: EngagementEvent): Promise { this.events.set(event.id, event); } async findById(id: string): Promise { return this.events.get(id) ?? null; } async findByEntityId(entityType: EngagementEntityType, entityId: string): Promise { return Array.from(this.events.values()).filter( e => e.entityType === entityType && e.entityId === entityId ); } async findByAction(action: EngagementAction): Promise { return Array.from(this.events.values()).filter( e => e.action === action ); } async findByDateRange(startDate: Date, endDate: Date): Promise { return Array.from(this.events.values()).filter( e => e.timestamp >= startDate && e.timestamp <= endDate ); } async countByAction(action: EngagementAction, entityId?: string, since?: Date): Promise { return Array.from(this.events.values()).filter( e => e.action === action && (!entityId || e.entityId === entityId) && (!since || e.timestamp >= since) ).length; } async getSponsorClicksForEntity(entityId: string, since?: Date): Promise { return Array.from(this.events.values()).filter( e => e.entityId === entityId && (e.action === 'click_sponsor_logo' || e.action === 'click_sponsor_url') && (!since || e.timestamp >= since) ).length; } // Helper for testing clear(): void { this.events.clear(); } // Helper for seeding demo data seed(events: EngagementEvent[]): void { events.forEach(e => this.events.set(e.id, e)); } }