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

74 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-09-09 18:13:54 +00:00
import { JsonTaxCatalogProvider } from "@erp/core";
import {
MapperParamsType,
2025-09-11 12:05:50 +00:00
SequelizeDomainMapper,
2025-09-09 18:13:54 +00:00
Tax,
ValidationErrorCollection,
ValidationErrorDetail,
extractOrPushError,
} from "@erp/core/api";
import { Result } from "@repo/rdx-utils";
2025-09-11 12:05:50 +00:00
import { CustomerInvoiceProps } from "../../../domain";
import { InvoiceTax } from "../../../domain/entities/invoice-taxes";
import { CustomerInvoiceTaxCreationAttributes, CustomerInvoiceTaxModel } from "../../sequelize";
2025-09-09 15:48:12 +00:00
2025-09-11 12:05:50 +00:00
export class TaxesMapper extends SequelizeDomainMapper<
2025-09-09 18:13:54 +00:00
CustomerInvoiceTaxModel,
CustomerInvoiceTaxCreationAttributes,
InvoiceTax
> {
private _taxCatalog: JsonTaxCatalogProvider;
2025-09-10 16:06:29 +00:00
constructor(params: MapperParamsType) {
2025-09-09 18:13:54 +00:00
super();
2025-09-10 16:06:29 +00:00
const { taxCatalog } = params as {
taxCatalog: JsonTaxCatalogProvider;
};
if (!taxCatalog) {
throw new Error('taxCatalog not defined ("TaxesMapper")');
}
2025-09-09 18:13:54 +00:00
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(
2025-09-10 16:06:29 +00:00
new ValidationErrorCollection("Invoice tax creation failed", [
2025-09-09 18:13:54 +00:00
{ path: `taxes[${index}]`, message: createResult.error.message },
])
);
}
return createResult;
}
2025-09-09 15:48:12 +00:00
public mapToPersistence(
2025-09-10 16:06:29 +00:00
source: InvoiceTax,
2025-09-09 15:48:12 +00:00
params?: MapperParamsType
2025-09-10 16:06:29 +00:00
): CustomerInvoiceTaxCreationAttributes {
throw new Error("not implemented");
2025-09-09 15:48:12 +00:00
}
}