import { ValidationErrorCollection, ValidationErrorDetail } from "@erp/core/api"; import { UniqueID, UtcDate } from "@repo/rdx-ddd"; import { Result } from "@repo/rdx-utils"; import { CreateCustomerCommandDTO } from "../../../common/dto"; import { CustomerNumber, CustomerProps, CustomerSerie, CustomerStatus } from "../../domain"; import { extractOrPushError } from "./extract-or-push-error"; import { mapDTOToCustomerItemsProps } from "./map-dto-to-customer-items-props"; /** * 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 mapDTOToCustomerProps(dto: CreateCustomerCommandDTO) { const errors: ValidationErrorDetail[] = []; const customerId = extractOrPushError(UniqueID.create(dto.id), "id", errors); const customerNumber = extractOrPushError( CustomerNumber.create(dto.customer_number), "customer_number", errors ); const customerSeries = extractOrPushError( CustomerSerie.create(dto.customer_series), "customer_series", errors ); const issueDate = extractOrPushError(UtcDate.createFromISO(dto.issue_date), "issue_date", errors); const operationDate = extractOrPushError( UtcDate.createFromISO(dto.operation_date), "operation_date", errors ); //const currency = extractOrPushError(Currency.(dto.currency), "currency", errors); const currency = dto.currency; // 🔄 Validar y construir los items de factura con helper especializado const itemsResult = mapDTOToCustomerItemsProps(dto.items); if (itemsResult.isFailure) { return Result.fail(itemsResult.error); } if (errors.length > 0) { return Result.fail(new ValidationErrorCollection(errors)); } const customerProps: CustomerProps = { customerNumber: customerNumber!, customerSeries: customerSeries!, issueDate: issueDate!, operationDate: operationDate!, status: CustomerStatus.createDraft(), currency, }; return Result.ok({ id: customerId!, props: customerProps }); /*if (hasNoUndefinedFields(customerProps)) { const customerOrError = Customer.create(customerProps, customerId); if (customerOrError.isFailure) { return Result.fail(customerOrError.error); } return Result.ok(customerOrError.data); } return Result.fail( new ValidationErrorCollection([ { path: "", message: "Error building from DTO: Some fields are undefined" }, ]) );*/ }