68 lines
2.2 KiB
TypeScript
68 lines
2.2 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 si es necesario
|
|
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);
|
|
|
|
// Emitir evento de dominio "FacturaDraft"
|
|
factura.addDomainEvent(new FacturaDraft(factura.id, props.referencia));
|
|
|
|
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.id, this.props.referencia));
|
|
}
|
|
|
|
send(): Result<void, Error> {
|
|
this.props.estado = FacturaStatus.createSent();
|
|
this.addDomainEvent(new FacturaSent(this.id, this.props.referencia));
|
|
}
|
|
|
|
reject(): Result<void, Error> {
|
|
this.props.estado = FacturaStatus.createRejected();
|
|
this.addDomainEvent(new FacturaRejected(this.id, this.props.referencia));
|
|
}
|
|
|
|
get referencia(): FacturaReference {
|
|
return this.props.referencia;
|
|
}
|
|
|
|
get estado(): FacturaStatus {
|
|
return this.props.estado;
|
|
}
|
|
|
|
get fecha(): UTCDate {
|
|
return this.props.fecha;
|
|
}
|
|
} |