88 lines
2.6 KiB
TypeScript
88 lines
2.6 KiB
TypeScript
import { ValidationErrorCollection, ValidationErrorDetail } from "@erp/core/api";
|
|
import { UniqueID, UtcDate } from "@repo/rdx-ddd";
|
|
import { Result } from "@repo/rdx-utils";
|
|
import { CreateCustomerInvoiceCommandDTO } from "../../../common/dto";
|
|
import {
|
|
CustomerInvoiceNumber,
|
|
CustomerInvoiceProps,
|
|
CustomerInvoiceSerie,
|
|
CustomerInvoiceStatus,
|
|
} from "../../domain";
|
|
import { extractOrPushError } from "./extract-or-push-error";
|
|
import { mapDTOToCustomerInvoiceItemsProps } from "./map-dto-to-customer-invoice-items-props";
|
|
|
|
/**
|
|
* Convierte el DTO a las props validadas (CustomerInvoiceProps).
|
|
* No construye directamente el agregado.
|
|
*
|
|
* @param dto - DTO con los datos de la factura de cliente
|
|
* @returns
|
|
|
|
*
|
|
*/
|
|
|
|
export function mapDTOToCustomerInvoiceProps(dto: CreateCustomerInvoiceCommandDTO) {
|
|
const errors: ValidationErrorDetail[] = [];
|
|
|
|
const invoiceId = extractOrPushError(UniqueID.create(dto.id), "id", errors);
|
|
|
|
const invoiceNumber = extractOrPushError(
|
|
CustomerInvoiceNumber.create(dto.invoice_number),
|
|
"invoice_number",
|
|
errors
|
|
);
|
|
const invoiceSeries = extractOrPushError(
|
|
CustomerInvoiceSerie.create(dto.invoice_series),
|
|
"invoice_series",
|
|
errors
|
|
);
|
|
const invoiceDate = extractOrPushError(
|
|
UtcDate.createFromISO(dto.invoice_date),
|
|
"invoice_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 = mapDTOToCustomerInvoiceItemsProps(dto.items);
|
|
if (itemsResult.isFailure) {
|
|
return Result.fail(itemsResult.error);
|
|
}
|
|
|
|
if (errors.length > 0) {
|
|
return Result.fail(new ValidationErrorCollection(errors));
|
|
}
|
|
|
|
const invoiceProps: CustomerInvoiceProps = {
|
|
invoiceNumber: invoiceNumber!,
|
|
invoiceSeries: invoiceSeries!,
|
|
invoiceDate: invoiceDate!,
|
|
operationDate: operationDate!,
|
|
status: CustomerInvoiceStatus.createDraft(),
|
|
currency,
|
|
};
|
|
|
|
return Result.ok({ id: invoiceId!, props: invoiceProps });
|
|
|
|
/*if (hasNoUndefinedFields(invoiceProps)) {
|
|
const invoiceOrError = CustomerInvoice.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" },
|
|
])
|
|
);*/
|
|
}
|