diff --git a/backend/internal/modules/listing/public_query.go b/backend/internal/modules/listing/public_query.go index ca467b4d..3ba58044 100644 --- a/backend/internal/modules/listing/public_query.go +++ b/backend/internal/modules/listing/public_query.go @@ -324,7 +324,7 @@ func applyTenantOfferVisibility(ctx context.Context, db *gorm.DB) *gorm.DB { return db.Joins( "INNER JOIN tenant_listing_offers AS tlo ON tlo.listing_id = l.id AND tlo.tenant_id = ? AND tlo.display_status = 'enabled'", tenantID, - ).Where("(l.distribution_site_id IS NULL OR l.distribution_site_id = 0)") + ) } func (r *Repository) applyTenantOfferPrices(ctx context.Context, items []ListingDTO) error { diff --git a/backend/internal/modules/listing/tenant_offer_test.go b/backend/internal/modules/listing/tenant_offer_test.go index 995d53e1..d327fdac 100644 --- a/backend/internal/modules/listing/tenant_offer_test.go +++ b/backend/internal/modules/listing/tenant_offer_test.go @@ -29,13 +29,6 @@ func TestPublicListingUsesCurrentTenantOffer(t *testing.T) { if err := db.Create(&other).Error; err != nil { t.Fatal(err) } - legacySite := model.DistributionSite{ - ID: 999, SiteCode: "legacy_listing_offer", SiteName: "历史分销测试站", - Domain: "legacy-listing-offer.example.com", Status: "active", - } - if err := db.Create(&legacySite).Error; err != nil { - t.Fatal(err) - } owner := model.User{TenantID: dachui.ID, Phone: "13100000001", Status: "active"} if err := db.Create(&owner).Error; err != nil { t.Fatal(err) @@ -64,20 +57,6 @@ func TestPublicListingUsesCurrentTenantOffer(t *testing.T) { if err := db.Create(&offers).Error; err != nil { t.Fatal(err) } - legacySiteID := legacySite.ID - legacyListing := model.RentalListing{ - SourceTenantID: dachui.ID, ListingNo: "L-LEGACY-DIST", - AccountID: account.ID, OwnerID: owner.ID, PriceCent: 9000, - Status: "published", ReviewStatus: "approved", DistributionSiteID: &legacySiteID, - } - if err := db.Create(&legacyListing).Error; err != nil { - t.Fatal(err) - } - if err := db.Create(&model.TenantListingOffer{ - TenantID: jieyou.ID, ListingID: legacyListing.ID, DisplayStatus: "enabled", - }).Error; err != nil { - t.Fatal(err) - } repo := NewRepository(db, nil) dachuiResult, err := repo.ListPublic(listingTenantContext(dachui.ID), PublicListQuery{}) @@ -168,3 +147,87 @@ func TestPublishedListingCreatesOffersForAllActiveTenants(t *testing.T) { } } } + +func TestPublicListingSharedPoolUsesTenantOffers(t *testing.T) { + db := database.NewTestDB() + dachui := model.Tenant{Code: "dachui", Name: "大锤商行", Status: "active"} + jieyou := model.Tenant{Code: "jieyou", Name: "解忧商行", Status: "active"} + other := model.Tenant{Code: "other", Name: "其他租户", Status: "active"} + if err := db.Create(&dachui).Error; err != nil { + t.Fatal(err) + } + if err := db.Create(&jieyou).Error; err != nil { + t.Fatal(err) + } + if err := db.Create(&other).Error; err != nil { + t.Fatal(err) + } + site := model.DistributionSite{ + SiteCode: "shared_pool_dist", SiteName: "天域商行", + Domain: "shared-pool-dist.example.com", Status: "active", + } + if err := db.Create(&site).Error; err != nil { + t.Fatal(err) + } + owner := model.User{TenantID: 0, DistributionSiteID: site.ID, Phone: "13100000003", Status: "active"} + if err := db.Create(&owner).Error; err != nil { + t.Fatal(err) + } + account := model.GameAccount{ + SourceTenantID: 0, OwnerID: owner.ID, ServerRegion: "国服", + LoginPlatform: "steam", Title: "分销站关联商品", Status: "published", + ScreenshotURLS: datatypes.JSON([]byte(`["/api/files/object?key=listing%2Flinked-dist.jpg"]`)), + } + if err := db.Create(&account).Error; err != nil { + t.Fatal(err) + } + siteID := site.ID + listing := model.RentalListing{ + SourceTenantID: 0, ListingNo: "L-LINKED-DIST-1", + AccountID: account.ID, OwnerID: owner.ID, PriceCent: 10000, + Status: "published", ReviewStatus: "approved", DistributionSiteID: &siteID, + } + if err := db.Create(&listing).Error; err != nil { + t.Fatal(err) + } + if err := db.Create(&[]model.TenantListingOffer{ + {TenantID: dachui.ID, ListingID: listing.ID, DisplayStatus: "enabled"}, + {TenantID: jieyou.ID, ListingID: listing.ID, DisplayStatus: "enabled"}, + }).Error; err != nil { + t.Fatal(err) + } + + repo := NewRepository(db, nil) + dachuiResult, err := repo.ListPublic(listingTenantContext(dachui.ID), PublicListQuery{}) + if err != nil { + t.Fatal(err) + } + if dachuiResult.Total != 1 || dachuiResult.Items[0].ID != listing.ID { + t.Fatalf("linked distribution listing not visible to dachui: %+v", dachuiResult) + } + if _, err := repo.FindPublic(listingTenantContext(dachui.ID), listing.ID, "", 0); err != nil { + t.Fatalf("linked distribution listing detail should be visible to dachui: %v", err) + } + + jieyouResult, err := repo.ListPublic(listingTenantContext(jieyou.ID), PublicListQuery{}) + if err != nil { + t.Fatal(err) + } + if jieyouResult.Total != 1 || jieyouResult.Items[0].ID != listing.ID { + t.Fatalf("shared distribution listing not visible to jieyou: %+v", jieyouResult) + } + if _, err := repo.FindPublic(listingTenantContext(jieyou.ID), listing.ID, "", 0); err != nil { + t.Fatalf("shared distribution listing detail should be visible to jieyou: %v", err) + } + + otherResult, err := repo.ListPublic(listingTenantContext(other.ID), PublicListQuery{}) + if err != nil { + t.Fatal(err) + } + if otherResult.Total != 0 { + t.Fatalf("distribution listing leaked to tenant without offer: %+v", otherResult) + } + if _, err := repo.FindPublic(listingTenantContext(other.ID), listing.ID, "", 0); err == nil { + t.Fatal("distribution listing detail must not be visible without tenant offer") + } +}