From a63b2a878b374f3076990131977f16d923d8a63b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 5 Mar 2024 14:58:52 -0300 Subject: check min in conversion admin --- packages/demobank-ui/src/hooks/circuit.ts | 40 +++++++++++++++------- .../demobank-ui/src/pages/ConversionConfig.tsx | 30 +++++++++------- .../src/pages/business/CreateCashout.tsx | 12 ++++--- 3 files changed, 51 insertions(+), 31 deletions(-) (limited to 'packages') diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts index 384e48259..f87cbd843 100644 --- a/packages/demobank-ui/src/hooks/circuit.ts +++ b/packages/demobank-ui/src/hooks/circuit.ts @@ -93,10 +93,15 @@ export function useCashinEstimator(): ConversionEstimators { credit: fiatAmount, }); if (resp.type === "fail") { - // can't happen - // not-supported: it should not be able to call this function - // wrong-calculation: we are using just one parameter - throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint); + switch (resp.case) { + case HttpStatusCode.Conflict: { + return "amount-is-too-small" + } + // this below can't happen + case HttpStatusCode.NotImplemented: //it should not be able to call this function + case HttpStatusCode.BadRequest: //we are using just one parameter + throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint); + } } const credit = Amounts.parseOrThrow(resp.body.amount_credit); const debit = Amounts.parseOrThrow(resp.body.amount_debit); @@ -113,10 +118,15 @@ export function useCashinEstimator(): ConversionEstimators { debit: regionalAmount, }); if (resp.type === "fail") { - // can't happen - // not-supported: it should not be able to call this function - // wrong-calculation: we are using just one parameter - throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint); + switch (resp.case) { + case HttpStatusCode.Conflict: { + return "amount-is-too-small" + } + // this below can't happen + case HttpStatusCode.NotImplemented: //it should not be able to call this function + case HttpStatusCode.BadRequest: //we are using just one parameter + throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint); + } } const credit = Amounts.parseOrThrow(resp.body.amount_credit); const debit = Amounts.parseOrThrow(resp.body.amount_debit); @@ -148,7 +158,6 @@ export function useCashoutEstimator(): ConversionEstimators { case HttpStatusCode.BadRequest: //we are using just one parameter throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint); } - } const credit = Amounts.parseOrThrow(resp.body.amount_credit); const debit = Amounts.parseOrThrow(resp.body.amount_debit); @@ -165,10 +174,15 @@ export function useCashoutEstimator(): ConversionEstimators { debit: regionalAmount, }); if (resp.type === "fail") { - // can't happen - // not-supported: it should not be able to call this function - // wrong-calculation: we are using just one parameter - throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint); + switch (resp.case) { + case HttpStatusCode.Conflict: { + return "amount-is-too-small" + } + // this below can't happen + case HttpStatusCode.NotImplemented: //it should not be able to call this function + case HttpStatusCode.BadRequest: //we are using just one parameter + throw TalerError.fromDetail(resp.detail.code, {}, resp.detail.hint); + } } const credit = Amounts.parseOrThrow(resp.body.amount_credit); const debit = Amounts.parseOrThrow(resp.body.amount_debit); diff --git a/packages/demobank-ui/src/pages/ConversionConfig.tsx b/packages/demobank-ui/src/pages/ConversionConfig.tsx index 37dd580c2..2d52cd99f 100644 --- a/packages/demobank-ui/src/pages/ConversionConfig.tsx +++ b/packages/demobank-ui/src/pages/ConversionConfig.tsx @@ -198,7 +198,7 @@ function useComponentState({ estimateByDebit: calculateCashinFromDebit, } = useCashinEstimator(); - const [calc, setCalc] = useState<{ cashin: TransferCalculation, cashout: TransferCalculation }>() + const [calculationResult, setCalc] = useState<{ cashin: TransferCalculation, cashout: TransferCalculation }>() useEffect(() => { async function doAsync() { @@ -209,7 +209,10 @@ function useComponentState({ const in_fee = Amounts.parseOrThrow(info.conversion_rate.cashin_fee) const cashin = await calculateCashinFromDebit(in_amount, in_fee); - + if (cashin === "amount-is-too-small") { + setCalc(undefined) + return; + } // const out_amount = Amounts.parseOrThrow(`${info.regional_currency}:${form.amount.value}`) const out_fee = Amounts.parseOrThrow(info.conversion_rate.cashout_fee) const cashout = await calculateCashoutFromDebit(cashin.credit, out_fee); @@ -221,7 +224,8 @@ function useComponentState({ }, [form.amount?.value, form.conv?.cashin_fee?.value, form.conv?.cashout_fee?.value]); const [section, setSection] = useState<"detail" | "cashout" | "cashin">("detail") - + const cashinCalc = calculationResult?.cashin === "amount-is-too-small" ? undefined : calculationResult?.cashin + const cashoutCalc = calculationResult?.cashout === "amount-is-too-small" ? undefined : calculationResult?.cashout async function doUpdate() { if (!creds) return await handleError(async () => { @@ -447,7 +451,7 @@ function useComponentState({ - {!calc ? undefined : ( + {!cashoutCalc || !cashinCalc ? undefined : (
@@ -457,7 +461,7 @@ function useComponentState({
- {Amounts.isZero(calc.cashin.beforeFee) ? undefined : ( + {Amounts.isZero(cashinCalc.beforeFee) ? undefined : (
@@ -474,7 +478,7 @@ function useComponentState({
@@ -486,7 +490,7 @@ function useComponentState({
@@ -503,7 +507,7 @@ function useComponentState({
- {Amounts.isZero(calc.cashout.beforeFee) ? undefined : ( + {Amounts.isZero(cashoutCalc.beforeFee) ? undefined : (
@@ -520,7 +524,7 @@ function useComponentState({
@@ -532,7 +536,7 @@ function useComponentState({
@@ -541,7 +545,7 @@ function useComponentState({
- {calc && status.status === "ok" && Amounts.cmp(status.result.amount, calc.cashout.credit) < 0 ?
+ {cashoutCalc && status.status === "ok" && Amounts.cmp(status.result.amount, cashoutCalc.credit) < 0 ?
This configuration allows users to cash out more of what has been cashed in. diff --git a/packages/demobank-ui/src/pages/business/CreateCashout.tsx b/packages/demobank-ui/src/pages/business/CreateCashout.tsx index 76678b6d2..80cb64c4c 100644 --- a/packages/demobank-ui/src/pages/business/CreateCashout.tsx +++ b/packages/demobank-ui/src/pages/business/CreateCashout.tsx @@ -233,11 +233,11 @@ export function CreateCashout({ ? i18n.str`Balance is not enough` : form.isDebit && Amounts.cmp(inputAmount, conversionInfo.cashout_min_amount) < 1 ? i18n.str`Needs to be higher than ${Amounts.stringifyValueWithSpec(Amounts.parseOrThrow(conversionInfo.cashout_min_amount), regional_currency_specification).normal}` - : calculationResult === "amount-is-too-small" - ? i18n.str`Amount needs to be higher` - : Amounts.isZero(calc.credit) - ? i18n.str`The total transfer at destination will be zero` - : undefined, + : calculationResult === "amount-is-too-small" + ? i18n.str`Amount needs to be higher` + : Amounts.isZero(calc.credit) + ? i18n.str`The total transfer at destination will be zero` + : undefined, channel: OLD_CASHOUT_API && !form.channel ? i18n.str`Required` : undefined, }); const trimmedAmountStr = form.amount?.trim(); @@ -438,6 +438,7 @@ export function CreateCashout({ for="subject" > {i18n.str`Transfer subject`} + *
*