fix: issues
This commit is contained in:
@@ -54,6 +54,7 @@ func (c *Controller) Info(ctx fiber.Ctx) error {
|
||||
return errors.Wrapf(err, "get tenant: %d", claim.TenantID)
|
||||
}
|
||||
info.IsAdmin = tenant.BindUserID == claim.UserID
|
||||
info.AdminContact = tenant.BindUserContact
|
||||
|
||||
return ctx.JSON(info)
|
||||
}
|
||||
@@ -92,3 +93,26 @@ func (c *Controller) GetChargeCodes(ctx fiber.Ctx) error {
|
||||
|
||||
return ctx.JSON(codes)
|
||||
}
|
||||
|
||||
// BalanceHistory
|
||||
func (c *Controller) BalanceHistory(ctx fiber.Ctx) error {
|
||||
claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim)
|
||||
log.Debug(claim)
|
||||
|
||||
histories, err := c.svc.GetBalanceHistory(ctx.Context(), claim.TenantID, claim.UserID)
|
||||
if err != nil {
|
||||
return errorx.Wrap(err)
|
||||
}
|
||||
|
||||
items := []BalanceHistory{}
|
||||
for _, h := range histories {
|
||||
items = append(items, BalanceHistory{
|
||||
Balance: h.Balance,
|
||||
Type: h.Type,
|
||||
Target: h.Target,
|
||||
CreatedAt: h.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
return ctx.JSON(items)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"backend/pkg/pg"
|
||||
)
|
||||
|
||||
type UserInfo struct {
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
Balance int64 `json:"balance"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
AdminContact string `json:"admin_contact"`
|
||||
Balance int64 `json:"balance"`
|
||||
}
|
||||
|
||||
type BalanceHistory struct {
|
||||
Type pg.BalanceType `json:"type"`
|
||||
Target pg.BalanceTarget `json:"target"`
|
||||
Balance int64 `json:"balance"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
@@ -29,4 +29,5 @@ func (r *Router) Register(router fiber.Router) {
|
||||
group.Get("info", r.controller.Info)
|
||||
group.Patch("charge/:code", r.controller.Charge)
|
||||
group.Get("codes", r.controller.GetChargeCodes)
|
||||
group.Get("balance-histories", r.controller.BalanceHistory)
|
||||
}
|
||||
|
||||
@@ -413,3 +413,28 @@ func (svc *Service) SetTenantBindUserID(ctx context.Context, tenantID, adminUser
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetBalanceHistory
|
||||
func (svc *Service) GetBalanceHistory(ctx context.Context, tenantID, userID int64) ([]model.UserBalanceHistories, error) {
|
||||
log := svc.log.WithField("method", "GetBalanceHistory")
|
||||
|
||||
tbl := table.UserBalanceHistories
|
||||
stmt := tbl.
|
||||
SELECT(
|
||||
tbl.AllColumns,
|
||||
).
|
||||
WHERE(
|
||||
tbl.TenantID.EQ(Int64(tenantID)).AND(tbl.UserID.EQ(Int64(userID))),
|
||||
).
|
||||
ORDER_BY(
|
||||
tbl.CreatedAt.DESC(),
|
||||
).
|
||||
LIMIT(20)
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var items []model.UserBalanceHistories
|
||||
if err := stmt.QueryContext(ctx, db.FromContext(ctx, svc.db), &items); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to query balance history")
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user