47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
/**
|
|
* Use Case: RecordPageViewUseCase
|
|
*
|
|
* Records a page view event when a visitor accesses an entity page.
|
|
*/
|
|
|
|
import { PageView, type EntityType, type VisitorType } from '../../domain/entities/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 {
|
|
constructor(private readonly pageViewRepository: IPageViewRepository) {}
|
|
|
|
async execute(input: RecordPageViewInput): Promise<RecordPageViewOutput> {
|
|
const pageViewId = `pv-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
|
|
const pageView = PageView.create({
|
|
id: pageViewId,
|
|
entityType: input.entityType,
|
|
entityId: input.entityId,
|
|
visitorId: input.visitorId,
|
|
visitorType: input.visitorType,
|
|
sessionId: input.sessionId,
|
|
referrer: input.referrer,
|
|
userAgent: input.userAgent,
|
|
country: input.country,
|
|
});
|
|
|
|
await this.pageViewRepository.save(pageView);
|
|
|
|
return { pageViewId };
|
|
}
|
|
} |