Uecko_ERP/modules/customer-invoices/src/api/infrastructure/mappers/domain/item-taxes.mapper.ts

104 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-09-10 16:06:29 +00:00
import { JsonTaxCatalogProvider } from "@erp/core";
2025-09-26 15:00:11 +00:00
import {
ISequelizeDomainMapper,
MapperParamsType,
SequelizeDomainMapper,
Tax,
} from "@erp/core/api";
import { CustomerInvoiceItem, ItemTax } from "@erp/customer-invoices/api/domain";
2025-09-10 16:06:29 +00:00
import {
ValidationErrorCollection,
ValidationErrorDetail,
extractOrPushError,
2025-09-16 11:29:45 +00:00
} from "@repo/rdx-ddd";
2025-09-10 16:06:29 +00:00
import { Result } from "@repo/rdx-utils";
2025-09-11 12:05:50 +00:00
import {
2025-09-26 15:00:11 +00:00
CustomerInvoiceItemTaxCreationAttributes,
2025-09-11 12:05:50 +00:00
CustomerInvoiceItemTaxModel,
} from "../../sequelize";
2025-09-10 16:06:29 +00:00
2025-09-26 15:00:11 +00:00
export interface IItemTaxesDomainMapper
extends ISequelizeDomainMapper<
CustomerInvoiceItemTaxModel,
CustomerInvoiceItemTaxCreationAttributes,
ItemTax
> {}
export class ItemTaxesDomainMapper
extends SequelizeDomainMapper<
CustomerInvoiceItemTaxModel,
CustomerInvoiceItemTaxCreationAttributes,
ItemTax
>
implements IItemTaxesDomainMapper
{
2025-09-10 16:06:29 +00:00
private _taxCatalog!: JsonTaxCatalogProvider;
constructor(params: MapperParamsType) {
super();
const { taxCatalog } = params as {
taxCatalog: JsonTaxCatalogProvider;
};
if (!taxCatalog) {
throw new Error('taxCatalog not defined ("ItemTaxesMapper")');
}
this._taxCatalog = taxCatalog;
2025-09-09 15:48:12 +00:00
}
2025-09-10 16:06:29 +00:00
public mapToDomain(
source: CustomerInvoiceItemTaxModel,
2025-09-09 15:48:12 +00:00
params?: MapperParamsType
2025-09-10 16:06:29 +00:00
): Result<ItemTax, Error> {
const { errors, index } = params as {
2025-09-09 15:48:12 +00:00
index: number;
2025-09-10 16:06:29 +00:00
errors: ValidationErrorDetail[];
};
const tax = extractOrPushError(
Tax.createFromCode(source.tax_code, this._taxCatalog),
`taxes[${index}].tax_code`,
errors
);
// Creación del objeto de dominio
const createResult = ItemTax.create({ tax: tax! });
if (createResult.isFailure) {
return Result.fail(
new ValidationErrorCollection("Invoice item tax creation failed", [
{ path: `taxes[${index}]`, message: createResult.error.message },
])
);
}
return createResult;
}
2025-09-26 15:00:11 +00:00
public mapToPersistence(
2025-09-10 16:06:29 +00:00
source: ItemTax,
params?: MapperParamsType
2025-09-26 15:00:11 +00:00
): Result<CustomerInvoiceItemTaxCreationAttributes, Error> {
const { errors, parent } = params as {
parent: CustomerInvoiceItem;
errors: ValidationErrorDetail[];
};
const taxableAmount = parent.getTaxableAmount();
const taxAmount = source.getTaxAmount(taxableAmount);
return Result.ok({
tax_id: source.id.toPrimitive(),
item_id: parent.id.toPrimitive(),
tax_code: source.tax.code,
taxable_amount_value: taxableAmount.value,
taxable_amount_scale: taxableAmount.scale,
taxes_amount_value: taxAmount.value,
taxes_amount_scale: taxAmount.scale,
});
}
2025-09-09 15:48:12 +00:00
}