This commit is contained in:
2025-12-09 10:32:59 +01:00
parent 35f988f885
commit a780139692
26 changed files with 2224 additions and 344 deletions

View File

@@ -26,6 +26,11 @@ export * from './use-cases/GetLeagueFullConfigQuery';
export * from './use-cases/PreviewLeagueScheduleQuery';
export * from './use-cases/GetRaceWithSOFQuery';
export * from './use-cases/GetLeagueStatsQuery';
export * from './use-cases/FileProtestUseCase';
export * from './use-cases/ReviewProtestUseCase';
export * from './use-cases/ApplyPenaltyUseCase';
export * from './use-cases/GetRaceProtestsQuery';
export * from './use-cases/GetRacePenaltiesQuery';
// Export ports
export * from './ports/DriverRatingProvider';

View File

@@ -0,0 +1,84 @@
/**
* Application Use Case: ApplyPenaltyUseCase
*
* Allows a steward to apply a penalty to a driver for an incident during a race.
* The penalty can be standalone or linked to an upheld protest.
*/
import { Penalty, type PenaltyType } from '../../domain/entities/Penalty';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import { randomUUID } from 'crypto';
export interface ApplyPenaltyCommand {
raceId: string;
driverId: string;
stewardId: string;
type: PenaltyType;
value?: number;
reason: string;
protestId?: string;
notes?: string;
}
export class ApplyPenaltyUseCase {
constructor(
private readonly penaltyRepository: IPenaltyRepository,
private readonly protestRepository: IProtestRepository,
private readonly raceRepository: IRaceRepository,
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
) {}
async execute(command: ApplyPenaltyCommand): Promise<{ penaltyId: string }> {
// Validate race exists
const race = await this.raceRepository.findById(command.raceId);
if (!race) {
throw new Error('Race not found');
}
// Validate steward has authority (owner or admin of the league)
const memberships = await this.leagueMembershipRepository.getLeagueMembers(race.leagueId);
const stewardMembership = memberships.find(
m => m.driverId === command.stewardId && m.status === 'active'
);
if (!stewardMembership || (stewardMembership.role !== 'owner' && stewardMembership.role !== 'admin')) {
throw new Error('Only league owners and admins can apply penalties');
}
// If linked to a protest, validate the protest exists and is upheld
if (command.protestId) {
const protest = await this.protestRepository.findById(command.protestId);
if (!protest) {
throw new Error('Protest not found');
}
if (protest.status !== 'upheld') {
throw new Error('Can only create penalties for upheld protests');
}
if (protest.raceId !== command.raceId) {
throw new Error('Protest is not for this race');
}
}
// Create the penalty
const penalty = Penalty.create({
id: randomUUID(),
raceId: command.raceId,
driverId: command.driverId,
type: command.type,
value: command.value,
reason: command.reason,
protestId: command.protestId,
issuedBy: command.stewardId,
status: 'pending',
issuedAt: new Date(),
notes: command.notes,
});
await this.penaltyRepository.create(penalty);
return { penaltyId: penalty.id };
}
}

View File

@@ -0,0 +1,68 @@
/**
* Application Use Case: FileProtestUseCase
*
* Allows a driver to file a protest against another driver for an incident during a race.
*/
import { Protest, type ProtestIncident } from '../../domain/entities/Protest';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import { randomUUID } from 'crypto';
export interface FileProtestCommand {
raceId: string;
protestingDriverId: string;
accusedDriverId: string;
incident: ProtestIncident;
comment?: string;
proofVideoUrl?: string;
}
export class FileProtestUseCase {
constructor(
private readonly protestRepository: IProtestRepository,
private readonly raceRepository: IRaceRepository,
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
) {}
async execute(command: FileProtestCommand): Promise<{ protestId: string }> {
// Validate race exists
const race = await this.raceRepository.findById(command.raceId);
if (!race) {
throw new Error('Race not found');
}
// Validate drivers are not the same
if (command.protestingDriverId === command.accusedDriverId) {
throw new Error('Cannot file a protest against yourself');
}
// Validate protesting driver is a member of the league
const memberships = await this.leagueMembershipRepository.getLeagueMembers(race.leagueId);
const protestingDriverMembership = memberships.find(
m => m.driverId === command.protestingDriverId && m.status === 'active'
);
if (!protestingDriverMembership) {
throw new Error('Protesting driver is not an active member of this league');
}
// Create the protest
const protest = Protest.create({
id: randomUUID(),
raceId: command.raceId,
protestingDriverId: command.protestingDriverId,
accusedDriverId: command.accusedDriverId,
incident: command.incident,
comment: command.comment,
proofVideoUrl: command.proofVideoUrl,
status: 'pending',
filedAt: new Date(),
});
await this.protestRepository.create(protest);
return { protestId: protest.id };
}
}

