Files
gridpilot.gg/packages/identity/domain/entities/User.ts
2025-12-12 14:23:40 +01:00

73 lines
1.8 KiB
TypeScript

import type { EmailValidationResult } from '../types/EmailAddress';
import { validateEmail } from '../types/EmailAddress';
import { UserId } from '../value-objects/UserId';
export interface UserProps {
id: UserId;
displayName: string;
email?: string;
iracingCustomerId?: string;
primaryDriverId?: string;
avatarUrl?: string;
}
export class User {
private readonly id: UserId;
private displayName: string;
private email: string | undefined;
private iracingCustomerId: string | undefined;
private primaryDriverId: string | undefined;
private avatarUrl: string | undefined;
private constructor(props: UserProps) {
if (!props.displayName || !props.displayName.trim()) {
throw new Error('User displayName cannot be empty');
}
this.id = props.id;
this.displayName = props.displayName.trim();
this.email = props.email;
this.iracingCustomerId = props.iracingCustomerId;
this.primaryDriverId = props.primaryDriverId;
this.avatarUrl = props.avatarUrl;
}
public static create(props: UserProps): User {
if (props.email) {
const result: EmailValidationResult = validateEmail(props.email);
if (!result.success) {
throw new Error(result.error);
}
return new User({
...props,
email: result.email,
});
}
return new User(props);
}
public getId(): UserId {
return this.id;
}
public getDisplayName(): string {
return this.displayName;
}
public getEmail(): string | undefined {
return this.email;
}
public getIracingCustomerId(): string | undefined {
return this.iracingCustomerId;
}
public getPrimaryDriverId(): string | undefined {
return this.primaryDriverId;
}
public getAvatarUrl(): string | undefined {
return this.avatarUrl;
}
}