fix: group order queries to preserve tenant scope

This commit is contained in:
2026-01-12 10:34:07 +08:00
parent 75d13b5c4e
commit 16d13a2ab1
2 changed files with 15 additions and 6 deletions

View File

@@ -14,6 +14,7 @@ import (
"quyun/v2/pkg/consts"
"github.com/google/uuid"
"go.ipao.vip/gen/field"
"go.ipao.vip/gen/types"
"gorm.io/gorm"
)
@@ -29,8 +30,10 @@ func (s *order) ListUserOrders(ctx context.Context, tenantID, userID int64, stat
tbl, q := models.OrderQuery.QueryContext(ctx)
if tenantID > 0 {
q = q.Where(tbl.UserID.Eq(uid), tbl.TenantID.Eq(tenantID)).
Or(tbl.UserID.Eq(uid), tbl.Type.Eq(consts.OrderTypeRecharge))
q = q.Where(field.Or(
field.And(tbl.UserID.Eq(uid), tbl.TenantID.Eq(tenantID)),
field.And(tbl.UserID.Eq(uid), tbl.Type.Eq(consts.OrderTypeRecharge)),
))
} else {
q = q.Where(tbl.UserID.Eq(uid))
}
@@ -60,8 +63,10 @@ func (s *order) GetUserOrder(ctx context.Context, tenantID, userID, id int64) (*
tbl, q := models.OrderQuery.QueryContext(ctx)
itemQuery := q
if tenantID > 0 {
itemQuery = itemQuery.Where(tbl.ID.Eq(id), tbl.UserID.Eq(uid), tbl.TenantID.Eq(tenantID)).
Or(tbl.ID.Eq(id), tbl.UserID.Eq(uid), tbl.Type.Eq(consts.OrderTypeRecharge))
itemQuery = itemQuery.Where(field.Or(
field.And(tbl.ID.Eq(id), tbl.UserID.Eq(uid), tbl.TenantID.Eq(tenantID)),
field.And(tbl.ID.Eq(id), tbl.UserID.Eq(uid), tbl.Type.Eq(consts.OrderTypeRecharge)),
))
} else {
itemQuery = itemQuery.Where(tbl.ID.Eq(id), tbl.UserID.Eq(uid))
}

View File

@@ -12,6 +12,7 @@ import (
"quyun/v2/pkg/consts"
"github.com/google/uuid"
"go.ipao.vip/gen/field"
"go.ipao.vip/gen/types"
"gorm.io/gorm"
)
@@ -33,8 +34,11 @@ func (s *wallet) GetWallet(ctx context.Context, tenantID, userID int64) (*user_d
// Both purchase (expense) and recharge (income - if paid)
tbl, q := models.OrderQuery.QueryContext(ctx)
if tenantID > 0 {
q = q.Where(tbl.UserID.Eq(userID), tbl.Status.Eq(consts.OrderStatusPaid), tbl.TenantID.Eq(tenantID)).
Or(tbl.UserID.Eq(userID), tbl.Status.Eq(consts.OrderStatusPaid), tbl.Type.Eq(consts.OrderTypeRecharge))
q = q.Where(tbl.Status.Eq(consts.OrderStatusPaid)).
Where(field.Or(
field.And(tbl.UserID.Eq(userID), tbl.TenantID.Eq(tenantID)),
field.And(tbl.UserID.Eq(userID), tbl.Type.Eq(consts.OrderTypeRecharge)),
))
} else {
q = q.Where(tbl.UserID.Eq(userID), tbl.Status.Eq(consts.OrderStatusPaid))
}