fix(distribution): 收紧分销权限与货架状态,开通时创建站点金库
无站点成员不再视为超管;分站下架只改展示状态;装修、批量加价与提现参数对齐。
This commit is contained in:
@@ -18,7 +18,8 @@ func distributionTestDB(t *testing.T) *gorm.DB {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(
|
||||
&model.AdminUser{}, &model.Role{}, &model.ChatConversation{}, &model.RentalOrder{},
|
||||
&model.AdminUser{}, &model.Role{}, &model.Permission{}, &model.AdminUserRole{}, &model.RolePermission{},
|
||||
&model.ChatConversation{}, &model.RentalOrder{},
|
||||
&model.AuditLog{}, &model.PaymentMerchantConfig{}, &model.DistributionSite{},
|
||||
&model.DistributionSiteMember{}, &model.DistributionRuleVersion{}, &model.DistributionOrderFinancial{},
|
||||
&model.DistributionOrderParty{}, &model.DistributionSiteAccount{}, &model.DistributionSitePaymentChannel{},
|
||||
@@ -177,8 +178,37 @@ func TestScopeSiteIDRestrictsBoundMember(t *testing.T) {
|
||||
if _, err := repo.ScopeSiteID(t.Context(), 7, site.ID+1); err != ErrSiteAccessDenied {
|
||||
t.Fatalf("access error = %v", err)
|
||||
}
|
||||
if got, err := repo.ScopeSiteID(t.Context(), 99, 123); err != nil || got != 123 {
|
||||
t.Fatalf("global scope = %d, err=%v", got, err)
|
||||
if _, err := repo.ScopeSiteID(t.Context(), 99, 123); err != ErrSiteAccessDenied {
|
||||
t.Fatalf("unbound admin must not inherit superadmin, err=%v", err)
|
||||
}
|
||||
|
||||
superRole := model.Role{Code: "super_admin", Name: "超级管理员"}
|
||||
if err := db.Create(&superRole).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.AdminUserRole{AdminID: 88, RoleID: superRole.ID}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := repo.ScopeSiteID(t.Context(), 88, other.ID); err != nil || got != other.ID {
|
||||
t.Fatalf("superadmin scope = %d, err=%v", got, err)
|
||||
}
|
||||
|
||||
allView := model.Permission{Code: "distribution:all_view", Name: "查看全部分销站点", Resource: "distribution", Action: "all_view"}
|
||||
if err := db.Create(&allView).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
opsRole := model.Role{Code: "platform_ops", Name: "平台运营"}
|
||||
if err := db.Create(&opsRole).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.RolePermission{RoleID: opsRole.ID, PermissionID: allView.ID}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.AdminUserRole{AdminID: 77, RoleID: opsRole.ID}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := repo.ScopeSiteID(t.Context(), 77, other.ID); err != nil || got != other.ID {
|
||||
t.Fatalf("all_view scope = %d, err=%v", got, err)
|
||||
}
|
||||
|
||||
// 被限制到站点 A 的成员不能通过资源 ID 跨站点访问。
|
||||
@@ -195,6 +225,16 @@ func TestScopeSiteIDRestrictsBoundMember(t *testing.T) {
|
||||
if _, err := repo.UpdateAccountDisplay(t.Context(), 7, display.ID, "disabled"); err != ErrSiteAccessDenied {
|
||||
t.Fatalf("UpdateAccountDisplay cross-site err = %v", err)
|
||||
}
|
||||
|
||||
svc := NewService(repo)
|
||||
unboundSites, err := svc.ListSitesForAdmin(t.Context(), 99)
|
||||
if err != nil || len(unboundSites) != 0 {
|
||||
t.Fatalf("unbound admin must not list all sites, got %d err=%v", len(unboundSites), err)
|
||||
}
|
||||
superSites, err := svc.ListSitesForAdmin(t.Context(), 88)
|
||||
if err != nil || len(superSites) != 2 {
|
||||
t.Fatalf("superadmin should list all sites, got %d err=%v", len(superSites), err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncSiteAccountsAndPublicListings(t *testing.T) {
|
||||
@@ -396,6 +436,10 @@ func TestOnboardSiteDoesNotDumpCatalog(t *testing.T) {
|
||||
if len(displays) != 0 {
|
||||
t.Fatalf("onboard must not dump catalog, got %d displays", len(displays))
|
||||
}
|
||||
var treasury model.WalletAccount
|
||||
if err := db.Where("user_id = 0 AND distribution_site_id = ?", onboardResp.Site.ID).First(&treasury).Error; err != nil {
|
||||
t.Fatalf("onboard must create site treasury wallet, err=%v", err)
|
||||
}
|
||||
if onboardResp.Rule.Status != "draft" || onboardResp.Rule.DistributorReceiveRatio != 0 || onboardResp.Rule.PlatformSellRatio != 0 {
|
||||
t.Fatalf("onboard must not activate default 3.5/4 split, got %+v", onboardResp.Rule)
|
||||
}
|
||||
@@ -456,13 +500,17 @@ func TestDistributionBrandingAndMarkup(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 1. Update branding
|
||||
logo := "https://cdn.example.com/logo.png"
|
||||
favicon := "https://cdn.example.com/favicon.ico"
|
||||
banners := `[{"image_url":"https://cdn.example.com/b1.jpg"}]`
|
||||
announcements := `[{"title":"开业大吉"}]`
|
||||
footer := `{"copyright":"2026 Brand"}`
|
||||
updated, err := repo.UpdateSiteBranding(t.Context(), 0, site.ID, UpdateSiteBrandingRequest{
|
||||
LogoURL: "https://cdn.example.com/logo.png",
|
||||
FaviconURL: "https://cdn.example.com/favicon.ico",
|
||||
BannersJSON: `[{"image_url":"https://cdn.example.com/b1.jpg"}]`,
|
||||
AnnouncementsJSON: `[{"title":"开业大吉"}]`,
|
||||
FooterInfoJSON: `{"copyright":"2026 Brand"}`,
|
||||
LogoURL: &logo,
|
||||
FaviconURL: &favicon,
|
||||
BannersJSON: &banners,
|
||||
AnnouncementsJSON: &announcements,
|
||||
FooterInfoJSON: &footer,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("UpdateSiteBranding failed: %v", err)
|
||||
@@ -480,6 +528,15 @@ func TestDistributionBrandingAndMarkup(t *testing.T) {
|
||||
t.Fatalf("expected LogoURL, got: %+v", pubInfo)
|
||||
}
|
||||
|
||||
faviconOnly := "https://cdn.example.com/favicon2.ico"
|
||||
partial, err := repo.UpdateSiteBranding(t.Context(), 0, site.ID, UpdateSiteBrandingRequest{FaviconURL: &faviconOnly})
|
||||
if err != nil {
|
||||
t.Fatalf("partial branding update failed: %v", err)
|
||||
}
|
||||
if partial.LogoURL != "https://cdn.example.com/logo.png" || partial.FaviconURL != faviconOnly {
|
||||
t.Fatalf("omitted branding fields must stay, got %+v", partial)
|
||||
}
|
||||
|
||||
// 2. Account Markup
|
||||
dsa := model.DistributionSiteAccount{
|
||||
SiteID: site.ID,
|
||||
@@ -500,6 +557,12 @@ func TestDistributionBrandingAndMarkup(t *testing.T) {
|
||||
}
|
||||
|
||||
// Batch update markup
|
||||
if err := repo.BatchUpdateAccountMarkup(t.Context(), 0, site.ID, BatchUpdateAccountMarkupRequest{
|
||||
ListingIDs: nil,
|
||||
MarkupAmountCent: 800,
|
||||
}); !errors.Is(err, ErrEmptyListingIDs) {
|
||||
t.Fatalf("empty listing_ids err = %v", err)
|
||||
}
|
||||
if err := repo.BatchUpdateAccountMarkup(t.Context(), 0, site.ID, BatchUpdateAccountMarkupRequest{
|
||||
ListingIDs: []uint64{201},
|
||||
MarkupAmountCent: 800,
|
||||
@@ -577,10 +640,33 @@ func TestDistributionReceivedListingsAndWallet(t *testing.T) {
|
||||
t.Fatalf("unexpected received listing: %+v", item)
|
||||
}
|
||||
|
||||
// Update received listing status
|
||||
if err := repo.UpdateReceivedListingStatus(t.Context(), 0, listing.ID, "inactive"); err != nil {
|
||||
if err := repo.UpdateReceivedListingStatus(t.Context(), 0, listing.ID, "inactive"); !errors.Is(err, ErrInvalidListingStatus) {
|
||||
t.Fatalf("inactive status err = %v", err)
|
||||
}
|
||||
if err := repo.UpdateReceivedListingStatus(t.Context(), 0, listing.ID, "offline"); err != nil {
|
||||
t.Fatalf("UpdateReceivedListingStatus failed: %v", err)
|
||||
}
|
||||
var listingAfter model.RentalListing
|
||||
if err := db.First(&listingAfter, listing.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if listingAfter.Status != "published" {
|
||||
t.Fatalf("shelf offline must not change listing.status, got %s", listingAfter.Status)
|
||||
}
|
||||
var dsa model.DistributionSiteAccount
|
||||
if err := db.Where("site_id = ? AND listing_id = ?", site.ID, listing.ID).First(&dsa).Error; err != nil {
|
||||
t.Fatalf("expected display row, err=%v", err)
|
||||
}
|
||||
if dsa.DisplayStatus != "disabled" {
|
||||
t.Fatalf("expected display disabled, got %s", dsa.DisplayStatus)
|
||||
}
|
||||
offlinePage, err := repo.ListReceivedListings(t.Context(), 0, site.ID, ReceivedListingQuery{Page: 1, PageSize: 10})
|
||||
if err != nil {
|
||||
t.Fatalf("ListReceivedListings after offline failed: %v", err)
|
||||
}
|
||||
if len(offlinePage.Items) != 1 || offlinePage.Items[0].Status != "offline" {
|
||||
t.Fatalf("expected shelf status offline, got %+v", offlinePage.Items)
|
||||
}
|
||||
|
||||
// 2. Settlement batch only credits the site party, never the platform share.
|
||||
rule := model.DistributionRuleVersion{SiteID: &site.ID, Mode: ModeTwoPartyDistributorReceive, Version: 1, Status: "active", EffectiveFrom: time.Now().Add(-time.Hour)}
|
||||
|
||||
Reference in New Issue
Block a user