64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
import type { Logger, UseCase, UseCaseOutputPort } from '@core/shared/application';
|
|
import { Result } from '@core/shared/application/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
|
|
export interface GetAnalyticsMetricsInput {
|
|
startDate?: Date;
|
|
endDate?: Date;
|
|
}
|
|
|
|
export interface GetAnalyticsMetricsOutput {
|
|
pageViews: number;
|
|
uniqueVisitors: number;
|
|
averageSessionDuration: number;
|
|
bounceRate: number;
|
|
}
|
|
|
|
export type GetAnalyticsMetricsErrorCode = 'REPOSITORY_ERROR';
|
|
|
|
export class GetAnalyticsMetricsUseCase implements UseCase<GetAnalyticsMetricsInput, void, GetAnalyticsMetricsErrorCode> {
|
|
constructor(
|
|
// private readonly pageViewRepository: IPageViewRepository, // TODO: Use when implementation is ready
|
|
private readonly logger: Logger,
|
|
private readonly output: UseCaseOutputPort<GetAnalyticsMetricsOutput>,
|
|
) {}
|
|
|
|
async execute(input: GetAnalyticsMetricsInput = {}): Promise<Result<void, ApplicationErrorCode<GetAnalyticsMetricsErrorCode, { message: string }>>> {
|
|
try {
|
|
const startDate = input.startDate ?? new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); // 30 days ago
|
|
const endDate = input.endDate ?? new Date();
|
|
|
|
// TODO: Use pageViewRepository when implemented
|
|
// const pageViews = await this.pageViewRepository.countByDateRange(startDate, endDate);
|
|
const pageViews = 0;
|
|
const uniqueVisitors = 0;
|
|
const averageSessionDuration = 0;
|
|
const bounceRate = 0;
|
|
|
|
const resultModel: GetAnalyticsMetricsOutput = {
|
|
pageViews,
|
|
uniqueVisitors,
|
|
averageSessionDuration,
|
|
bounceRate,
|
|
};
|
|
|
|
this.output.present(resultModel);
|
|
|
|
this.logger.info('Analytics metrics retrieved', {
|
|
startDate,
|
|
endDate,
|
|
pageViews,
|
|
uniqueVisitors,
|
|
});
|
|
|
|
return Result.ok(undefined);
|
|
} catch (error) {
|
|
const err = error as Error;
|
|
this.logger.error('Failed to get analytics metrics', err, { input });
|
|
return Result.err({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: { message: err.message ?? 'Failed to get analytics metrics' },
|
|
});
|
|
}
|
|
}
|
|
} |