From 044b7236572089b98a9f230499bb4cd9ad0342a3 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 1 Sep 2020 20:37:50 +0530 Subject: correct refund amounts and better testing --- .../taler-wallet-core/src/operations/transactions.ts | 17 +++++++++++------ packages/taler-wallet-core/src/util/amounts.ts | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'packages/taler-wallet-core') diff --git a/packages/taler-wallet-core/src/operations/transactions.ts b/packages/taler-wallet-core/src/operations/transactions.ts index 2515415d2..da75f6e53 100644 --- a/packages/taler-wallet-core/src/operations/transactions.ts +++ b/packages/taler-wallet-core/src/operations/transactions.ts @@ -281,22 +281,27 @@ export async function getTransactions( groupKey, ); let r0: WalletRefundItem | undefined; - let amountEffective = Amounts.getZero( + let amountRaw = Amounts.getZero( pr.contractData.amount.currency, ); - let amountRaw = Amounts.getZero(pr.contractData.amount.currency); + let amountEffective = Amounts.getZero(pr.contractData.amount.currency); for (const rk of Object.keys(pr.refunds)) { const refund = pr.refunds[rk]; + const myGroupKey = `${refund.executionTime.t_ms}`; + if (myGroupKey !== groupKey) { + continue; + } if (!r0) { r0 = refund; } + if (refund.type === RefundState.Applied) { - amountEffective = Amounts.add( - amountEffective, - refund.refundAmount, - ).amount; amountRaw = Amounts.add( amountRaw, + refund.refundAmount, + ).amount; + amountEffective = Amounts.add( + amountEffective, Amounts.sub( refund.refundAmount, refund.refundFee, diff --git a/packages/taler-wallet-core/src/util/amounts.ts b/packages/taler-wallet-core/src/util/amounts.ts index 2a8c47905..2f912cff4 100644 --- a/packages/taler-wallet-core/src/util/amounts.ts +++ b/packages/taler-wallet-core/src/util/amounts.ts @@ -101,11 +101,21 @@ export function getZero(currency: string): AmountJson { }; } -export function sum(amounts: AmountJson[]): Result { +export type AmountLike = AmountString | AmountJson; + +export function jsonifyAmount(amt: AmountLike): AmountJson { + if (typeof amt === "string") { + return parseOrThrow(amt); + } + return amt; +} + +export function sum(amounts: AmountLike[]): Result { if (amounts.length <= 0) { throw Error("can't sum zero amounts"); } - return add(amounts[0], ...amounts.slice(1)); + const jsonAmounts = amounts.map((x) => jsonifyAmount(x)); + return add(jsonAmounts[0], ...jsonAmounts.slice(1)); } /** -- cgit v1.2.3