module cleanup
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
|
||||
|
||||
export interface GetAnalyticsMetricsInput {
|
||||
startDate?: Date;
|
||||
endDate?: Date;
|
||||
}
|
||||
|
||||
export interface GetAnalyticsMetricsOutput {
|
||||
pageViews: number;
|
||||
uniqueVisitors: number;
|
||||
averageSessionDuration: number;
|
||||
bounceRate: number;
|
||||
}
|
||||
|
||||
export class GetAnalyticsMetricsUseCase {
|
||||
constructor(
|
||||
private readonly pageViewRepository: IPageViewRepository,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(input: GetAnalyticsMetricsInput = {}): Promise<GetAnalyticsMetricsOutput> {
|
||||
try {
|
||||
const startDate = input.startDate ?? new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); // 30 days ago
|
||||
const endDate = input.endDate ?? new Date();
|
||||
|
||||
// For now, return placeholder values as actual implementation would require
|
||||
// aggregating data across all entities or specifying which entity
|
||||
// This is a simplified version
|
||||
const pageViews = 0;
|
||||
const uniqueVisitors = 0;
|
||||
const averageSessionDuration = 0;
|
||||
const bounceRate = 0;
|
||||
|
||||
this.logger.info('Analytics metrics retrieved', {
|
||||
startDate,
|
||||
endDate,
|
||||
pageViews,
|
||||
uniqueVisitors,
|
||||
});
|
||||
|
||||
return {
|
||||
pageViews,
|
||||
uniqueVisitors,
|
||||
averageSessionDuration,
|
||||
bounceRate,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to get analytics metrics', { error, input });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
export interface GetDashboardDataInput {}
|
||||
|
||||
export interface GetDashboardDataOutput {
|
||||
totalUsers: number;
|
||||
activeUsers: number;
|
||||
totalRaces: number;
|
||||
totalLeagues: number;
|
||||
}
|
||||
|
||||
export class GetDashboardDataUseCase {
|
||||
constructor(
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(_input: GetDashboardDataInput = {}): Promise<GetDashboardDataOutput> {
|
||||
try {
|
||||
// Placeholder implementation - would need repositories from identity and racing domains
|
||||
const totalUsers = 0;
|
||||
const activeUsers = 0;
|
||||
const totalRaces = 0;
|
||||
const totalLeagues = 0;
|
||||
|
||||
this.logger.info('Dashboard data retrieved', {
|
||||
totalUsers,
|
||||
activeUsers,
|
||||
totalRaces,
|
||||
totalLeagues,
|
||||
});
|
||||
|
||||
return {
|
||||
totalUsers,
|
||||
activeUsers,
|
||||
totalRaces,
|
||||
totalLeagues,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to get dashboard data', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,7 @@
|
||||
/**
|
||||
* Use Case: RecordEngagementUseCase
|
||||
*
|
||||
* Records an engagement event when a visitor interacts with an entity.
|
||||
*/
|
||||
|
||||
import type { AsyncUseCase } from '@core/shared/application';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { EngagementEvent, type EngagementAction, type EngagementEntityType } from '../../domain/entities/EngagementEvent';
|
||||
import type { IEngagementRepository } from '../../domain/repositories/IEngagementRepository';
|
||||
import { EngagementEvent } from '../../domain/entities/EngagementEvent';
|
||||
import type { EngagementAction, EngagementEntityType } from '../../domain/types/EngagementEvent';
|
||||
|
||||
export interface RecordEngagementInput {
|
||||
action: EngagementAction;
|
||||
@@ -24,43 +18,41 @@ export interface RecordEngagementOutput {
|
||||
engagementWeight: number;
|
||||
}
|
||||
|
||||
export class RecordEngagementUseCase
|
||||
implements AsyncUseCase<RecordEngagementInput, RecordEngagementOutput> {
|
||||
export class RecordEngagementUseCase {
|
||||
constructor(
|
||||
private readonly engagementRepository: IEngagementRepository,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(input: RecordEngagementInput): Promise<RecordEngagementOutput> {
|
||||
this.logger.debug('Executing RecordEngagementUseCase', { input });
|
||||
try {
|
||||
const eventId = `eng-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
|
||||
const baseProps: Omit<Parameters<typeof EngagementEvent.create>[0], 'timestamp'> = {
|
||||
id: eventId,
|
||||
const engagementEvent = EngagementEvent.create({
|
||||
id: crypto.randomUUID(),
|
||||
action: input.action,
|
||||
entityType: input.entityType,
|
||||
entityId: input.entityId,
|
||||
actorId: input.actorId,
|
||||
actorType: input.actorType,
|
||||
sessionId: input.sessionId,
|
||||
};
|
||||
|
||||
const event = EngagementEvent.create({
|
||||
...baseProps,
|
||||
...(input.actorId !== undefined ? { actorId: input.actorId } : {}),
|
||||
...(input.metadata !== undefined ? { metadata: input.metadata } : {}),
|
||||
metadata: input.metadata,
|
||||
});
|
||||
|
||||
await this.engagementRepository.save(event);
|
||||
this.logger.info('Engagement recorded successfully', { eventId, input });
|
||||
await this.engagementRepository.save(engagementEvent);
|
||||
|
||||
this.logger.info('Engagement event recorded', {
|
||||
engagementId: engagementEvent.id,
|
||||
action: input.action,
|
||||
entityId: input.entityId,
|
||||
entityType: input.entityType,
|
||||
});
|
||||
|
||||
return {
|
||||
eventId,
|
||||
engagementWeight: event.getEngagementWeight(),
|
||||
eventId: engagementEvent.id,
|
||||
engagementWeight: engagementEvent.getEngagementWeight(),
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error('Error recording engagement', error instanceof Error ? error : new Error(String(error)), { input });
|
||||
this.logger.error('Failed to record engagement event', { error: error as Error, input });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,7 @@
|
||||
/**
|
||||
* Use Case: RecordPageViewUseCase
|
||||
*
|
||||
* Records a page view event when a visitor accesses an entity page.
|
||||
*/
|
||||
|
||||
import type { AsyncUseCase } from '@core/shared/application';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
|
||||
import { PageView } from '../../domain/entities/PageView';
|
||||
import type { EntityType, VisitorType } from '../../domain/types/PageView';
|
||||
import type { IPageViewRepository } from '../repositories/IPageViewRepository';
|
||||
|
||||
export interface RecordPageViewInput {
|
||||
entityType: EntityType;
|
||||
@@ -25,41 +18,40 @@ export interface RecordPageViewOutput {
|
||||
pageViewId: string;
|
||||
}
|
||||
|
||||
export class RecordPageViewUseCase
|
||||
implements AsyncUseCase<RecordPageViewInput, RecordPageViewOutput> {
|
||||
export class RecordPageViewUseCase {
|
||||
constructor(
|
||||
private readonly pageViewRepository: IPageViewRepository,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(input: RecordPageViewInput): Promise<RecordPageViewOutput> {
|
||||
this.logger.debug('Executing RecordPageViewUseCase', { input });
|
||||
try {
|
||||
const pageViewId = `pv-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
|
||||
const baseProps: Omit<Parameters<typeof PageView.create>[0], 'timestamp'> = {
|
||||
id: pageViewId,
|
||||
const pageView = PageView.create({
|
||||
id: crypto.randomUUID(),
|
||||
entityType: input.entityType,
|
||||
entityId: input.entityId,
|
||||
visitorId: input.visitorId,
|
||||
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 } : {}),
|
||||
referrer: input.referrer,
|
||||
userAgent: input.userAgent,
|
||||
country: input.country,
|
||||
});
|
||||
|
||||
await this.pageViewRepository.save(pageView);
|
||||
this.logger.info('Page view recorded successfully', { pageViewId, input });
|
||||
return { pageViewId };
|
||||
|
||||
this.logger.info('Page view recorded', {
|
||||
pageViewId: pageView.id,
|
||||
entityId: input.entityId,
|
||||
entityType: input.entityType,
|
||||
});
|
||||
|
||||
return {
|
||||
pageViewId: pageView.id,
|
||||
};
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
this.logger.error('Error recording page view', err, { input });
|
||||
this.logger.error('Failed to record page view', { error, input });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
core/analytics/domain/repositories/IPageViewRepository.ts
Normal file
12
core/analytics/domain/repositories/IPageViewRepository.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { PageView } from '../entities/PageView';
|
||||
|
||||
export interface IPageViewRepository {
|
||||
save(pageView: PageView): Promise<void>;
|
||||
findById(id: string): Promise<PageView | null>;
|
||||
findByEntityId(entityId: string): Promise<PageView[]>;
|
||||
findBySessionId(sessionId: string): Promise<PageView[]>;
|
||||
countByEntityId(entityId: string): Promise<number>;
|
||||
getUniqueVisitorsCount(entityId: string, startDate: Date, endDate: Date): Promise<number>;
|
||||
getAverageSessionDuration(entityId: string, startDate: Date, endDate: Date): Promise<number>;
|
||||
getBounceRate(entityId: string, startDate: Date, endDate: Date): Promise<number>;
|
||||
}
|
||||
@@ -5,24 +5,30 @@
|
||||
* Kept in domain/types so domain/entities contains only entity classes.
|
||||
*/
|
||||
|
||||
export type EngagementAction =
|
||||
| 'click_sponsor_logo'
|
||||
| 'click_sponsor_url'
|
||||
| 'download_livery_pack'
|
||||
| 'join_league'
|
||||
| 'register_race'
|
||||
| 'view_standings'
|
||||
| 'view_schedule'
|
||||
| 'share_social'
|
||||
| 'contact_sponsor';
|
||||
export const EngagementAction = {
|
||||
CLICK_SPONSOR_LOGO: 'click_sponsor_logo',
|
||||
CLICK_SPONSOR_URL: 'click_sponsor_url',
|
||||
DOWNLOAD_LIVERY_PACK: 'download_livery_pack',
|
||||
JOIN_LEAGUE: 'join_league',
|
||||
REGISTER_RACE: 'register_race',
|
||||
VIEW_STANDINGS: 'view_standings',
|
||||
VIEW_SCHEDULE: 'view_schedule',
|
||||
SHARE_SOCIAL: 'share_social',
|
||||
CONTACT_SPONSOR: 'contact_sponsor',
|
||||
} as const;
|
||||
|
||||
export type EngagementEntityType =
|
||||
| 'league'
|
||||
| 'driver'
|
||||
| 'team'
|
||||
| 'race'
|
||||
| 'sponsor'
|
||||
| 'sponsorship';
|
||||
export type EngagementAction = typeof EngagementAction[keyof typeof EngagementAction];
|
||||
|
||||
export const EngagementEntityType = {
|
||||
LEAGUE: 'league',
|
||||
DRIVER: 'driver',
|
||||
TEAM: 'team',
|
||||
RACE: 'race',
|
||||
SPONSOR: 'sponsor',
|
||||
SPONSORSHIP: 'sponsorship',
|
||||
} as const;
|
||||
|
||||
export type EngagementEntityType = typeof EngagementEntityType[keyof typeof EngagementEntityType];
|
||||
|
||||
export interface EngagementEventProps {
|
||||
id: string;
|
||||
|
||||
@@ -5,19 +5,23 @@
|
||||
* Kept in domain/types so domain/entities contains only entity classes.
|
||||
*/
|
||||
|
||||
export enum EntityType {
|
||||
LEAGUE = 'league',
|
||||
DRIVER = 'driver',
|
||||
TEAM = 'team',
|
||||
RACE = 'race',
|
||||
SPONSOR = 'sponsor',
|
||||
}
|
||||
export const EntityType = {
|
||||
LEAGUE: 'league',
|
||||
DRIVER: 'driver',
|
||||
TEAM: 'team',
|
||||
RACE: 'race',
|
||||
SPONSOR: 'sponsor',
|
||||
} as const;
|
||||
|
||||
export enum VisitorType {
|
||||
ANONYMOUS = 'anonymous',
|
||||
DRIVER = 'driver',
|
||||
SPONSOR = 'sponsor',
|
||||
}
|
||||
export type EntityType = typeof EntityType[keyof typeof EntityType];
|
||||
|
||||
export const VisitorType = {
|
||||
ANONYMOUS: 'anonymous',
|
||||
DRIVER: 'driver',
|
||||
SPONSOR: 'sponsor',
|
||||
} as const;
|
||||
|
||||
export type VisitorType = typeof VisitorType[keyof typeof VisitorType];
|
||||
|
||||
export interface PageViewProps {
|
||||
id: string;
|
||||
|
||||
Reference in New Issue
Block a user