Files
gridpilot.gg/core/racing/domain/entities/championship/ChampionshipStanding.ts
2025-12-17 00:33:13 +01:00

69 lines
2.5 KiB
TypeScript

import type { IEntity } from '@core/shared/domain';
import { RacingDomainValidationError } from '../../errors/RacingDomainError';
import type { ParticipantRef } from '../../types/ParticipantRef';
import { Points } from '../../value-objects/Points';
import { Position } from './Position';
import { ResultsCount } from './ResultsCount';
export class ChampionshipStanding implements IEntity<string> {
readonly id: string;
readonly seasonId: string;
readonly championshipId: string;
readonly participant: ParticipantRef;
readonly totalPoints: Points;
readonly resultsCounted: ResultsCount;
readonly resultsDropped: ResultsCount;
readonly position: Position;
private constructor(props: {
seasonId: string;
championshipId: string;
participant: ParticipantRef;
totalPoints: number;
resultsCounted: number;
resultsDropped: number;
position: number;
}) {
this.seasonId = props.seasonId;
this.championshipId = props.championshipId;
this.participant = props.participant;
this.totalPoints = Points.create(props.totalPoints);
this.resultsCounted = ResultsCount.create(props.resultsCounted);
this.resultsDropped = ResultsCount.create(props.resultsDropped);
this.position = Position.create(props.position);
this.id = `${this.seasonId}-${this.championshipId}-${this.participant.id}`;
}
static create(props: {
seasonId: string;
championshipId: string;
participant: ParticipantRef;
totalPoints: number;
resultsCounted: number;
resultsDropped: number;
position: number;
}): ChampionshipStanding {
if (!props.seasonId || props.seasonId.trim().length === 0) {
throw new RacingDomainValidationError('Season ID is required');
}
if (!props.championshipId || props.championshipId.trim().length === 0) {
throw new RacingDomainValidationError('Championship ID is required');
}
if (!props.participant || !props.participant.id || props.participant.id.trim().length === 0) {
throw new RacingDomainValidationError('Participant is required');
}
return new ChampionshipStanding(props);
}
withPosition(position: number): ChampionshipStanding {
return ChampionshipStanding.create({
seasonId: this.seasonId,
championshipId: this.championshipId,
participant: this.participant,
totalPoints: this.totalPoints.toNumber(),
resultsCounted: this.resultsCounted.toNumber(),
resultsDropped: this.resultsDropped.toNumber(),
position,
});
}
}