// filepath: 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 { static create(props: IFacturaProps): Result { const factura = new Factura(props, props.id); factura.addDomainEvent(new FacturaDraft(props.id)); return Result.ok(factura); } modify(): Result { if (this.props.estado !== FacturaStatus.createDraft()) { return Result.fail(new Error("La factura solo se puede modificar en estado 'draft'.")); } // Lógica para modificar la factura return Result.ok(); } delete(): Result { if (this.props.estado !== FacturaStatus.createDraft()) { return Result.fail(new Error("La factura solo se puede eliminar en estado 'draft'.")); } // Lógica para eliminar la factura return Result.ok(); } emit(): Result { this.props.estado = FacturaStatus.createEmitted(); this.addDomainEvent(new FacturaEmitted(this.props.id)); return Result.ok(); } send(): Result { this.props.estado = FacturaStatus.createSent(); this.addDomainEvent(new FacturaSent(this.props.id)); return Result.ok(); } reject(): Result { this.props.estado = FacturaStatus.createRejected(); this.addDomainEvent(new FacturaRejected(this.props.id)); return Result.ok(); } }