60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { ValueObject } from "@/core/common/domain";
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
interface IAccountStatusProps {
|
|
value: string;
|
|
}
|
|
|
|
export enum ACCOUNT_STATUS {
|
|
INACTIVE = "inactive",
|
|
ACTIVE = "active",
|
|
}
|
|
|
|
export class AccountStatus extends ValueObject<IAccountStatusProps> {
|
|
private static readonly ALLOWED_STATUSES = ["inactive", "active"];
|
|
|
|
private static readonly TRANSITIONS: Record<string, string[]> = {
|
|
inactive: [ACCOUNT_STATUS.ACTIVE],
|
|
active: [ACCOUNT_STATUS.INACTIVE],
|
|
};
|
|
|
|
static create(value: string): Result<AccountStatus, Error> {
|
|
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<AccountStatus, Error> {
|
|
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();
|
|
}
|
|
}
|