rename to core
This commit is contained in:
150
core/racing/application/use-cases/QuickPenaltyUseCase.ts
Normal file
150
core/racing/application/use-cases/QuickPenaltyUseCase.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* Use Case: QuickPenaltyUseCase
|
||||
*
|
||||
* Allows league admins to quickly issue common penalties without protest process.
|
||||
* Designed for fast, common penalty scenarios like track limits, warnings, etc.
|
||||
*/
|
||||
|
||||
import { Penalty, type PenaltyType } from '../../domain/entities/Penalty';
|
||||
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import { randomUUID } from 'crypto';
|
||||
import type { AsyncUseCase } from '@gridpilot/shared/application';
|
||||
import type { ILogger } from '../../../shared/src/logging/ILogger';
|
||||
|
||||
export interface QuickPenaltyCommand {
|
||||
raceId: string;
|
||||
driverId: string;
|
||||
adminId: string;
|
||||
infractionType: 'track_limits' | 'unsafe_rejoin' | 'aggressive_driving' | 'false_start' | 'other';
|
||||
severity: 'warning' | 'minor' | 'major' | 'severe';
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
export class QuickPenaltyUseCase
|
||||
implements AsyncUseCase<QuickPenaltyCommand, { penaltyId: string }> {
|
||||
constructor(
|
||||
private readonly penaltyRepository: IPenaltyRepository,
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
private readonly logger: ILogger,
|
||||
) {}
|
||||
|
||||
async execute(command: QuickPenaltyCommand): Promise<{ penaltyId: string }> {
|
||||
this.logger.debug('Executing QuickPenaltyUseCase', { command });
|
||||
try {
|
||||
// Validate race exists
|
||||
const race = await this.raceRepository.findById(command.raceId);
|
||||
if (!race) {
|
||||
this.logger.warn('Race not found', { raceId: command.raceId });
|
||||
throw new Error('Race not found');
|
||||
}
|
||||
|
||||
// Validate admin has authority
|
||||
const memberships = await this.leagueMembershipRepository.getLeagueMembers(race.leagueId);
|
||||
const adminMembership = memberships.find(
|
||||
m => m.driverId === command.adminId && m.status === 'active'
|
||||
);
|
||||
|
||||
if (!adminMembership || (adminMembership.role !== 'owner' && adminMembership.role !== 'admin')) {
|
||||
this.logger.warn('Unauthorized admin attempting to issue penalty', { adminId: command.adminId, leagueId: race.leagueId });
|
||||
throw new Error('Only league owners and admins can issue penalties');
|
||||
}
|
||||
|
||||
// Map infraction + severity to penalty type and value
|
||||
const { type, value, reason } = this.mapInfractionToPenalty(
|
||||
command.infractionType,
|
||||
command.severity
|
||||
);
|
||||
|
||||
// Create the penalty
|
||||
const penalty = Penalty.create({
|
||||
id: randomUUID(),
|
||||
leagueId: race.leagueId,
|
||||
raceId: command.raceId,
|
||||
driverId: command.driverId,
|
||||
type,
|
||||
...(value !== undefined ? { value } : {}),
|
||||
reason,
|
||||
issuedBy: command.adminId,
|
||||
status: 'applied', // Quick penalties are applied immediately
|
||||
issuedAt: new Date(),
|
||||
appliedAt: new Date(),
|
||||
...(command.notes !== undefined ? { notes: command.notes } : {}),
|
||||
});
|
||||
|
||||
await this.penaltyRepository.create(penalty);
|
||||
|
||||
this.logger.info('Quick penalty applied successfully', { penaltyId: penalty.id, raceId: command.raceId, driverId: command.driverId });
|
||||
return { penaltyId: penalty.id };
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to apply quick penalty', { command, error: error.message });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private mapInfractionToPenalty(
|
||||
infractionType: QuickPenaltyCommand['infractionType'],
|
||||
severity: QuickPenaltyCommand['severity']
|
||||
): { type: PenaltyType; value?: number; reason: string } {
|
||||
const severityMultipliers = {
|
||||
warning: 1,
|
||||
minor: 2,
|
||||
major: 3,
|
||||
severe: 4,
|
||||
};
|
||||
|
||||
const multiplier = severityMultipliers[severity];
|
||||
|
||||
switch (infractionType) {
|
||||
case 'track_limits':
|
||||
if (severity === 'warning') {
|
||||
return { type: 'warning', reason: 'Track limits violation - warning' };
|
||||
}
|
||||
return {
|
||||
type: 'points_deduction',
|
||||
value: multiplier,
|
||||
reason: `Track limits violation - ${multiplier} point${multiplier > 1 ? 's' : ''} deducted`
|
||||
};
|
||||
|
||||
case 'unsafe_rejoin':
|
||||
return {
|
||||
type: 'time_penalty',
|
||||
value: 5 * multiplier,
|
||||
reason: `Unsafe rejoining to track - +${5 * multiplier}s time penalty`
|
||||
};
|
||||
|
||||
case 'aggressive_driving':
|
||||
if (severity === 'warning') {
|
||||
return { type: 'warning', reason: 'Aggressive driving - warning' };
|
||||
}
|
||||
return {
|
||||
type: 'points_deduction',
|
||||
value: 2 * multiplier,
|
||||
reason: `Aggressive driving - ${2 * multiplier} point${multiplier > 1 ? 's' : ''} deducted`
|
||||
};
|
||||
|
||||
case 'false_start':
|
||||
return {
|
||||
type: 'grid_penalty',
|
||||
value: multiplier,
|
||||
reason: `False start - ${multiplier} grid position${multiplier > 1 ? 's' : ''} penalty`
|
||||
};
|
||||
|
||||
case 'other':
|
||||
if (severity === 'warning') {
|
||||
return { type: 'warning', reason: 'General infraction - warning' };
|
||||
}
|
||||
return {
|
||||
type: 'points_deduction',
|
||||
value: 3 * multiplier,
|
||||
reason: `General infraction - ${3 * multiplier} point${multiplier > 1 ? 's' : ''} deducted`
|
||||
};
|
||||
|
||||
default:
|
||||
this.logger.error(`Unknown infraction type: ${infractionType}`);
|
||||
throw new Error(`Unknown infraction type: ${infractionType}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user