fix(chat): 分销站点客服会话按归属租户可见
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
@@ -18,7 +19,8 @@ func distributionTestDB(t *testing.T) *gorm.DB {
|
||||
db := database.NewTestDB()
|
||||
if err := database.AutoMigrateIfMissing(db,
|
||||
&model.AdminUser{}, &model.Role{}, &model.Permission{}, &model.AdminUserRole{}, &model.RolePermission{},
|
||||
&model.ChatConversation{}, &model.RentalOrder{},
|
||||
&model.ChatConversation{},
|
||||
&model.ChatSiteTenantLink{}, &model.Tenant{}, &model.RentalOrder{},
|
||||
&model.AuditLog{}, &model.PaymentMerchantConfig{}, &model.DistributionSite{},
|
||||
&model.DistributionSiteMember{}, &model.DistributionRuleVersion{}, &model.DistributionOrderFinancial{},
|
||||
&model.DistributionOrderParty{}, &model.DistributionSiteAccount{}, &model.DistributionSitePaymentChannel{},
|
||||
@@ -1884,3 +1886,75 @@ func TestListOrdersPerformanceAndScoping(t *testing.T) {
|
||||
t.Fatalf("expected 1 receive order for site2, got %d", recvPage.Total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSiteAddsDefaultSupportTenantMapping(t *testing.T) {
|
||||
db := distributionTestDB(t)
|
||||
tenant := model.Tenant{Code: "dachui", Name: "大锤商行", Status: "active"}
|
||||
if err := db.Create(&tenant).Error; err != nil {
|
||||
t.Fatalf("创建大锤租户: %v", err)
|
||||
}
|
||||
site, err := NewRepository(db).CreateSite(t.Context(), CreateSiteRequest{
|
||||
SiteCode: "auto_map_site",
|
||||
SiteName: "自动映射站点",
|
||||
Domain: "auto-map.example.com",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("创建分销站点: %v", err)
|
||||
}
|
||||
var link model.ChatSiteTenantLink
|
||||
if err := db.Where("site_id = ?", site.ID).First(&link).Error; err != nil {
|
||||
t.Fatalf("查询客服租户映射: %v", err)
|
||||
}
|
||||
if link.TenantID != tenant.ID || !link.Enabled {
|
||||
t.Fatalf("映射 = %+v, want tenant=%d enabled=true", link, tenant.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnboardSiteAddsDefaultSupportTenantMapping(t *testing.T) {
|
||||
db := distributionTestDB(t)
|
||||
tenant := model.Tenant{Code: "dachui", Name: "大锤商行", Status: "active"}
|
||||
if err := db.Create(&tenant).Error; err != nil {
|
||||
t.Fatalf("创建大锤租户: %v", err)
|
||||
}
|
||||
resp, err := NewRepository(db).OnboardSite(t.Context(), OnboardSiteRequest{
|
||||
SiteCode: "onboard_map_site",
|
||||
SiteName: "开通映射站点",
|
||||
Domain: "onboard-map.example.com",
|
||||
AdminUsername: "onboard_map_admin",
|
||||
AdminPassword: "password123",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("开通分销站点: %v", err)
|
||||
}
|
||||
var link model.ChatSiteTenantLink
|
||||
if err := db.Where("site_id = ?", resp.Site.ID).First(&link).Error; err != nil {
|
||||
t.Fatalf("查询客服租户映射: %v", err)
|
||||
}
|
||||
if link.TenantID != tenant.ID || !link.Enabled {
|
||||
t.Fatalf("映射 = %+v, want tenant=%d enabled=true", link, tenant.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSiteUsesCurrentTenantForSupportMapping(t *testing.T) {
|
||||
db := distributionTestDB(t)
|
||||
tenant := model.Tenant{Code: "jieyou", Name: "解忧商行", Status: "active"}
|
||||
if err := db.Create(&tenant).Error; err != nil {
|
||||
t.Fatalf("创建解忧租户: %v", err)
|
||||
}
|
||||
ctx := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenant.ID})
|
||||
site, err := NewRepository(db).CreateSite(ctx, CreateSiteRequest{
|
||||
SiteCode: "jieyou_map_site",
|
||||
SiteName: "解忧映射站点",
|
||||
Domain: "jieyou-map.example.com",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("创建分销站点: %v", err)
|
||||
}
|
||||
var link model.ChatSiteTenantLink
|
||||
if err := db.Where("site_id = ?", site.ID).First(&link).Error; err != nil {
|
||||
t.Fatalf("查询客服租户映射: %v", err)
|
||||
}
|
||||
if link.TenantID != tenant.ID {
|
||||
t.Fatalf("映射 tenant_id = %d, want %d", link.TenantID, tenant.ID)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user