refactor use cases

This commit is contained in:
2025-12-21 01:31:31 +01:00
parent 8ecd638396
commit 22f28728ce
17 changed files with 402 additions and 286 deletions

View File

@@ -1,16 +0,0 @@
export interface RawStanding {
id: string;
leagueId: string;
driverId: string;
position: number;
points: number;
wins: number;
racesCompleted: number;
// These properties might be optional or present depending on the data source
seasonId?: string;
podiums?: number;
}
export interface ILeagueStandingsRepository {
getLeagueStandings(leagueId: string): Promise<RawStanding[]>;
}

View File

@@ -1,22 +0,0 @@
// TODO is this even used? either remove or it must be within racing domain
export interface GetLeagueStandingsUseCase {
execute(leagueId: string): Promise<LeagueStandingsViewModel>;
}
export interface StandingItemViewModel {
id: string;
leagueId: string;
seasonId: string;
driverId: string;
position: number;
points: number;
wins: number;
podiums: number;
racesCompleted: number;
}
export interface LeagueStandingsViewModel {
leagueId: string;
standings: StandingItemViewModel[];
}

View File

@@ -1,77 +0,0 @@
import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetLeagueStandingsUseCaseImpl } from './GetLeagueStandingsUseCaseImpl';
import type { ILeagueStandingsRepository, RawStanding } from '../ports/ILeagueStandingsRepository';
describe('GetLeagueStandingsUseCaseImpl', () => {
let repository: {
getLeagueStandings: Mock;
};
let useCase: GetLeagueStandingsUseCaseImpl;
beforeEach(() => {
repository = {
getLeagueStandings: vi.fn(),
} as unknown as ILeagueStandingsRepository as any;
useCase = new GetLeagueStandingsUseCaseImpl(repository as unknown as ILeagueStandingsRepository);
});
it('maps raw standings from repository to view model', async () => {
const leagueId = 'league-1';
const rawStandings: RawStanding[] = [
{
id: 's1',
leagueId,
seasonId: 'season-1',
driverId: 'driver-1',
position: 1,
points: 100,
wins: 3,
podiums: 5,
racesCompleted: 10,
},
{
id: 's2',
leagueId,
seasonId: null,
driverId: 'driver-2',
position: 2,
points: 80,
wins: 1,
podiums: null,
racesCompleted: 10,
},
];
repository.getLeagueStandings.mockResolvedValue(rawStandings);
const result = await useCase.execute(leagueId);
expect(repository.getLeagueStandings).toHaveBeenCalledWith(leagueId);
expect(result.leagueId).toBe(leagueId);
expect(result.standings).toEqual([
{
id: 's1',
leagueId,
seasonId: 'season-1',
driverId: 'driver-1',
position: 1,
points: 100,
wins: 3,
podiums: 5,
racesCompleted: 10,
},
{
id: 's2',
leagueId,
seasonId: '',
driverId: 'driver-2',
position: 2,
points: 80,
wins: 1,
podiums: 0,
racesCompleted: 10,
},
]);
});
});

View File

@@ -1,31 +0,0 @@
import { ILeagueStandingsRepository, RawStanding } from '../ports/ILeagueStandingsRepository';
import { GetLeagueStandingsUseCase, LeagueStandingsViewModel, StandingItemViewModel } from './GetLeagueStandingsUseCase';
// TODO is this even used? either remove or it must be within racing domain
export class GetLeagueStandingsUseCaseImpl implements GetLeagueStandingsUseCase {
constructor(private repository: ILeagueStandingsRepository) {}
async execute(leagueId: string): Promise<LeagueStandingsViewModel> {
const rawStandings = await this.repository.getLeagueStandings(leagueId);
const standingItems: StandingItemViewModel[] = rawStandings.map((standing: RawStanding) => {
return {
id: standing.id,
leagueId: standing.leagueId,
seasonId: standing.seasonId ?? '',
driverId: standing.driverId,
position: standing.position,
points: standing.points,
wins: standing.wins,
podiums: standing.podiums ?? 0,
racesCompleted: standing.racesCompleted,
};
});
return {
leagueId: leagueId,
standings: standingItems,
};
}
}