refactor
This commit is contained in:
@@ -4,25 +4,30 @@
|
||||
* Represents a driver profile in the GridPilot platform.
|
||||
* Immutable entity with factory methods and domain validation.
|
||||
*/
|
||||
|
||||
|
||||
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
||||
import type { IEntity } from '@core/shared/domain';
|
||||
|
||||
import { IRacingId } from '../value-objects/IRacingId';
|
||||
import { DriverName } from '../value-objects/DriverName';
|
||||
import { CountryCode } from '../value-objects/CountryCode';
|
||||
import { DriverBio } from '../value-objects/DriverBio';
|
||||
import { JoinedAt } from '../value-objects/JoinedAt';
|
||||
|
||||
export class Driver implements IEntity<string> {
|
||||
readonly id: string;
|
||||
readonly iracingId: string;
|
||||
readonly name: string;
|
||||
readonly country: string;
|
||||
readonly bio: string | undefined;
|
||||
readonly joinedAt: Date;
|
||||
readonly iracingId: IRacingId;
|
||||
readonly name: DriverName;
|
||||
readonly country: CountryCode;
|
||||
readonly bio: DriverBio | undefined;
|
||||
readonly joinedAt: JoinedAt;
|
||||
|
||||
private constructor(props: {
|
||||
id: string;
|
||||
iracingId: string;
|
||||
name: string;
|
||||
country: string;
|
||||
bio?: string;
|
||||
joinedAt: Date;
|
||||
iracingId: IRacingId;
|
||||
name: DriverName;
|
||||
country: CountryCode;
|
||||
bio?: DriverBio;
|
||||
joinedAt: JoinedAt;
|
||||
}) {
|
||||
this.id = props.id;
|
||||
this.iracingId = props.iracingId;
|
||||
@@ -43,47 +48,18 @@ export class Driver implements IEntity<string> {
|
||||
bio?: string;
|
||||
joinedAt?: Date;
|
||||
}): Driver {
|
||||
this.validate(props);
|
||||
|
||||
return new Driver({
|
||||
id: props.id,
|
||||
iracingId: props.iracingId,
|
||||
name: props.name,
|
||||
country: props.country,
|
||||
...(props.bio !== undefined ? { bio: props.bio } : {}),
|
||||
joinedAt: props.joinedAt ?? new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Domain validation logic
|
||||
*/
|
||||
private static validate(props: {
|
||||
id: string;
|
||||
iracingId: string;
|
||||
name: string;
|
||||
country: string;
|
||||
}): void {
|
||||
if (!props.id || props.id.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('Driver ID is required');
|
||||
}
|
||||
|
||||
if (!props.iracingId || props.iracingId.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('iRacing ID is required');
|
||||
}
|
||||
|
||||
if (!props.name || props.name.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('Driver name is required');
|
||||
}
|
||||
|
||||
if (!props.country || props.country.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('Country code is required');
|
||||
}
|
||||
|
||||
// Validate ISO country code format (2-3 letters)
|
||||
if (!/^[A-Z]{2,3}$/i.test(props.country)) {
|
||||
throw new RacingDomainValidationError('Country must be a valid ISO code (2-3 letters)');
|
||||
}
|
||||
return new Driver({
|
||||
id: props.id,
|
||||
iracingId: IRacingId.create(props.iracingId),
|
||||
name: DriverName.create(props.name),
|
||||
country: CountryCode.create(props.country),
|
||||
...(props.bio !== undefined ? { bio: DriverBio.create(props.bio) } : {}),
|
||||
joinedAt: JoinedAt.create(props.joinedAt ?? new Date()),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,11 +68,11 @@ export class Driver implements IEntity<string> {
|
||||
update(props: Partial<{
|
||||
name: string;
|
||||
country: string;
|
||||
bio?: string;
|
||||
bio: string | undefined;
|
||||
}>): Driver {
|
||||
const nextName = props.name ?? this.name;
|
||||
const nextCountry = props.country ?? this.country;
|
||||
const nextBio = props.bio ?? this.bio;
|
||||
const nextName = 'name' in props ? DriverName.create(props.name!) : this.name;
|
||||
const nextCountry = 'country' in props ? CountryCode.create(props.country!) : this.country;
|
||||
const nextBio = 'bio' in props ? (props.bio ? DriverBio.create(props.bio) : undefined) : this.bio;
|
||||
|
||||
return new Driver({
|
||||
id: this.id,
|
||||
|
||||
Reference in New Issue
Block a user