react-query
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export interface GetAnalyticsMetricsInput {
|
||||
startDate?: Date;
|
||||
@@ -13,13 +15,17 @@ export interface GetAnalyticsMetricsOutput {
|
||||
bounceRate: number;
|
||||
}
|
||||
|
||||
export type GetAnalyticsMetricsErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetAnalyticsMetricsUseCase {
|
||||
constructor(
|
||||
private readonly pageViewRepository: IPageViewRepository,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(input: GetAnalyticsMetricsInput = {}): Promise<GetAnalyticsMetricsOutput> {
|
||||
async execute(input: GetAnalyticsMetricsInput = {}): Promise<
|
||||
Result<GetAnalyticsMetricsOutput, 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();
|
||||
@@ -39,15 +45,19 @@ export class GetAnalyticsMetricsUseCase {
|
||||
uniqueVisitors,
|
||||
});
|
||||
|
||||
return {
|
||||
return Result.ok({
|
||||
pageViews,
|
||||
uniqueVisitors,
|
||||
averageSessionDuration,
|
||||
bounceRate,
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
const err = error as Error;
|
||||
this.logger.error('Failed to get analytics metrics', { error, input });
|
||||
throw error;
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message ?? 'Failed to get analytics metrics' },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user