Uecko_ERP/modules/customer-invoices/src/api/infrastructure/mappers/domain/item-taxes.mapper.ts
2025-09-16 13:29:45 +02:00

70 lines
1.8 KiB
TypeScript

import { JsonTaxCatalogProvider } from "@erp/core";
import { MapperParamsType, SequelizeDomainMapper, Tax } from "@erp/core/api";
import {
ValidationErrorCollection,
ValidationErrorDetail,
extractOrPushError,
} from "@repo/rdx-ddd";
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<ItemTax, Error> {
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");
}*/
}