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

149 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-08-11 17:49:52 +00:00
import {
AggregateRoot,
2025-09-01 14:07:59 +00:00
CurrencyCode,
2025-08-11 17:49:52 +00:00
EmailAddress,
2025-09-01 14:07:59 +00:00
LanguageCode,
Name,
2025-08-11 17:49:52 +00:00
PhoneNumber,
PostalAddress,
2025-09-01 14:07:59 +00:00
PostalAddressPatchProps,
2025-08-11 17:49:52 +00:00
TINNumber,
2025-09-01 14:07:59 +00:00
TaxCode,
TextValue,
URLAddress,
2025-08-11 17:49:52 +00:00
UniqueID,
} from "@repo/rdx-ddd";
2025-09-01 14:07:59 +00:00
import { Collection, Maybe, Result } from "@repo/rdx-utils";
2025-08-26 18:55:59 +00:00
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-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-01 14:07:59 +00:00
email: Maybe<EmailAddress>;
phone: Maybe<PhoneNumber>;
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>;
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;
};
export class Customer extends AggregateRoot<CustomerProps> {
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
// ...
// ...
// 🔹 Disparar evento de dominio "CustomerAuthenticatedEvent"
//const { contact } = props;
//user.addDomainEvent(new CustomerAuthenticatedEvent(id, contact.toString()));
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-01 14:07:59 +00:00
public get email(): Maybe<EmailAddress> {
2025-08-11 17:49:52 +00:00
return this.props.email;
}
2025-09-01 14:07:59 +00:00
public get phone(): Maybe<PhoneNumber> {
2025-08-11 17:49:52 +00:00
return this.props.phone;
}
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;
}
}