From 140c4c55dd072296d7a03300ee24ce2126ea7dde Mon Sep 17 00:00:00 2001 From: meskio <meskio@sindominio.net> Date: Mon, 28 Sep 2020 19:31:49 +0200 Subject: [PATCH] Block empty purchases --- api/purchase.go | 5 +++++ src/ShowTransaction.js | 2 ++ src/purchase/Purchase.js | 6 +++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/api/purchase.go b/api/purchase.go index 270bc3b..d57b024 100644 --- a/api/purchase.go +++ b/api/purchase.go @@ -39,6 +39,11 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) { total += product.Price * p.Ammount purchase[i].Price = product.Price } + if total == 0 { + log.Printf("Empty purchase (%d)", num) + w.WriteHeader(http.StatusNotAcceptable) + return + } httpStatus := a.substractMemberBalance(num, total) if httpStatus != http.StatusOK { diff --git a/src/ShowTransaction.js b/src/ShowTransaction.js index 28f2ede..304f0b6 100644 --- a/src/ShowTransaction.js +++ b/src/ShowTransaction.js @@ -14,6 +14,8 @@ function ShowTransaction() { case "purchase": show_list = <ShowPurchase purchase={transaction.purchase} />; break; + default: + show_list = null; } return ( diff --git a/src/purchase/Purchase.js b/src/purchase/Purchase.js index 1e27cb5..7b7ca43 100644 --- a/src/purchase/Purchase.js +++ b/src/purchase/Purchase.js @@ -93,9 +93,9 @@ class Purchase extends React.Component { }) .catch(error => { if (error.name === "not-money") { - this.setState({isLoading: false, error: null, noMoney: true}); + this.setState({isLoading: false, noMoney: true}); } else { - this.setState({isLoading: false, error: error}) + this.setState({isLoading: false, error: error.message}) } }); } @@ -106,7 +106,7 @@ class Purchase extends React.Component { } let alert; - if (this.state.noMoney != null) { + if (this.state.noMoney) { alert = ( <Alert variant="warning"> No tienes suficiente dinero para realizar esta compra. -- GitLab