2025-08-11 17:49:52 +00:00
|
|
|
import {
|
|
|
|
|
AggregateRoot,
|
2025-11-17 17:20:18 +00:00
|
|
|
type CurrencyCode,
|
|
|
|
|
type EmailAddress,
|
|
|
|
|
type LanguageCode,
|
|
|
|
|
type Name,
|
|
|
|
|
type PhoneNumber,
|
|
|
|
|
type PostalAddress,
|
|
|
|
|
type PostalAddressPatchProps,
|
|
|
|
|
type TINNumber,
|
|
|
|
|
type TaxCode,
|
|
|
|
|
type TextValue,
|
|
|
|
|
type URLAddress,
|
|
|
|
|
type UniqueID,
|
2025-08-11 17:49:52 +00:00
|
|
|
} from "@repo/rdx-ddd";
|
2025-11-17 17:20:18 +00:00
|
|
|
import { type Collection, type Maybe, Result } from "@repo/rdx-utils";
|
|
|
|
|
|
|
|
|
|
import type { 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-09-01 14:07:59 +00:00
|
|
|
reference: Maybe<Name>;
|
2025-08-26 18:55:59 +00:00
|
|
|
|
2025-08-25 17:42:56 +00:00
|
|
|
isCompany: boolean;
|
2025-09-01 14:07:59 +00:00
|
|
|
name: Name;
|
|
|
|
|
tradeName: Maybe<Name>;
|
|
|
|
|
tin: Maybe<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-09-16 09:17:29 +00:00
|
|
|
emailPrimary: Maybe<EmailAddress>;
|
|
|
|
|
emailSecondary: Maybe<EmailAddress>;
|
2025-09-26 18:09:14 +00:00
|
|
|
|
2025-09-16 09:17:29 +00:00
|
|
|
phonePrimary: Maybe<PhoneNumber>;
|
|
|
|
|
phoneSecondary: Maybe<PhoneNumber>;
|
2025-09-26 18:09:14 +00:00
|
|
|
|
2025-09-16 09:17:29 +00:00
|
|
|
mobilePrimary: Maybe<PhoneNumber>;
|
|
|
|
|
mobileSecondary: Maybe<PhoneNumber>;
|
2025-09-26 18:09:14 +00:00
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
fax: Maybe<PhoneNumber>;
|
|
|
|
|
website: Maybe<URLAddress>;
|
2025-08-26 18:55:59 +00:00
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
legalRecord: Maybe<TextValue>;
|
2025-09-26 18:09:14 +00:00
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
defaultTaxes: Collection<TaxCode>;
|
2025-08-26 18:55:59 +00:00
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
languageCode: LanguageCode;
|
|
|
|
|
currencyCode: CurrencyCode;
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
export type CustomerPatchProps = Partial<Omit<CustomerProps, "companyId" | "address">> & {
|
|
|
|
|
address?: PostalAddressPatchProps;
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-27 11:52:30 +00:00
|
|
|
// Customer
|
|
|
|
|
export interface ICustomer {
|
|
|
|
|
// comportamiento
|
|
|
|
|
update(partialCustomer: CustomerPatchProps): Result<Customer, Error>;
|
|
|
|
|
|
|
|
|
|
// propiedades (getters)
|
|
|
|
|
readonly isIndividual: boolean;
|
|
|
|
|
readonly isCompany: boolean;
|
|
|
|
|
readonly isActive: boolean;
|
|
|
|
|
|
|
|
|
|
readonly companyId: UniqueID;
|
|
|
|
|
|
|
|
|
|
readonly reference: Maybe<Name>;
|
|
|
|
|
readonly name: Name;
|
|
|
|
|
readonly tradeName: Maybe<Name>;
|
|
|
|
|
readonly tin: Maybe<TINNumber>;
|
|
|
|
|
|
|
|
|
|
readonly address: PostalAddress;
|
|
|
|
|
|
|
|
|
|
readonly emailPrimary: Maybe<EmailAddress>;
|
|
|
|
|
readonly emailSecondary: Maybe<EmailAddress>;
|
|
|
|
|
|
|
|
|
|
readonly phonePrimary: Maybe<PhoneNumber>;
|
|
|
|
|
readonly phoneSecondary: Maybe<PhoneNumber>;
|
|
|
|
|
readonly mobilePrimary: Maybe<PhoneNumber>;
|
|
|
|
|
readonly mobileSecondary: Maybe<PhoneNumber>;
|
|
|
|
|
readonly fax: Maybe<PhoneNumber>;
|
|
|
|
|
|
|
|
|
|
readonly website: Maybe<URLAddress>;
|
|
|
|
|
readonly legalRecord: Maybe<TextValue>;
|
|
|
|
|
|
|
|
|
|
readonly defaultTaxes: Collection<TaxCode>;
|
|
|
|
|
|
|
|
|
|
readonly languageCode: LanguageCode;
|
|
|
|
|
readonly currencyCode: CurrencyCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Customer extends AggregateRoot<CustomerProps> implements ICustomer {
|
2025-08-11 17:49:52 +00:00
|
|
|
static create(props: CustomerProps, id?: UniqueID): Result<Customer, Error> {
|
|
|
|
|
const contact = new Customer(props, id);
|
|
|
|
|
|
|
|
|
|
// Reglas de negocio / validaciones
|
|
|
|
|
// ...
|
|
|
|
|
// ...
|
|
|
|
|
|
2025-09-27 11:52:30 +00:00
|
|
|
// Disparar eventos de dominio
|
|
|
|
|
// ...
|
|
|
|
|
// ...
|
2025-08-11 17:49:52 +00:00
|
|
|
|
|
|
|
|
return Result.ok(contact);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 10:55:45 +00:00
|
|
|
public update(partialCustomer: CustomerPatchProps): Result<Customer, Error> {
|
|
|
|
|
const { address: partialAddress, ...rest } = partialCustomer;
|
2025-09-01 14:07:59 +00:00
|
|
|
const updatedProps = {
|
|
|
|
|
...this.props,
|
2025-09-02 10:55:45 +00:00
|
|
|
...rest,
|
2025-09-01 14:07:59 +00:00
|
|
|
} as CustomerProps;
|
|
|
|
|
|
2025-09-02 10:55:45 +00:00
|
|
|
if (partialAddress) {
|
|
|
|
|
const updatedAddressOrError = this.address.update(partialAddress);
|
2025-09-01 14:07:59 +00:00
|
|
|
if (updatedAddressOrError.isFailure) {
|
|
|
|
|
return Result.fail(updatedAddressOrError.error);
|
|
|
|
|
}
|
2025-09-02 10:55:45 +00:00
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
updatedProps.address = updatedAddressOrError.data;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 10:55:45 +00:00
|
|
|
return Customer.create(updatedProps, this.id);
|
2025-09-01 14:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2025-08-25 17:42:56 +00:00
|
|
|
return this.props.companyId;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get reference(): Maybe<Name> {
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.props.reference;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get name(): Name {
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.props.name;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get tradeName(): Maybe<Name> {
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.props.tradeName;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get tin(): Maybe<TINNumber> {
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.props.tin;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get address(): PostalAddress {
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.props.address;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 09:17:29 +00:00
|
|
|
public get emailPrimary(): Maybe<EmailAddress> {
|
|
|
|
|
return this.props.emailPrimary;
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-16 09:17:29 +00:00
|
|
|
public get emailSecondary(): Maybe<EmailAddress> {
|
|
|
|
|
return this.props.emailSecondary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get phonePrimary(): Maybe<PhoneNumber> {
|
|
|
|
|
return this.props.phonePrimary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get phoneSecondary(): Maybe<PhoneNumber> {
|
|
|
|
|
return this.props.phoneSecondary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get mobilePrimary(): Maybe<PhoneNumber> {
|
|
|
|
|
return this.props.mobilePrimary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get mobileSecondary(): Maybe<PhoneNumber> {
|
|
|
|
|
return this.props.mobileSecondary;
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get fax(): Maybe<PhoneNumber> {
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.props.fax;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get website(): Maybe<URLAddress> {
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.props.website;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get legalRecord(): Maybe<TextValue> {
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.props.legalRecord;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get defaultTaxes(): Collection<TaxCode> {
|
|
|
|
|
return this.props.defaultTaxes;
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get languageCode(): LanguageCode {
|
|
|
|
|
return this.props.languageCode;
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
public get currencyCode(): CurrencyCode {
|
2025-08-11 17:49:52 +00:00
|
|
|
return this.props.currencyCode;
|
|
|
|
|
}
|
|
|
|
|
}
|