fix(listing): 分销站商品审核按所属租户可见
分销站商品按站点归属租户参与审核;商品管理、审核详情及首页待审数量统一租户范围。
This commit is contained in:
@@ -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账号密码",
|
||||
|
||||
Reference in New Issue
Block a user