Uecko_ERP/modules/supplier/src/api/domain/aggregates/supplier.aggregate.ts
2026-03-30 13:57:42 +02:00

251 lines
5.6 KiB
TypeScript

import {
AggregateRoot,
type CurrencyCode,
type EmailAddress,
type LanguageCode,
type Name,
type PhoneNumber,
type PostalAddress,
type PostalAddressPatchProps,
type PostalAddressProps,
type TINNumber,
type URLAddress,
type UniqueID,
} from "@repo/rdx-ddd";
import { type Maybe, Result } from "@repo/rdx-utils";
import { SupplierStatus } from "../value-objects/supplier-status.value-object";
/**
* Props de creación
*/
export interface ISupplierCreateProps {
companyId: UniqueID;
status?: SupplierStatus;
reference: Maybe<Name>;
isCompany: boolean;
name: Name;
tradeName: Maybe<Name>;
tin: Maybe<TINNumber>;
address: PostalAddressProps;
emailPrimary: Maybe<EmailAddress>;
emailSecondary: Maybe<EmailAddress>;
phonePrimary: Maybe<PhoneNumber>;
phoneSecondary: Maybe<PhoneNumber>;
mobilePrimary: Maybe<PhoneNumber>;
mobileSecondary: Maybe<PhoneNumber>;
fax: Maybe<PhoneNumber>;
website: Maybe<URLAddress>;
languageCode: LanguageCode;
currencyCode: CurrencyCode;
}
export type SupplierPatchProps = Partial<
Omit<ISupplierCreateProps, "companyId" | "address" | "status">
> & {
address?: PostalAddressPatchProps;
};
// Supplier
export interface ISupplier {
// comportamiento
update(partialSupplier: SupplierPatchProps): Result<Supplier, 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 languageCode: LanguageCode;
readonly currencyCode: CurrencyCode;
}
type SupplierInternalProps = Omit<ISupplierCreateProps, "address"> & {
readonly address: PostalAddress;
};
/**
* Aggregate Root: Supplier
*/
export class Supplier extends AggregateRoot<SupplierInternalProps> implements ISupplier {
static create(props: ISupplierCreateProps, id?: UniqueID): Result<Supplier, Error> {
const validationResult = Supplier.validateCreateProps(props);
if (validationResult.isFailure) {
return Result.fail(validationResult.error);
}
const { address, ...internalProps } = props;
const postalAddressResult = PostalAddress.create(address);
if (postalAddressResult.isFailure) {
return Result.fail(postalAddressResult.error);
}
const contact = new Supplier(
{
...internalProps,
address: postalAddressResult.data,
},
id
);
// Reglas de negocio / validaciones
// ...
// ...
// Disparar eventos de dominio
// ...
// ...
return Result.ok(contact);
}
private static validateCreateProps(props: ISupplierCreateProps): Result<void, Error> {
return Result.ok();
}
// Rehidratación desde persistencia
static rehydrate(props: SupplierInternalProps, id: UniqueID): Supplier {
return new Supplier(props, id);
}
// -------------------------
// BEHAVIOUR
// -------------------------
public update(partialSupplier: SupplierPatchProps): Result<Supplier, Error> {
const { address: partialAddress, ...rest } = partialSupplier;
Object.assign(this.props, rest);
if (partialAddress) {
const addressResult = this.address.update(partialAddress);
if (addressResult.isFailure) {
return Result.fail(addressResult.error);
}
}
return Result.ok();
}
public activate(): Result<Supplier, Error> {
this.props.status = SupplierStatus.active();
return Result.ok(this);
}
public deactivate(): Result<Supplier, Error> {
this.props.status = SupplierStatus.inactive();
return Result.ok(this);
}
// -------------------------
// GETTERS
// -------------------------
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<Name> {
return this.props.reference;
}
public get name(): Name {
return this.props.name;
}
public get tradeName(): Maybe<Name> {
return this.props.tradeName;
}
public get tin(): Maybe<TINNumber> {
return this.props.tin;
}
public get address(): PostalAddress {
return this.props.address;
}
public get emailPrimary(): Maybe<EmailAddress> {
return this.props.emailPrimary;
}
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;
}
public get fax(): Maybe<PhoneNumber> {
return this.props.fax;
}
public get website(): Maybe<URLAddress> {
return this.props.website;
}
public get languageCode(): LanguageCode {
return this.props.languageCode;
}
public get currencyCode(): CurrencyCode {
return this.props.currencyCode;
}
}