diff --git a/internal/service/handlers/submit_bonus_code.go b/internal/service/handlers/submit_bonus_code.go index 9e7e272..821e4bc 100644 --- a/internal/service/handlers/submit_bonus_code.go +++ b/internal/service/handlers/submit_bonus_code.go @@ -58,9 +58,28 @@ func SubmitBonusCode(w http.ResponseWriter, r *http.Request) { 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")