Uecko_ERP/apps/server/src/contexts/accounts/domain/value-objects/account-status.ts

60 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-04-22 15:09:57 +00:00
import { ValueObject } from "core/common/domain";
import { Result } from "core/common/helpers";
2025-03-04 17:08:33 +00:00
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);
}
2025-04-01 14:26:15 +00:00
toPrimitive(): string {
2025-03-04 17:08:33 +00:00
return this.getValue();
}
}