2025-09-10 16:06:29 +00:00
|
|
|
import { JsonTaxCatalogProvider } from "@erp/core";
|
|
|
|
|
import {
|
|
|
|
|
MapperParamsType,
|
2025-09-11 12:05:50 +00:00
|
|
|
SequelizeDomainMapper,
|
2025-09-10 16:06:29 +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 { ItemTax } from "../../../domain";
|
|
|
|
|
import {
|
|
|
|
|
CustomerInvoiceItemCreationAttributes,
|
|
|
|
|
CustomerInvoiceItemTaxModel,
|
|
|
|
|
} from "../../sequelize";
|
2025-09-10 16:06:29 +00:00
|
|
|
|
2025-09-13 18:45:55 +00:00
|
|
|
export class ItemTaxesDomainMapper extends SequelizeDomainMapper<
|
2025-09-10 16:06:29 +00:00
|
|
|
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;
|
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-13 18:45:55 +00:00
|
|
|
/*public mapToPersistence(
|
2025-09-10 16:06:29 +00:00
|
|
|
source: ItemTax,
|
|
|
|
|
params?: MapperParamsType
|
|
|
|
|
): CustomerInvoiceItemCreationAttributes {
|
|
|
|
|
throw new Error("not implemented");
|
2025-09-13 18:45:55 +00:00
|
|
|
}*/
|
2025-09-09 15:48:12 +00:00
|
|
|
}
|