From 647528b12806a80e2e5dae1227082743071e141f Mon Sep 17 00:00:00 2001 From: david Date: Wed, 29 Jul 2026 11:32:25 +0200 Subject: [PATCH] . --- .../mappers/update-proforma-input.mapper.ts | 13 +++---- .../models/proforma-update-input.model.ts | 5 ++- .../proformas/services/proforma-creator.ts | 39 ++++++++++--------- .../proformas/services/proforma-updater.ts | 5 +-- ...roforma-recipient-full-snapshot-builder.ts | 4 +- .../aggregates/proforma.aggregate.ts | 6 ++- 6 files changed, 39 insertions(+), 33 deletions(-) diff --git a/modules/customer-invoices/src/api/application/proformas/mappers/update-proforma-input.mapper.ts b/modules/customer-invoices/src/api/application/proformas/mappers/update-proforma-input.mapper.ts index b68a21a9..0b2af75c 100644 --- a/modules/customer-invoices/src/api/application/proformas/mappers/update-proforma-input.mapper.ts +++ b/modules/customer-invoices/src/api/application/proformas/mappers/update-proforma-input.mapper.ts @@ -70,18 +70,18 @@ export class UpdateProformaInputMapper implements IUpdateProformaInputMapper { ); }); - toPatchField(dto.invoice_date).ifSet((invoiceDate) => { - if (isNullishOrEmpty(invoiceDate)) { + toPatchField(dto.proforma_date).ifSet((proformaDate) => { + if (isNullishOrEmpty(proformaDate)) { errors.push({ - path: "invoice_date", - message: "Invoice date cannot be empty", + path: "proforma_date", + message: "Proforma date cannot be empty", }); return; } proformaPatchProps.proformaDate = extractOrPushError( - UtcDate.createFromISO(invoiceDate), - "invoice_date", + UtcDate.createFromISO(proformaDate), + "proforma_date", errors ); }); @@ -340,5 +340,4 @@ export class UpdateProformaInputMapper implements IUpdateProformaInputMapper { throw new ValidationErrorCollection("Proforma props mapping failed", errors); } } - } diff --git a/modules/customer-invoices/src/api/application/proformas/models/proforma-update-input.model.ts b/modules/customer-invoices/src/api/application/proformas/models/proforma-update-input.model.ts index 5ebf66f1..d4c1a1b5 100644 --- a/modules/customer-invoices/src/api/application/proformas/models/proforma-update-input.model.ts +++ b/modules/customer-invoices/src/api/application/proformas/models/proforma-update-input.model.ts @@ -1,10 +1,11 @@ +import type { Maybe } from "@repo/rdx-utils"; + import type { + InvoiceSerie, ProformaItemPatchProps, ProformaPatchProps, ProformaTaxMode, } from "../../../domain"; -import type { Maybe } from "@repo/rdx-utils"; -import type { InvoiceSerie } from "../../../domain"; import type { ProformaItemTaxCodesInput } from "./proforma-item-tax-codes-input.model"; diff --git a/modules/customer-invoices/src/api/application/proformas/services/proforma-creator.ts b/modules/customer-invoices/src/api/application/proformas/services/proforma-creator.ts index e2ce203a..a2a9b4c9 100644 --- a/modules/customer-invoices/src/api/application/proformas/services/proforma-creator.ts +++ b/modules/customer-invoices/src/api/application/proformas/services/proforma-creator.ts @@ -8,16 +8,15 @@ import { InvoiceNumber, Proforma, ProformaTaxConfig, - type ProformaTaxMode, } from "../../../domain"; import type { ProformaCreateInputProps, ProformaTaxConfigInputProps } from "../models"; import type { IProformaRepository } from "../repositories"; import type { ProformaPaymentResolver } from "./catalog-resolver/proforma-payment-resolver"; -import type { ProformaSeriesValidator } from "./proforma-series-validator.interface"; -import type { ProformaTargetInvoiceSeriesValidator } from "./proforma-target-invoice-series-validator.interface"; import type { ProformaTaxResolver } from "./catalog-resolver/proforma-tax-resolver"; import type { IProformaNumberGenerator } from "./proforma-number-generator.interface"; +import type { ProformaSeriesValidator } from "./proforma-series-validator.interface"; +import type { ProformaTargetInvoiceSeriesValidator } from "./proforma-target-invoice-series-validator.interface"; export interface IProformaCreatorParams { companyId: UniqueID; @@ -46,16 +45,18 @@ export class ProformaCreator implements IProformaCreator { async create(params: IProformaCreatorParams): Promise> { const { companyId, id, props, transaction } = params; - const resolvedProps = await this.resolveCreateProps(props, transaction); + const resolvedPropsResult = await this.resolveCreateProps(props, transaction); - if (resolvedProps.isFailure) { - return Result.fail(resolvedProps.error); + if (resolvedPropsResult.isFailure) { + return Result.fail(resolvedPropsResult.error); } + const resolvedProps = resolvedPropsResult.data; + const proformaSeriesValidationResult = await this.deps.proformaSeriesValidator.ensureValidProformaSeries({ companyId, - proformaSeriesCode: resolvedProps.data.proformaSeriesCode, + proformaSeriesCode: resolvedProps.proformaSeriesCode, transaction, }); @@ -66,7 +67,7 @@ export class ProformaCreator implements IProformaCreator { // 1. Obtener siguiente nĂºmero const numberResult = await this.deps.numberService.getNextForCompany( companyId, - resolvedProps.data.proformaSeriesCode, + resolvedProps.proformaSeriesCode, transaction ); @@ -77,7 +78,7 @@ export class ProformaCreator implements IProformaCreator { const targetInvoiceSeriesValidationResult = await this.deps.targetInvoiceSeriesValidator.ensureValidIssuedInvoiceSeries({ companyId, - targetInvoiceSeriesCode: resolvedProps.data.targetInvoiceSeriesCode, + targetInvoiceSeriesCode: resolvedPropsResult.data.targetInvoiceSeriesCode, transaction, }); @@ -102,12 +103,12 @@ export class ProformaCreator implements IProformaCreator { // 2. Crear agregado const proformaResult = Proforma.create( { - ...resolvedProps.data, + ...resolvedPropsResult.data, companyId, documentSeriesId: Maybe.some(numberResult.data.documentSeriesId), proformaNumber: Maybe.some(proformaNumberResult.data), proformaReference, - series: resolvedProps.data.targetInvoiceSeriesCode, + series: resolvedPropsResult.data.targetInvoiceSeriesCode, }, id ); @@ -139,7 +140,10 @@ export class ProformaCreator implements IProformaCreator { props: ProformaCreateInputProps, transaction?: unknown ): Promise< - Result, Error> + Result< + Omit, + Error + > > { const customerResult = await this.deps.customerServices.getCustomerById(props.customerId, { companyId: props.companyId, @@ -154,12 +158,11 @@ export class ProformaCreator implements IProformaCreator { const _newProps = { ...props, - taxRegimeCode: - props.taxRegimeCode.isNone() - ? customer.taxRegimeCode.isSome() - ? customer.taxRegimeCode - : Maybe.some("01") - : props.taxRegimeCode, + taxRegimeCode: props.taxRegimeCode.isNone() + ? customer.taxRegimeCode.isSome() + ? customer.taxRegimeCode + : Maybe.some("01") + : props.taxRegimeCode, }; // Tax Regime => comprobar que existe diff --git a/modules/customer-invoices/src/api/application/proformas/services/proforma-updater.ts b/modules/customer-invoices/src/api/application/proformas/services/proforma-updater.ts index 9f44d699..6ecaba62 100644 --- a/modules/customer-invoices/src/api/application/proformas/services/proforma-updater.ts +++ b/modules/customer-invoices/src/api/application/proformas/services/proforma-updater.ts @@ -2,10 +2,10 @@ import { DomainValidationError, type UniqueID, type UtcDate } from "@repo/rdx-dd import { Maybe, Result } from "@repo/rdx-utils"; import { - ProformaTaxConfig, type Proforma, type ProformaItemPatchProps, type ProformaPatchProps, + ProformaTaxConfig, } from "../../../domain"; import type { ProformaPatchInputProps } from "../models"; import type { IProformaRepository } from "../repositories"; @@ -246,8 +246,7 @@ export class ProformaUpdater implements IProformaUpdater { companyId: params.companyId, atDate: params.currentInvoiceDate, usesRetention, - requestedCode: - patchTaxConfig.defaultRetentionCode ?? currentTaxConfig.defaultRetentionCode, + requestedCode: patchTaxConfig.defaultRetentionCode ?? currentTaxConfig.defaultRetentionCode, }); if (defaultRetentionCode.isFailure) { diff --git a/modules/customer-invoices/src/api/application/proformas/snapshot-builders/full/proforma-recipient-full-snapshot-builder.ts b/modules/customer-invoices/src/api/application/proformas/snapshot-builders/full/proforma-recipient-full-snapshot-builder.ts index 0cefef45..2deef385 100644 --- a/modules/customer-invoices/src/api/application/proformas/snapshot-builders/full/proforma-recipient-full-snapshot-builder.ts +++ b/modules/customer-invoices/src/api/application/proformas/snapshot-builders/full/proforma-recipient-full-snapshot-builder.ts @@ -2,7 +2,7 @@ import type { ISnapshotBuilder } from "@erp/core/api"; import { DomainValidationError, maybeToNullable } from "@repo/rdx-ddd"; import type { ProformaRecipientSummaryDTO } from "../../../../../common"; -import type { InvoiceRecipient, Proforma } from "../../../../domain"; +import type { Proforma } from "../../../../domain"; export interface IProformaRecipientFullSnapshotBuilder extends ISnapshotBuilder {} @@ -16,7 +16,7 @@ export class ProformaRecipientFullSnapshotBuilder implements IProformaRecipientF } return proforma.recipient.match( - (recipient: InvoiceRecipient) => ({ + (recipient) => ({ id: proforma.customerId.toString(), name: recipient.name.toString(), tin: maybeToNullable(recipient.tin, (v) => v.toString()), diff --git a/modules/customer-invoices/src/api/domain/proformas/aggregates/proforma.aggregate.ts b/modules/customer-invoices/src/api/domain/proformas/aggregates/proforma.aggregate.ts index 9aaedaea..20ec2a54 100644 --- a/modules/customer-invoices/src/api/domain/proformas/aggregates/proforma.aggregate.ts +++ b/modules/customer-invoices/src/api/domain/proformas/aggregates/proforma.aggregate.ts @@ -29,7 +29,11 @@ import { import { InvalidProformaTransitionError, ProformaItemMismatch } from "../errors"; import type { IProformaTaxTotals, ProformaCalculationContext } from "../services"; import { canManuallyTransitionProformaStatus } from "../services"; -import { ProformaItemTaxes, ProformaTaxConfig, type ProformaRecipient } from "../value-objects"; +import { + ProformaItemTaxes, + type ProformaRecipient, + type ProformaTaxConfig, +} from "../value-objects"; export interface IProformaCreateProps { companyId: UniqueID;