refactor
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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' },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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>,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user