From cf7f819685c19adfe6443a6bdd10f3afe10b247d Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 19 Dec 2022 16:25:09 -0300 Subject: templates --- .../merchant-backoffice-ui/src/utils/amount.ts | 25 ++++++++-------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'packages/merchant-backoffice-ui/src/utils') diff --git a/packages/merchant-backoffice-ui/src/utils/amount.ts b/packages/merchant-backoffice-ui/src/utils/amount.ts index bdc37952f..93d6a3a4a 100644 --- a/packages/merchant-backoffice-ui/src/utils/amount.ts +++ b/packages/merchant-backoffice-ui/src/utils/amount.ts @@ -20,18 +20,6 @@ import { } from "@gnu-taler/taler-util"; import { MerchantBackend } from "../declaration.js"; -/** - * sums two prices, - * @param one - * @param two - * @returns - */ -const sumPrices = (one: string, two: string) => { - const [currency, valueOne] = one.split(":"); - const [, valueTwo] = two.split(":"); - return `${currency}:${parseInt(valueOne, 10) + parseInt(valueTwo, 10)}`; -}; - /** * merge refund with the same description and a difference less than one minute * @param prev list of refunds that will hold the merged refunds @@ -41,7 +29,7 @@ const sumPrices = (one: string, two: string) => { export function mergeRefunds( prev: MerchantBackend.Orders.RefundDetails[], cur: MerchantBackend.Orders.RefundDetails, -) { +): MerchantBackend.Orders.RefundDetails[] { let tail; if ( @@ -54,19 +42,24 @@ export function mergeRefunds( ) { //more than 1 minute difference + //can't merge refunds, they are different or to distant in time prev.push(cur); return prev; } + const a = Amounts.parseOrThrow(tail.amount); + const b = Amounts.parseOrThrow(cur.amount); + const r = Amounts.add(a, b).amount; + prev[prev.length - 1] = { ...tail, - amount: sumPrices(tail.amount, cur.amount), + amount: Amounts.stringify(r), }; return prev; } -export const rate = (one: string, two: string) => { +export const rate = (one: string, two: string): number => { const a = Amounts.parseOrThrow(one); const b = Amounts.parseOrThrow(two); const af = toFloat(a); @@ -75,6 +68,6 @@ export const rate = (one: string, two: string) => { return af / bf; }; -function toFloat(amount: AmountJson) { +function toFloat(amount: AmountJson): number { return amount.value + amount.fraction / amountFractionalBase; } -- cgit v1.2.3