harden business rules

This commit is contained in:
2025-12-27 17:53:01 +01:00
parent 3efa978ee0
commit 0e7a01d81c
9 changed files with 1486 additions and 365 deletions

View File

@@ -1,14 +1,10 @@
/**
* Domain Value Object: LeagueVisibility
*
*
* Represents the visibility and ranking status of a league.
*
* - 'ranked' (public): Competitive leagues visible to everyone, affects driver ratings.
* Requires minimum 10 players to ensure competitive integrity.
* - 'unranked' (private): Casual leagues for friends/private groups, no rating impact.
* Can have any number of players.
* This is a hardened version that enforces strict business rules.
*/
import type { IValueObject } from '@core/shared/domain';
import { RacingDomainValidationError } from '../errors/RacingDomainError';
@@ -16,6 +12,7 @@ export type LeagueVisibilityType = 'ranked' | 'unranked';
export interface LeagueVisibilityConstraints {
readonly minDrivers: number;
readonly maxDrivers: number;
readonly isPubliclyVisible: boolean;
readonly affectsRatings: boolean;
readonly requiresApproval: boolean;
@@ -24,22 +21,24 @@ export interface LeagueVisibilityConstraints {
const VISIBILITY_CONSTRAINTS: Record<LeagueVisibilityType, LeagueVisibilityConstraints> = {
ranked: {
minDrivers: 10,
maxDrivers: 100,
isPubliclyVisible: true,
affectsRatings: true,
requiresApproval: false, // Anyone can join public leagues
requiresApproval: false,
},
unranked: {
minDrivers: 2,
maxDrivers: 50,
isPubliclyVisible: false,
affectsRatings: false,
requiresApproval: true, // Private leagues require invite/approval
requiresApproval: true,
},
};
export interface LeagueVisibilityProps {
type: LeagueVisibilityType;
}
export class LeagueVisibility implements IValueObject<LeagueVisibilityProps> {
readonly type: LeagueVisibilityType;
readonly constraints: LeagueVisibilityConstraints;
@@ -58,7 +57,6 @@ export class LeagueVisibility implements IValueObject<LeagueVisibilityProps> {
}
static fromString(value: string): LeagueVisibility {
// Support both old ('public'/'private') and new ('ranked'/'unranked') terminology
if (value === 'ranked' || value === 'public') {
return LeagueVisibility.ranked();
}
@@ -70,32 +68,76 @@ export class LeagueVisibility implements IValueObject<LeagueVisibilityProps> {
/**
* Validates that the given driver count meets the minimum requirement
* for this visibility type.
*/
validateDriverCount(driverCount: number): { valid: boolean; error?: string } {
if (driverCount < this.constraints.minDrivers) {
return {
valid: false,
error: `${this.type === 'ranked' ? 'Ranked' : 'Unranked'} leagues require at least ${this.constraints.minDrivers} drivers`,
error: `${this.type === 'ranked' ? 'Ranked' : 'Unranked'} leagues require at least ${this.constraints.minDrivers} drivers`
};
}
if (driverCount > this.constraints.maxDrivers) {
return {
valid: false,
error: `${this.type === 'ranked' ? 'Ranked' : 'Unranked'} leagues cannot exceed ${this.constraints.maxDrivers} drivers`
};
}
return { valid: true };
}
/**
* Returns true if this is a ranked/public league
* Validates that the given max participants is appropriate for this visibility
*/
validateMaxParticipants(maxParticipants: number): { valid: boolean; error?: string } {
if (maxParticipants < this.constraints.minDrivers) {
return {
valid: false,
error: `Max participants must be at least ${this.constraints.minDrivers} for ${this.type} leagues`
};
}
if (maxParticipants > this.constraints.maxDrivers) {
return {
valid: false,
error: `Max participants cannot exceed ${this.constraints.maxDrivers} for ${this.type} leagues`
};
}
return { valid: true };
}
/**
* Check if this is a ranked/public league
*/
isRanked(): boolean {
return this.type === 'ranked';
}
/**
* Returns true if this is an unranked/private league
* Check if this is an unranked/private league
*/
isUnranked(): boolean {
return this.type === 'unranked';
}
/**
* Get minimum required drivers
*/
getMinDrivers(): number {
return this.constraints.minDrivers;
}
/**
* Get maximum allowed drivers
*/
getMaxDrivers(): number {
return this.constraints.maxDrivers;
}
/**
* Check if the given driver count meets minimum requirements
*/
meetsMinimumForVisibility(driverCount: number): boolean {
return driverCount >= this.constraints.minDrivers;
}
/**
* Convert to string for serialization
@@ -104,10 +146,6 @@ export class LeagueVisibility implements IValueObject<LeagueVisibilityProps> {
return this.type;
}
get props(): LeagueVisibilityProps {
return { type: this.type };
}
/**
* For backward compatibility with existing 'public'/'private' terminology
*/
@@ -115,6 +153,10 @@ export class LeagueVisibility implements IValueObject<LeagueVisibilityProps> {
return this.type === 'ranked' ? 'public' : 'private';
}
get props(): LeagueVisibilityProps {
return { type: this.type };
}
equals(other: IValueObject<LeagueVisibilityProps>): boolean {
return this.props.type === other.props.type;
}
@@ -122,4 +164,6 @@ export class LeagueVisibility implements IValueObject<LeagueVisibilityProps> {
// Export constants for validation
export const MIN_RANKED_LEAGUE_DRIVERS = VISIBILITY_CONSTRAINTS.ranked.minDrivers;
export const MIN_UNRANKED_LEAGUE_DRIVERS = VISIBILITY_CONSTRAINTS.unranked.minDrivers;
export const MAX_RANKED_LEAGUE_DRIVERS = VISIBILITY_CONSTRAINTS.ranked.maxDrivers;
export const MIN_UNRANKED_LEAGUE_DRIVERS = VISIBILITY_CONSTRAINTS.unranked.minDrivers;
export const MAX_UNRANKED_LEAGUE_DRIVERS = VISIBILITY_CONSTRAINTS.unranked.maxDrivers;