import { ValueObject } from "core/common/domain"; import { Result } from "core/common/helpers"; interface IAccountStatusProps { value: string; } export enum ACCOUNT_STATUS { INACTIVE = "inactive", ACTIVE = "active", } export class AccountStatus extends ValueObject { private static readonly ALLOWED_STATUSES = ["inactive", "active"]; private static readonly TRANSITIONS: Record = { inactive: [ACCOUNT_STATUS.ACTIVE], active: [ACCOUNT_STATUS.INACTIVE], }; static create(value: string): Result { if (!this.ALLOWED_STATUSES.includes(value)) { return Result.fail(new Error(`Estado de la cuenta no válido: ${value}`)); } return Result.ok( value === "active" ? AccountStatus.createActive() : AccountStatus.createInactive() ); } public static createInactive(): AccountStatus { return new AccountStatus({ value: ACCOUNT_STATUS.INACTIVE }); } public static createActive(): AccountStatus { return new AccountStatus({ value: ACCOUNT_STATUS.ACTIVE }); } getValue(): string { return this.props.value; } canTransitionTo(nextStatus: string): boolean { return AccountStatus.TRANSITIONS[this.props.value].includes(nextStatus); } transitionTo(nextStatus: string): Result { if (!this.canTransitionTo(nextStatus)) { return Result.fail( new Error(`Transición no permitida de ${this.props.value} a ${nextStatus}`) ); } return AccountStatus.create(nextStatus); } toPrimitive(): string { return this.getValue(); } }