From c67d94c56e154be4b2cf91572cdc2d8d2da7f8e4 Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Sat, 11 Mar 2023 18:19:38 -0300
Subject: fix: #7753

---
 packages/demobank-ui/src/pages/BusinessAccount.tsx | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

(limited to 'packages/demobank-ui/src/pages/BusinessAccount.tsx')

diff --git a/packages/demobank-ui/src/pages/BusinessAccount.tsx b/packages/demobank-ui/src/pages/BusinessAccount.tsx
index 9bd799746..128b47114 100644
--- a/packages/demobank-ui/src/pages/BusinessAccount.tsx
+++ b/packages/demobank-ui/src/pages/BusinessAccount.tsx
@@ -212,8 +212,7 @@ function useRatiosAndFeeConfigWithChangeDetection(): HttpResponse<
         oldResult.ratios_and_fees.sell_at_ratio ||
       result.data.ratios_and_fees.sell_out_fee !==
         oldResult.ratios_and_fees.sell_out_fee ||
-      result.data.ratios_and_fees.fiat_currency !==
-        oldResult.ratios_and_fees.fiat_currency);
+      result.data.fiat_currency !== oldResult.fiat_currency);
 
   return {
     ...result,
@@ -238,16 +237,19 @@ function CreateCashout({
   if (!result.ok) return onLoadNotOk(result);
   if (!ratiosResult.ok) return onLoadNotOk(ratiosResult);
   const config = ratiosResult.data;
-  const maybeBalance = Amounts.parse(result.data.balance.amount);
-  if (!maybeBalance) return <div>error</div>;
-  const balance = maybeBalance;
+  const balance = Amounts.parseOrThrow(result.data.balance.amount);
+  const debitThreshold = Amounts.parseOrThrow(result.data.debitThreshold);
   const zero = Amounts.zeroOfCurrency(balance.currency);
+  const balanceIsDebit = result.data.balance.credit_debit_indicator == "debit";
+  const limit = balanceIsDebit
+    ? Amounts.sub(debitThreshold, balance).amount
+    : Amounts.add(balance, debitThreshold).amount;
 
   const sellRate = config.ratios_and_fees.sell_at_ratio;
   const sellFee = !config.ratios_and_fees.sell_out_fee
     ? zero
     : Amounts.fromFloat(config.ratios_and_fees.sell_out_fee, balance.currency);
-  const fiatCurrency = config.ratios_and_fees.fiat_currency;
+  const fiatCurrency = config.fiat_currency;
 
   if (!sellRate || sellRate < 0) return <div>error rate</div>;
 
@@ -278,12 +280,12 @@ function CreateCashout({
       ? i18n.str`required`
       : !amount
       ? i18n.str`could not be parsed`
-      : Amounts.cmp(balance, amount_debit) === -1
+      : Amounts.cmp(limit, amount_debit) === -1
       ? i18n.str`balance is not enough`
       : Amounts.cmp(credit_before_fee, sellFee) === -1
-      ? i18n.str`amount is not enough`
+      ? i18n.str`the total amount to transfer does not cover the fees`
       : Amounts.isZero(amount_credit)
-      ? i18n.str`amount is not enough`
+      ? i18n.str`the total transfer at destination will be zero`
       : undefined,
     channel: !form.channel ? i18n.str`required` : undefined,
   });
-- 
cgit v1.2.3

