FactuGES_ERP/modules/customers/src/api/domain/aggregates/customer.ts

151 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-08-11 17:49:52 +00:00
import {
AggregateRoot,
EmailAddress,
PhoneNumber,
PostalAddress,
TINNumber,
UniqueID,
} from "@repo/rdx-ddd";
2025-08-26 18:55:59 +00:00
import { Result } from "@repo/rdx-utils";
import { CustomerStatus } from "../value-objects";
2025-08-11 17:49:52 +00:00
export interface CustomerProps {
2025-08-25 17:42:56 +00:00
companyId: UniqueID;
2025-08-26 18:55:59 +00:00
status: CustomerStatus;
2025-08-11 17:49:52 +00:00
reference: string;
2025-08-26 18:55:59 +00:00
2025-08-25 17:42:56 +00:00
isCompany: boolean;
2025-08-11 17:49:52 +00:00
name: string;
2025-08-26 18:55:59 +00:00
tradeName: string;
2025-08-11 17:49:52 +00:00
tin: TINNumber;
2025-08-26 18:55:59 +00:00
2025-08-11 17:49:52 +00:00
address: PostalAddress;
2025-08-26 18:55:59 +00:00
2025-08-11 17:49:52 +00:00
email: EmailAddress;
phone: PhoneNumber;
2025-08-26 18:55:59 +00:00
fax: PhoneNumber;
website: string;
2025-08-11 17:49:52 +00:00
legalRecord: string;
2025-08-26 18:55:59 +00:00
defaultTax: string[];
2025-08-11 17:49:52 +00:00
langCode: string;
currencyCode: string;
}
export interface ICustomer {
id: UniqueID;
2025-08-25 17:42:56 +00:00
companyId: UniqueID;
2025-08-11 17:49:52 +00:00
reference: string;
2025-08-26 18:55:59 +00:00
2025-08-11 17:49:52 +00:00
tin: TINNumber;
2025-08-26 18:55:59 +00:00
name: string;
tradeName: string;
2025-08-11 17:49:52 +00:00
address: PostalAddress;
2025-08-26 18:55:59 +00:00
2025-08-11 17:49:52 +00:00
email: EmailAddress;
phone: PhoneNumber;
2025-08-26 18:55:59 +00:00
fax: PhoneNumber;
website: string;
2025-08-11 17:49:52 +00:00
legalRecord: string;
2025-08-26 18:55:59 +00:00
defaultTax: string[];
2025-08-11 17:49:52 +00:00
langCode: string;
currencyCode: string;
2025-08-25 17:42:56 +00:00
isIndividual: boolean;
isCompany: boolean;
2025-08-11 17:49:52 +00:00
isActive: boolean;
}
export class Customer extends AggregateRoot<CustomerProps> implements ICustomer {
static create(props: CustomerProps, id?: UniqueID): Result<Customer, Error> {
const contact = new Customer(props, id);
// Reglas de negocio / validaciones
// ...
// ...
// 🔹 Disparar evento de dominio "CustomerAuthenticatedEvent"
//const { contact } = props;
//user.addDomainEvent(new CustomerAuthenticatedEvent(id, contact.toString()));
return Result.ok(contact);
}
2025-08-25 17:42:56 +00:00
update(partial: Partial<Omit<CustomerProps, "companyId">>): Result<Customer, Error> {
const updatedCustomer = new Customer({ ...this.props, ...partial }, this.id);
return Result.ok(updatedCustomer);
}
get companyId(): UniqueID {
return this.props.companyId;
}
2025-08-11 17:49:52 +00:00
get reference() {
return this.props.reference;
}
get name() {
return this.props.name;
}
get tradeName() {
return this.props.tradeName;
}
get tin(): TINNumber {
return this.props.tin;
}
get address(): PostalAddress {
return this.props.address;
}
get email(): EmailAddress {
return this.props.email;
}
get phone(): PhoneNumber {
return this.props.phone;
}
2025-08-26 18:55:59 +00:00
get fax(): PhoneNumber {
2025-08-11 17:49:52 +00:00
return this.props.fax;
}
2025-08-26 18:55:59 +00:00
get website(): string {
2025-08-11 17:49:52 +00:00
return this.props.website;
}
get legalRecord() {
return this.props.legalRecord;
}
get defaultTax() {
return this.props.defaultTax;
}
get langCode() {
return this.props.langCode;
}
get currencyCode() {
return this.props.currencyCode;
}
2025-08-25 17:42:56 +00:00
get isIndividual(): boolean {
return !this.props.isCompany;
2025-08-11 17:49:52 +00:00
}
2025-08-25 17:42:56 +00:00
get isCompany(): boolean {
return this.props.isCompany;
2025-08-11 17:49:52 +00:00
}
get isActive(): boolean {
2025-08-26 18:55:59 +00:00
return this.props.status.isActive();
2025-08-11 17:49:52 +00:00
}
}