driver to user id

This commit is contained in:
2026-01-07 13:26:31 +01:00
parent 0db80fa98d
commit 94d60527f4
27 changed files with 856 additions and 1170 deletions

View File

@@ -0,0 +1,98 @@
import { UserId } from '../value-objects/UserId';
export interface CompanyProps {
id: string;
name: string;
ownerUserId: UserId;
contactEmail: string | undefined;
createdAt: Date;
}
export class Company {
private readonly id: string;
private readonly name: string;
private readonly ownerUserId: UserId;
private readonly contactEmail: string | undefined;
private readonly createdAt: Date;
private constructor(props: CompanyProps) {
this.validateName(props.name);
this.id = props.id;
this.name = props.name;
this.ownerUserId = props.ownerUserId;
this.contactEmail = props.contactEmail;
this.createdAt = props.createdAt;
}
private validateName(name: string): void {
const trimmed = name?.trim();
if (!trimmed) {
throw new Error('Company name cannot be empty');
}
if (trimmed.length < 2) {
throw new Error('Company name must be at least 2 characters long');
}
if (trimmed.length > 100) {
throw new Error('Company name must be no more than 100 characters');
}
}
public static create(props: {
name: string;
ownerUserId: UserId;
contactEmail?: string;
}): Company {
return new Company({
id: this.generateId(),
name: props.name,
ownerUserId: props.ownerUserId,
contactEmail: props.contactEmail,
createdAt: new Date(),
});
}
public static rehydrate(props: {
id: string;
name: string;
ownerUserId: string;
contactEmail?: string;
createdAt: Date;
}): Company {
return new Company({
id: props.id,
name: props.name,
ownerUserId: UserId.fromString(props.ownerUserId),
contactEmail: props.contactEmail,
createdAt: props.createdAt,
});
}
private static generateId(): string {
// Simple UUID-like generation for now
return `comp_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
public getId(): string {
return this.id;
}
public getName(): string {
return this.name;
}
public getOwnerUserId(): UserId {
return this.ownerUserId;
}
public getContactEmail(): string | undefined {
return this.contactEmail;
}
public getCreatedAt(): Date {
return this.createdAt;
}
}

View File

@@ -12,6 +12,7 @@ export interface UserProps {
iracingCustomerId?: string;
primaryDriverId?: string;
avatarUrl?: string;
companyId?: string;
}
export class User {
@@ -22,6 +23,7 @@ export class User {
private iracingCustomerId: string | undefined;
private primaryDriverId: string | undefined;
private avatarUrl: string | undefined;
private companyId: string | undefined;
private constructor(props: UserProps) {
this.validateDisplayName(props.displayName);
@@ -33,6 +35,7 @@ export class User {
this.iracingCustomerId = props.iracingCustomerId;
this.primaryDriverId = props.primaryDriverId;
this.avatarUrl = props.avatarUrl;
this.companyId = props.companyId;
}
private validateDisplayName(displayName: string): void {
@@ -108,6 +111,7 @@ export class User {
iracingCustomerId?: string;
primaryDriverId?: string;
avatarUrl?: string;
companyId?: string;
}): User {
const email =
props.email !== undefined
@@ -128,6 +132,7 @@ export class User {
...(props.iracingCustomerId !== undefined ? { iracingCustomerId: props.iracingCustomerId } : {}),
...(props.primaryDriverId !== undefined ? { primaryDriverId: props.primaryDriverId } : {}),
...(props.avatarUrl !== undefined ? { avatarUrl: props.avatarUrl } : {}),
...(props.companyId !== undefined ? { companyId: props.companyId } : {}),
});
}
@@ -144,6 +149,7 @@ export class User {
...(stored.primaryDriverId !== undefined
? { primaryDriverId: stored.primaryDriverId }
: {}),
...(stored.companyId !== undefined ? { companyId: stored.companyId } : {}),
};
return new User(userProps);
@@ -177,6 +183,10 @@ export class User {
return this.avatarUrl;
}
public getCompanyId(): string | undefined {
return this.companyId;
}
/**
* Update display name - NOT ALLOWED after initial creation
* This method will always throw an error to enforce immutability
@@ -193,4 +203,13 @@ export class User {
// All users created through proper channels have immutable names
return true;
}
/**
* Set company ID (for linking user to company during sponsor signup)
* This is only allowed during initial creation via rehydrate/fromStored
* For runtime updates, a separate method would be needed
*/
public setCompanyId(companyId: string): void {
this.companyId = companyId;
}
}

View File

@@ -0,0 +1,33 @@
import { Company } from '../entities/Company';
/**
* Domain Repository: ICompanyRepository
*
* Repository interface for Company entity operations.
*/
export interface ICompanyRepository {
/**
* Create a new company (returns unsaved entity)
*/
create(company: Pick<Company, 'getName' | 'getOwnerUserId' | 'getContactEmail'>): Company;
/**
* Save a company to the database
*/
save(company: Company): Promise<void>;
/**
* Delete a company by ID
*/
delete(id: string): Promise<void>;
/**
* Find company by ID
*/
findById(id: string): Promise<Company | null>;
/**
* Find company by owner user ID
*/
findByOwnerUserId(userId: string): Promise<Company | null>;
}

View File

@@ -16,6 +16,7 @@ export interface StoredUser {
passwordHash: string;
salt?: string;
primaryDriverId?: string | undefined;
companyId?: string | undefined;
createdAt: Date;
}