Uecko_ERP/modules/customer-invoices/src/api/application/helpers.bak/map-dto-to-customer-invoice-items-props.ts

86 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-09-18 08:49:32 +00:00
import {
2025-11-12 17:22:05 +00:00
ValidationErrorCollection,
type ValidationErrorDetail,
2025-09-18 08:49:32 +00:00
extractOrPushError,
2025-10-18 19:57:52 +00:00
maybeFromNullableVO,
2025-09-18 08:49:32 +00:00
} from "@repo/rdx-ddd";
2025-06-24 18:38:57 +00:00
import { Result } from "@repo/rdx-utils";
2025-11-12 17:22:05 +00:00
import type { CreateCustomerInvoiceRequestDTO } from "../../../common";
2025-06-24 18:38:57 +00:00
import {
CustomerInvoiceItem,
CustomerInvoiceItemDescription,
2025-11-12 17:22:05 +00:00
type CustomerInvoiceItemProps,
2025-09-05 11:23:45 +00:00
ItemAmount,
ItemDiscount,
2025-09-18 08:49:32 +00:00
ItemQuantity,
2025-06-24 18:38:57 +00:00
} from "../../domain";
2025-11-12 17:22:05 +00:00
2025-06-24 18:38:57 +00:00
import { hasNoUndefinedFields } from "./has-no-undefined-fields";
2025-06-26 11:32:55 +00:00
export function mapDTOToCustomerInvoiceItemsProps(
2025-09-18 08:49:32 +00:00
dtoItems: Pick<CreateCustomerInvoiceRequestDTO, "items">["items"]
2025-06-24 18:38:57 +00:00
): Result<CustomerInvoiceItem[], ValidationErrorCollection> {
const errors: ValidationErrorDetail[] = [];
const items: CustomerInvoiceItem[] = [];
dtoItems.forEach((item, index) => {
const path = (field: string) => `items[${index}].${field}`;
const description = extractOrPushError(
2025-10-18 19:57:52 +00:00
maybeFromNullableVO(item.description, (value) =>
CustomerInvoiceItemDescription.create(value)
),
2025-06-24 18:38:57 +00:00
path("description"),
errors
);
const quantity = extractOrPushError(
2025-10-18 19:57:52 +00:00
maybeFromNullableVO(item.quantity, (value) => ItemQuantity.create({ value })),
2025-06-24 18:38:57 +00:00
path("quantity"),
errors
);
2025-10-18 19:57:52 +00:00
const unitAmount = extractOrPushError(
maybeFromNullableVO(item.unit_amount, (value) => ItemAmount.create({ value })),
path("unit_amount"),
2025-06-24 18:38:57 +00:00
errors
);
2025-10-18 19:57:52 +00:00
const discountPercentage = extractOrPushError(
maybeFromNullableVO(item.discount_percentage, (value) => ItemDiscount.create({ value })),
path("discount_percentage"),
2025-06-24 18:38:57 +00:00
errors
);
if (errors.length === 0) {
2025-10-18 19:57:52 +00:00
const itemProps: CustomerInvoiceItemProps = {
2025-06-24 18:38:57 +00:00
description: description,
quantity: quantity,
2025-10-18 19:57:52 +00:00
unitAmount: unitAmount,
itemDiscountPercentage: discountPercentage,
2025-10-18 19:57:52 +00:00
//currencyCode,
//languageCode,
//taxes:
2025-06-24 18:38:57 +00:00
};
if (hasNoUndefinedFields(itemProps)) {
// Validar y crear el item de factura
const itemOrError = CustomerInvoiceItem.create(itemProps);
if (itemOrError.isSuccess) {
items.push(itemOrError.data);
} else {
errors.push({ path: `items[${index}]`, message: itemOrError.error.message });
}
}
}
if (errors.length > 0) {
2025-10-18 19:57:52 +00:00
return Result.fail(new ValidationErrorCollection("Invoice items dto mapping failed", errors));
2025-06-24 18:38:57 +00:00
}
});
return Result.ok(items);
}