Files
gridpilot.gg/core/admin/domain/value-objects/UserRole.ts
2026-01-16 16:46:57 +01:00

74 lines
1.7 KiB
TypeScript

import { ValueObject } from '@core/shared/domain/ValueObject';
import { AdminDomainValidationError } from '../errors/AdminDomainError';
export type UserRoleValue = string;
export interface UserRoleProps {
value: UserRoleValue;
}
export class UserRole implements ValueObject<UserRoleProps> {
readonly value: UserRoleValue;
private constructor(value: UserRoleValue) {
this.value = value;
}
static create(value: UserRoleValue): UserRole {
// Handle null/undefined
if (value === null || value === undefined) {
throw new AdminDomainValidationError('Role cannot be empty');
}
const trimmed = value.trim();
if (!trimmed) {
throw new AdminDomainValidationError('Role cannot be empty');
}
return new UserRole(trimmed);
}
static fromString(value: string): UserRole {
return this.create(value);
}
get props(): UserRoleProps {
return { value: this.value };
}
toString(): UserRoleValue {
return this.value;
}
equals(other: ValueObject<UserRoleProps>): boolean {
return this.value === other.props.value;
}
/**
* Check if this role is a system administrator role
*/
isSystemAdmin(): boolean {
const lower = this.value.toLowerCase();
return lower === 'owner' || lower === 'admin';
}
/**
* Check if this role has higher authority than another role
*/
hasHigherAuthorityThan(other: UserRole): boolean {
const hierarchy: Record<string, number> = {
user: 0,
admin: 1,
owner: 2,
};
const myValue = this.value.toLowerCase();
const otherValue = other.value.toLowerCase();
const myRank = hierarchy[myValue] ?? 0;
const otherRank = hierarchy[otherValue] ?? 0;
return myRank > otherRank;
}
}