refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -1,10 +1,9 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetAnalyticsMetricsUseCase, type GetAnalyticsMetricsInput, type GetAnalyticsMetricsOutput } from './GetAnalyticsMetricsUseCase';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { GetAnalyticsMetricsUseCase, type GetAnalyticsMetricsInput } from './GetAnalyticsMetricsUseCase';
import type { Logger } from '@core/shared/application';
describe('GetAnalyticsMetricsUseCase', () => {
let logger: Logger;
let output: UseCaseOutputPort<GetAnalyticsMetricsOutput> & { present: Mock };
let useCase: GetAnalyticsMetricsUseCase;
beforeEach(() => {
@@ -15,21 +14,15 @@ describe('GetAnalyticsMetricsUseCase', () => {
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new GetAnalyticsMetricsUseCase(
logger,
output,
);
useCase = new GetAnalyticsMetricsUseCase(logger);
});
it('presents default metrics and logs retrieval when no input is provided', async () => {
it('returns default metrics when no input is provided', async () => {
const result = await useCase.execute();
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledWith({
const data = result.unwrap();
expect(data).toEqual({
pageViews: 0,
uniqueVisitors: 0,
averageSessionDuration: 0,
@@ -38,7 +31,21 @@ describe('GetAnalyticsMetricsUseCase', () => {
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
it('uses provided date range and presents error when execute throws', async () => {
it('uses provided date range and returns metrics', async () => {
const input: GetAnalyticsMetricsInput = {
startDate: new Date('2024-01-01'),
endDate: new Date('2024-01-31'),
};
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
const data = result.unwrap();
expect(data.pageViews).toBe(0);
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
it('returns error when execute throws', async () => {
const input: GetAnalyticsMetricsInput = {
startDate: new Date('2024-01-01'),
endDate: new Date('2024-01-31'),

View File

@@ -1,4 +1,4 @@
import type { Logger, UseCase, UseCaseOutputPort } from '@core/shared/application';
import type { Logger, UseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { IPageViewRepository } from '../repositories/IPageViewRepository';
@@ -17,16 +17,15 @@ export interface GetAnalyticsMetricsOutput {
export type GetAnalyticsMetricsErrorCode = 'REPOSITORY_ERROR';
export class GetAnalyticsMetricsUseCase implements UseCase<GetAnalyticsMetricsInput, void, GetAnalyticsMetricsErrorCode> {
export class GetAnalyticsMetricsUseCase implements UseCase<GetAnalyticsMetricsInput, GetAnalyticsMetricsOutput, GetAnalyticsMetricsErrorCode> {
constructor(
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<GetAnalyticsMetricsOutput>,
private readonly pageViewRepository?: IPageViewRepository,
) {}
async execute(
input: GetAnalyticsMetricsInput = {},
): Promise<Result<void, ApplicationErrorCode<GetAnalyticsMetricsErrorCode, { message: string }>>> {
): 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();
@@ -47,8 +46,6 @@ export class GetAnalyticsMetricsUseCase implements UseCase<GetAnalyticsMetricsIn
bounceRate,
};
this.output.present(resultModel);
this.logger.info('Analytics metrics retrieved', {
startDate,
endDate,
@@ -56,7 +53,7 @@ export class GetAnalyticsMetricsUseCase implements UseCase<GetAnalyticsMetricsIn
uniqueVisitors,
});
return Result.ok(undefined);
return Result.ok(resultModel);
} catch (error) {
const err = error as Error;
this.logger.error('Failed to get analytics metrics', err, { input });

View File

@@ -1,10 +1,9 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetDashboardDataUseCase, type GetDashboardDataOutput } from './GetDashboardDataUseCase';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { GetDashboardDataUseCase } from './GetDashboardDataUseCase';
import type { Logger } from '@core/shared/application';
describe('GetDashboardDataUseCase', () => {
let logger: Logger;
let output: UseCaseOutputPort<GetDashboardDataOutput> & { present: Mock };
let useCase: GetDashboardDataUseCase;
beforeEach(() => {
@@ -15,18 +14,15 @@ describe('GetDashboardDataUseCase', () => {
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new GetDashboardDataUseCase(logger, output);
useCase = new GetDashboardDataUseCase(logger);
});
it('presents placeholder dashboard metrics and logs retrieval', async () => {
it('returns placeholder dashboard metrics and logs retrieval', async () => {
const result = await useCase.execute();
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledWith({
const data = result.unwrap();
expect(data).toEqual({
totalUsers: 0,
activeUsers: 0,
totalRaces: 0,

View File

@@ -1,4 +1,4 @@
import type { Logger, UseCaseOutputPort, UseCase } from '@core/shared/application';
import type { Logger, UseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
@@ -13,13 +13,12 @@ export interface GetDashboardDataOutput {
export type GetDashboardDataErrorCode = 'REPOSITORY_ERROR';
export class GetDashboardDataUseCase implements UseCase<GetDashboardDataInput, void, GetDashboardDataErrorCode> {
export class GetDashboardDataUseCase implements UseCase<GetDashboardDataInput, GetDashboardDataOutput, GetDashboardDataErrorCode> {
constructor(
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<GetDashboardDataOutput>,
) {}
async execute(): Promise<Result<void, ApplicationErrorCode<GetDashboardDataErrorCode, { message: string }>>> {
async execute(): Promise<Result<GetDashboardDataOutput, ApplicationErrorCode<GetDashboardDataErrorCode, { message: string }>>> {
try {
// Placeholder implementation - would need repositories from identity and racing domains
const totalUsers = 0;
@@ -34,8 +33,6 @@ export class GetDashboardDataUseCase implements UseCase<GetDashboardDataInput, v
totalLeagues,
};
this.output.present(resultModel);
this.logger.info('Dashboard data retrieved', {
totalUsers,
activeUsers,
@@ -43,7 +40,7 @@ export class GetDashboardDataUseCase implements UseCase<GetDashboardDataInput, v
totalLeagues,
});
return Result.ok(undefined);
return Result.ok(resultModel);
} catch (error) {
const err = error as Error;
this.logger.error('Failed to get dashboard data', err);

View File

@@ -1,8 +1,8 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { RecordEngagementUseCase, type RecordEngagementInput, type RecordEngagementOutput } from './RecordEngagementUseCase';
import { RecordEngagementUseCase, type RecordEngagementInput } from './RecordEngagementUseCase';
import type { IEngagementRepository } from '../../domain/repositories/IEngagementRepository';
import { EngagementEvent } from '../../domain/entities/EngagementEvent';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import type { EngagementAction, EngagementEntityType } from '../../domain/types/EngagementEvent';
describe('RecordEngagementUseCase', () => {
@@ -10,7 +10,6 @@ describe('RecordEngagementUseCase', () => {
save: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<RecordEngagementOutput> & { present: Mock };
let useCase: RecordEngagementUseCase;
beforeEach(() => {
@@ -25,18 +24,13 @@ describe('RecordEngagementUseCase', () => {
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new RecordEngagementUseCase(
engagementRepository as unknown as IEngagementRepository,
logger,
output,
);
});
it('creates and saves an EngagementEvent and presents its id and weight', async () => {
it('creates and saves an EngagementEvent and returns its id and weight', async () => {
const input: RecordEngagementInput = {
action: 'view' as EngagementAction,
entityType: 'league' as EngagementEntityType,
@@ -52,6 +46,7 @@ describe('RecordEngagementUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
const data = result.unwrap();
expect(engagementRepository.save).toHaveBeenCalledTimes(1);
const saved = (engagementRepository.save as unknown as Mock).mock.calls?.[0]?.[0] as EngagementEvent;
@@ -60,14 +55,12 @@ 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(data.eventId).toBe(saved.id);
expect(data.engagementWeight).toBe(saved.getEngagementWeight());
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
it('logs and presents error when repository save fails', async () => {
it('logs and returns error when repository save fails', async () => {
const input: RecordEngagementInput = {
action: 'view' as EngagementAction,
entityType: 'league' as EngagementEntityType,

View File

@@ -1,4 +1,4 @@
import type { Logger, UseCase, UseCaseOutputPort } from '@core/shared/application';
import type { Logger, UseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { EngagementEvent } from '../../domain/entities/EngagementEvent';
@@ -22,14 +22,13 @@ export interface RecordEngagementOutput {
export type RecordEngagementErrorCode = 'REPOSITORY_ERROR';
export class RecordEngagementUseCase implements UseCase<RecordEngagementInput, void, RecordEngagementErrorCode> {
export class RecordEngagementUseCase implements UseCase<RecordEngagementInput, RecordEngagementOutput, RecordEngagementErrorCode> {
constructor(
private readonly engagementRepository: IEngagementRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<RecordEngagementOutput>,
) {}
async execute(input: RecordEngagementInput): Promise<Result<void, ApplicationErrorCode<RecordEngagementErrorCode, { message: string }>>> {
async execute(input: RecordEngagementInput): Promise<Result<RecordEngagementOutput, ApplicationErrorCode<RecordEngagementErrorCode, { message: string }>>> {
try {
const engagementEvent = EngagementEvent.create({
id: crypto.randomUUID(),
@@ -49,8 +48,6 @@ export class RecordEngagementUseCase implements UseCase<RecordEngagementInput, v
engagementWeight: engagementEvent.getEngagementWeight(),
};
this.output.present(resultModel);
this.logger.info('Engagement event recorded', {
engagementId: engagementEvent.id,
action: input.action,
@@ -58,7 +55,7 @@ export class RecordEngagementUseCase implements UseCase<RecordEngagementInput, v
entityType: input.entityType,
});
return Result.ok(undefined);
return Result.ok(resultModel);
} catch (error) {
const err = error as Error;
this.logger.error('Failed to record engagement event', err, { input });

View File

@@ -1,7 +1,7 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { RecordPageViewUseCase, type RecordPageViewInput, type RecordPageViewOutput } from './RecordPageViewUseCase';
import { RecordPageViewUseCase, type RecordPageViewInput } from './RecordPageViewUseCase';
import { PageView } from '../../domain/entities/PageView';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import type { EntityType, VisitorType } from '../../domain/types/PageView';
describe('RecordPageViewUseCase', () => {
@@ -9,7 +9,6 @@ describe('RecordPageViewUseCase', () => {
save: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<RecordPageViewOutput> & { present: Mock };
let useCase: RecordPageViewUseCase;
beforeEach(() => {
@@ -26,18 +25,13 @@ describe('RecordPageViewUseCase', () => {
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
};
useCase = new RecordPageViewUseCase(
pageViewRepository as unknown as PageViewRepository,
logger,
output,
);
});
it('creates and saves a PageView and presents its id', async () => {
it('creates and saves a PageView and returns its id', async () => {
const input: RecordPageViewInput = {
entityType: 'league' as EntityType,
entityId: 'league-1',
@@ -54,6 +48,7 @@ describe('RecordPageViewUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
const data = result.unwrap();
expect(pageViewRepository.save).toHaveBeenCalledTimes(1);
const saved = (pageViewRepository.save as unknown as Mock).mock.calls?.[0]?.[0] as PageView;
@@ -62,13 +57,11 @@ describe('RecordPageViewUseCase', () => {
expect(saved.entityId).toBe(input.entityId);
expect(saved.entityType).toBe(input.entityType);
expect(output.present).toHaveBeenCalledWith({
pageViewId: saved.id,
});
expect(data.pageViewId).toBe(saved.id);
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
it('logs and presents error when repository save fails', async () => {
it('logs and returns error when repository save fails', async () => {
const input: RecordPageViewInput = {
entityType: 'league' as EntityType,
entityId: 'league-1',

View File

@@ -1,4 +1,4 @@
import type { Logger, UseCaseOutputPort, UseCase } from '@core/shared/application';
import type { Logger, UseCase } from '@core/shared/application';
import type { IPageViewRepository } from '../repositories/IPageViewRepository';
import { PageView } from '../../domain/entities/PageView';
import type { EntityType, VisitorType } from '../../domain/types/PageView';
@@ -22,14 +22,13 @@ export interface RecordPageViewOutput {
export type RecordPageViewErrorCode = 'REPOSITORY_ERROR';
export class RecordPageViewUseCase implements UseCase<RecordPageViewInput, void, RecordPageViewErrorCode> {
export class RecordPageViewUseCase implements UseCase<RecordPageViewInput, RecordPageViewOutput, RecordPageViewErrorCode> {
constructor(
private readonly pageViewRepository: IPageViewRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<RecordPageViewOutput>,
) {}
async execute(input: RecordPageViewInput): Promise<Result<void, ApplicationErrorCode<RecordPageViewErrorCode, { message: string }>>> {
async execute(input: RecordPageViewInput): Promise<Result<RecordPageViewOutput, ApplicationErrorCode<RecordPageViewErrorCode, { message: string }>>> {
try {
type PageViewCreateProps = Parameters<(typeof PageView)['create']>[0];
@@ -53,15 +52,13 @@ export class RecordPageViewUseCase implements UseCase<RecordPageViewInput, void,
pageViewId: pageView.id,
};
this.output.present(resultModel);
this.logger.info('Page view recorded', {
pageViewId: pageView.id,
entityId: input.entityId,
entityType: input.entityType,
});
return Result.ok(undefined);
return Result.ok(resultModel);
} catch (error) {
const err = error as Error;
this.logger.error('Failed to record page view', err, { input });