website refactor

This commit is contained in:
2026-01-21 16:52:43 +01:00
parent ac37871bef
commit 2325eef8b5
18 changed files with 835 additions and 47 deletions

View File

@@ -2,6 +2,8 @@ import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Result as RaceResult } from '../../domain/entities/result/Result';
import { Standing } from '../../domain/entities/Standing';
import type { League } from '../../domain/entities/League';
import type { LeagueRepository } from '../../domain/repositories/LeagueRepository';
import type { RaceRegistrationRepository } from '../../domain/repositories/RaceRegistrationRepository';
import type { RaceRepository } from '../../domain/repositories/RaceRepository';
import type { ResultRepository } from '../../domain/repositories/ResultRepository';
@@ -40,6 +42,7 @@ interface DriverRatingOutput {
export class CompleteRaceUseCase {
constructor(
private readonly leagueRepository: LeagueRepository,
private readonly raceRepository: RaceRepository,
private readonly raceRegistrationRepository: RaceRegistrationRepository,
private readonly resultRepository: ResultRepository,
@@ -93,8 +96,17 @@ export class CompleteRaceUseCase {
await this.resultRepository.create(result);
}
// Get league to retrieve points system
const league = await this.leagueRepository.findById(race.leagueId);
if (!league) {
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: `League not found: ${race.leagueId}` },
});
}
// Update standings
await this.updateStandings(race.leagueId, results);
await this.updateStandings(league, results);
// Complete the race
const completedRace = race.complete();
@@ -175,7 +187,7 @@ export class CompleteRaceUseCase {
return results;
}
private async updateStandings(leagueId: string, results: RaceResult[]): Promise<void> {
private async updateStandings(league: League, results: RaceResult[]): Promise<void> {
// Group results by driver
const resultsByDriver = new Map<string, RaceResult[]>();
for (const result of results) {
@@ -187,23 +199,33 @@ export class CompleteRaceUseCase {
// Update or create standings for each driver
for (const [driverId, driverResults] of resultsByDriver) {
let standing = await this.standingRepository.findByDriverIdAndLeagueId(driverId, leagueId);
let standing = await this.standingRepository.findByDriverIdAndLeagueId(driverId, league.id.toString());
if (!standing) {
standing = Standing.create({
leagueId,
leagueId: league.id.toString(),
driverId,
});
}
// Get points system from league configuration
const pointsSystem = league.settings.customPoints ??
this.getPointsSystem(league.settings.pointsSystem);
// Add all results for this driver (should be just one for this race)
for (const result of driverResults) {
standing = standing.addRaceResult(result.position.toNumber(), {
1: 25, 2: 18, 3: 15, 4: 12, 5: 10, 6: 8, 7: 6, 8: 4, 9: 2, 10: 1
});
standing = standing.addRaceResult(result.position.toNumber(), pointsSystem);
}
await this.standingRepository.save(standing);
}
}
private getPointsSystem(pointsSystem: 'f1-2024' | 'indycar' | 'custom'): Record<number, number> {
const systems: Record<string, Record<number, number>> = {
'f1-2024': { 1: 25, 2: 18, 3: 15, 4: 12, 5: 10, 6: 8, 7: 6, 8: 4, 9: 2, 10: 1 },
'indycar': { 1: 50, 2: 40, 3: 35, 4: 32, 5: 30, 6: 28, 7: 26, 8: 24, 9: 22, 10: 20, 11: 19, 12: 18, 13: 17, 14: 16, 15: 15 },
};
return systems[pointsSystem] ?? systems['f1-2024'] ?? { 1: 25, 2: 18, 3: 15, 4: 12, 5: 10, 6: 8, 7: 6, 8: 4, 9: 2, 10: 1 };
}
}