This commit is contained in:
2025-12-12 14:23:40 +01:00
parent 6a88fe93ab
commit 2cd3bfbb47
58 changed files with 2866 additions and 260 deletions

View File

@@ -30,15 +30,19 @@ export class RecordEngagementUseCase
async execute(input: RecordEngagementInput): Promise<RecordEngagementOutput> {
const eventId = `eng-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
const event = EngagementEvent.create({
const baseProps: Omit<Parameters<typeof EngagementEvent.create>[0], 'timestamp'> = {
id: eventId,
action: input.action,
entityType: input.entityType,
entityId: input.entityId,
actorId: input.actorId,
actorType: input.actorType,
sessionId: input.sessionId,
metadata: input.metadata,
};
const event = EngagementEvent.create({
...baseProps,
...(input.actorId !== undefined ? { actorId: input.actorId } : {}),
...(input.metadata !== undefined ? { metadata: input.metadata } : {}),
});
await this.engagementRepository.save(event);

View File

@@ -31,16 +31,20 @@ export class RecordPageViewUseCase
async execute(input: RecordPageViewInput): Promise<RecordPageViewOutput> {
const pageViewId = `pv-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
const pageView = PageView.create({
const baseProps: Omit<Parameters<typeof PageView.create>[0], 'timestamp'> = {
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,
};
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);

View File

@@ -12,6 +12,7 @@ import type {
SnapshotEntityType,
SnapshotPeriod,
} from '../types/AnalyticsSnapshot';
export type { SnapshotEntityType, SnapshotPeriod } from '../types/AnalyticsSnapshot';
import { AnalyticsEntityId } from '../value-objects/AnalyticsEntityId';
export class AnalyticsSnapshot implements IEntity<string> {

View File

@@ -56,10 +56,21 @@ export class PageView implements IEntity<string> {
static create(props: Omit<PageViewProps, 'timestamp'> & { timestamp?: Date }): PageView {
this.validate(props);
return new PageView({
...props,
const baseProps: PageViewProps = {
id: props.id,
entityType: props.entityType,
entityId: props.entityId,
visitorType: props.visitorType,
sessionId: props.sessionId,
timestamp: props.timestamp ?? new Date(),
});
...(props.visitorId !== undefined ? { visitorId: props.visitorId } : {}),
...(props.referrer !== undefined ? { referrer: props.referrer } : {}),
...(props.userAgent !== undefined ? { userAgent: props.userAgent } : {}),
...(props.country !== undefined ? { country: props.country } : {}),
...(props.durationMs !== undefined ? { durationMs: props.durationMs } : {}),
};
return new PageView(baseProps);
}
private static validate(props: Omit<PageViewProps, 'timestamp'>): void {
@@ -88,8 +99,17 @@ export class PageView implements IEntity<string> {
throw new Error('Duration must be non-negative');
}
return new PageView({
...this,
return PageView.create({
id: this.id,
entityType: this.entityType,
entityId: this.entityId,
visitorType: this.visitorType,
sessionId: this.sessionId,
timestamp: this.timestamp,
...(this.visitorId !== undefined ? { visitorId: this.visitorId } : {}),
...(this.referrer !== undefined ? { referrer: this.referrer } : {}),
...(this.userAgent !== undefined ? { userAgent: this.userAgent } : {}),
...(this.country !== undefined ? { country: this.country } : {}),
durationMs,
});
}