106 lines
3.2 KiB
TypeScript
106 lines
3.2 KiB
TypeScript
import { DomainValidationError } from "@erp/core/api";
|
|
import { ValueObject } from "@repo/rdx-ddd";
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
interface ICustomerInvoiceStatusProps {
|
|
value: string;
|
|
}
|
|
|
|
export enum INVOICE_STATUS {
|
|
DRAFT = "draft", // <- Proforma
|
|
SENT = "sent", // <- Proforma
|
|
APPROVED = "approved", // <- Proforma
|
|
REJECTED = "rejected", // <- Proforma
|
|
EMITTED = "emitted", // <- Factura y enviada a Veri*Factu
|
|
}
|
|
export class CustomerInvoiceStatus extends ValueObject<ICustomerInvoiceStatusProps> {
|
|
private static readonly ALLOWED_STATUSES = ["draft", "sent", "approved", "rejected", "emitted"];
|
|
private static readonly FIELD = "invoiceStatus";
|
|
private static readonly ERROR_CODE = "INVALID_INVOICE_STATUS";
|
|
|
|
private static readonly TRANSITIONS: Record<string, string[]> = {
|
|
draft: [INVOICE_STATUS.SENT],
|
|
sent: [INVOICE_STATUS.APPROVED, INVOICE_STATUS.REJECTED],
|
|
approved: [INVOICE_STATUS.EMITTED],
|
|
rejected: [INVOICE_STATUS.DRAFT],
|
|
};
|
|
|
|
static create(value: string): Result<CustomerInvoiceStatus, Error> {
|
|
if (!CustomerInvoiceStatus.ALLOWED_STATUSES.includes(value)) {
|
|
const detail = `Estado de la factura no válido: ${value}`;
|
|
return Result.fail(
|
|
new DomainValidationError(
|
|
CustomerInvoiceStatus.ERROR_CODE,
|
|
CustomerInvoiceStatus.FIELD,
|
|
detail
|
|
)
|
|
);
|
|
}
|
|
|
|
return Result.ok(
|
|
value === "rejected"
|
|
? CustomerInvoiceStatus.createRejected()
|
|
: value === "sent"
|
|
? CustomerInvoiceStatus.createSent()
|
|
: value === "emitted"
|
|
? CustomerInvoiceStatus.createEmitted()
|
|
: value === "approved"
|
|
? CustomerInvoiceStatus.createApproved()
|
|
: CustomerInvoiceStatus.createDraft()
|
|
);
|
|
}
|
|
|
|
public static createDraft(): CustomerInvoiceStatus {
|
|
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.DRAFT });
|
|
}
|
|
|
|
public static createEmitted(): CustomerInvoiceStatus {
|
|
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.EMITTED });
|
|
}
|
|
|
|
public static createSent(): CustomerInvoiceStatus {
|
|
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.SENT });
|
|
}
|
|
|
|
public static createApproved(): CustomerInvoiceStatus {
|
|
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.APPROVED });
|
|
}
|
|
|
|
public static createRejected(): CustomerInvoiceStatus {
|
|
return new CustomerInvoiceStatus({ value: INVOICE_STATUS.REJECTED });
|
|
}
|
|
|
|
isDraft(): boolean {
|
|
return this.props.value === INVOICE_STATUS.DRAFT;
|
|
}
|
|
|
|
isApproved(): boolean {
|
|
return this.props.value === INVOICE_STATUS.APPROVED;
|
|
}
|
|
|
|
getProps(): string {
|
|
return this.props.value;
|
|
}
|
|
|
|
toPrimitive() {
|
|
return this.getProps();
|
|
}
|
|
|
|
canTransitionTo(nextStatus: string): boolean {
|
|
return CustomerInvoiceStatus.TRANSITIONS[this.props.value].includes(nextStatus);
|
|
}
|
|
|
|
transitionTo(nextStatus: string): Result<CustomerInvoiceStatus, Error> {
|
|
if (!this.canTransitionTo(nextStatus)) {
|
|
return Result.fail(
|
|
new Error(`Transición no permitida de ${this.props.value} a ${nextStatus}`)
|
|
);
|
|
}
|
|
return CustomerInvoiceStatus.create(nextStatus);
|
|
}
|
|
|
|
toString() {
|
|
return String(this.props.value);
|
|
}
|
|
}
|