View File

@@ -1,6 +1,7 @@
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { LeagueDriverSeasonStatsDTO } from '../dto/LeagueDriverSeasonStatsDTO';
export interface DriverRatingPort {
@@ -16,26 +17,39 @@ export class GetLeagueDriverSeasonStatsQuery {
private readonly standingRepository: IStandingRepository,
private readonly resultRepository: IResultRepository,
private readonly penaltyRepository: IPenaltyRepository,
private readonly raceRepository: IRaceRepository,
private readonly driverRatingPort: DriverRatingPort,
) {}
async execute(params: GetLeagueDriverSeasonStatsQueryParamsDTO): Promise<LeagueDriverSeasonStatsDTO[]> {
const { leagueId } = params;
const [standings, penaltiesForLeague] = await Promise.all([
// Get standings and races for the league
const [standings, races] = await Promise.all([
this.standingRepository.findByLeagueId(leagueId),
this.penaltyRepository.findByLeagueId(leagueId),
this.raceRepository.findByLeagueId(leagueId),
]);
// Fetch all penalties for all races in the league
const penaltiesArrays = await Promise.all(
races.map(race => this.penaltyRepository.findByRaceId(race.id))
);
const penaltiesForLeague = penaltiesArrays.flat();
// Group penalties by driver for quick lookup
const penaltiesByDriver = new Map<string, { baseDelta: number; bonusDelta: number }>();
for (const p of penaltiesForLeague) {
// Only count applied penalties
if (p.status !== 'applied') continue;
const current = penaltiesByDriver.get(p.driverId) ?? { baseDelta: 0, bonusDelta: 0 };
if (p.pointsDelta < 0) {
current.baseDelta += p.pointsDelta;
} else {
current.bonusDelta += p.pointsDelta;
// Convert penalty to points delta based on type
if (p.type === 'points_deduction' && p.value) {
// Points deductions are negative
current.baseDelta -= p.value;
}
penaltiesByDriver.set(p.driverId, current);
}

View File

@@ -0,0 +1,74 @@
/**
* Application Query: GetRacePenaltiesQuery
*
* Returns all penalties applied for a specific race, with driver details.
*/
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { PenaltyType, PenaltyStatus } from '../../domain/entities/Penalty';
export interface RacePenaltyDTO {
id: string;
raceId: string;
driverId: string;
driverName: string;
type: PenaltyType;
value?: number;
reason: string;
protestId?: string;
issuedBy: string;
issuedByName: string;
status: PenaltyStatus;
description: string;
issuedAt: string;
appliedAt?: string;
notes?: string;
}
export class GetRacePenaltiesQuery {
constructor(
private readonly penaltyRepository: IPenaltyRepository,
private readonly driverRepository: IDriverRepository,
) {}
async execute(raceId: string): Promise<RacePenaltyDTO[]> {
const penalties = await this.penaltyRepository.findByRaceId(raceId);
// Load all driver details in parallel
const driverIds = new Set<string>();
penalties.forEach(penalty => {
driverIds.add(penalty.driverId);
driverIds.add(penalty.issuedBy);
});
const drivers = await Promise.all(
Array.from(driverIds).map(id => this.driverRepository.findById(id))
);
const driverMap = new Map<string, string>();
drivers.forEach(driver => {
if (driver) {
driverMap.set(driver.id, driver.name);
}
});
return penalties.map(penalty => ({
id: penalty.id,
raceId: penalty.raceId,
driverId: penalty.driverId,
driverName: driverMap.get(penalty.driverId) || 'Unknown',
type: penalty.type,
value: penalty.value,
reason: penalty.reason,
protestId: penalty.protestId,
issuedBy: penalty.issuedBy,
issuedByName: driverMap.get(penalty.issuedBy) || 'Unknown',
status: penalty.status,
description: penalty.getDescription(),
issuedAt: penalty.issuedAt.toISOString(),
appliedAt: penalty.appliedAt?.toISOString(),
notes: penalty.notes,
}));
}
}

View File

@@ -0,0 +1,77 @@
/**
* Application Query: GetRaceProtestsQuery
*
* Returns all protests filed for a specific race, with driver details.
*/
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { ProtestStatus, ProtestIncident } from '../../domain/entities/Protest';
export interface RaceProtestDTO {
id: string;
raceId: string;
protestingDriverId: string;
protestingDriverName: string;
accusedDriverId: string;
accusedDriverName: string;
incident: ProtestIncident;
comment?: string;
proofVideoUrl?: string;
status: ProtestStatus;
reviewedBy?: string;
reviewedByName?: string;
decisionNotes?: string;
filedAt: string;
reviewedAt?: string;
}
export class GetRaceProtestsQuery {
constructor(
private readonly protestRepository: IProtestRepository,
private readonly driverRepository: IDriverRepository,
) {}
async execute(raceId: string): Promise<RaceProtestDTO[]> {
const protests = await this.protestRepository.findByRaceId(raceId);
// Load all driver details in parallel
const driverIds = new Set<string>();
protests.forEach(protest => {
driverIds.add(protest.protestingDriverId);
driverIds.add(protest.accusedDriverId);
if (protest.reviewedBy) {
driverIds.add(protest.reviewedBy);
}
});
const drivers = await Promise.all(
Array.from(driverIds).map(id => this.driverRepository.findById(id))
);
const driverMap = new Map<string, string>();
drivers.forEach(driver => {
if (driver) {
driverMap.set(driver.id, driver.name);
}
});
return protests.map(protest => ({
id: protest.id,
raceId: protest.raceId,
protestingDriverId: protest.protestingDriverId,
protestingDriverName: driverMap.get(protest.protestingDriverId) || 'Unknown',
accusedDriverId: protest.accusedDriverId,
accusedDriverName: driverMap.get(protest.accusedDriverId) || 'Unknown',
incident: protest.incident,
comment: protest.comment,
proofVideoUrl: protest.proofVideoUrl,
status: protest.status,
reviewedBy: protest.reviewedBy,
reviewedByName: protest.reviewedBy ? driverMap.get(protest.reviewedBy) : undefined,
decisionNotes: protest.decisionNotes,
filedAt: protest.filedAt.toISOString(),
reviewedAt: protest.reviewedAt?.toISOString(),
}));
}
}

View File

@@ -64,9 +64,8 @@ export class RecalculateChampionshipStandingsUseCase {
const results = await this.resultRepository.findByRaceId(race.id);
// For this slice, penalties are league-level and not race-specific,
// so we simply ignore them in the use case to keep behavior minimal.
const penalties = await this.penaltyRepository.findByLeagueId(season.leagueId);
// Fetch penalties for this specific race
const penalties = await this.penaltyRepository.findByRaceId(race.id);
const participantPoints = this.eventScoringService.scoreSession({
seasonId,

View File

@@ -0,0 +1,58 @@
/**
* Application Use Case: ReviewProtestUseCase
*
* Allows a steward to review a protest and make a decision (uphold or dismiss).
*/
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
export interface ReviewProtestCommand {
protestId: string;
stewardId: string;
decision: 'uphold' | 'dismiss';
decisionNotes: string;
}
export class ReviewProtestUseCase {
constructor(
private readonly protestRepository: IProtestRepository,
private readonly raceRepository: IRaceRepository,
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
) {}
async execute(command: ReviewProtestCommand): Promise<void> {
// Load the protest
const protest = await this.protestRepository.findById(command.protestId);
if (!protest) {
throw new Error('Protest not found');
}
// Load the race to get league ID
const race = await this.raceRepository.findById(protest.raceId);
if (!race) {
throw new Error('Race not found');
}
// Validate steward has authority (owner or admin of the league)
const memberships = await this.leagueMembershipRepository.getLeagueMembers(race.leagueId);
const stewardMembership = memberships.find(
m => m.driverId === command.stewardId && m.status === 'active'
);
if (!stewardMembership || (stewardMembership.role !== 'owner' && stewardMembership.role !== 'admin')) {
throw new Error('Only league owners and admins can review protests');
}
// Apply the decision
let updatedProtest;
if (command.decision === 'uphold') {
updatedProtest = protest.uphold(command.stewardId, command.decisionNotes);
} else {
updatedProtest = protest.dismiss(command.stewardId, command.decisionNotes);
}
await this.protestRepository.update(updatedProtest);
}
}

View File

@@ -1,29 +1,142 @@
/**
* Domain Entity: Penalty
*
* Represents a season-long penalty or bonus applied to a driver
* within a specific league. This is intentionally simple for the
* alpha demo and models points adjustments only.
*
* Represents a penalty applied to a driver for an incident during a race.
* Penalties can be applied as a result of an upheld protest or directly by stewards.
*/
export type PenaltyType = 'points-deduction' | 'points-bonus';
export interface Penalty {
export type PenaltyType =
| 'time_penalty' // Add time to race result (e.g., +5 seconds)
| 'grid_penalty' // Grid position penalty for next race
| 'points_deduction' // Deduct championship points
| 'disqualification' // DSQ from the race
| 'warning' // Official warning (no immediate consequence)
| 'license_points'; // Add penalty points to license (future feature)
export type PenaltyStatus = 'pending' | 'applied' | 'appealed' | 'overturned';
export interface PenaltyProps {
id: string;
leagueId: string;
raceId: string;
/** The driver receiving the penalty */
driverId: string;
/** Type of penalty */
type: PenaltyType;
/** Value depends on type: seconds for time_penalty, positions for grid_penalty, points for points_deduction */
value?: number;
/** Reason for the penalty */
reason: string;
/** ID of the protest that led to this penalty (if applicable) */
protestId?: string;
/** ID of the steward who issued the penalty */
issuedBy: string;
/** Current status of the penalty */
status: PenaltyStatus;
/** Timestamp when the penalty was issued */
issuedAt: Date;
/** Timestamp when the penalty was applied to results */
appliedAt?: Date;
/** Notes about the penalty application */
notes?: string;
}
export class Penalty {
private constructor(private readonly props: PenaltyProps) {}
static create(props: PenaltyProps): Penalty {
if (!props.id) throw new Error('Penalty ID is required');
if (!props.raceId) throw new Error('Race ID is required');
if (!props.driverId) throw new Error('Driver ID is required');
if (!props.type) throw new Error('Penalty type is required');
if (!props.reason?.trim()) throw new Error('Penalty reason is required');
if (!props.issuedBy) throw new Error('Penalty must be issued by a steward');
// Validate value based on type
if (['time_penalty', 'grid_penalty', 'points_deduction'].includes(props.type)) {
if (props.value === undefined || props.value <= 0) {
throw new Error(`${props.type} requires a positive value`);
}
}
return new Penalty({
...props,
status: props.status || 'pending',
issuedAt: props.issuedAt || new Date(),
});
}
get id(): string { return this.props.id; }
get raceId(): string { return this.props.raceId; }
get driverId(): string { return this.props.driverId; }
get type(): PenaltyType { return this.props.type; }
get value(): number | undefined { return this.props.value; }
get reason(): string { return this.props.reason; }
get protestId(): string | undefined { return this.props.protestId; }
get issuedBy(): string { return this.props.issuedBy; }
get status(): PenaltyStatus { return this.props.status; }
get issuedAt(): Date { return this.props.issuedAt; }
get appliedAt(): Date | undefined { return this.props.appliedAt; }
get notes(): string | undefined { return this.props.notes; }
isPending(): boolean {
return this.props.status === 'pending';
}
isApplied(): boolean {
return this.props.status === 'applied';
}
/**
* Signed integer representing points adjustment:
* - negative for deductions
* - positive for bonuses
* Mark penalty as applied (after recalculating results)
*/
pointsDelta: number;
markAsApplied(notes?: string): Penalty {
if (this.isApplied()) {
throw new Error('Penalty is already applied');
}
if (this.props.status === 'overturned') {
throw new Error('Cannot apply an overturned penalty');
}
return new Penalty({
...this.props,
status: 'applied',
appliedAt: new Date(),
notes,
});
}
/**
* Optional short reason/label (e.g. "Incident penalty", "Fastest laps bonus").
* Overturn the penalty (e.g., after successful appeal)
*/
reason?: string;
overturn(reason: string): Penalty {
if (this.props.status === 'overturned') {
throw new Error('Penalty is already overturned');
}
return new Penalty({
...this.props,
status: 'overturned',
notes: reason,
});
}
/**
* When this penalty was applied.
* Get a human-readable description of the penalty
*/
appliedAt: Date;
getDescription(): string {
switch (this.props.type) {
case 'time_penalty':
return `+${this.props.value}s time penalty`;
case 'grid_penalty':
return `${this.props.value} place grid penalty (next race)`;
case 'points_deduction':
return `${this.props.value} championship points deducted`;
case 'disqualification':
return 'Disqualified from race';
case 'warning':
return 'Official warning';
case 'license_points':
return `${this.props.value} license penalty points`;
default:
return 'Penalty';
}
}
}

View File

@@ -1,25 +1,54 @@
/**
* Application Port: IPenaltyRepository
*
* Repository interface for season-long penalties and bonuses applied
* to drivers within a league. This is intentionally simple for the
* alpha demo and operates purely on in-memory data.
* Repository Interface: IPenaltyRepository
*
* Defines the contract for persisting and retrieving Penalty entities.
*/
import type { Penalty } from '../entities/Penalty';
export interface IPenaltyRepository {
/**
* Get all penalties for a given league.
* Find a penalty by ID
*/
findByLeagueId(leagueId: string): Promise<Penalty[]>;
findById(id: string): Promise<Penalty | null>;
/**
* Get all penalties for a driver in a specific league.
* Find all penalties for a race
*/
findByLeagueIdAndDriverId(leagueId: string, driverId: string): Promise<Penalty[]>;
findByRaceId(raceId: string): Promise<Penalty[]>;
/**
* Get all penalties in the system.
* Find all penalties for a specific driver
*/
findAll(): Promise<Penalty[]>;
findByDriverId(driverId: string): Promise<Penalty[]>;
/**
* Find all penalties related to a specific protest
*/
findByProtestId(protestId: string): Promise<Penalty[]>;
/**
* Find all pending penalties (not yet applied)
*/
findPending(): Promise<Penalty[]>;
/**
* Find all penalties issued by a specific steward
*/
findIssuedBy(stewardId: string): Promise<Penalty[]>;
/**
* Save a new penalty
*/
create(penalty: Penalty): Promise<void>;
/**
* Update an existing penalty
*/
update(penalty: Penalty): Promise<void>;
/**
* Check if a penalty exists
*/
exists(id: string): Promise<boolean>;
}

View File

@@ -0,0 +1,54 @@
/**
* Repository Interface: IProtestRepository
*
* Defines the contract for persisting and retrieving Protest entities.
*/
import type { Protest } from '../entities/Protest';
export interface IProtestRepository {
/**
* Find a protest by ID
*/
findById(id: string): Promise<Protest | null>;
/**
* Find all protests for a race
*/
findByRaceId(raceId: string): Promise<Protest[]>;
/**
* Find all protests filed by a specific driver
*/
findByProtestingDriverId(driverId: string): Promise<Protest[]>;
/**
* Find all protests against a specific driver
*/
findByAccusedDriverId(driverId: string): Promise<Protest[]>;
/**
* Find all pending protests (for steward review queue)
*/
findPending(): Promise<Protest[]>;
/**
* Find all protests under review by a specific steward
*/
findUnderReviewBy(stewardId: string): Promise<Protest[]>;
/**
* Save a new protest
*/
create(protest: Protest): Promise<void>;
/**
* Update an existing protest
*/
update(protest: Protest): Promise<void>;
/**
* Check if a protest exists
*/
exists(id: string): Promise<boolean>;
}

View File

@@ -120,8 +120,14 @@ export class EventScoringService {
private aggregatePenalties(penalties: Penalty[]): Map<string, number> {
const map = new Map<string, number>();
for (const penalty of penalties) {
// Only count applied points_deduction penalties
if (penalty.status !== 'applied' || penalty.type !== 'points_deduction') {
continue;
}
const current = map.get(penalty.driverId) ?? 0;
map.set(penalty.driverId, current + penalty.pointsDelta);
const delta = penalty.value ?? 0;
map.set(penalty.driverId, current + delta);
}
return map;
}

View File

@@ -8,6 +8,8 @@ export * from './domain/entities/RaceRegistration';
export * from './domain/entities/Team';
export * from './domain/entities/Track';
export * from './domain/entities/Car';
export * from './domain/entities/Protest';
export * from './domain/entities/Penalty';
export * from './domain/repositories/IDriverRepository';
export * from './domain/repositories/ILeagueRepository';
@@ -20,6 +22,8 @@ export * from './domain/repositories/ITeamRepository';
export * from './domain/repositories/ITeamMembershipRepository';
export * from './domain/repositories/ITrackRepository';
export * from './domain/repositories/ICarRepository';
export * from './domain/repositories/IProtestRepository';
export * from './domain/repositories/IPenaltyRepository';
export * from './domain/services/StrengthOfFieldCalculator';

View File

@@ -1,85 +1,70 @@
/**
* Infrastructure Adapter: InMemoryPenaltyRepository
*
* Simple in-memory implementation of IPenaltyRepository seeded with
* a handful of demo penalties and bonuses for leagues/drivers.
* In-Memory Implementation: InMemoryPenaltyRepository
*
* Provides an in-memory storage implementation for penalties.
*/
import type { Penalty } from '@gridpilot/racing/domain/entities/Penalty';
import type { IPenaltyRepository } from '@gridpilot/racing/domain/repositories/IPenaltyRepository';
import type { Penalty } from '../../domain/entities/Penalty';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
export class InMemoryPenaltyRepository implements IPenaltyRepository {
private readonly penalties: Penalty[];
private penalties: Map<string, Penalty> = new Map();
constructor(seedPenalties?: Penalty[]) {
this.penalties = seedPenalties ? [...seedPenalties] : InMemoryPenaltyRepository.createDefaultSeed();
constructor(initialPenalties: Penalty[] = []) {
initialPenalties.forEach(penalty => {
this.penalties.set(penalty.id, penalty);
});
}
async findByLeagueId(leagueId: string): Promise<Penalty[]> {
return this.penalties.filter((p) => p.leagueId === leagueId);
async findById(id: string): Promise<Penalty | null> {
return this.penalties.get(id) || null;
}
async findByLeagueIdAndDriverId(leagueId: string, driverId: string): Promise<Penalty[]> {
return this.penalties.filter((p) => p.leagueId === leagueId && p.driverId === driverId);
async findByRaceId(raceId: string): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.raceId === raceId
);
}
async findAll(): Promise<Penalty[]> {
return [...this.penalties];
async findByDriverId(driverId: string): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.driverId === driverId
);
}
/**
* Default demo seed with a mix of deductions and bonuses
* across a couple of leagues and drivers.
*/
private static createDefaultSeed(): Penalty[] {
const now = new Date();
const daysAgo = (n: number) => new Date(now.getTime() - n * 24 * 60 * 60 * 1000);
async findByProtestId(protestId: string): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.protestId === protestId
);
}
return [
{
id: 'pen-league-1-driver-1-main',
leagueId: 'league-1',
driverId: 'driver-1',
type: 'points-deduction',
pointsDelta: -3,
reason: 'Incident points penalty',
appliedAt: daysAgo(7),
},
{
id: 'pen-league-1-driver-2-bonus',
leagueId: 'league-1',
driverId: 'driver-2',
type: 'points-bonus',
pointsDelta: 2,
reason: 'Fastest laps bonus',
appliedAt: daysAgo(5),
},
{
id: 'pen-league-1-driver-3-bonus',
leagueId: 'league-1',
driverId: 'driver-3',
type: 'points-bonus',
pointsDelta: 1,
reason: 'Pole position bonus',
appliedAt: daysAgo(3),
},
{
id: 'pen-league-2-driver-4-main',
leagueId: 'league-2',
driverId: 'driver-4',
type: 'points-deduction',
pointsDelta: -5,
reason: 'Post-race steward decision',
appliedAt: daysAgo(10),
},
{
id: 'pen-league-2-driver-5-bonus',
leagueId: 'league-2',
driverId: 'driver-5',
type: 'points-bonus',
pointsDelta: 3,
reason: 'Clean race awards',
appliedAt: daysAgo(2),
},
];
async findPending(): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.isPending()
);
}
async findIssuedBy(stewardId: string): Promise<Penalty[]> {
return Array.from(this.penalties.values()).filter(
penalty => penalty.issuedBy === stewardId
);
}
async create(penalty: Penalty): Promise<void> {
if (this.penalties.has(penalty.id)) {
throw new Error(`Penalty with ID ${penalty.id} already exists`);
}
this.penalties.set(penalty.id, penalty);
}
async update(penalty: Penalty): Promise<void> {
if (!this.penalties.has(penalty.id)) {
throw new Error(`Penalty with ID ${penalty.id} not found`);
}
this.penalties.set(penalty.id, penalty);
}
async exists(id: string): Promise<boolean> {
return this.penalties.has(id);
}
}

View File

@@ -0,0 +1,70 @@
/**
* In-Memory Implementation: InMemoryProtestRepository
*
* Provides an in-memory storage implementation for protests.
*/
import type { Protest } from '../../domain/entities/Protest';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
export class InMemoryProtestRepository implements IProtestRepository {
private protests: Map<string, Protest> = new Map();
constructor(initialProtests: Protest[] = []) {
initialProtests.forEach(protest => {
this.protests.set(protest.id, protest);
});
}
async findById(id: string): Promise<Protest | null> {
return this.protests.get(id) || null;
}
async findByRaceId(raceId: string): Promise<Protest[]> {
return Array.from(this.protests.values()).filter(
protest => protest.raceId === raceId
);
}
async findByProtestingDriverId(driverId: string): Promise<Protest[]> {
return Array.from(this.protests.values()).filter(
protest => protest.protestingDriverId === driverId
);
}
async findByAccusedDriverId(driverId: string): Promise<Protest[]> {
return Array.from(this.protests.values()).filter(
protest => protest.accusedDriverId === driverId
);
}
async findPending(): Promise<Protest[]> {
return Array.from(this.protests.values()).filter(
protest => protest.isPending()
);
}
async findUnderReviewBy(stewardId: string): Promise<Protest[]> {
return Array.from(this.protests.values()).filter(
protest => protest.reviewedBy === stewardId && protest.isUnderReview()
);
}
async create(protest: Protest): Promise<void> {
if (this.protests.has(protest.id)) {
throw new Error(`Protest with ID ${protest.id} already exists`);
}
this.protests.set(protest.id, protest);
}
async update(protest: Protest): Promise<void> {
if (!this.protests.has(protest.id)) {
throw new Error(`Protest with ID ${protest.id} not found`);
}
this.protests.set(protest.id, protest);
}
async exists(id: string): Promise<boolean> {
return this.protests.has(id);
}
}