fix issues in core
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
|
||||
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 { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
|
||||
import type { DriverRatingPort } from '../ports/DriverRatingPort';
|
||||
|
||||
export type DriverSeasonStats = {
|
||||
@@ -56,7 +55,6 @@ export class GetLeagueDriverSeasonStatsUseCase {
|
||||
private readonly penaltyRepository: IPenaltyRepository,
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
private readonly teamRepository: ITeamRepository,
|
||||
private readonly driverRatingPort: DriverRatingPort,
|
||||
private readonly output: UseCaseOutputPort<GetLeagueDriverSeasonStatsResult>,
|
||||
) {}
|
||||
@@ -101,39 +99,32 @@ export class GetLeagueDriverSeasonStatsUseCase {
|
||||
|
||||
const driverRatings = new Map<string, { rating: number | null; ratingChange: number | null }>();
|
||||
for (const standing of standings) {
|
||||
const driverId = String(standing.driverId);
|
||||
const driverId = standing.driverId.toString();
|
||||
const rating = await this.driverRatingPort.getDriverRating(driverId);
|
||||
driverRatings.set(driverId, { rating, ratingChange: null });
|
||||
}
|
||||
|
||||
const driverResults = new Map<string, Array<{ position: number }>>();
|
||||
for (const standing of standings) {
|
||||
const driverId = String(standing.driverId);
|
||||
const driverId = standing.driverId.toString();
|
||||
const results = await this.resultRepository.findByDriverIdAndLeagueId(driverId, leagueId);
|
||||
driverResults.set(
|
||||
driverId,
|
||||
results.map(result => ({ position: Number((result as any).position) })),
|
||||
results.map(result => ({ position: result.position.toNumber() })),
|
||||
);
|
||||
}
|
||||
|
||||
const driverIds = standings.map(s => String(s.driverId));
|
||||
const driverIds = standings.map(s => s.driverId.toString());
|
||||
const drivers = await Promise.all(driverIds.map(id => this.driverRepository.findById(id)));
|
||||
const driversMap = new Map(drivers.filter(d => d).map(d => [String(d!.id), d!]));
|
||||
const teamIds = Array.from(
|
||||
new Set(
|
||||
drivers
|
||||
.filter(d => (d as any)?.teamId)
|
||||
.map(d => (d as any).teamId as string),
|
||||
),
|
||||
const driversMap = new Map(
|
||||
drivers
|
||||
.filter((driver): driver is NonNullable<typeof driver> => driver !== null)
|
||||
.map(driver => [driver.id, driver]),
|
||||
);
|
||||
const teams = await Promise.all(teamIds.map(id => this.teamRepository.findById(id)));
|
||||
const teamsMap = new Map(teams.filter(t => t).map(t => [String(t!.id), t!]));
|
||||
|
||||
const stats: DriverSeasonStats[] = standings.map(standing => {
|
||||
const driverId = String(standing.driverId);
|
||||
const driver = driversMap.get(driverId) as any;
|
||||
const teamId = driver?.teamId as string | undefined;
|
||||
const team = teamId ? teamsMap.get(String(teamId)) : undefined;
|
||||
const driverId = standing.driverId.toString();
|
||||
const driver = driversMap.get(driverId);
|
||||
const penalties = penaltiesByDriver.get(driverId) ?? { baseDelta: 0, bonusDelta: 0 };
|
||||
const results = driverResults.get(driverId) ?? [];
|
||||
const rating = driverRatings.get(driverId);
|
||||
@@ -146,16 +137,16 @@ export class GetLeagueDriverSeasonStatsUseCase {
|
||||
results.length > 0
|
||||
? results.reduce((sum, r) => sum + r.position, 0) / results.length
|
||||
: null;
|
||||
const totalPoints = Number(standing.points);
|
||||
const totalPoints = standing.points.toNumber();
|
||||
const pointsPerRace = racesStarted > 0 ? totalPoints / racesStarted : 0;
|
||||
|
||||
return {
|
||||
leagueId,
|
||||
driverId,
|
||||
position: Number(standing.position),
|
||||
driverName: String(driver?.name ?? ''),
|
||||
teamId,
|
||||
teamName: (team as any)?.name as string | undefined,
|
||||
position: standing.position.toNumber(),
|
||||
driverName: driver ? driver.name.toString() : '',
|
||||
teamId: undefined,
|
||||
teamName: undefined,
|
||||
totalPoints,
|
||||
basePoints: totalPoints - penalties.baseDelta,
|
||||
penaltyPoints: penalties.baseDelta,
|
||||
|
||||
Reference in New Issue
Block a user