Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions controller/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,21 @@ func ListModels(c *gin.Context, modelType int) {
}

userModelNames := make([]string, 0)
groups, err := getModelListGroups(c)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "get user group failed",
})
return
}
ownerGroups := groups.ownerGroups
modelLimitEnable := common.GetContextKeyBool(c, constant.ContextKeyTokenModelLimitEnabled)
var groups modelListGroups
var ownerGroups []string
if !modelLimitEnable {
var err error
groups, err = getModelListGroups(c)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "get user group failed",
})
return
}
ownerGroups = groups.ownerGroups
}
if modelLimitEnable {
s, ok := common.GetContextKey(c, constant.ContextKeyTokenModelLimit)
var tokenModelLimit map[string]bool
Expand Down
45 changes: 38 additions & 7 deletions controller/redemption.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ func AddRedemption(c *gin.Context) {
common.ApiErrorI18n(c, i18n.MsgRedemptionCountMax)
return
}
redemption.RedemptionType = model.NormalizeRedemptionType(redemption.RedemptionType)
if redemption.RedemptionType == model.RedemptionTypeSubscription {
if redemption.SubscriptionPlanId <= 0 {
common.ApiErrorI18n(c, i18n.MsgSubscriptionInvalidId)
return
}
if _, err := model.GetSubscriptionPlanById(redemption.SubscriptionPlanId); err != nil {
common.ApiErrorI18n(c, i18n.MsgSubscriptionInvalidId)
return
}
redemption.Quota = 0
} else {
redemption.SubscriptionPlanId = 0
}
if valid, msg := validateExpiredTime(c, redemption.ExpiredTime); !valid {
c.JSON(http.StatusOK, gin.H{"success": false, "message": msg})
return
Expand All @@ -91,12 +105,14 @@ func AddRedemption(c *gin.Context) {
for i := 0; i < redemption.Count; i++ {
key := common.GetUUID()
cleanRedemption := model.Redemption{
UserId: c.GetInt("id"),
Name: redemption.Name,
Key: key,
CreatedTime: common.GetTimestamp(),
Quota: redemption.Quota,
ExpiredTime: redemption.ExpiredTime,
UserId: c.GetInt("id"),
Name: redemption.Name,
Key: key,
CreatedTime: common.GetTimestamp(),
Quota: redemption.Quota,
RedemptionType: redemption.RedemptionType,
SubscriptionPlanId: redemption.SubscriptionPlanId,
ExpiredTime: redemption.ExpiredTime,
}
err = cleanRedemption.Insert()
if err != nil {
Expand Down Expand Up @@ -152,7 +168,22 @@ func UpdateRedemption(c *gin.Context) {
}
// If you add more fields, please also update redemption.Update()
cleanRedemption.Name = redemption.Name
cleanRedemption.Quota = redemption.Quota
cleanRedemption.RedemptionType = model.NormalizeRedemptionType(redemption.RedemptionType)
if cleanRedemption.RedemptionType == model.RedemptionTypeSubscription {
if redemption.SubscriptionPlanId <= 0 {
common.ApiErrorI18n(c, i18n.MsgSubscriptionInvalidId)
return
}
if _, err := model.GetSubscriptionPlanById(redemption.SubscriptionPlanId); err != nil {
common.ApiErrorI18n(c, i18n.MsgSubscriptionInvalidId)
return
}
cleanRedemption.Quota = 0
cleanRedemption.SubscriptionPlanId = redemption.SubscriptionPlanId
} else {
cleanRedemption.Quota = redemption.Quota
cleanRedemption.SubscriptionPlanId = 0
}
cleanRedemption.ExpiredTime = redemption.ExpiredTime
}
if statusOnly != "" {
Expand Down
53 changes: 53 additions & 0 deletions controller/redemption_topup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package controller

import (
"encoding/json"
"net/http"
"testing"

"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTopUpQuotaRedemptionKeepsLegacyNumberResponse(t *testing.T) {
db := openTokenControllerTestDB(t)
require.NoError(t, db.AutoMigrate(&model.User{}, &model.Redemption{}, &model.Log{}))
confirmPaymentComplianceForTest(t)

user := &model.User{
Id: 701,
Username: "topup_quota_user",
Status: common.UserStatusEnabled,
Quota: 10,
Group: "default",
}
require.NoError(t, db.Create(user).Error)
require.NoError(t, db.Create(&model.Redemption{
UserId: 1,
Key: "topup-quota-code",
Status: common.RedemptionCodeStatusEnabled,
Name: "TopUp Quota",
RedemptionType: model.RedemptionTypeQuota,
Quota: 25,
CreatedTime: common.GetTimestamp(),
}).Error)

ctx, recorder := newAuthenticatedContext(t, http.MethodPost, "/api/user/topup", map[string]string{
"key": "topup-quota-code",
}, user.Id)
TopUp(ctx)

require.Equal(t, http.StatusOK, recorder.Code)
response := decodeAPIResponse(t, recorder)
require.True(t, response.Success)

var quota int
require.NoError(t, json.Unmarshal(response.Data, &quota))
assert.Equal(t, 25, quota)

var updatedUser model.User
require.NoError(t, db.Select("quota").Where("id = ?", user.Id).First(&updatedUser).Error)
assert.Equal(t, 35, updatedUser.Quota)
}
20 changes: 17 additions & 3 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1122,19 +1122,33 @@ 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) {
switch {
case errors.Is(err, model.ErrInvalidCode):
common.ApiErrorI18n(c, i18n.MsgRedemptionInvalid)
return
case errors.Is(err, model.ErrCodeUsed):
common.ApiErrorI18n(c, i18n.MsgRedemptionUsed)
return
case errors.Is(err, model.ErrCodeExpired):
common.ApiErrorI18n(c, i18n.MsgRedemptionExpired)
return
case errors.Is(err, model.ErrRedeemFailed):
common.ApiErrorI18n(c, i18n.MsgRedeemFailed)
return
}
common.ApiError(c, err)
return
}
data := any(result)
if result.RedemptionType == model.RedemptionTypeQuota {
data = result.Quota
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": quota,
"data": data,
})
}

Expand Down
7 changes: 6 additions & 1 deletion model/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ var (
)

// Redemption errors
var ErrRedeemFailed = errors.New("redeem.failed")
var (
ErrRedeemFailed = errors.New("redeem.failed")
ErrInvalidCode = errors.New("redeem.invalid_code")
ErrCodeUsed = errors.New("redeem.code_used")
ErrCodeExpired = errors.New("redeem.code_expired")
)

// 2FA errors
var ErrTwoFANotEnabled = errors.New("2fa not enabled")
Loading