feat: wire admin UI to credit ledger APIs

This commit is contained in:
OpenClaw Agent
2026-09-17 06:08:20 +08:00
parent d9df512e0a
commit 3025019504
4 changed files with 146 additions and 2 deletions
+91
View File
@@ -0,0 +1,91 @@
package main
import (
"github.com/gin-gonic/gin"
"strconv"
)
func (a *App) adminUserCreditTransactions(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
c.JSON(400, gin.H{"error": "invalid user id"})
return
}
limit := 50
if n, e := strconv.Atoi(c.DefaultQuery("limit", "50")); e == nil && n > 0 && n <= 200 {
limit = n
}
var txs []CreditTransaction
q := a.db.Where("user_id = ?", id).Order("id desc").Limit(limit)
if before := c.Query("before"); before != "" {
if n, e := strconv.ParseUint(before, 10, 64); e == nil {
q = q.Where("id < ?", n)
}
}
if err := q.Find(&txs).Error; err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
type item struct {
ID uint `json:"id"`
Type string `json:"type"`
Amount int64 `json:"amount"`
BalanceAfter int64 `json:"balanceAfter"`
ReferenceType string `json:"referenceType"`
ReferenceID string `json:"referenceId"`
Reason string `json:"reason"`
CreatedAt interface{} `json:"createdAt"`
}
out := make([]item, 0, len(txs))
for _, t := range txs {
out = append(out, item{t.ID, t.Type, t.Amount, t.BalanceAfter, t.ReferenceType, t.ReferenceID, t.Reason, t.CreatedAt})
}
next := uint(0)
if len(txs) == limit {
next = txs[len(txs)-1].ID
}
c.JSON(200, gin.H{"data": out, "nextBefore": next})
}
func (a *App) adminCreditTransactions(c *gin.Context) {
limit := 100
if n, e := strconv.Atoi(c.DefaultQuery("limit", "100")); e == nil && n > 0 && n <= 300 {
limit = n
}
var txs []CreditTransaction
q := a.db.Order("id desc").Limit(limit)
if uid := c.Query("user_id"); uid != "" {
if n, e := strconv.ParseUint(uid, 10, 64); e == nil {
q = q.Where("user_id = ?", n)
}
}
if before := c.Query("before"); before != "" {
if n, e := strconv.ParseUint(before, 10, 64); e == nil {
q = q.Where("id < ?", n)
}
}
if err := q.Find(&txs).Error; err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
type item struct {
ID uint `json:"id"`
UserID uint `json:"userId"`
Type string `json:"type"`
Amount int64 `json:"amount"`
BalanceAfter int64 `json:"balanceAfter"`
ReferenceType string `json:"referenceType"`
ReferenceID string `json:"referenceId"`
Reason string `json:"reason"`
CreatedAt interface{} `json:"createdAt"`
}
out := make([]item, 0, len(txs))
for _, t := range txs {
out = append(out, item{t.ID, t.UserID, t.Type, t.Amount, t.BalanceAfter, t.ReferenceType, t.ReferenceID, t.Reason, t.CreatedAt})
}
next := uint(0)
if len(txs) == limit {
next = txs[len(txs)-1].ID
}
c.JSON(200, gin.H{"data": out, "nextBefore": next})
}