Files
gridpilot.gg/packages/analytics/infrastructure/repositories/InMemoryPageViewRepository.ts
2025-12-10 12:38:55 +01:00

70 lines
2.2 KiB
TypeScript

/**
* Infrastructure: InMemoryPageViewRepository
*
* In-memory implementation of IPageViewRepository for development/testing.
*/
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
import { PageView, type EntityType } from '../../domain/entities/PageView';
export class InMemoryPageViewRepository implements IPageViewRepository {
private pageViews: Map<string, PageView> = new Map();
async save(pageView: PageView): Promise<void> {
this.pageViews.set(pageView.id, pageView);
}
async findById(id: string): Promise<PageView | null> {
return this.pageViews.get(id) ?? null;
}
async findByEntityId(entityType: EntityType, entityId: string): Promise<PageView[]> {
return Array.from(this.pageViews.values()).filter(
pv => pv.entityType === entityType && pv.entityId === entityId
);
}
async findByDateRange(startDate: Date, endDate: Date): Promise<PageView[]> {
return Array.from(this.pageViews.values()).filter(
pv => pv.timestamp >= startDate && pv.timestamp <= endDate
);
}
async findBySession(sessionId: string): Promise<PageView[]> {
return Array.from(this.pageViews.values()).filter(
pv => pv.sessionId === sessionId
);
}
async countByEntityId(entityType: EntityType, entityId: string, since?: Date): Promise<number> {
return Array.from(this.pageViews.values()).filter(
pv => pv.entityType === entityType &&
pv.entityId === entityId &&
(!since || pv.timestamp >= since)
).length;
}
async countUniqueVisitors(entityType: EntityType, entityId: string, since?: Date): Promise<number> {
const visitors = new Set<string>();
Array.from(this.pageViews.values())
.filter(
pv => pv.entityType === entityType &&
pv.entityId === entityId &&
(!since || pv.timestamp >= since)
)
.forEach(pv => {
visitors.add(pv.visitorId ?? pv.sessionId);
});
return visitors.size;
}
// Helper for testing
clear(): void {
this.pageViews.clear();
}
// Helper for seeding demo data
seed(pageViews: PageView[]): void {
pageViews.forEach(pv => this.pageViews.set(pv.id, pv));
}
}