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

73 lines
1.9 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 { CustomerInvoiceProps } from "../../../domain";
import { InvoiceTax } from "../../../domain/entities/invoice-taxes";
import { CustomerInvoiceTaxCreationAttributes, CustomerInvoiceTaxModel } from "../../sequelize";
export class TaxesDomainMapper extends SequelizeDomainMapper<
CustomerInvoiceTaxModel,
CustomerInvoiceTaxCreationAttributes,
InvoiceTax
> {
private _taxCatalog: JsonTaxCatalogProvider;
constructor(params: MapperParamsType) {
super();
const { taxCatalog } = params as {
taxCatalog: JsonTaxCatalogProvider;
};
if (!taxCatalog) {
throw new Error('taxCatalog not defined ("TaxesMapper")');
}
this._taxCatalog = taxCatalog;
}
public mapToDomain(
source: CustomerInvoiceTaxModel,
params?: MapperParamsType
): Result<InvoiceTax, Error> {
const { errors, index, attributes } = params as {
index: number;
errors: ValidationErrorDetail[];
attributes: Partial<CustomerInvoiceProps>;
};
const tax = extractOrPushError(
Tax.createFromCode(source.tax_code, this._taxCatalog),
`taxes[${index}].tax_code`,
errors
);
// Creación del objeto de dominio
const createResult = InvoiceTax.create({
tax: tax!,
});
if (createResult.isFailure) {
return Result.fail(
new ValidationErrorCollection("Invoice tax creation failed", [
{ path: `taxes[${index}]`, message: createResult.error.message },
])
);
}
return createResult;
}
/*public mapToPersistence(
source: InvoiceTax,
params?: MapperParamsType
): CustomerInvoiceTaxCreationAttributes {
throw new Error("not implemented");
}*/
}