fix(listing): 分销站商品审核按所属租户可见
分销站商品按站点归属租户参与审核;商品管理、审核详情及首页待审数量统一租户范围。
This commit is contained in:
@@ -82,7 +82,8 @@ func (r *Repository) Summary(ctx context.Context) (*DashboardDTO, error) {
|
||||
Scan(&metrics.TodayLedgerAmountCent).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := listingQuery.Session(&gorm.Session{}).Where("review_status = ?", "pending").Count(&pending.ListingReviews).Error; err != nil {
|
||||
reviewListingQuery := tenancy.ApplyListingTenantScope(ctx, db.Model(&model.RentalListing{}), "")
|
||||
if err := reviewListingQuery.Where("review_status = ? AND status <> ?", "pending", "offline").Count(&pending.ListingReviews).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
disputeQuery := db.Table("disputes AS d")
|
||||
|
||||
@@ -127,3 +127,83 @@ func TestSummaryScopesBuyerAndSourceMetricsByTenant(t *testing.T) {
|
||||
t.Fatalf("source tenant stats = %+v", summary.Tenant)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSummaryPendingReviewsIncludeTenantDistributionSites(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
tenants := []model.Tenant{
|
||||
{ID: 101, Code: "dashboard_scope_a", Name: "A站", Status: "active"},
|
||||
{ID: 202, Code: "dashboard_scope_b", Name: "B站", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sites := []model.DistributionSite{
|
||||
{SiteCode: "dashboard_scope_site_a", SiteName: "A分销站", Domain: "dash-scope-a.example.com", Status: "active", DisplayAccountEnabled: true},
|
||||
{SiteCode: "dashboard_scope_site_b", SiteName: "B分销站", Domain: "dash-scope-b.example.com", Status: "active", DisplayAccountEnabled: true},
|
||||
}
|
||||
if err := db.Create(&sites).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&[]model.ChatSiteTenantLink{
|
||||
{SiteID: sites[0].ID, TenantID: 101, Enabled: true},
|
||||
{SiteID: sites[1].ID, TenantID: 202, Enabled: true},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users := []model.User{
|
||||
{TenantID: 101, Phone: "13200000301", Status: "active"},
|
||||
{TenantID: 202, Phone: "13200000302", Status: "active"},
|
||||
{TenantID: 0, DistributionSiteID: sites[0].ID, Phone: "13200000303", Status: "active"},
|
||||
{TenantID: 0, DistributionSiteID: sites[1].ID, Phone: "13200000304", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
createAccount := func(tenantID, ownerID uint64, title string) model.GameAccount {
|
||||
account := model.GameAccount{
|
||||
SourceTenantID: tenantID,
|
||||
OwnerID: ownerID,
|
||||
GameName: "delta_force",
|
||||
ServerRegion: "国服",
|
||||
LoginPlatform: "steam",
|
||||
Title: title,
|
||||
Status: "draft",
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return account
|
||||
}
|
||||
createPending := func(sourceTenantID, ownerID uint64, siteID *uint64, suffix, status string) model.RentalListing {
|
||||
account := createAccount(sourceTenantID, ownerID, suffix)
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: sourceTenantID,
|
||||
ListingNo: "DASH-" + suffix,
|
||||
AccountID: account.ID,
|
||||
OwnerID: ownerID,
|
||||
PriceCent: 1000,
|
||||
Status: status,
|
||||
ReviewStatus: "pending",
|
||||
DistributionSiteID: siteID,
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return listing
|
||||
}
|
||||
|
||||
createPending(101, users[0].ID, nil, "MAIN-A", "draft")
|
||||
createPending(0, users[2].ID, &sites[0].ID, "SITE-A", "draft")
|
||||
createPending(202, users[1].ID, nil, "MAIN-B", "draft")
|
||||
createPending(0, users[3].ID, &sites[1].ID, "SITE-B", "draft")
|
||||
createPending(101, users[0].ID, nil, "OFFLINE-A", "offline")
|
||||
|
||||
summary, err := NewRepository(db).Summary(dashboardTenantContext(101))
|
||||
if err != nil {
|
||||
t.Fatalf("tenant summary: %v", err)
|
||||
}
|
||||
if summary.Pending.ListingReviews != 2 {
|
||||
t.Fatalf("pending listing reviews = %d, want 2 (main A + site A)", summary.Pending.ListingReviews)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminListQuery) (*Admi
|
||||
|
||||
if hasAdminAssetFilters(query) {
|
||||
var rows []listingRow
|
||||
err := r.applyAdminListFilters(r.baseQuery(ctx), query).
|
||||
err := r.applyAdminListFilters(tenancy.ApplyListingTenantScope(ctx, r.baseQuery(ctx), "l"), query).
|
||||
Order("l.id DESC").
|
||||
Scan(&rows).Error
|
||||
if err != nil {
|
||||
@@ -67,7 +67,7 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminListQuery) (*Admi
|
||||
}, nil
|
||||
}
|
||||
|
||||
db := r.applyAdminListFilters(r.baseQuery(ctx), query)
|
||||
db := r.applyAdminListFilters(tenancy.ApplyListingTenantScope(ctx, r.baseQuery(ctx), "l"), query)
|
||||
offset := (page - 1) * pageSize
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
@@ -83,7 +83,7 @@ func (r *Repository) ListAdmin(ctx context.Context, query AdminListQuery) (*Admi
|
||||
if page == 1 && len(rows) < pageSize {
|
||||
total = int64(len(rows))
|
||||
} else {
|
||||
countDB := r.applyAdminListFilters(r.db.WithContext(ctx).Table("rental_listings AS l"), query)
|
||||
countDB := r.applyAdminListFilters(tenancy.ApplyListingTenantScope(ctx, r.db.WithContext(ctx).Table("rental_listings AS l"), "l"), query)
|
||||
if err := countDB.Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -177,7 +177,7 @@ func yuanToAdminCent(value float64) int64 {
|
||||
}
|
||||
|
||||
func (r *Repository) FindAdmin(ctx context.Context, listingID uint64) (*ListingDTO, error) {
|
||||
query := applyListingSourceTenantScope(ctx, r.baseQuery(ctx), "l")
|
||||
query := tenancy.ApplyListingTenantScope(ctx, r.baseQuery(ctx), "l")
|
||||
var row listingRow
|
||||
if err := query.Where("l.id = ?", listingID).First(&row).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -260,25 +260,9 @@ func (r *Repository) baseQuery(ctx context.Context) *gorm.DB {
|
||||
)`)
|
||||
}
|
||||
|
||||
func applyListingSourceTenantScope(ctx context.Context, query *gorm.DB, alias string) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return query
|
||||
}
|
||||
column := "source_tenant_id"
|
||||
distributionColumn := "distribution_site_id"
|
||||
if alias != "" {
|
||||
column = alias + "." + column
|
||||
distributionColumn = alias + "." + distributionColumn
|
||||
}
|
||||
return query.
|
||||
Where(column+" = ?", tenantID).
|
||||
Where("(" + distributionColumn + " IS NULL OR " + distributionColumn + " = 0)")
|
||||
}
|
||||
|
||||
func (r *Repository) findForReviewUpdate(ctx context.Context, tx *gorm.DB, listingID uint64) (*model.RentalListing, *model.GameAccount, error) {
|
||||
var listing model.RentalListing
|
||||
query := applyListingSourceTenantScope(ctx, tx.Where("id = ?", listingID), "")
|
||||
query := tenancy.ApplyListingTenantScope(ctx, tx.Where("id = ?", listingID), "")
|
||||
if err := query.Clauses(clause.Locking{Strength: "UPDATE"}).First(&listing).Error; err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
|
||||
func (r *Repository) ListPendingReview(ctx context.Context) ([]ListingDTO, error) {
|
||||
var rows []listingRow
|
||||
query := applyListingSourceTenantScope(ctx, r.baseQuery(ctx), "l")
|
||||
query := tenancy.ApplyListingTenantScope(ctx, r.baseQuery(ctx), "l")
|
||||
err := query.
|
||||
Where("l.review_status = ? AND l.status <> ?", "pending", "offline").
|
||||
Order("l.updated_at ASC, l.id ASC").
|
||||
|
||||
@@ -548,6 +548,140 @@ func TestPendingReviewAndApprovalAreScopedToSourceTenant(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPendingReviewIncludesDistributionSitesOwnedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
if err := database.MigrateListingLifecycleTestSchema(db); err != nil {
|
||||
t.Fatalf("migrate err: %v", err)
|
||||
}
|
||||
if err := database.AutoMigrateIfMissing(db, &model.ChatSiteTenantLink{}); err != nil {
|
||||
t.Fatalf("migrate site tenant link: %v", err)
|
||||
}
|
||||
|
||||
tenants := []model.Tenant{
|
||||
{Code: "tenant_scope_a", Name: "A站", Status: "active"},
|
||||
{Code: "tenant_scope_b", Name: "B站", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatalf("create tenants: %v", err)
|
||||
}
|
||||
sites := []model.DistributionSite{
|
||||
{SiteCode: "tenant_scope_site_a", SiteName: "A分销站", Domain: "scope-a.example.com", Status: "active", DisplayAccountEnabled: true},
|
||||
{SiteCode: "tenant_scope_site_b", SiteName: "B分销站", Domain: "scope-b.example.com", Status: "active", DisplayAccountEnabled: true},
|
||||
}
|
||||
if err := db.Create(&sites).Error; err != nil {
|
||||
t.Fatalf("create sites: %v", err)
|
||||
}
|
||||
if err := db.Create(&[]model.ChatSiteTenantLink{
|
||||
{SiteID: sites[0].ID, TenantID: tenants[0].ID, Enabled: true},
|
||||
{SiteID: sites[1].ID, TenantID: tenants[1].ID, Enabled: true},
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("create site tenant links: %v", err)
|
||||
}
|
||||
|
||||
users := []model.User{
|
||||
{TenantID: tenants[0].ID, Phone: "18800000201", RealnameStatus: "verified", Status: "active"},
|
||||
{TenantID: tenants[1].ID, Phone: "18800000202", RealnameStatus: "verified", Status: "active"},
|
||||
{TenantID: 0, DistributionSiteID: sites[0].ID, Phone: "18800000203", RealnameStatus: "verified", Status: "active"},
|
||||
{TenantID: 0, DistributionSiteID: sites[1].ID, Phone: "18800000204", RealnameStatus: "verified", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatalf("create users: %v", err)
|
||||
}
|
||||
|
||||
createListing := func(sourceTenantID, ownerID uint64, siteID *uint64, suffix string) model.RentalListing {
|
||||
account := model.GameAccount{
|
||||
SourceTenantID: sourceTenantID,
|
||||
OwnerID: ownerID,
|
||||
GameName: "delta_force",
|
||||
ServerRegion: "QQ",
|
||||
LoginPlatform: "扫码",
|
||||
Title: suffix,
|
||||
Status: "draft",
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatalf("create account %s: %v", suffix, err)
|
||||
}
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: sourceTenantID,
|
||||
ListingNo: "SCOPE-" + suffix,
|
||||
AccountID: account.ID,
|
||||
OwnerID: ownerID,
|
||||
PriceCent: 10000,
|
||||
Status: "draft",
|
||||
ReviewStatus: "pending",
|
||||
DistributionSiteID: siteID,
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatalf("create listing %s: %v", suffix, err)
|
||||
}
|
||||
return listing
|
||||
}
|
||||
|
||||
mainA := createListing(tenants[0].ID, users[0].ID, nil, "MAIN-A")
|
||||
siteA := createListing(0, users[2].ID, &sites[0].ID, "SITE-A")
|
||||
mainB := createListing(tenants[1].ID, users[1].ID, nil, "MAIN-B")
|
||||
siteB := createListing(0, users[3].ID, &sites[1].ID, "SITE-B")
|
||||
offlineA := createListing(tenants[0].ID, users[0].ID, nil, "OFFLINE-A")
|
||||
if err := db.Model(&offlineA).Updates(map[string]any{"status": "offline"}).Error; err != nil {
|
||||
t.Fatalf("mark offline: %v", err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db, nil)
|
||||
ctxA := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenants[0].ID})
|
||||
items, err := repo.ListPendingReview(ctxA)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPendingReview() error = %v", err)
|
||||
}
|
||||
got := make(map[uint64]bool, len(items))
|
||||
for _, item := range items {
|
||||
got[item.ID] = true
|
||||
}
|
||||
if len(items) != 2 || !got[mainA.ID] || !got[siteA.ID] {
|
||||
t.Fatalf("pending items = %+v, want main A and site A only", items)
|
||||
}
|
||||
if got[mainB.ID] || got[siteB.ID] || got[offlineA.ID] {
|
||||
t.Fatalf("pending items leaked cross-tenant/offline listings: %+v", items)
|
||||
}
|
||||
|
||||
ctxB := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenants[1].ID})
|
||||
itemsB, err := repo.ListPendingReview(ctxB)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPendingReview(B) error = %v", err)
|
||||
}
|
||||
gotB := make(map[uint64]bool, len(itemsB))
|
||||
for _, item := range itemsB {
|
||||
gotB[item.ID] = true
|
||||
}
|
||||
if len(itemsB) != 2 || !gotB[mainB.ID] || !gotB[siteB.ID] {
|
||||
t.Fatalf("pending items(B) = %+v, want main B and site B only", itemsB)
|
||||
}
|
||||
if gotB[mainA.ID] || gotB[siteA.ID] {
|
||||
t.Fatalf("pending items(B) leaked tenant A listings: %+v", itemsB)
|
||||
}
|
||||
|
||||
adminResult, err := repo.ListAdmin(ctxA, AdminListQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatalf("ListAdmin() error = %v", err)
|
||||
}
|
||||
if adminResult.Total != 3 || len(adminResult.Items) != 3 {
|
||||
t.Fatalf("admin listings total=%d items=%d, want 3 (main A, site A, offline A)", adminResult.Total, len(adminResult.Items))
|
||||
}
|
||||
for _, item := range adminResult.Items {
|
||||
if item.ID == mainB.ID || item.ID == siteB.ID {
|
||||
t.Fatalf("ListAdmin leaked cross-tenant listing %d", item.ID)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := repo.FindAdmin(ctxA, siteB.ID); !IsNotFound(err) {
|
||||
t.Fatalf("cross-tenant site FindAdmin error = %v, want not found", err)
|
||||
}
|
||||
if _, err := repo.Approve(ctxA, siteB.ID); !IsNotFound(err) {
|
||||
t.Fatalf("cross-tenant site Approve error = %v, want not found", err)
|
||||
}
|
||||
if _, err := repo.Approve(ctxA, siteA.ID); err != nil {
|
||||
t.Fatalf("approve own distribution site listing: %v", err)
|
||||
}
|
||||
}
|
||||
func TestExternalAccountToCreateRequestMapsUploadFields(t *testing.T) {
|
||||
req := externalAccountToCreateRequest("客服1", 1772526103000, ExternalAccountData{
|
||||
LoginMethod: "QQ账号密码",
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package tenancy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ApplyListingTenantScope 统一经营租户管理后台的商品可见范围。
|
||||
//
|
||||
// 主站商品按 source_tenant_id 归属租户;历史分销站商品按站点归属租户判定,
|
||||
// 因为分销用户的 tenant_id 历史数据存在 0/经营租户混用,不能作为唯一依据。
|
||||
// 平台超管(tenant_id=0)保持全量可见。
|
||||
func ApplyListingTenantScope(ctx context.Context, query *gorm.DB, listingAlias string) *gorm.DB {
|
||||
if query == nil {
|
||||
return query
|
||||
}
|
||||
tenantID := IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return query
|
||||
}
|
||||
sourceTenantColumn := qualifyScopeColumn(listingAlias, "source_tenant_id")
|
||||
distributionSiteColumn := qualifyScopeColumn(listingAlias, "distribution_site_id")
|
||||
condition := `(
|
||||
((` + distributionSiteColumn + ` IS NULL OR ` + distributionSiteColumn + ` = 0) AND ` + sourceTenantColumn + ` = ?)
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM chat_site_tenant_links AS st
|
||||
JOIN tenants AS st_tenant ON st_tenant.id = st.tenant_id AND st_tenant.status = ?
|
||||
WHERE st.site_id = ` + distributionSiteColumn + `
|
||||
AND st.enabled = 1
|
||||
AND st.tenant_id = ?
|
||||
)
|
||||
)`
|
||||
return query.Where(condition, tenantID, "active", tenantID)
|
||||
}
|
||||
|
||||
func qualifyScopeColumn(alias, column string) string {
|
||||
alias = strings.TrimSpace(alias)
|
||||
if alias == "" {
|
||||
return column
|
||||
}
|
||||
return alias + "." + column
|
||||
}
|
||||
Reference in New Issue
Block a user