wip
This commit is contained in:
@@ -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';
|
||||
|
||||
84
packages/racing/application/use-cases/ApplyPenaltyUseCase.ts
Normal file
84
packages/racing/application/use-cases/ApplyPenaltyUseCase.ts
Normal 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 };
|
||||
}
|
||||
}
|
||||
68
packages/racing/application/use-cases/FileProtestUseCase.ts
Normal file
68
packages/racing/application/use-cases/FileProtestUseCase.ts
Normal 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 };
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user