Add subscription redemption code support
This commit is contained in:
+88
-16
@@ -3,6 +3,7 @@ package controller
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
@@ -19,10 +20,10 @@ func GetAllRedemptions(c *gin.Context) {
|
||||
common.ApiError(c, err)
|
||||
return
|
||||
}
|
||||
enrichRedemptions(redemptions)
|
||||
pageInfo.SetTotal(int(total))
|
||||
pageInfo.SetItems(redemptions)
|
||||
common.ApiSuccess(c, pageInfo)
|
||||
return
|
||||
}
|
||||
|
||||
func SearchRedemptions(c *gin.Context) {
|
||||
@@ -33,10 +34,10 @@ func SearchRedemptions(c *gin.Context) {
|
||||
common.ApiError(c, err)
|
||||
return
|
||||
}
|
||||
enrichRedemptions(redemptions)
|
||||
pageInfo.SetTotal(int(total))
|
||||
pageInfo.SetItems(redemptions)
|
||||
common.ApiSuccess(c, pageInfo)
|
||||
return
|
||||
}
|
||||
|
||||
func GetRedemption(c *gin.Context) {
|
||||
@@ -50,12 +51,12 @@ func GetRedemption(c *gin.Context) {
|
||||
common.ApiError(c, err)
|
||||
return
|
||||
}
|
||||
enrichRedemption(redemption)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "",
|
||||
"data": redemption,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func AddRedemption(c *gin.Context) {
|
||||
@@ -65,8 +66,7 @@ func AddRedemption(c *gin.Context) {
|
||||
common.ApiError(c, err)
|
||||
return
|
||||
}
|
||||
if utf8.RuneCountInString(redemption.Name) == 0 || utf8.RuneCountInString(redemption.Name) > 20 {
|
||||
common.ApiErrorI18n(c, i18n.MsgRedemptionNameLength)
|
||||
if !validateRedemptionPayload(c, &redemption, true) {
|
||||
return
|
||||
}
|
||||
if redemption.Count <= 0 {
|
||||
@@ -77,10 +77,6 @@ func AddRedemption(c *gin.Context) {
|
||||
common.ApiErrorI18n(c, i18n.MsgRedemptionCountMax)
|
||||
return
|
||||
}
|
||||
if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
|
||||
return
|
||||
}
|
||||
var keys []string
|
||||
for i := 0; i < redemption.Count; i++ {
|
||||
key := common.GetUUID()
|
||||
@@ -90,6 +86,9 @@ func AddRedemption(c *gin.Context) {
|
||||
Key: key,
|
||||
CreatedTime: common.GetTimestamp(),
|
||||
Quota: redemption.Quota,
|
||||
RedeemType: redemption.RedeemType,
|
||||
PlanId: redemption.PlanId,
|
||||
SourceNote: redemption.SourceNote,
|
||||
ExpiredTime: redemption.ExpiredTime,
|
||||
}
|
||||
err = cleanRedemption.Insert()
|
||||
@@ -109,7 +108,6 @@ func AddRedemption(c *gin.Context) {
|
||||
"message": "",
|
||||
"data": keys,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteRedemption(c *gin.Context) {
|
||||
@@ -123,7 +121,6 @@ func DeleteRedemption(c *gin.Context) {
|
||||
"success": true,
|
||||
"message": "",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateRedemption(c *gin.Context) {
|
||||
@@ -140,13 +137,14 @@ func UpdateRedemption(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if statusOnly == "" {
|
||||
if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
|
||||
if !validateRedemptionPayload(c, &redemption, true) {
|
||||
return
|
||||
}
|
||||
// If you add more fields, please also update redemption.Update()
|
||||
cleanRedemption.Name = redemption.Name
|
||||
cleanRedemption.Quota = redemption.Quota
|
||||
cleanRedemption.RedeemType = redemption.RedeemType
|
||||
cleanRedemption.PlanId = redemption.PlanId
|
||||
cleanRedemption.SourceNote = redemption.SourceNote
|
||||
cleanRedemption.ExpiredTime = redemption.ExpiredTime
|
||||
}
|
||||
if statusOnly != "" {
|
||||
@@ -157,12 +155,12 @@ func UpdateRedemption(c *gin.Context) {
|
||||
common.ApiError(c, err)
|
||||
return
|
||||
}
|
||||
enrichRedemption(cleanRedemption)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "",
|
||||
"data": cleanRedemption,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteInvalidRedemption(c *gin.Context) {
|
||||
@@ -176,7 +174,6 @@ func DeleteInvalidRedemption(c *gin.Context) {
|
||||
"message": "",
|
||||
"data": rows,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func validateExpiredTime(c *gin.Context, expired int64) (bool, string) {
|
||||
@@ -185,3 +182,78 @@ func validateExpiredTime(c *gin.Context, expired int64) (bool, string) {
|
||||
}
|
||||
return true, ""
|
||||
}
|
||||
|
||||
func validateRedemptionPayload(c *gin.Context, redemption *model.Redemption, requireName bool) bool {
|
||||
if redemption == nil {
|
||||
common.ApiErrorMsg(c, "invalid redemption payload")
|
||||
return false
|
||||
}
|
||||
redemption.Name = strings.TrimSpace(redemption.Name)
|
||||
redemption.SourceNote = strings.TrimSpace(redemption.SourceNote)
|
||||
redemption.RedeemType = model.NormalizeRedemptionType(redemption.RedeemType)
|
||||
if redemption.RedeemType == "" {
|
||||
common.ApiErrorMsg(c, "invalid redeem type")
|
||||
return false
|
||||
}
|
||||
if requireName && (utf8.RuneCountInString(redemption.Name) == 0 || utf8.RuneCountInString(redemption.Name) > 20) {
|
||||
common.ApiErrorI18n(c, i18n.MsgRedemptionNameLength)
|
||||
return false
|
||||
}
|
||||
if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
|
||||
c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
|
||||
return false
|
||||
}
|
||||
switch redemption.RedeemType {
|
||||
case model.RedemptionTypeQuota:
|
||||
if redemption.Quota <= 0 {
|
||||
common.ApiErrorMsg(c, "quota must be greater than 0")
|
||||
return false
|
||||
}
|
||||
redemption.PlanId = 0
|
||||
case model.RedemptionTypeSubscription:
|
||||
if redemption.PlanId <= 0 {
|
||||
common.ApiErrorMsg(c, "plan_id is required")
|
||||
return false
|
||||
}
|
||||
plan, err := model.GetSubscriptionPlanById(redemption.PlanId)
|
||||
if err != nil || plan == nil {
|
||||
common.ApiErrorMsg(c, "subscription plan not found")
|
||||
return false
|
||||
}
|
||||
redemption.Quota = 0
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func enrichRedemptions(redemptions []*model.Redemption) {
|
||||
if len(redemptions) == 0 {
|
||||
return
|
||||
}
|
||||
planTitles := make(map[int]string)
|
||||
for _, redemption := range redemptions {
|
||||
if redemption == nil || model.NormalizeRedemptionType(redemption.RedeemType) != model.RedemptionTypeSubscription || redemption.PlanId <= 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := planTitles[redemption.PlanId]; ok {
|
||||
redemption.PlanTitle = planTitles[redemption.PlanId]
|
||||
continue
|
||||
}
|
||||
plan, err := model.GetSubscriptionPlanById(redemption.PlanId)
|
||||
if err != nil || plan == nil {
|
||||
continue
|
||||
}
|
||||
planTitles[redemption.PlanId] = plan.Title
|
||||
redemption.PlanTitle = plan.Title
|
||||
}
|
||||
}
|
||||
|
||||
func enrichRedemption(redemption *model.Redemption) {
|
||||
if redemption == nil || model.NormalizeRedemptionType(redemption.RedeemType) != model.RedemptionTypeSubscription || redemption.PlanId <= 0 {
|
||||
return
|
||||
}
|
||||
plan, err := model.GetSubscriptionPlanById(redemption.PlanId)
|
||||
if err != nil || plan == nil {
|
||||
return
|
||||
}
|
||||
redemption.PlanTitle = plan.Title
|
||||
}
|
||||
|
||||
+2
-2
@@ -1025,7 +1025,7 @@ func TopUp(c *gin.Context) {
|
||||
common.ApiError(c, err)
|
||||
return
|
||||
}
|
||||
quota, err := model.Redeem(req.Key, id)
|
||||
result, err := model.Redeem(req.Key, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrRedeemFailed) {
|
||||
common.ApiErrorI18n(c, i18n.MsgRedeemFailed)
|
||||
@@ -1037,7 +1037,7 @@ func TopUp(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "",
|
||||
"data": quota,
|
||||
"data": result,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user