From cc83382d73a1f6a52abb319fa58a706cca38b58e Mon Sep 17 00:00:00 2001 From: Zaptoss Date: Tue, 8 Oct 2024 18:58:28 +0300 Subject: [PATCH 1/2] Backward compability --- .../service/handlers/submit_bonus_code.go | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/service/handlers/submit_bonus_code.go b/internal/service/handlers/submit_bonus_code.go index 9e7e272..5ff581f 100644 --- a/internal/service/handlers/submit_bonus_code.go +++ b/internal/service/handlers/submit_bonus_code.go @@ -26,6 +26,8 @@ func SubmitBonusCode(w http.ResponseWriter, r *http.Request) { return } + var bonus *data.BonusCode + // never panic because of auth validation nullifier := UserClaims(r)[0].Nullifier @@ -51,16 +53,35 @@ func SubmitBonusCode(w http.ResponseWriter, r *http.Request) { return } - bonus, err := BonusCodesQ(r).FilterByID(bonusCode).Get() + bonus, err = BonusCodesQ(r).FilterByID(bonusCode).Get() if err != nil { log.WithError(err).Error("Failed to get bonus code") ape.RenderErr(w, problems.InternalError()) return } if bonus == nil { - log.Debug("Bonus code absent in db") - ape.RenderErr(w, problems.NotFound()) - return + evTypes := EventTypes(r).List(func(ev models.EventType) bool { + return ev.QRCodeValue == nil || *ev.QRCodeValue != bonusCode + }) + if len(evTypes) == 0 { + log.Debugf("Bonus code absent and old event also") + ape.RenderErr(w, problems.NotFound()) + return + } + + bonus = &data.BonusCode{ + ID: *evTypes[0].QRCodeValue, + Reward: int(evTypes[0].Reward), + UsageCount: 0, + Infinity: true, + } + + err = BonusCodesQ(r).Insert(*bonus) + if err != nil { + log.WithError(err).Error("Failed to get insert bonus code") + ape.RenderErr(w, problems.InternalError()) + return + } } if !bonus.Infinity && bonus.UsageCount <= 0 { log.Debug("Bonus code usage count exceed") From 7b34baa6ab10fe08e74d7c46b36430fa61ef5675 Mon Sep 17 00:00:00 2001 From: Zaptoss Date: Wed, 9 Oct 2024 12:12:48 +0300 Subject: [PATCH 2/2] Remove unnecessary var --- internal/service/handlers/submit_bonus_code.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/service/handlers/submit_bonus_code.go b/internal/service/handlers/submit_bonus_code.go index 5ff581f..821e4bc 100644 --- a/internal/service/handlers/submit_bonus_code.go +++ b/internal/service/handlers/submit_bonus_code.go @@ -26,8 +26,6 @@ func SubmitBonusCode(w http.ResponseWriter, r *http.Request) { return } - var bonus *data.BonusCode - // never panic because of auth validation nullifier := UserClaims(r)[0].Nullifier @@ -53,7 +51,7 @@ func SubmitBonusCode(w http.ResponseWriter, r *http.Request) { return } - bonus, err = BonusCodesQ(r).FilterByID(bonusCode).Get() + bonus, err := BonusCodesQ(r).FilterByID(bonusCode).Get() if err != nil { log.WithError(err).Error("Failed to get bonus code") ape.RenderErr(w, problems.InternalError())