[server] Allow adding support bonus

This commit is contained in:
Neeraj Gupta 2024-08-27 11:32:48 +05:30
parent bf7f1d43c0
commit 0925f7f0a2
3 changed files with 31 additions and 5 deletions

View File

@ -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")
}
}
}

View File

@ -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

View File

@ -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, ""))