import { JsonTaxCatalogProvider } from "@erp/core"; import { MapperParamsType, SequelizeDomainMapper, Tax, ValidationErrorCollection, ValidationErrorDetail, extractOrPushError, } from "@erp/core/api"; import { Result } from "@repo/rdx-utils"; import { ItemTax } from "../../../domain"; import { CustomerInvoiceItemCreationAttributes, CustomerInvoiceItemTaxModel, } from "../../sequelize"; export class ItemTaxesDomainMapper extends SequelizeDomainMapper< CustomerInvoiceItemTaxModel, CustomerInvoiceItemCreationAttributes, ItemTax > { 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; } public mapToDomain( source: CustomerInvoiceItemTaxModel, params?: MapperParamsType ): Result { const { errors, index } = params as { index: number; 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; } /*public mapToPersistence( source: ItemTax, params?: MapperParamsType ): CustomerInvoiceItemCreationAttributes { throw new Error("not implemented"); }*/ }