FactuGES_ERP/modules/customer-invoices/src/api/infrastructure/mappers/customer-invoice-item.mapper.ts
2025-09-09 17:48:12 +02:00

181 lines
5.0 KiB
TypeScript

import {
ISequelizeMapper,
MapperParamsType,
SequelizeMapper,
ValidationErrorDetail,
extractOrPushError,
} from "@erp/core/api";
import {
CurrencyCode,
LanguageCode,
UniqueID,
maybeFromNullableVO,
toNullable,
} from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import { InferCreationAttributes } from "sequelize";
import {
CustomerInvoice,
CustomerInvoiceItem,
CustomerInvoiceItemDescription,
ItemAmount,
ItemDiscount,
ItemQuantity,
} from "../../domain";
import {
CustomerInvoiceItemCreationAttributes,
CustomerInvoiceItemModel,
CustomerInvoiceModel,
} from "../sequelize";
export interface ICustomerInvoiceItemMapper
extends ISequelizeMapper<
CustomerInvoiceItemModel,
CustomerInvoiceItemCreationAttributes,
CustomerInvoiceItem
> {}
export class CustomerInvoiceItemMapper
extends SequelizeMapper<
CustomerInvoiceItemModel,
CustomerInvoiceItemCreationAttributes,
CustomerInvoiceItem
>
implements ICustomerInvoiceItemMapper
{
public mapToDomain(
source: CustomerInvoiceItemModel,
params?: MapperParamsType
): Result<CustomerInvoiceItem, Error> {
const { sourceParent, errors } = params as {
sourceParent: CustomerInvoiceModel;
errors: ValidationErrorDetail[];
};
const itemId = extractOrPushError(UniqueID.create(source.item_id), "item_id", errors);
const languageCode = extractOrPushError(
LanguageCode.create(sourceParent.language_code),
"language_code",
errors
);
const currencyCode = extractOrPushError(
CurrencyCode.create(sourceParent.currency_code),
"currency_code",
errors
);
const description = extractOrPushError(
maybeFromNullableVO(source.description, (value) =>
CustomerInvoiceItemDescription.create(value)
),
"description",
errors
);
const quantity = extractOrPushError(
maybeFromNullableVO(source.quantity_value, (value) => ItemQuantity.create({ value })),
"discount_percentage",
errors
);
const unitAmount = extractOrPushError(
maybeFromNullableVO(source.unit_amount_value, (value) =>
ItemAmount.create({ value, currency_code: currencyCode!.code })
),
"unit_amount",
errors
);
const discountPercentage = extractOrPushError(
maybeFromNullableVO(source.discount_percentage_value, (value) =>
ItemDiscount.create({ value })
),
"discount_percentage",
errors
);
// Creación del objeto de dominio
const itemOrError = CustomerInvoiceItem.create(
{
languageCode: languageCode!,
currencyCode: currencyCode!,
description: description!,
quantity: quantity!,
unitAmount: unitAmount!,
discountPercentage: discountPercentage!,
taxes: "",
},
itemId
);
if (itemOrError.isFailure) {
errors.push({ path: "item", message: itemOrError.error.message });
}
return itemOrError;
}
public mapToPersistence(
source: CustomerInvoiceItem,
params?: MapperParamsType
): InferCreationAttributes<CustomerInvoiceItemModel, {}> {
1;
const { index, sourceParent } = params as {
index: number;
sourceParent: CustomerInvoice;
};
return {
item_id: source.id.toPrimitive(),
invoice_id: sourceParent.id.toPrimitive(),
position: index,
description: toNullable(source.description, (description) => description.toPrimitive()),
quantity_value: toNullable(source.quantity, (value) => value.toPrimitive().value),
quantity_scale: source.quantity.match(
(value) => value.toPrimitive().scale,
() => ItemQuantity.DEFAULT_SCALE
),
unit_amount_value: toNullable(source.unitAmount, (value) => value.toPrimitive().value),
unit_amount_scale: source.unitAmount.match(
(value) => value.toPrimitive().scale,
() => ItemAmount.DEFAULT_SCALE
),
subtotal_amount_value: source.subtotalAmount.toPrimitive().value,
subtotal_amount_scale: source.subtotalAmount.toPrimitive().scale,
discount_percentage_value: toNullable(
source.discountPercentage,
(value) => value.toPrimitive().value
),
discount_percentage_scale: source.discountPercentage.match(
(value) => value.toPrimitive().scale,
() => ItemAmount.DEFAULT_SCALE
),
discount_amount_value: toNullable(
source.discountAmount,
(value) => value.toPrimitive().value
),
discount_amount_scale: source.discountAmount.match(
(value) => value.toPrimitive().scale,
() => ItemDiscount.DEFAULT_SCALE
),
taxable_amount_value: source.taxableAmount.toPrimitive().value,
taxable_amount_scale: source.taxableAmount.toPrimitive().value,
taxes_amount_value: source.taxesAmount.toPrimitive().value,
taxes_amount_scale: source.taxesAmount.toPrimitive().value,
total_amount_value: source.totalAmount.toPrimitive().value,
total_amount_scale: source.totalAmount.toPrimitive().scale,
};
}
}