mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-10 18:43:41 +08:00
chore: refactor updateTokenDto struct in token.go for better clarity
This commit is contained in:
@@ -215,8 +215,9 @@ func DeleteToken(c *gin.Context) {
|
|||||||
|
|
||||||
type updateTokenDto struct {
|
type updateTokenDto struct {
|
||||||
model.Token
|
model.Token
|
||||||
// AddUsedQuota add or subtract used quota
|
// AddUsedQuota add or subtract used quota from another source
|
||||||
AddUsedQuota int `json:"add_used_quota" gorm:"-"`
|
AddUsedQuota int `json:"add_used_quota" gorm:"-"`
|
||||||
|
// AddReason is the reason for adding or subtracting used quota
|
||||||
AddReason string `json:"add_reason" gorm:"-"`
|
AddReason string `json:"add_reason" gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,21 +277,13 @@ func UpdateToken(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if statusOnly != "" {
|
|
||||||
cleanToken.Status = token.Status
|
|
||||||
} else {
|
|
||||||
// If you add more fields, please also update token.Update()
|
|
||||||
cleanToken.Name = token.Name
|
|
||||||
cleanToken.ExpiredTime = token.ExpiredTime
|
|
||||||
cleanToken.RemainQuota = token.RemainQuota
|
|
||||||
cleanToken.UnlimitedQuota = token.UnlimitedQuota
|
|
||||||
cleanToken.Models = token.Models
|
|
||||||
cleanToken.Subnet = token.Subnet
|
|
||||||
}
|
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case statusOnly != "":
|
||||||
|
cleanToken.Status = token.Status
|
||||||
|
case tokenPatch.AddUsedQuota != 0:
|
||||||
// let admin to add or subtract used quota,
|
// let admin to add or subtract used quota,
|
||||||
// make it possible to aggregate billings from different sources.
|
// make it possible to aggregate billings from different sources.
|
||||||
if tokenPatch.AddUsedQuota != 0 {
|
|
||||||
if cleanToken.RemainQuota < int64(tokenPatch.AddUsedQuota) {
|
if cleanToken.RemainQuota < int64(tokenPatch.AddUsedQuota) {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
@@ -302,6 +295,13 @@ func UpdateToken(c *gin.Context) {
|
|||||||
cleanToken.UsedQuota += int64(tokenPatch.AddUsedQuota)
|
cleanToken.UsedQuota += int64(tokenPatch.AddUsedQuota)
|
||||||
cleanToken.RemainQuota -= int64(tokenPatch.AddUsedQuota)
|
cleanToken.RemainQuota -= int64(tokenPatch.AddUsedQuota)
|
||||||
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("外部(%s)消耗 %s", tokenPatch.AddReason, common.LogQuota(int64(tokenPatch.AddUsedQuota))))
|
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("外部(%s)消耗 %s", tokenPatch.AddReason, common.LogQuota(int64(tokenPatch.AddUsedQuota))))
|
||||||
|
default:
|
||||||
|
// If you add more fields, please also update token.Update()
|
||||||
|
cleanToken.Name = token.Name
|
||||||
|
cleanToken.ExpiredTime = token.ExpiredTime
|
||||||
|
cleanToken.UnlimitedQuota = token.UnlimitedQuota
|
||||||
|
cleanToken.Models = token.Models
|
||||||
|
cleanToken.Subnet = token.Subnet
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cleanToken.Update()
|
err = cleanToken.Update()
|
||||||
|
|||||||
Reference in New Issue
Block a user