Uecko_ERP/modules/customers/src/api/application/use-cases/create/map-dto-to-create-customer-props.ts

203 lines
5.1 KiB
TypeScript
Raw Normal View History

2025-09-01 14:07:59 +00:00
import {
City,
Country,
CurrencyCode,
2025-09-16 11:29:45 +00:00
DomainError,
2025-09-01 14:07:59 +00:00
EmailAddress,
LanguageCode,
Name,
PhoneNumber,
PostalAddress,
PostalCode,
Province,
Street,
TINNumber,
TaxCode,
TextValue,
URLAddress,
UniqueID,
2025-09-16 11:29:45 +00:00
ValidationErrorCollection,
ValidationErrorDetail,
extractOrPushError,
2025-09-01 14:07:59 +00:00
maybeFromNullableVO,
} from "@repo/rdx-ddd";
2025-09-02 08:57:41 +00:00
import { Collection, Result, isNullishOrEmpty } from "@repo/rdx-utils";
2025-09-14 10:04:57 +00:00
import { CreateCustomerRequestDTO } from "../../../../common";
import { CustomerProps, CustomerStatus } from "../../../domain";
2025-09-01 14:07:59 +00:00
/**
* Convierte el DTO a las props validadas (CustomerProps).
* No construye directamente el agregado.
*
* @param dto - DTO con los datos de la factura de cliente
* @returns
*
*/
export function mapDTOToCreateCustomerProps(dto: CreateCustomerRequestDTO) {
try {
const errors: ValidationErrorDetail[] = [];
const customerId = extractOrPushError(UniqueID.create(dto.id), "id", errors);
const companyId = extractOrPushError(UniqueID.create(dto.company_id), "company_id", errors);
2025-09-01 16:38:00 +00:00
const isCompany = dto.is_company === "true";
2025-09-01 14:07:59 +00:00
const status = extractOrPushError(CustomerStatus.create(dto.status), "status", errors);
const reference = extractOrPushError(
maybeFromNullableVO(dto.reference, (value) => Name.create(value)),
"reference",
errors
);
const name = extractOrPushError(Name.create(dto.name), "name", errors);
const tradeName = extractOrPushError(
maybeFromNullableVO(dto.trade_name, (value) => Name.create(value)),
"trade_name",
errors
);
const tinNumber = extractOrPushError(
maybeFromNullableVO(dto.tin, (value) => TINNumber.create(value)),
"tin",
errors
);
const street = extractOrPushError(
maybeFromNullableVO(dto.street, (value) => Street.create(value)),
"street",
errors
);
const street2 = extractOrPushError(
maybeFromNullableVO(dto.street2, (value) => Street.create(value)),
"street2",
errors
);
const city = extractOrPushError(
maybeFromNullableVO(dto.city, (value) => City.create(value)),
"city",
errors
);
const province = extractOrPushError(
maybeFromNullableVO(dto.province, (value) => Province.create(value)),
"province",
errors
);
const postalCode = extractOrPushError(
maybeFromNullableVO(dto.postal_code, (value) => PostalCode.create(value)),
"postal_code",
errors
);
const country = extractOrPushError(
maybeFromNullableVO(dto.country, (value) => Country.create(value)),
"country",
errors
);
const emailAddress = extractOrPushError(
maybeFromNullableVO(dto.email, (value) => EmailAddress.create(value)),
"email",
errors
);
const phoneNumber = extractOrPushError(
maybeFromNullableVO(dto.phone, (value) => PhoneNumber.create(value)),
"phone",
errors
);
const faxNumber = extractOrPushError(
maybeFromNullableVO(dto.fax, (value) => PhoneNumber.create(value)),
"fax",
errors
);
const website = extractOrPushError(
maybeFromNullableVO(dto.website, (value) => URLAddress.create(value)),
"website",
errors
);
const legalRecord = extractOrPushError(
maybeFromNullableVO(dto.legal_record, (value) => TextValue.create(value)),
"legal_record",
errors
);
const languageCode = extractOrPushError(
LanguageCode.create(dto.language_code),
"language_code",
errors
);
const currencyCode = extractOrPushError(
CurrencyCode.create(dto.currency_code),
"currency_code",
errors
);
const defaultTaxes = new Collection<TaxCode>();
2025-09-02 08:57:41 +00:00
if (!isNullishOrEmpty(dto.default_taxes)) {
dto.default_taxes.split(",").map((taxCode, index) => {
const tax = extractOrPushError(TaxCode.create(taxCode), `default_taxes.${index}`, errors);
if (tax) {
defaultTaxes.add(tax!);
}
});
}
2025-09-01 14:07:59 +00:00
if (errors.length > 0) {
return Result.fail(new ValidationErrorCollection("Customer props mapping failed", errors));
}
const postalAddressProps = {
street: street!,
street2: street2!,
city: city!,
postalCode: postalCode!,
province: province!,
country: country!,
};
const postalAddress = extractOrPushError(
PostalAddress.create(postalAddressProps),
"address",
errors
);
const customerProps: CustomerProps = {
companyId: companyId!,
status: status!,
reference: reference!,
isCompany: isCompany,
name: name!,
tradeName: tradeName!,
tin: tinNumber!,
address: postalAddress!,
email: emailAddress!,
phone: phoneNumber!,
fax: faxNumber!,
website: website!,
legalRecord: legalRecord!,
defaultTaxes: defaultTaxes!,
languageCode: languageCode!,
currencyCode: currencyCode!,
};
return Result.ok({ id: customerId!, props: customerProps });
} catch (err: unknown) {
return Result.fail(new DomainError("Customer props mapping failed", { cause: err }));
}
}