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;
}
}
}

View File

@@ -24,43 +24,38 @@ export type LoginApplicationError = ApplicationErrorCode<LoginErrorCode, { messa
*
* Handles user login by verifying credentials.
*/
export class LoginUseCase implements UseCase<LoginInput, LoginResult, LoginErrorCode> {
export class LoginUseCase implements UseCase<LoginInput, void, LoginErrorCode> {
constructor(
private readonly authRepo: IAuthRepository,
private readonly passwordService: IPasswordHashingService,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<Result<LoginResult, LoginApplicationError>>,
private readonly output: UseCaseOutputPort<LoginResult>,
) {}
async execute(input: LoginInput): Promise<Result<LoginResult, LoginApplicationError>> {
async execute(input: LoginInput): Promise<Result<void, LoginApplicationError>> {
try {
const emailVO = EmailAddress.create(input.email);
const user = await this.authRepo.findByEmail(emailVO);
if (!user || !user.getPasswordHash()) {
const result = Result.err<LoginResult, LoginApplicationError>({
return Result.err({
code: 'INVALID_CREDENTIALS',
details: { message: 'Invalid credentials' },
});
this.output.present(result);
return result;
}
const passwordHash = user.getPasswordHash()!;
const isValid = await this.passwordService.verify(input.password, passwordHash.value);
if (!isValid) {
const result = Result.err<LoginResult, LoginApplicationError>({
return Result.err<LoginApplicationError>({
code: 'INVALID_CREDENTIALS',
details: { message: 'Invalid credentials' },
});
this.output.present(result);
return result;
}
const result = Result.ok<LoginResult, LoginApplicationError>({ user });
this.output.present(result);
return result;
this.output.present({ user });
return Result.ok(undefined);
} catch (error) {
const message =
error instanceof Error && error.message
@@ -71,12 +66,10 @@ export class LoginUseCase implements UseCase<LoginInput, LoginResult, LoginError
input,
});
const result = Result.err<LoginResult, LoginApplicationError>({
return Result.err<LoginApplicationError>({
code: 'REPOSITORY_ERROR',
details: { message },
});
this.output.present(result);
return result;
}
}
}

View File

@@ -26,26 +26,24 @@ export type SignupApplicationError = ApplicationErrorCode<SignupErrorCode, { mes
*
* Handles user registration.
*/
export class SignupUseCase implements UseCase<SignupInput, SignupResult, SignupErrorCode> {
export class SignupUseCase implements UseCase<SignupInput, void, SignupErrorCode> {
constructor(
private readonly authRepo: IAuthRepository,
private readonly passwordService: IPasswordHashingService,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<Result<SignupResult, SignupApplicationError>>,
private readonly output: UseCaseOutputPort<SignupResult>,
) {}
async execute(input: SignupInput): Promise<Result<SignupResult, SignupApplicationError>> {
async execute(input: SignupInput): Promise<Result<void, SignupApplicationError>> {
try {
const emailVO = EmailAddress.create(input.email);
const existingUser = await this.authRepo.findByEmail(emailVO);
if (existingUser) {
const result = Result.err<SignupResult, SignupApplicationError>({
return Result.err({
code: 'USER_ALREADY_EXISTS',
details: { message: 'User already exists' },
});
this.output.present(result);
return result;
}
const hashedPassword = await this.passwordService.hash(input.password);
@@ -62,9 +60,8 @@ export class SignupUseCase implements UseCase<SignupInput, SignupResult, SignupE
await this.authRepo.save(user);
const result = Result.ok<SignupResult, SignupApplicationError>({ user });
this.output.present(result);
return result;
this.output.present({ user });
return Result.ok(undefined);
} catch (error) {
const message =
error instanceof Error && error.message
@@ -75,12 +72,10 @@ export class SignupUseCase implements UseCase<SignupInput, SignupResult, SignupE
input,
});
const result = Result.err<SignupResult, SignupApplicationError>({
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message },
});
this.output.present(result);
return result;
}
}
}

View File

@@ -2,7 +2,7 @@ import type { IDriverRepository } from '../../domain/repositories/IDriverReposit
import { Driver } from '../../domain/entities/Driver';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { UseCaseOutputPort, UseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application/Logger';
export interface CompleteDriverOnboardingInput {
@@ -30,7 +30,7 @@ export type CompleteDriverOnboardingApplicationError = ApplicationErrorCode<
/**
* Use Case for completing driver onboarding.
*/
export class CompleteDriverOnboardingUseCase {
export class CompleteDriverOnboardingUseCase implements UseCase<CompleteDriverOnboardingInput, void, CompleteDriverOnboardingErrorCode> {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly logger: Logger,

View File

@@ -9,6 +9,7 @@ import type { IFeedRepository } from '@core/social/domain/repositories/IFeedRepo
import type { ISocialGraphRepository } from '@core/social/domain/repositories/ISocialGraphRepository';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import { League } from '../../domain/entities/League';
import { Race } from '../../domain/entities/Race';
import { Result as RaceResult } from '../../domain/entities/Result';
@@ -96,13 +97,14 @@ export class DashboardOverviewUseCase {
private readonly getDriverStats: (
driverId: string,
) => DashboardDriverStatsAdapter | null,
private readonly output: UseCaseOutputPort<DashboardOverviewResult>,
) {}
async execute(
input: DashboardOverviewInput,
): Promise<
Result<
DashboardOverviewResult,
void,
ApplicationErrorCode<'DRIVER_NOT_FOUND' | 'REPOSITORY_ERROR', { message: string }>
>
> {
@@ -207,7 +209,9 @@ export class DashboardOverviewUseCase {
friends: friendsSummary,
};
return Result.ok(result);
this.output.present(result);
return Result.ok(undefined);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',

View File

@@ -1,16 +1,15 @@
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { IRankingService } from '../../domain/services/IRankingService';
import type { IDriverStatsService } from '../../domain/services/IDriverStatsService';
import { SkillLevelService, type SkillLevel } from '../../domain/services/SkillLevelService';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { Driver } from '../../domain/entities/Driver';
import type { Team } from '../../domain/entities/Team';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { IDriverStatsService } from '../../domain/services/IDriverStatsService';
import type { IRankingService } from '../../domain/services/IRankingService';
import { SkillLevelService, type SkillLevel } from '../../domain/services/SkillLevelService';
export type GetDriversLeaderboardInput = {
leagueId: string;
leagueId?: string;
seasonId?: string;
};
@@ -34,11 +33,14 @@ export interface GetDriversLeaderboardResult {
activeCount: number;
}
export type GetDriversLeaderboardErrorCode = 'LEAGUE_NOT_FOUND' | 'SEASON_NOT_FOUND' | 'REPOSITORY_ERROR';
export type GetDriversLeaderboardErrorCode =
| 'LEAGUE_NOT_FOUND'
| 'SEASON_NOT_FOUND'
| 'REPOSITORY_ERROR';
/**
* Use Case for retrieving driver leaderboard data.
* Orchestrates domain logic and returns result.
* Returns a Result containing the domain leaderboard model.
*/
export class GetDriversLeaderboardUseCase {
constructor(
@@ -47,13 +49,18 @@ export class GetDriversLeaderboardUseCase {
private readonly driverStatsService: IDriverStatsService,
private readonly getDriverAvatar: (driverId: string) => Promise<string | undefined>,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<GetDriversLeaderboardResult>,
) {}
async execute(
_input: GetDriversLeaderboardInput,
): Promise<Result<GetDriversLeaderboardResult, ApplicationErrorCode<GetDriversLeaderboardErrorCode, { message: string }>>> {
this.logger.debug('Executing GetDriversLeaderboardUseCase');
input: GetDriversLeaderboardInput,
): Promise<
Result<
GetDriversLeaderboardResult,
ApplicationErrorCode<GetDriversLeaderboardErrorCode, { message: string }>
>
> {
this.logger.debug('Executing GetDriversLeaderboardUseCase', { input });
try {
const drivers = await this.driverRepository.findAll();
const rankings = this.rankingService.getAllDriverRankings();
@@ -64,12 +71,15 @@ export class GetDriversLeaderboardUseCase {
avatarUrls[driver.id] = await this.getDriverAvatar(driver.id);
}
const items: DriverLeaderboardItem[] = drivers.map((driver) => {
const ranking = rankings.find((r) => r.driverId === driver.id);
// TODO maps way too much data, should just create Domain Objects
const items: DriverLeaderboardItem[] = drivers.map(driver => {
const ranking = rankings.find(r => r.driverId === driver.id);
const stats = this.driverStatsService.getDriverStats(driver.id);
const rating = ranking?.rating ?? 0;
const racesCompleted = stats?.totalRaces ?? 0;
const skillLevel: SkillLevel = SkillLevelService.getSkillLevel(rating);
const avatarUrl = avatarUrls[driver.id];
return {
driver,
@@ -80,30 +90,32 @@ export class GetDriversLeaderboardUseCase {
podiums: stats?.podiums ?? 0,
isActive: racesCompleted > 0,
rank: ranking?.overallRank ?? 0,
avatarUrl: avatarUrls[driver.id],
...(avatarUrl !== undefined ? { avatarUrl } : {}),
};
});
const totalRaces = items.reduce((sum, d) => sum + d.racesCompleted, 0);
const totalWins = items.reduce((sum, d) => sum + d.wins, 0);
const activeCount = items.filter((d) => d.isActive).length;
const activeCount = items.filter(d => d.isActive).length;
this.logger.debug('Successfully retrieved drivers leaderboard.');
return Result.ok({
const result: GetDriversLeaderboardResult = {
items: items.sort((a, b) => b.rating - a.rating),
totalRaces,
totalWins,
activeCount,
});
};
this.logger.debug('Successfully computed drivers leaderboard');
return Result.ok(result);
} catch (error) {
this.logger.error(
'Error executing GetDriversLeaderboardUseCase',
error instanceof Error ? error : new Error(String(error)),
);
const err = error instanceof Error ? error : new Error(String(error));
this.logger.error('Error executing GetDriversLeaderboardUseCase', err);
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: error instanceof Error ? error.message : 'Unknown error occurred' },
details: { message: err.message ?? 'Unknown error occurred' },
});
}
}

View File

@@ -9,7 +9,7 @@ import type { Team } from '../../domain/entities/Team';
import type { TeamMembership } from '../../domain/types/TeamMembership';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { UseCaseOutputPort, UseCase } from '@core/shared/application';
interface ProfileDriverStatsAdapter {
rating: number | null;
@@ -92,7 +92,7 @@ export type GetProfileOverviewErrorCode =
| 'DRIVER_NOT_FOUND'
| 'REPOSITORY_ERROR';
export class GetProfileOverviewUseCase {
export class GetProfileOverviewUseCase implements UseCase<GetProfileOverviewInput, void, GetProfileOverviewErrorCode> {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly teamRepository: ITeamRepository,

View File

@@ -1,7 +1,7 @@
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { UseCaseOutputPort, UseCase } from '@core/shared/application';
/**
* Input type for retrieving total number of drivers.
@@ -17,7 +17,7 @@ export type GetTotalDriversResult = {
export type GetTotalDriversErrorCode = 'REPOSITORY_ERROR';
export class GetTotalDriversUseCase {
export class GetTotalDriversUseCase implements UseCase<GetTotalDriversInput, void, GetTotalDriversErrorCode> {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly output: UseCaseOutputPort<GetTotalDriversResult>,

View File

@@ -1,5 +1,5 @@
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { Logger, UseCaseOutputPort, UseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
@@ -26,7 +26,7 @@ export type IsDriverRegisteredForRaceResult = {
*
* Checks if a driver is registered for a specific race.
*/
export class IsDriverRegisteredForRaceUseCase {
export class IsDriverRegisteredForRaceUseCase implements UseCase<IsDriverRegisteredForRaceInput, void, IsDriverRegisteredForRaceErrorCode> {
constructor(
private readonly registrationRepository: IRaceRegistrationRepository,
private readonly logger: Logger,

View File

@@ -1,5 +1,5 @@
import { Result } from '@core/shared/application/Result';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { UseCaseOutputPort, UseCase } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Logger } from '@core/shared/application/Logger';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
@@ -25,7 +25,7 @@ export type UpdateDriverProfileErrorCode =
* Encapsulates domain entity mutation. Mapping to DTOs is handled by presenters
* in the presentation layer through the output port.
*/
export class UpdateDriverProfileUseCase {
export class UpdateDriverProfileUseCase implements UseCase<UpdateDriverProfileInput, void, UpdateDriverProfileErrorCode> {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly output: UseCaseOutputPort<UpdateDriverProfileResult>,

View File

@@ -1,15 +1,11 @@
import type { Result } from './Result';
import type { ApplicationErrorCode } from '../errors/ApplicationErrorCode';
/**
* Output Port interface for use cases.
*
* Defines how the core communicates outward. A behavioral boundary that allows
* the use case to present results without knowing the presentation details.
*
* @template T - The success type in the Result
* @template E - The error code type
* @template T - The result model type
*/
export interface UseCaseOutputPort<T, E extends string = string> {
present(result: Result<T, ApplicationErrorCode<E>>): any;
export interface UseCaseOutputPort<T> {
present(data: T): void;
}