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

102 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-06-24 18:38:57 +00:00
import { DomainValidationError } from "@erp/core/api";
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 {
2025-09-09 15:48:12 +00:00
DRAFT = "draft", // <- Proforma
SENT = "sent", // <- Proforma
APPROVED = "approved", // <- Proforma
REJECTED = "rejected", // <- Proforma
EMITTED = "emitted", // <- Factura y enviada a Veri*Factu
2025-03-18 08:05:00 +00:00
}
2025-06-11 15:13:44 +00:00
export class CustomerInvoiceStatus extends ValueObject<ICustomerInvoiceStatusProps> {
2025-09-09 15:48:12 +00:00
private static readonly ALLOWED_STATUSES = ["draft", "sent", "approved", "rejected", "emitted"];
2025-06-24 18:38:57 +00:00
private static readonly FIELD = "invoiceStatus";
private static readonly ERROR_CODE = "INVALID_INVOICE_STATUS";
2025-03-18 08:05:00 +00:00
private static readonly TRANSITIONS: Record<string, string[]> = {
2025-09-09 15:48:12 +00:00
draft: [INVOICE_STATUS.SENT],
sent: [INVOICE_STATUS.APPROVED, INVOICE_STATUS.REJECTED],
approved: [INVOICE_STATUS.EMITTED],
rejected: [INVOICE_STATUS.DRAFT],
2025-03-18 08:05:00 +00:00
};
2025-06-11 15:13:44 +00:00
static create(value: string): Result<CustomerInvoiceStatus, Error> {
if (!CustomerInvoiceStatus.ALLOWED_STATUSES.includes(value)) {
2025-06-24 18:38:57 +00:00
const detail = `Estado de la factura no válido: ${value}`;
return Result.fail(
new DomainValidationError(
CustomerInvoiceStatus.ERROR_CODE,
CustomerInvoiceStatus.FIELD,
detail
)
);
2025-03-18 08:05:00 +00:00
}
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-09-09 15:48:12 +00:00
? CustomerInvoiceStatus.createEmitted()
: value === "approved"
? CustomerInvoiceStatus.createApproved()
2025-07-02 08:57:09 +00:00
: 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-09-09 15:48:12 +00:00
public static createApproved(): CustomerInvoiceStatus {
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.APPROVED });
2025-07-02 08:57:09 +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
}
2025-09-08 17:24:38 +00:00
isDraft(): boolean {
2025-09-09 15:48:12 +00:00
return this.props.value === INVOICE_STATUS.DRAFT;
2025-09-08 17:24:38 +00:00
}
2025-09-04 10:02:24 +00:00
getProps(): string {
2025-03-18 08:05:00 +00:00
return this.props.value;
}
2025-04-01 14:26:15 +00:00
toPrimitive() {
2025-09-04 10:02:24 +00:00
return this.getProps();
2025-04-01 14:26:15 +00:00
}
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 {
2025-09-04 10:02:24 +00:00
return this.getProps();
2025-03-18 08:05:00 +00:00
}
}