/** * Use Case: RecordPageViewUseCase * * Records a page view event when a visitor accesses an entity page. */ import type { AsyncUseCase } from '@gridpilot/shared/application'; import { PageView } from '../../domain/entities/PageView'; import type { EntityType, VisitorType } from '../../domain/types/PageView'; import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository'; export interface RecordPageViewInput { entityType: EntityType; entityId: string; visitorId?: string; visitorType: VisitorType; sessionId: string; referrer?: string; userAgent?: string; country?: string; } export interface RecordPageViewOutput { pageViewId: string; } export class RecordPageViewUseCase implements AsyncUseCase { constructor(private readonly pageViewRepository: IPageViewRepository) {} async execute(input: RecordPageViewInput): Promise { const pageViewId = `pv-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; const baseProps: Omit[0], 'timestamp'> = { id: pageViewId, entityType: input.entityType, entityId: input.entityId, visitorType: input.visitorType, sessionId: input.sessionId, }; const pageView = PageView.create({ ...baseProps, ...(input.visitorId !== undefined ? { visitorId: input.visitorId } : {}), ...(input.referrer !== undefined ? { referrer: input.referrer } : {}), ...(input.userAgent !== undefined ? { userAgent: input.userAgent } : {}), ...(input.country !== undefined ? { country: input.country } : {}), }); await this.pageViewRepository.save(pageView); return { pageViewId }; } }