diff --git a/server/ente/admin.go b/server/ente/admin.go index 75ab9f3b53..3990635324 100644 --- a/server/ente/admin.go +++ b/server/ente/admin.go @@ -87,6 +87,7 @@ const ( ) type SupportUpdateBonus struct { + BonusType string `json:"bonusType" binding:"required"` Action AddOnAction `json:"action" binding:"required"` UserID int64 `json:"userID" binding:"required"` Year int `json:"year"` @@ -105,17 +106,26 @@ func (u SupportUpdateBonus) UpdateLog() string { } func (u SupportUpdateBonus) Validate() error { + isSupportBonus := u.BonusType == "ADD_ON_SUPPORT" + if u.BonusType != "ADD_ON_SUPPORT" && u.BonusType != "ADD_ON_BF_2023" { + return errors.New("invalid bonus type") + } if u.Action == ADD || u.Action == UPDATE { if u.Testing { if u.StorageInMB == 0 && u.Minute == 0 { return errors.New("invalid input, set in MB and minute for test") } } else { - if u.StorageInGB != 100 && u.StorageInGB != 2000 && u.StorageInGB != 500 { + if u.StorageInGB != 200 && u.StorageInGB != 2000 && u.StorageInGB != 500 { return errors.New("invalid input for deal, only 100, 500, 2000 allowed") } - if u.Year != 3 && u.Year != 5 { + if isSupportBonus { + if u.Year == 0 || u.Year > 100 { + return errors.New("invalid input for year, only 1-100") + } + } else if u.Year != 3 && u.Year != 5 { return errors.New("invalid input for year, only 3 or 5") + } } } diff --git a/server/ente/storagebonus/storge_bonus.go b/server/ente/storagebonus/storge_bonus.go index 9a876fb4d7..6ea63386a0 100644 --- a/server/ente/storagebonus/storge_bonus.go +++ b/server/ente/storagebonus/storge_bonus.go @@ -34,6 +34,21 @@ func (t BonusType) ExtendsExpiry() bool { } } +func BonusFromType(bonusType string) BonusType { + switch bonusType { + case "REFERRAL": + return Referral + case "SIGN_UP": + return SignUp + case "ADD_ON_SUPPORT": + return AddOnSupport + case "ADD_ON_BF_2023": + return AddOnBf2023 + default: + return "" + } +} + // RestrictToDoublingStorage returns true if the bonus type restricts the doubling of storage. // This indicates, the usable bonus storage should not exceed the current plan storage. // Note: Current plan storage includes both base subscription and storage bonus that can ExtendsExpiry diff --git a/server/pkg/api/admin.go b/server/pkg/api/admin.go index 6e301df7e1..92786c3d28 100644 --- a/server/pkg/api/admin.go +++ b/server/pkg/api/admin.go @@ -472,13 +472,14 @@ func (h *AdminHandler) UpdateBonus(c *gin.Context) { validTill = gTime.Now().AddDate(r.Year, 0, 0).UnixMicro() } var err error + bonusType := bonusEntity.BonusType(r.BonusType) switch r.Action { case ente.ADD: - err = h.StorageBonusRepo.InsertAddOnBonus(c, bonusEntity.AddOnBf2023, r.UserID, validTill, storage) + err = h.StorageBonusRepo.InsertAddOnBonus(c, bonusType, r.UserID, validTill, storage) case ente.UPDATE: - err = h.StorageBonusRepo.UpdateAddOnBonus(c, bonusEntity.AddOnBf2023, r.UserID, validTill, storage) + err = h.StorageBonusRepo.UpdateAddOnBonus(c, bonusType, r.UserID, validTill, storage) case ente.REMOVE: - _, err = h.StorageBonusRepo.RemoveAddOnBonus(c, bonusEntity.AddOnBf2023, r.UserID) + _, err = h.StorageBonusRepo.RemoveAddOnBonus(c, bonusType, r.UserID) } if err != nil { handler.Error(c, stacktrace.Propagate(err, ""))