diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/utils/amount.ts')
-rw-r--r-- | packages/merchant-backoffice-ui/src/utils/amount.ts | 25 |
1 files changed, 9 insertions, 16 deletions
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 @@ -21,18 +21,6 @@ import { 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 * @param cur new refund to add to the list @@ -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; } |