import { ISequelizeMapper, MapperParamsType, SequelizeMapper, ValidationErrorDetail, extractOrPushError, } from "@erp/core/api"; import { CurrencyCode, LanguageCode, Quantity, UniqueID, maybeFromNullableVO, toNullable } from "@repo/rdx-ddd"; import { Result } from "@repo/rdx-utils"; import { InferCreationAttributes } from "sequelize"; import { CustomerInvoice, CustomerInvoiceItem, CustomerInvoiceItemDescription, CustomerInvoiceItemDiscount, CustomerInvoiceItemQuantity, CustomerInvoiceItemUnitAmount, } 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 { 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) => Quantity.create({ value, }) CustomerInvoiceItemQuantity.create({ value: source.quantity_amouwnt, scale: source.quantity_scale, }), "discount_percentage", errors ); const unitAmount = extractOrPushError( CustomerInvoiceItemUnitAmount.create({ value: source.unit_price_amount, scale: source.unit_price_scale, }), "discount_percentage", errors ); const discountPercentage = extractOrPushError( CustomerInvoiceItemDiscount.create({ value: source.discount_amount, scale: source.discount_scale, }), "discount_percentage", errors ); // Creación del objeto de dominio return CustomerInvoiceItem.create( { languageCode: languageCode!, currencyCode: currencyCode!, description: description!, quantity: quantity!, unitAmount: unitAmount!, discountPercentage: discountPercentage!, },º id ); } public mapToPersistence( source: CustomerInvoiceItem, params?: MapperParamsType ): InferCreationAttributes { 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, () => CustomerInvoiceItemQuantity.DEFAULT_SCALE), unit_amount_value: toNullable(source.unitAmount, (value) => value.toPrimitive().value), unit_amount_scale: source.unitAmount.match( (value) => value.toPrimitive().scale, () => CustomerInvoiceItemUnitAmount.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, () => CustomerInvoiceItemUnitAmount.DEFAULT_SCALE), discount_amount_value: source.subtotalAmount.toPrimitive().value, discount_amount_scale: source.subtotalAmount.toPrimitive().scale, total_amount_value: source.totalAmount.toPrimitive().value, total_amount_scale: source.totalAmount.toPrimitive().scale, }; } }