/** * Domain Entity: JoinRequest * * Represents a request to join a league. */ import { Entity } from '@core/shared/domain/Entity'; import { RacingDomainValidationError } from '../errors/RacingDomainError'; import { JoinedAt } from '../value-objects/JoinedAt'; import { LeagueId } from './LeagueId'; import { LeagueOwnerId } from './LeagueOwnerId'; export interface JoinRequestProps { id?: string; leagueId: string; driverId: string; requestedAt?: Date; message?: string; } export class JoinRequest extends Entity { readonly leagueId: LeagueId; readonly driverId: LeagueOwnerId; readonly requestedAt: JoinedAt; readonly message: string | undefined; private constructor(props: { id: string; leagueId: string; driverId: string; requestedAt: Date; message?: string }) { super(props.id); this.leagueId = LeagueId.create(props.leagueId); this.driverId = LeagueOwnerId.create(props.driverId); this.requestedAt = JoinedAt.create(props.requestedAt); this.message = props.message; } static create(props: JoinRequestProps): JoinRequest { this.validate(props); const id = props.id && props.id.trim().length > 0 ? props.id : `${props.leagueId}:${props.driverId}`; const requestedAt = props.requestedAt ?? new Date(); const message = props.message; return new JoinRequest({ id, leagueId: props.leagueId, driverId: props.driverId, requestedAt, ...(message !== undefined && { message }), }); } static rehydrate(props: { id: string; leagueId: string; driverId: string; requestedAt: Date; message?: string; }): JoinRequest { return new JoinRequest({ id: props.id, leagueId: props.leagueId, driverId: props.driverId, requestedAt: props.requestedAt, ...(props.message !== undefined && { message: props.message }), }); } private static validate(props: JoinRequestProps): void { if (!props.leagueId || props.leagueId.trim().length === 0) { throw new RacingDomainValidationError('League ID is required'); } if (!props.driverId || props.driverId.trim().length === 0) { throw new RacingDomainValidationError('Driver ID is required'); } } equals(other: Entity): boolean { if (!(other instanceof JoinRequest)) { return false; } return this.id === other.id; } }