From 0e35db82d5241b82a3d5bc527a631bf66bce0c71 Mon Sep 17 00:00:00 2001 From: "Laisky.Cai" Date: Mon, 24 Jun 2024 05:52:23 +0000 Subject: [PATCH] chore: Update token.go to allow admin to add or subtract used quota --- controller/token.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/controller/token.go b/controller/token.go index db556ad2..4df32dc7 100644 --- a/controller/token.go +++ b/controller/token.go @@ -288,7 +288,19 @@ func UpdateToken(c *gin.Context) { cleanToken.Subnet = token.Subnet } + // let admin to add or subtract used quota, + // make it possible to aggregate billings from different sources. if tokenPatch.AddUsedQuota != 0 { + if cleanToken.RemainQuota < int64(tokenPatch.AddUsedQuota) { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "剩余额度不足", + }) + return + } + + cleanToken.UsedQuota += int64(tokenPatch.AddUsedQuota) + cleanToken.RemainQuota -= int64(tokenPatch.AddUsedQuota) model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("外部(%s)消耗 %s", tokenPatch.AddReason, common.LogQuota(int64(tokenPatch.AddUsedQuota)))) }