@open-mercato/core 0.6.3-develop.3838.1.7f2657a47c → 0.6.3-develop.3857.1.da89d7530c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -425,13 +425,49 @@ const createPaymentCommand: CommandHandler<
425
425
  metadata: null,
426
426
  },
427
427
  ]
428
- allocations.forEach((allocation) => {
429
- const orderRef = allocation.orderId ? tx.getReference(SalesOrder, allocation.orderId) : order
430
- const invoiceRef = allocation.invoiceId ? tx.getReference(SalesInvoice, allocation.invoiceId) : null
428
+ const orderCache = new Map<string, SalesOrder>([[order.id, order]])
429
+ const invoiceCache = new Map<string, SalesInvoice>()
430
+ for (const allocation of allocations) {
431
+ let allocationOrder: SalesOrder | null = null
432
+ if (allocation.orderId) {
433
+ allocationOrder = orderCache.get(allocation.orderId) ?? null
434
+ if (!allocationOrder) {
435
+ allocationOrder = assertFound(
436
+ await findOneWithDecryption(
437
+ tx,
438
+ SalesOrder,
439
+ { id: allocation.orderId },
440
+ {},
441
+ { tenantId: input.tenantId, organizationId: input.organizationId },
442
+ ),
443
+ 'sales.payments.order_not_found',
444
+ )
445
+ ensureSameScope(allocationOrder, input.organizationId, input.tenantId)
446
+ orderCache.set(allocation.orderId, allocationOrder)
447
+ }
448
+ }
449
+ let allocationInvoice: SalesInvoice | null = null
450
+ if (allocation.invoiceId) {
451
+ allocationInvoice = invoiceCache.get(allocation.invoiceId) ?? null
452
+ if (!allocationInvoice) {
453
+ allocationInvoice = assertFound(
454
+ await findOneWithDecryption(
455
+ tx,
456
+ SalesInvoice,
457
+ { id: allocation.invoiceId },
458
+ {},
459
+ { tenantId: input.tenantId, organizationId: input.organizationId },
460
+ ),
461
+ 'sales.payments.invoice_not_found',
462
+ )
463
+ ensureSameScope(allocationInvoice, input.organizationId, input.tenantId)
464
+ invoiceCache.set(allocation.invoiceId, allocationInvoice)
465
+ }
466
+ }
431
467
  const entity = tx.create(SalesPaymentAllocation, {
432
468
  payment,
433
- order: orderRef,
434
- invoice: invoiceRef,
469
+ order: allocationOrder,
470
+ invoice: allocationInvoice,
435
471
  organizationId: input.organizationId,
436
472
  tenantId: input.tenantId,
437
473
  amount: toNumericString(allocation.amount) ?? '0',
@@ -439,7 +475,7 @@ const createPaymentCommand: CommandHandler<
439
475
  metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,
440
476
  })
441
477
  tx.persist(entity)
442
- })
478
+ }
443
479
  tx.persist(payment)
444
480
  if (input.customFields !== undefined) {
445
481
  if (!payment.id) {
@@ -722,16 +758,49 @@ const updatePaymentCommand: CommandHandler<
722
758
  const existingAllocations = await findWithDecryption(em, SalesPaymentAllocation, { payment }, {}, { tenantId: payment.tenantId, organizationId: payment.organizationId })
723
759
  existingAllocations.forEach((allocation) => em.remove(allocation))
724
760
  const allocationInputs = Array.isArray(input.allocations) ? input.allocations : []
725
- allocationInputs.forEach((allocation) => {
726
- const orderRef =
727
- allocation.orderId ??
728
- (typeof payment.order === 'string' ? payment.order : payment.order?.id) ??
729
- null
730
- const order =
731
- orderRef && typeof orderRef === 'string' ? em.getReference(SalesOrder, orderRef) : null
732
- const invoice = allocation.invoiceId
733
- ? em.getReference(SalesInvoice, allocation.invoiceId)
734
- : null
761
+ const paymentOrderId =
762
+ (typeof payment.order === 'string' ? payment.order : payment.order?.id) ?? null
763
+ const orderCache = new Map<string, SalesOrder>()
764
+ if (currentOrder) orderCache.set(currentOrder.id, currentOrder)
765
+ const invoiceCache = new Map<string, SalesInvoice>()
766
+ for (const allocation of allocationInputs) {
767
+ const orderId = allocation.orderId ?? paymentOrderId
768
+ let order: SalesOrder | null = null
769
+ if (orderId && typeof orderId === 'string') {
770
+ order = orderCache.get(orderId) ?? null
771
+ if (!order) {
772
+ order = assertFound(
773
+ await findOneWithDecryption(
774
+ em,
775
+ SalesOrder,
776
+ { id: orderId },
777
+ {},
778
+ { tenantId: payment.tenantId, organizationId: payment.organizationId },
779
+ ),
780
+ 'sales.payments.order_not_found',
781
+ )
782
+ ensureSameScope(order, payment.organizationId, payment.tenantId)
783
+ orderCache.set(orderId, order)
784
+ }
785
+ }
786
+ let invoice: SalesInvoice | null = null
787
+ if (allocation.invoiceId) {
788
+ invoice = invoiceCache.get(allocation.invoiceId) ?? null
789
+ if (!invoice) {
790
+ invoice = assertFound(
791
+ await findOneWithDecryption(
792
+ em,
793
+ SalesInvoice,
794
+ { id: allocation.invoiceId },
795
+ {},
796
+ { tenantId: payment.tenantId, organizationId: payment.organizationId },
797
+ ),
798
+ 'sales.payments.invoice_not_found',
799
+ )
800
+ ensureSameScope(invoice, payment.organizationId, payment.tenantId)
801
+ invoiceCache.set(allocation.invoiceId, invoice)
802
+ }
803
+ }
735
804
  const entity = em.create(SalesPaymentAllocation, {
736
805
  payment,
737
806
  order,
@@ -743,7 +812,7 @@ const updatePaymentCommand: CommandHandler<
743
812
  metadata: allocation.metadata ? cloneJson(allocation.metadata) : null,
744
813
  })
745
814
  em.persist(entity)
746
- })
815
+ }
747
816
  await em.flush()
748
817
  }
749
818