This commit is contained in:
2025-12-21 19:53:22 +01:00
parent f2d8a23583
commit 3c64f328e2
105 changed files with 3191 additions and 1706 deletions

View File

@@ -1,5 +1,5 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetAnalyticsMetricsUseCase, type GetAnalyticsMetricsInput } from './GetAnalyticsMetricsUseCase';
import { GetAnalyticsMetricsUseCase, type GetAnalyticsMetricsInput, type GetAnalyticsMetricsOutput } from './GetAnalyticsMetricsUseCase';
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
@@ -16,7 +16,7 @@ describe('GetAnalyticsMetricsUseCase', () => {
getBounceRate: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<Result<any, any>> & { present: Mock };
let output: UseCaseOutputPort<GetAnalyticsMetricsOutput> & { present: Mock };
let useCase: GetAnalyticsMetricsUseCase;
beforeEach(() => {
@@ -44,14 +44,21 @@ describe('GetAnalyticsMetricsUseCase', () => {
useCase = new GetAnalyticsMetricsUseCase(
pageViewRepository as unknown as IPageViewRepository,
output,
logger,
output,
);
});
it('presents default metrics and logs retrieval when no input is provided', async () => {
await useCase.execute();
const result = await useCase.execute();
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledWith({
pageViews: 0,
uniqueVisitors: 0,
averageSessionDuration: 0,
bounceRate: 0,
});
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
@@ -66,8 +73,9 @@ describe('GetAnalyticsMetricsUseCase', () => {
throw new Error('Logging failed');
});
await useCase.execute(input);
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
});
});

View File

@@ -1,7 +1,7 @@
import type { Logger, UseCaseOutputPort, UseCase } from '@core/shared/application';
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
import type { Logger, UseCase, UseCaseOutputPort } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
export interface GetAnalyticsMetricsInput {
startDate?: Date;
@@ -17,25 +17,33 @@ export interface GetAnalyticsMetricsOutput {
export type GetAnalyticsMetricsErrorCode = 'REPOSITORY_ERROR';
export class GetAnalyticsMetricsUseCase implements UseCase<GetAnalyticsMetricsInput, GetAnalyticsMetricsOutput, GetAnalyticsMetricsErrorCode> {
export class GetAnalyticsMetricsUseCase implements UseCase<GetAnalyticsMetricsInput, void, GetAnalyticsMetricsErrorCode> {
constructor(
private readonly pageViewRepository: IPageViewRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<GetAnalyticsMetricsOutput>,
) {}
async execute(input: GetAnalyticsMetricsInput = {}): Promise<Result<GetAnalyticsMetricsOutput, ApplicationErrorCode<GetAnalyticsMetricsErrorCode, { message: string }>>> {
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();
// For now, return placeholder values as actual implementation would require
// aggregating data across all entities or specifying which entity
// This is a simplified version
// TODO static data
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,
@@ -43,21 +51,14 @@ export class GetAnalyticsMetricsUseCase implements UseCase<GetAnalyticsMetricsIn
uniqueVisitors,
});
const result = Result.ok<GetAnalyticsMetricsOutput, ApplicationErrorCode<GetAnalyticsMetricsErrorCode, { message: string }>>({
pageViews,
uniqueVisitors,
averageSessionDuration,
bounceRate,
});
return result;
return Result.ok(undefined);
} catch (error) {
const err = error as Error;
this.logger.error('Failed to get analytics metrics', err, { input });
const result = Result.err<GetAnalyticsMetricsOutput, ApplicationErrorCode<GetAnalyticsMetricsErrorCode, { message: string }>>({
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message ?? 'Failed to get analytics metrics' },
});
return result;
}
}
}

View File

@@ -1,11 +1,11 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetDashboardDataUseCase } from './GetDashboardDataUseCase';
import { GetDashboardDataUseCase, type GetDashboardDataOutput } from './GetDashboardDataUseCase';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
describe('GetDashboardDataUseCase', () => {
let logger: Logger;
let output: UseCaseOutputPort<Result<any, any>> & { present: Mock };
let output: UseCaseOutputPort<GetDashboardDataOutput> & { present: Mock };
let useCase: GetDashboardDataUseCase;
beforeEach(() => {
@@ -20,18 +20,19 @@ describe('GetDashboardDataUseCase', () => {
present: vi.fn(),
};
useCase = new GetDashboardDataUseCase(output, logger);
useCase = new GetDashboardDataUseCase(logger, output);
});
it('presents placeholder dashboard metrics and logs retrieval', async () => {
await useCase.execute();
const result = await useCase.execute();
expect(output.present).toHaveBeenCalledWith(Result.ok({
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledWith({
totalUsers: 0,
activeUsers: 0,
totalRaces: 0,
totalLeagues: 0,
}));
});
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});

View File

@@ -13,12 +13,13 @@ export interface GetDashboardDataOutput {
export type GetDashboardDataErrorCode = 'REPOSITORY_ERROR';
export class GetDashboardDataUseCase implements UseCase<GetDashboardDataInput, GetDashboardDataOutput, GetDashboardDataErrorCode> {
export class GetDashboardDataUseCase implements UseCase<GetDashboardDataInput, void, GetDashboardDataErrorCode> {
constructor(
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<GetDashboardDataOutput>,
) {}
async execute(input: GetDashboardDataInput = {}): Promise<Result<GetDashboardDataOutput, ApplicationErrorCode<GetDashboardDataErrorCode, { message: string }>>> {
async execute(input: GetDashboardDataInput = {}): Promise<Result<void, ApplicationErrorCode<GetDashboardDataErrorCode, { message: string }>>> {
try {
// Placeholder implementation - would need repositories from identity and racing domains
const totalUsers = 0;
@@ -26,6 +27,15 @@ export class GetDashboardDataUseCase implements UseCase<GetDashboardDataInput, G
const totalRaces = 0;
const totalLeagues = 0;
const resultModel: GetDashboardDataOutput = {
totalUsers,
activeUsers,
totalRaces,
totalLeagues,
};
this.output.present(resultModel);
this.logger.info('Dashboard data retrieved', {
totalUsers,
activeUsers,
@@ -33,21 +43,14 @@ export class GetDashboardDataUseCase implements UseCase<GetDashboardDataInput, G
totalLeagues,
});
const result = Result.ok<GetDashboardDataOutput, ApplicationErrorCode<GetDashboardDataErrorCode, { message: string }>>({
totalUsers,
activeUsers,
totalRaces,
totalLeagues,
});
return result;
return Result.ok(undefined);
} catch (error) {
const err = error as Error;
this.logger.error('Failed to get dashboard data', err);
const result = Result.err<GetDashboardDataOutput, ApplicationErrorCode<GetDashboardDataErrorCode, { message: string }>>({
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message ?? 'Failed to get dashboard data' },
});
return result;
}
}
}

View File

@@ -66,20 +66,22 @@ describe('GetEntityAnalyticsQuery', () => {
const result = await useCase.execute(input);
expect(result.entityId).toBe(input.entityId);
expect(result.entityType).toBe(input.entityType);
expect(result.isOk()).toBe(true);
const data = result.unwrap();
expect(data.entityId).toBe(input.entityId);
expect(data.entityType).toBe(input.entityType);
expect(result.summary.totalPageViews).toBe(100);
expect(result.summary.uniqueVisitors).toBe(40);
expect(result.summary.sponsorClicks).toBe(10);
expect(typeof result.summary.engagementScore).toBe('number');
expect(result.summary.exposureValue).toBeGreaterThan(0);
expect(data.summary.totalPageViews).toBe(100);
expect(data.summary.uniqueVisitors).toBe(40);
expect(data.summary.sponsorClicks).toBe(10);
expect(typeof data.summary.engagementScore).toBe('number');
expect(data.summary.exposureValue).toBeGreaterThan(0);
expect(result.trends.pageViewsChange).toBeDefined();
expect(result.trends.uniqueVisitorsChange).toBeDefined();
expect(data.trends.pageViewsChange).toBeDefined();
expect(data.trends.uniqueVisitorsChange).toBeDefined();
expect(result.period.start).toBeInstanceOf(Date);
expect(result.period.end).toBeInstanceOf(Date);
expect(data.period.start).toBeInstanceOf(Date);
expect(data.period.end).toBeInstanceOf(Date);
});
it('propagates repository errors', async () => {
@@ -90,7 +92,9 @@ describe('GetEntityAnalyticsQuery', () => {
pageViewRepository.countByEntityId.mockRejectedValue(new Error('DB error'));
await expect(useCase.execute(input)).rejects.toThrow('DB error');
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
});
});

View File

@@ -52,7 +52,6 @@ export class GetEntityAnalyticsQuery
private readonly pageViewRepository: IPageViewRepository,
private readonly engagementRepository: IEngagementRepository,
private readonly snapshotRepository: IAnalyticsSnapshotRepository,
private readonly output: UseCaseOutputPort<Result<EntityAnalyticsOutput, ApplicationErrorCode<GetEntityAnalyticsErrorCode, { message: string }>>>,
private readonly logger: Logger
) {}
@@ -145,10 +144,8 @@ export class GetEntityAnalyticsQuery
label: this.formatPeriodLabel(since, now),
},
};
const result = Result.ok<EntityAnalyticsOutput, ApplicationErrorCode<GetEntityAnalyticsErrorCode, { message: string }>>(resultData);
this.output.present(result);
this.logger.info(`Successfully retrieved analytics for entity ${input.entityId}.`);
return result;
return Result.ok(resultData);
} catch (error) {
const err = error as Error;
this.logger.error(`Failed to get entity analytics for ${input.entityId}: ${err.message}`, err);
@@ -156,7 +153,6 @@ export class GetEntityAnalyticsQuery
code: 'REPOSITORY_ERROR',
details: { message: err.message ?? 'Failed to get entity analytics' },
});
this.output.present(result);
return result;
}
}

