Uecko_ERP/modules/customer-invoices/src/api/domain/value-objects/customer-invoice-status.ts

81 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-05-20 10:08:24 +00:00
import { ValueObject } from "@repo/rdx-ddd";
2025-05-09 10:45:32 +00:00
import { Result } from "@repo/rdx-utils";
2025-03-18 08:05:00 +00:00
2025-06-11 15:13:44 +00:00
interface ICustomerInvoiceStatusProps {
2025-03-18 08:05:00 +00:00
value: string;
}
export enum INVOICE_STATUS {
DRAFT = "draft",
EMITTED = "emitted",
SENT = "sent",
REJECTED = "rejected",
}
2025-06-11 15:13:44 +00:00
export class CustomerInvoiceStatus extends ValueObject<ICustomerInvoiceStatusProps> {
2025-03-18 08:05:00 +00:00
private static readonly ALLOWED_STATUSES = ["draft", "emitted", "sent", "rejected"];
private static readonly TRANSITIONS: Record<string, string[]> = {
draft: [INVOICE_STATUS.EMITTED],
emitted: [INVOICE_STATUS.SENT, INVOICE_STATUS.REJECTED, INVOICE_STATUS.DRAFT],
sent: [INVOICE_STATUS.REJECTED],
rejected: [],
};
2025-06-11 15:13:44 +00:00
static create(value: string): Result<CustomerInvoiceStatus, Error> {
if (!CustomerInvoiceStatus.ALLOWED_STATUSES.includes(value)) {
2025-03-18 08:05:00 +00:00
return Result.fail(new Error(`Estado de la factura no válido: ${value}`));
}
return Result.ok(
value === "rejected"
2025-06-11 15:13:44 +00:00
? CustomerInvoiceStatus.createRejected()
2025-03-18 08:05:00 +00:00
: value === "sent"
2025-06-11 15:13:44 +00:00
? CustomerInvoiceStatus.createSent()
2025-03-18 08:05:00 +00:00
: value === "emitted"
2025-06-11 15:13:44 +00:00
? CustomerInvoiceStatus.createSent()
: CustomerInvoiceStatus.createDraft()
2025-03-18 08:05:00 +00:00
);
}
2025-06-11 15:13:44 +00:00
public static createDraft(): CustomerInvoiceStatus {
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.DRAFT });
2025-03-18 08:05:00 +00:00
}
2025-06-11 15:13:44 +00:00
public static createEmitted(): CustomerInvoiceStatus {
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.EMITTED });
2025-03-18 08:05:00 +00:00
}
2025-06-11 15:13:44 +00:00
public static createSent(): CustomerInvoiceStatus {
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.SENT });
2025-03-18 08:05:00 +00:00
}
2025-06-11 15:13:44 +00:00
public static createRejected(): CustomerInvoiceStatus {
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.REJECTED });
2025-03-18 08:05:00 +00:00
}
getValue(): string {
return this.props.value;
}
2025-04-01 14:26:15 +00:00
toPrimitive() {
return this.getValue();
}
2025-03-18 08:05:00 +00:00
canTransitionTo(nextStatus: string): boolean {
2025-06-11 15:13:44 +00:00
return CustomerInvoiceStatus.TRANSITIONS[this.props.value].includes(nextStatus);
2025-03-18 08:05:00 +00:00
}
2025-06-11 15:13:44 +00:00
transitionTo(nextStatus: string): Result<CustomerInvoiceStatus, Error> {
2025-03-18 08:05:00 +00:00
if (!this.canTransitionTo(nextStatus)) {
return Result.fail(
new Error(`Transición no permitida de ${this.props.value} a ${nextStatus}`)
);
}
2025-06-11 15:13:44 +00:00
return CustomerInvoiceStatus.create(nextStatus);
2025-03-18 08:05:00 +00:00
}
toString(): string {
return this.getValue();
}
}