79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
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 invoiceId = extractOrPushError(UniqueID.create(dto.id), "id", errors);
|
|
|
|
const invoiceNumber = extractOrPushError(
|
|
CustomerNumber.create(dto.invoice_number),
|
|
"invoice_number",
|
|
errors
|
|
);
|
|
const invoiceSeries = extractOrPushError(
|
|
CustomerSerie.create(dto.invoice_series),
|
|
"invoice_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 invoiceProps: CustomerProps = {
|
|
invoiceNumber: invoiceNumber!,
|
|
invoiceSeries: invoiceSeries!,
|
|
issueDate: issueDate!,
|
|
operationDate: operationDate!,
|
|
status: CustomerStatus.createDraft(),
|
|
currency,
|
|
};
|
|
|
|
return Result.ok({ id: invoiceId!, props: invoiceProps });
|
|
|
|
/*if (hasNoUndefinedFields(invoiceProps)) {
|
|
const invoiceOrError = Customer.create(invoiceProps, invoiceId);
|
|
if (invoiceOrError.isFailure) {
|
|
return Result.fail(invoiceOrError.error);
|
|
}
|
|
return Result.ok(invoiceOrError.data);
|
|
}
|
|
|
|
return Result.fail(
|
|
new ValidationErrorCollection([
|
|
{ path: "", message: "Error building from DTO: Some fields are undefined" },
|
|
])
|
|
);*/
|
|
}
|