import { AggregateRoot, CurrencyCode, EmailAddress, LanguageCode, Name, PhoneNumber, PostalAddress, PostalAddressPatchProps, TINNumber, TaxCode, TextValue, URLAddress, UniqueID, } from "@repo/rdx-ddd"; import { Collection, Maybe, Result } from "@repo/rdx-utils"; import { CustomerStatus } from "../value-objects"; export interface CustomerProps { companyId: UniqueID; status: CustomerStatus; reference: Maybe; isCompany: boolean; name: Name; tradeName: Maybe; tin: Maybe; address: PostalAddress; emailPrimary: Maybe; emailSecondary: Maybe; phonePrimary: Maybe; phoneSecondary: Maybe; mobilePrimary: Maybe; mobileSecondary: Maybe; fax: Maybe; website: Maybe; legalRecord: Maybe; defaultTaxes: Collection; languageCode: LanguageCode; currencyCode: CurrencyCode; } export type CustomerPatchProps = Partial> & { address?: PostalAddressPatchProps; }; export class Customer extends AggregateRoot { static create(props: CustomerProps, id?: UniqueID): Result { 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); } public update(partialCustomer: CustomerPatchProps): Result { const { address: partialAddress, ...rest } = partialCustomer; const updatedProps = { ...this.props, ...rest, } as CustomerProps; if (partialAddress) { const updatedAddressOrError = this.address.update(partialAddress); if (updatedAddressOrError.isFailure) { return Result.fail(updatedAddressOrError.error); } updatedProps.address = updatedAddressOrError.data; } return Customer.create(updatedProps, this.id); } public get isIndividual(): boolean { return !this.props.isCompany; } public get isCompany(): boolean { return this.props.isCompany; } public get isActive(): boolean { return this.props.status.isActive(); } public get companyId(): UniqueID { return this.props.companyId; } public get reference(): Maybe { return this.props.reference; } public get name(): Name { return this.props.name; } public get tradeName(): Maybe { return this.props.tradeName; } public get tin(): Maybe { return this.props.tin; } public get address(): PostalAddress { return this.props.address; } public get emailPrimary(): Maybe { return this.props.emailPrimary; } public get emailSecondary(): Maybe { return this.props.emailSecondary; } public get phonePrimary(): Maybe { return this.props.phonePrimary; } public get phoneSecondary(): Maybe { return this.props.phoneSecondary; } public get mobilePrimary(): Maybe { return this.props.mobilePrimary; } public get mobileSecondary(): Maybe { return this.props.mobileSecondary; } public get fax(): Maybe { return this.props.fax; } public get website(): Maybe { return this.props.website; } public get legalRecord(): Maybe { return this.props.legalRecord; } public get defaultTaxes(): Collection { return this.props.defaultTaxes; } public get languageCode(): LanguageCode { return this.props.languageCode; } public get currencyCode(): CurrencyCode { return this.props.currencyCode; } }