import { DomainError, ValidationErrorCollection, ValidationErrorDetail, extractOrPushError, } from "@erp/core/api"; import { City, Country, CurrencyCode, EmailAddress, LanguageCode, Name, PhoneNumber, PostalAddress, PostalCode, Province, Street, TINNumber, TaxCode, TextValue, URLAddress, UniqueID, maybeFromNullableVO, } from "@repo/rdx-ddd"; import { Collection, Result } from "@repo/rdx-utils"; import { CreateCustomerRequestDTO } from "../../../common/dto"; import { CustomerProps, CustomerStatus } from "../../domain"; /** * 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); const isCompany = dto.is_company; 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(); dto.default_taxes.map((taxCode, index) => { const tax = extractOrPushError(TaxCode.create(taxCode), `default_taxes.${index}`, errors); if (tax) { defaultTaxes.add(tax!); } }); if (errors.length > 0) { console.error(errors); 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 ); console.debug("Mapped customer props:", { companyId, status, reference, isCompany, name, tradeName, tinNumber, street, street2, city, province, postalCode, country, emailAddress, phoneNumber, faxNumber, website, legalRecord, languageCode, currencyCode, defaultTaxes, }); 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) { console.error(err); return Result.fail(new DomainError("Customer props mapping failed", { cause: err })); } }