fix(tenant): 收紧双站后台边界并补齐货架管理

This commit is contained in:
yml
2026-11-15 22:28:40 +08:00
parent a68eb815a9
commit 0cb385d84f
42 changed files with 831 additions and 97 deletions
@@ -38,6 +38,9 @@ func (r *Repository) Arbitrate(ctx context.Context, adminID uint64, id uint64, r
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&order, row.OrderID).Error; err != nil {
return err
}
if !currentTenantCanAdminDisputeOrder(ctx, order.BuyerTenantID, order.DistributionSiteID) {
return gorm.ErrRecordNotFound
}
var listing model.RentalListing
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&listing, order.ListingID).Error; err != nil {
return err
@@ -171,6 +171,9 @@ func (r *Repository) AdminCreateByOrder(ctx context.Context, adminID uint64, ord
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&order, orderID).Error; err != nil {
return err
}
if !currentTenantCanAdminDisputeOrder(ctx, order.BuyerTenantID, order.DistributionSiteID) {
return gorm.ErrRecordNotFound
}
if order.Status == "completed" || order.Status == "cancelled" || order.Status == "closed" {
return ErrInvalidDispute
}
+29 -25
View File
@@ -5,6 +5,8 @@ import (
"strconv"
"strings"
"hfb_sys/backend/internal/tenancy"
"gorm.io/gorm"
)
@@ -48,7 +50,7 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminListQuery) (*Pagi
offset := (page - 1) * pageSize
var rows []disputeRow
err := applyAdminListFilters(r.baseQuery(ctx), query).
err := applyAdminListFilters(r.adminScopedBaseQuery(ctx), query).
Order("d.id DESC").
Offset(offset).
Limit(pageSize).
@@ -72,7 +74,7 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminListQuery) (*Pagi
func (r *Repository) FindAdminByID(ctx context.Context, id uint64) (*DisputeDTO, error) {
var row disputeRow
if err := r.baseQuery(ctx).Where("d.id = ?", id).First(&row).Error; err != nil {
if err := r.adminScopedBaseQuery(ctx).Where("d.id = ?", id).First(&row).Error; err != nil {
return nil, err
}
dto := row.toDTO()
@@ -105,6 +107,30 @@ func (r *Repository) baseQuery(ctx context.Context) *gorm.DB {
CASE WHEN c.deposit_deduct_amount_cent > 0 THEN c.deposit_deduct_amount_cent ELSE c.other_amount_cent END AS checkout_deposit_deduct_amount_cent`)
}
func (r *Repository) adminScopedBaseQuery(ctx context.Context) *gorm.DB {
return applyAdminDisputeTenantScope(ctx, r.baseQuery(ctx))
}
func applyAdminDisputeTenantScope(ctx context.Context, db *gorm.DB) *gorm.DB {
tenantID := tenancy.IDFromContext(ctx)
if tenantID == 0 {
return db
}
return db.Where("o.buyer_tenant_id = ?", tenantID).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)")
}
func currentTenantCanAdminDisputeOrder(ctx context.Context, orderBuyerTenantID uint64, distributionSiteID *uint64) bool {
tenantID := tenancy.IDFromContext(ctx)
if tenantID == 0 {
return true
}
if orderBuyerTenantID != tenantID {
return false
}
return distributionSiteID == nil || *distributionSiteID == 0
}
func (r *Repository) adminFilterQuery(ctx context.Context) *gorm.DB {
return r.db.WithContext(ctx).Table("disputes AS d").
Joins("JOIN rental_orders AS o ON o.id = d.order_id").
@@ -116,29 +142,7 @@ func (r *Repository) adminFilterQuery(ctx context.Context) *gorm.DB {
}
func (r *Repository) adminDisputeCountQuery(ctx context.Context, query AdminListQuery) *gorm.DB {
kw := strings.TrimSpace(query.Keyword)
if kw == "" {
return r.db.WithContext(ctx).Table("disputes AS d")
}
upper := strings.ToUpper(kw)
if id, err := strconv.ParseUint(kw, 10, 64); err == nil && id > 0 && kw[0] != '0' && id < 100000000 {
return r.db.WithContext(ctx).Table("disputes AS d").
Joins("JOIN rental_orders AS o ON o.id = d.order_id")
}
if strings.HasPrefix(upper, "RO") || strings.HasPrefix(upper, "ORD") {
return r.db.WithContext(ctx).Table("disputes AS d").
Joins("JOIN rental_orders AS o ON o.id = d.order_id")
}
if strings.HasPrefix(upper, "L") || (len(kw) >= 12 && isAllDigits(kw)) {
return r.db.WithContext(ctx).Table("disputes AS d").
Joins("JOIN rental_orders AS o ON o.id = d.order_id").
Joins("JOIN rental_listings AS l ON l.id = o.listing_id")
}
if isPhoneKeyword(kw) {
return r.db.WithContext(ctx).Table("disputes AS d").
Joins("JOIN rental_orders AS o ON o.id = d.order_id")
}
return r.adminFilterQuery(ctx)
return applyAdminDisputeTenantScope(ctx, r.adminFilterQuery(ctx))
}
func applyAdminListFilters(db *gorm.DB, query AdminListQuery) *gorm.DB {
@@ -3,6 +3,7 @@ package dispute
import (
"context"
"errors"
"fmt"
"math"
"strings"
"testing"
@@ -12,6 +13,7 @@ import (
"hfb_sys/backend/internal/model"
ordermodule "hfb_sys/backend/internal/modules/order"
"hfb_sys/backend/internal/modules/wallet"
"hfb_sys/backend/internal/tenancy"
"gorm.io/datatypes"
"gorm.io/gorm"
@@ -56,11 +58,15 @@ func setupDisputeTestDB(t *testing.T) *gorm.DB {
func createDisputeOrderFixture(t *testing.T, db *gorm.DB, order model.RentalOrder) (model.User, model.User, model.RentalOrder) {
t.Helper()
owner := model.User{Phone: "13800000001"}
var userCount int64
if err := db.Model(&model.User{}).Count(&userCount).Error; err != nil {
t.Fatalf("统计用户失败: %v", err)
}
owner := model.User{Phone: fmt.Sprintf("138%08d", userCount*2+1)}
if err := db.Create(&owner).Error; err != nil {
t.Fatalf("创建号主失败: %v", err)
}
renter := model.User{Phone: "13800000002"}
renter := model.User{Phone: fmt.Sprintf("138%08d", userCount*2+2)}
if err := db.Create(&renter).Error; err != nil {
t.Fatalf("创建租客失败: %v", err)
}
@@ -74,6 +80,7 @@ func createDisputeOrderFixture(t *testing.T, db *gorm.DB, order model.RentalOrde
t.Fatalf("创建账号失败: %v", err)
}
listing := model.RentalListing{
ListingNo: fmt.Sprintf("L-DISPUTE-%d", userCount/2+1),
AccountID: account.ID,
OwnerID: owner.ID,
PriceCent: 1000,
@@ -84,7 +91,9 @@ func createDisputeOrderFixture(t *testing.T, db *gorm.DB, order model.RentalOrde
if err := db.Create(&listing).Error; err != nil {
t.Fatalf("创建商品失败: %v", err)
}
order.OrderNo = "RO202606130001"
if order.OrderNo == "" {
order.OrderNo = "RO202606130001"
}
order.ListingID = listing.ID
order.AccountID = account.ID
order.OwnerID = owner.ID
@@ -812,6 +821,53 @@ func TestApplyAdminListFiltersSmartRouting(t *testing.T) {
}
}
func TestAdminDisputeQueriesAndActionsAreScopedByBuyerTenant(t *testing.T) {
db := setupDisputeTestDB(t)
tenantA := model.Tenant{ID: 101, Code: "tenant_a", Name: "A站", Status: "active"}
tenantB := model.Tenant{ID: 202, Code: "tenant_b", Name: "B站", Status: "active"}
if err := db.Create(&tenantA).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&tenantB).Error; err != nil {
t.Fatal(err)
}
_, _, orderA := createDisputeOrderFixture(t, db, model.RentalOrder{
OrderNo: "RO-DISPUTE-TENANT-A", BuyerTenantID: 101, SourceTenantID: 202, Status: "renting",
})
_, _, orderB := createDisputeOrderFixture(t, db, model.RentalOrder{
OrderNo: "RO-DISPUTE-TENANT-B", BuyerTenantID: 202, SourceTenantID: 101, Status: "renting",
})
disputes := []model.Dispute{
{OrderID: orderA.ID, InitiatorID: orderA.RenterID, InitiatorType: "user", TargetUserID: orderA.OwnerID, Type: "cannot_login", Status: "open"},
{OrderID: orderB.ID, InitiatorID: orderB.RenterID, InitiatorType: "user", TargetUserID: orderB.OwnerID, Type: "cannot_login", Status: "open"},
}
if err := db.Create(&disputes).Error; err != nil {
t.Fatal(err)
}
ctx := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101})
repo := NewRepository(db)
result, err := repo.ListAdmin(ctx, AdminListQuery{Page: 1, PageSize: 20})
if err != nil {
t.Fatal(err)
}
if result.Total != 1 || result.Items[0].ID != disputes[0].ID {
t.Fatalf("tenant dispute list = %+v", result)
}
if _, err := repo.FindAdminByID(ctx, disputes[1].ID); !errors.Is(err, gorm.ErrRecordNotFound) {
t.Fatalf("cross-tenant dispute detail error = %v", err)
}
if _, err := repo.AdminCreateByOrder(ctx, 9, orderB.ID, AdminCreateRequest{
Type: "cannot_login", Description: "越权测试", TargetRole: "owner",
}, AuditMeta{}); !errors.Is(err, gorm.ErrRecordNotFound) {
t.Fatalf("cross-tenant admin create error = %v", err)
}
if _, err := repo.Arbitrate(ctx, 9, disputes[1].ID, ArbitrateRequest{
Result: "full_refund", Remark: "越权测试",
}, AuditMeta{}); !errors.Is(err, gorm.ErrRecordNotFound) {
t.Fatalf("cross-tenant arbitrate error = %v", err)
}
}
func TestStampOwnerIncomeSiteUsesListingSite(t *testing.T) {
siteID := uint64(3)
orderID := uint64(9)