taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit a63b2a878b374f3076990131977f16d923d8a63b
parent 73ab9970e17f2a4749bad88631a4bd4d8e64721e
Author: Sebastian <sebasjm@gmail.com>
Date:   Tue,  5 Mar 2024 14:58:52 -0300

check min in conversion admin

Diffstat:
Mpackages/demobank-ui/src/hooks/circuit.ts | 40+++++++++++++++++++++++++++-------------
Mpackages/demobank-ui/src/pages/ConversionConfig.tsx | 30+++++++++++++++++-------------
Mpackages/demobank-ui/src/pages/business/CreateCashout.tsx | 12+++++++-----
3 files changed, 51 insertions(+), 31 deletions(-)

diff --git 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 @@ -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({ </div> </div> - {!calc ? undefined : ( + {!cashoutCalc || !cashinCalc ? undefined : ( <div class="px-6 pt-6"> <div class="sm:col-span-5"> <dl class="mt-4 space-y-4"> @@ -457,7 +461,7 @@ function useComponentState({ </dt> <dd class="text-sm text-gray-900"> <RenderAmount - value={calc.cashin.debit} + value={cashinCalc.debit} negative withColor spec={info.regional_currency_specification} @@ -465,7 +469,7 @@ function useComponentState({ </dd> </div> - {Amounts.isZero(calc.cashin.beforeFee) ? undefined : ( + {Amounts.isZero(cashinCalc.beforeFee) ? undefined : ( <div class="flex items-center justify-between afu "> <dt class="flex items-center text-sm text-gray-600"> <span> @@ -474,7 +478,7 @@ function useComponentState({ </dt> <dd class="text-sm text-gray-900"> <RenderAmount - value={calc.cashin.beforeFee} + value={cashinCalc.beforeFee} spec={info.fiat_currency_specification} /> </dd> @@ -486,7 +490,7 @@ function useComponentState({ </dt> <dd class="text-lg text-gray-900 font-medium"> <RenderAmount - value={calc.cashin.credit} + value={cashinCalc.credit} withColor spec={info.fiat_currency_specification} /> @@ -503,7 +507,7 @@ function useComponentState({ </dt> <dd class="text-sm text-gray-900"> <RenderAmount - value={calc.cashout.debit} + value={cashoutCalc.debit} negative withColor spec={info.fiat_currency_specification} @@ -511,7 +515,7 @@ function useComponentState({ </dd> </div> - {Amounts.isZero(calc.cashout.beforeFee) ? undefined : ( + {Amounts.isZero(cashoutCalc.beforeFee) ? undefined : ( <div class="flex items-center justify-between afu"> <dt class="flex items-center text-sm text-gray-600"> <span> @@ -520,7 +524,7 @@ function useComponentState({ </dt> <dd class="text-sm text-gray-900"> <RenderAmount - value={calc.cashout.beforeFee} + value={cashoutCalc.beforeFee} spec={info.regional_currency_specification} /> </dd> @@ -532,7 +536,7 @@ function useComponentState({ </dt> <dd class="text-lg text-gray-900 font-medium"> <RenderAmount - value={calc.cashout.credit} + value={cashoutCalc.credit} withColor spec={info.regional_currency_specification} /> @@ -541,7 +545,7 @@ function useComponentState({ </dl> </div> - {calc && status.status === "ok" && Amounts.cmp(status.result.amount, calc.cashout.credit) < 0 ? <div class="p-4"> + {cashoutCalc && status.status === "ok" && Amounts.cmp(status.result.amount, cashoutCalc.credit) < 0 ? <div class="p-4"> <Attention title={i18n.str`Bad configuration`} type="warning"> <i18n.Translate> 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 @@ -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`} + <b style={{ color: "red" }}> *</b> </label> <div class="mt-2"> <input @@ -472,6 +473,7 @@ export function CreateCashout({ {form.isDebit ? i18n.str`Amount to send` : i18n.str`Amount to receive`} + <b style={{ color: "red" }}> *</b> </label> <button type="button"