From 0f3b38745b7214abbbe638410a74fe5405ca24bb Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Fri, 31 Mar 2023 14:51:58 -0300
Subject: calculate deposit wire transfer

---
 .../src/wallet/Transaction.tsx                     | 45 ++++++++++++++++------
 1 file changed, 33 insertions(+), 12 deletions(-)

(limited to 'packages/taler-wallet-webextension/src/wallet/Transaction.tsx')

diff --git a/packages/taler-wallet-webextension/src/wallet/Transaction.tsx b/packages/taler-wallet-webextension/src/wallet/Transaction.tsx
index d95b10297..58e50e7b6 100644
--- a/packages/taler-wallet-webextension/src/wallet/Transaction.tsx
+++ b/packages/taler-wallet-webextension/src/wallet/Transaction.tsx
@@ -1571,6 +1571,38 @@ function RefundDetails({ amount }: { amount: AmountWithFee }): VNode {
   );
 }
 
+type AmountAmountByWireTransferByWire = {
+  id: string;
+  amount: string;
+}[];
+
+function calculateAmountByWireTransfer(
+  state: TransactionDeposit["trackingState"],
+): AmountAmountByWireTransferByWire {
+  const allTracking = Object.values(state ?? {});
+
+  //group tracking by wtid, sum amounts
+  const trackByWtid = allTracking.reduce((prev, cur) => {
+    const fee = Amounts.parseOrThrow(cur.wireFee);
+    const raw = Amounts.parseOrThrow(cur.amountRaw);
+    const total = !prev[cur.wireTransferId]
+      ? raw
+      : Amounts.add(prev[cur.wireTransferId].total, raw).amount;
+
+    prev[cur.wireTransferId] = {
+      total,
+      fee,
+    };
+    return prev;
+  }, {} as Record<string, { total: AmountJson; fee: AmountJson }>);
+
+  //remove wire fee from total amount
+  return Object.entries(trackByWtid).map(([id, info]) => ({
+    id,
+    amount: Amounts.stringify(Amounts.sub(info.total, info.fee).amount),
+  }));
+}
+
 function TrackingDepositDetails({
   trackingState,
 }: {
@@ -1578,18 +1610,7 @@ function TrackingDepositDetails({
 }): VNode {
   const { i18n } = useTranslationContext();
 
-  const trackByWtid = Object.values(trackingState ?? {}).reduce((prev, cur) => {
-    const am = Amounts.parseOrThrow(cur.amountEffective);
-    const sum = !prev[cur.wireTransferId]
-      ? am
-      : Amounts.add(prev[cur.wireTransferId], am).amount;
-    prev[cur.wireTransferId] = sum;
-    return prev;
-  }, {} as Record<string, AmountJson>);
-  const wireTransfers = Object.entries(trackByWtid).map(([id, amountJson]) => ({
-    id,
-    amount: Amounts.stringify(amountJson),
-  }));
+  const wireTransfers = calculateAmountByWireTransfer(trackingState);
 
   return (
     <PurchaseDetailsTable>
-- 
cgit v1.2.3

