From b00a69d9efbb4ed49b53cea883021c3455f64f86 Mon Sep 17 00:00:00 2001 From: kiwiidb Date: Thu, 6 Oct 2022 13:03:07 +0200 Subject: [PATCH 1/2] delete invoices when balance check fails --- controllers_v2/payinvoice.ctrl.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/controllers_v2/payinvoice.ctrl.go b/controllers_v2/payinvoice.ctrl.go index 1e8dcf2..de770dc 100644 --- a/controllers_v2/payinvoice.ctrl.go +++ b/controllers_v2/payinvoice.ctrl.go @@ -89,6 +89,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error { currentBalance, err := controller.svc.CurrentUserBalance(c.Request().Context(), userID) if err != nil { + controller.svc.DB.NewDelete().Model(&invoice).Where("id = ?", invoice.ID).Exec(c.Request().Context()) return err } @@ -98,6 +99,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error { } if currentBalance < minimumBalance { c.Logger().Errorf("User does not have enough balance invoice_id:%v user_id:%v balance:%v amount:%v", invoice.ID, userID, currentBalance, invoice.Amount) + controller.svc.DB.NewDelete().Model(&invoice).Where("id = ?", invoice.ID).Exec(c.Request().Context()) return c.JSON(http.StatusBadRequest, responses.NotEnoughBalanceError) } From f7dd956ac43057d108f5d4e8f289747cb309c84f Mon Sep 17 00:00:00 2001 From: kiwiidb Date: Fri, 28 Oct 2022 09:37:55 +0200 Subject: [PATCH 2/2] chore: also delete balance check failed invoices in v1 controllers --- controllers/payinvoice.ctrl.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/controllers/payinvoice.ctrl.go b/controllers/payinvoice.ctrl.go index bc0ef1e..cd4a445 100644 --- a/controllers/payinvoice.ctrl.go +++ b/controllers/payinvoice.ctrl.go @@ -87,6 +87,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error { currentBalance, err := controller.svc.CurrentUserBalance(c.Request().Context(), userID) if err != nil { + controller.svc.DB.NewDelete().Model(&invoice).Where("id = ?", invoice.ID).Exec(c.Request().Context()) return err } @@ -96,6 +97,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error { } if currentBalance < minimumBalance { c.Logger().Errorf("User does not have enough balance invoice_id:%v user_id:%v balance:%v amount:%v", invoice.ID, userID, currentBalance, invoice.Amount) + controller.svc.DB.NewDelete().Model(&invoice).Where("id = ?", invoice.ID).Exec(c.Request().Context()) return c.JSON(http.StatusBadRequest, responses.NotEnoughBalanceError) }