Uecko_ERP/src-1/contexts/billing/application/reject-factura/reject-factura.use-case.ts
2025-04-01 16:26:15 +02:00

73 lines
2.4 KiB
TypeScript

// filepath: /home/rodax/Documentos/uecko-erp/apps/server/src/contexts/billing/domain/aggregates/factura.ts
import { AggregateRoot, UniqueID, UTCDate } from "@common/domain";
import { Result } from "@common/helpers";
import { FacturaDraft } from "../events/factura-draft";
import { FacturaEmitted } from "../events/factura-emitted";
import { FacturaSent } from "../events/factura-sent";
import { FacturaRejected } from "../events/factura-rejected";
import { FacturaReference } from "../value-objects/factura-reference";
import { FacturaStatus } from "../value-objects/factura-status";
export interface IFacturaProps {
id: UniqueID;
referencia: FacturaReference;
lineasDetalle: any[]; // Aquí puedes definir un tipo más específico para las líneas de detalle
clienteOProveedor: string; // Puede ser un ID o un objeto más complejo
estado: FacturaStatus;
fecha: UTCDate;
}
export class Factura extends AggregateRoot<IFacturaProps> {
static create(props: IFacturaProps): Result<Factura, Error> {
const factura = new Factura(props);
factura.addDomainEvent(new FacturaDraft(factura.id));
return Result.ok(factura);
}
emit(): Result<void, Error> {
if (this.props.estado !== FacturaStatus.createDraft()) {
return Result.fail(new Error("La factura solo puede ser emitida en estado 'draft'."));
}
this.props.estado = FacturaStatus.createEmitted();
this.addDomainEvent(new FacturaEmitted(this.id));
return Result.ok();
}
send(): Result<void, Error> {
if (this.props.estado !== FacturaStatus.createEmitted()) {
return Result.fail(new Error("La factura solo puede ser enviada en estado 'emitted'."));
}
this.props.estado = FacturaStatus.createSent();
this.addDomainEvent(new FacturaSent(this.id));
return Result.ok();
}
reject(): Result<void, Error> {
if (this.props.estado !== FacturaStatus.createDraft()) {
return Result.fail(new Error("La factura solo puede ser rechazada en estado 'draft'."));
}
this.props.estado = FacturaStatus.createRejected();
this.addDomainEvent(new FacturaRejected(this.id));
return Result.ok();
}
get referencia(): FacturaReference {
return this.props.referencia;
}
get estado(): FacturaStatus {
return this.props.estado;
}
get fecha(): UTCDate {
return this.props.fecha;
}
get lineasDetalle(): any[] {
return this.props.lineasDetalle;
}
get clienteOProveedor(): string {
return this.props.clienteOProveedor;
}
}