67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import { DomainValidationError, ValueObject } from "@repo/rdx-ddd";
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
interface AccountStatusProps {
|
|
value: string;
|
|
}
|
|
|
|
export enum ACCOUNT_STATUS {
|
|
ACTIVE = "active",
|
|
DISABLED = "disabled",
|
|
PENDING_VERIFICATION = "pending_verification",
|
|
}
|
|
|
|
export class AccountStatus extends ValueObject<AccountStatusProps> {
|
|
private static readonly ALLOWED_STATUSES = [
|
|
ACCOUNT_STATUS.ACTIVE,
|
|
ACCOUNT_STATUS.DISABLED,
|
|
ACCOUNT_STATUS.PENDING_VERIFICATION,
|
|
] as const;
|
|
|
|
static create(value: string): Result<AccountStatus, Error> {
|
|
if (!AccountStatus.ALLOWED_STATUSES.includes(value as (typeof AccountStatus.ALLOWED_STATUSES)[number])) {
|
|
return Result.fail(
|
|
new DomainValidationError("INVALID_ACCOUNT_STATUS", "status", `Status value not valid: ${value}`)
|
|
);
|
|
}
|
|
|
|
return Result.ok(new AccountStatus({ value }));
|
|
}
|
|
|
|
static createActive(): AccountStatus {
|
|
return new AccountStatus({ value: ACCOUNT_STATUS.ACTIVE });
|
|
}
|
|
|
|
static createDisabled(): AccountStatus {
|
|
return new AccountStatus({ value: ACCOUNT_STATUS.DISABLED });
|
|
}
|
|
|
|
static createPendingVerification(): AccountStatus {
|
|
return new AccountStatus({ value: ACCOUNT_STATUS.PENDING_VERIFICATION });
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.props.value === ACCOUNT_STATUS.ACTIVE;
|
|
}
|
|
|
|
isDisabled(): boolean {
|
|
return this.props.value === ACCOUNT_STATUS.DISABLED;
|
|
}
|
|
|
|
isPendingVerification(): boolean {
|
|
return this.props.value === ACCOUNT_STATUS.PENDING_VERIFICATION;
|
|
}
|
|
|
|
getProps(): string {
|
|
return this.props.value;
|
|
}
|
|
|
|
toPrimitive(): string {
|
|
return this.getProps();
|
|
}
|
|
|
|
toString(): string {
|
|
return String(this.props.value);
|
|
}
|
|
}
|