View File

@@ -1,5 +1,5 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { RecordEngagementUseCase, type RecordEngagementInput } from './RecordEngagementUseCase';
import { RecordEngagementUseCase, type RecordEngagementInput, type RecordEngagementOutput } from './RecordEngagementUseCase';
import type { IEngagementRepository } from '../../domain/repositories/IEngagementRepository';
import { EngagementEvent } from '../../domain/entities/EngagementEvent';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
@@ -11,7 +11,7 @@ describe('RecordEngagementUseCase', () => {
save: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<Result<any, any>> & { present: Mock };
let output: UseCaseOutputPort<RecordEngagementOutput> & { present: Mock };
let useCase: RecordEngagementUseCase;
beforeEach(() => {
@@ -32,8 +32,8 @@ describe('RecordEngagementUseCase', () => {
useCase = new RecordEngagementUseCase(
engagementRepository as unknown as IEngagementRepository,
output,
logger,
output,
);
});
@@ -50,8 +50,9 @@ describe('RecordEngagementUseCase', () => {
engagementRepository.save.mockResolvedValue(undefined);
await useCase.execute(input);
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(engagementRepository.save).toHaveBeenCalledTimes(1);
const saved = (engagementRepository.save as unknown as Mock).mock.calls[0][0] as EngagementEvent;
@@ -60,6 +61,10 @@ describe('RecordEngagementUseCase', () => {
expect(saved.entityId).toBe(input.entityId);
expect(saved.entityType).toBe(input.entityType);
expect(output.present).toHaveBeenCalledWith({
eventId: saved.id,
engagementWeight: saved.getEngagementWeight(),
});
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
@@ -75,8 +80,9 @@ describe('RecordEngagementUseCase', () => {
const error = new Error('DB error');
engagementRepository.save.mockRejectedValue(error);
await useCase.execute(input);
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
});
});

View File

@@ -22,13 +22,14 @@ export interface RecordEngagementOutput {
export type RecordEngagementErrorCode = 'REPOSITORY_ERROR';
export class RecordEngagementUseCase implements UseCase<RecordEngagementInput, RecordEngagementOutput, RecordEngagementErrorCode> {
export class RecordEngagementUseCase implements UseCase<RecordEngagementInput, void, RecordEngagementErrorCode> {
constructor(
private readonly engagementRepository: IEngagementRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<RecordEngagementOutput>,
) {}
async execute(input: RecordEngagementInput): Promise<Result<RecordEngagementOutput, ApplicationErrorCode<RecordEngagementErrorCode, { message: string }>>> {
async execute(input: RecordEngagementInput): Promise<Result<void, ApplicationErrorCode<RecordEngagementErrorCode, { message: string }>>> {
try {
const engagementEvent = EngagementEvent.create({
id: crypto.randomUUID(),
@@ -43,6 +44,13 @@ export class RecordEngagementUseCase implements UseCase<RecordEngagementInput, R
await this.engagementRepository.save(engagementEvent);
const resultModel: RecordEngagementOutput = {
eventId: engagementEvent.id,
engagementWeight: engagementEvent.getEngagementWeight(),
};
this.output.present(resultModel);
this.logger.info('Engagement event recorded', {
engagementId: engagementEvent.id,
action: input.action,
@@ -50,19 +58,14 @@ export class RecordEngagementUseCase implements UseCase<RecordEngagementInput, R
entityType: input.entityType,
});
const result = Result.ok<RecordEngagementOutput, ApplicationErrorCode<RecordEngagementErrorCode, { message: string }>>({
eventId: engagementEvent.id,
engagementWeight: engagementEvent.getEngagementWeight(),
});
return result;
return Result.ok(undefined);
} catch (error) {
const err = error as Error;
this.logger.error('Failed to record engagement event', err, { input });
const result = Result.err<RecordEngagementOutput, ApplicationErrorCode<RecordEngagementErrorCode, { message: string }>>({
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message ?? 'Failed to record engagement event' },
});
return result;
}
}
}

View File

@@ -1,5 +1,5 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { RecordPageViewUseCase, type RecordPageViewInput } from './RecordPageViewUseCase';
import { RecordPageViewUseCase, type RecordPageViewInput, type RecordPageViewOutput } from './RecordPageViewUseCase';
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
import { PageView } from '../../domain/entities/PageView';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
@@ -11,7 +11,7 @@ describe('RecordPageViewUseCase', () => {
save: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<Result<any, any>> & { present: Mock };
let output: UseCaseOutputPort<RecordPageViewOutput> & { present: Mock };
let useCase: RecordPageViewUseCase;
beforeEach(() => {
@@ -32,8 +32,8 @@ describe('RecordPageViewUseCase', () => {
useCase = new RecordPageViewUseCase(
pageViewRepository as unknown as IPageViewRepository,
output,
logger,
output,
);
});
@@ -51,8 +51,9 @@ describe('RecordPageViewUseCase', () => {
pageViewRepository.save.mockResolvedValue(undefined);
await useCase.execute(input);
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(pageViewRepository.save).toHaveBeenCalledTimes(1);
const saved = (pageViewRepository.save as unknown as Mock).mock.calls[0][0] as PageView;
@@ -61,6 +62,9 @@ describe('RecordPageViewUseCase', () => {
expect(saved.entityId).toBe(input.entityId);
expect(saved.entityType).toBe(input.entityType);
expect(output.present).toHaveBeenCalledWith({
pageViewId: saved.id,
});
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
@@ -75,8 +79,9 @@ describe('RecordPageViewUseCase', () => {
const error = new Error('DB error');
pageViewRepository.save.mockRejectedValue(error);
await useCase.execute(input);
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
});
});

View File

@@ -22,13 +22,14 @@ export interface RecordPageViewOutput {
export type RecordPageViewErrorCode = 'REPOSITORY_ERROR';
export class RecordPageViewUseCase implements UseCase<RecordPageViewInput, RecordPageViewOutput, RecordPageViewErrorCode> {
export class RecordPageViewUseCase implements UseCase<RecordPageViewInput, void, RecordPageViewErrorCode> {
constructor(
private readonly pageViewRepository: IPageViewRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<RecordPageViewOutput>,
) {}
async execute(input: RecordPageViewInput): Promise<Result<RecordPageViewOutput, ApplicationErrorCode<RecordPageViewErrorCode, { message: string }>>> {
async execute(input: RecordPageViewInput): Promise<Result<void, ApplicationErrorCode<RecordPageViewErrorCode, { message: string }>>> {
try {
const pageView = PageView.create({
id: crypto.randomUUID(),
@@ -44,24 +45,26 @@ export class RecordPageViewUseCase implements UseCase<RecordPageViewInput, Recor
await this.pageViewRepository.save(pageView);
const resultModel: RecordPageViewOutput = {
pageViewId: pageView.id,
};
this.output.present(resultModel);
this.logger.info('Page view recorded', {
pageViewId: pageView.id,
entityId: input.entityId,
entityType: input.entityType,
});
const result = Result.ok<RecordPageViewOutput, ApplicationErrorCode<RecordPageViewErrorCode, { message: string }>>({
pageViewId: pageView.id,
});
return result;
return Result.ok(undefined);
} catch (error) {
const err = error as Error;
this.logger.error('Failed to record page view', err, { input });
const result = Result.err<RecordPageViewOutput, ApplicationErrorCode<RecordPageViewErrorCode, { message: string }>>({
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message ?? 'Failed to record page view' },
});
return result;
}
}
}