Uecko_ERP/src-1/contexts/billing/infrastructure/sequelize/index.ts
2025-04-01 16:26:15 +02:00

63 lines
2.1 KiB
TypeScript

// 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<IFacturaProps> {
static create(props: IFacturaProps): Result<Factura, Error> {
const factura = new Factura(props, props.id);
factura.addDomainEvent(new FacturaDraft(props.id));
return Result.ok(factura);
}
modify(newProps: Partial<IFacturaProps>): Result<void, Error> {
if (this.props.estado !== FacturaStatus.createDraft()) {
return Result.fail(new Error("La factura solo se puede modificar en estado 'draft'."));
}
Object.assign(this.props, newProps);
}
emit(): Result<void, Error> {
if (this.props.estado !== FacturaStatus.createDraft()) {
return Result.fail(new Error("La factura solo se puede emitir en estado 'draft'."));
}
this.props.estado = FacturaStatus.createEmitted();
this.addDomainEvent(new FacturaEmitted(this.props.id));
}
send(): Result<void, Error> {
this.props.estado = FacturaStatus.createSent();
this.addDomainEvent(new FacturaSent(this.props.id));
}
reject(): Result<void, Error> {
this.props.estado = FacturaStatus.createRejected();
this.addDomainEvent(new FacturaRejected(this.props.id));
}
get referencia(): FacturaReference {
return this.props.referencia;
}
get estado(): FacturaStatus {
return this.props.estado;
}
get fecha(): UTCDate {
return this.props.fecha;
}
}