feat(tenant): 隔离邀请返利与推广追踪数据
This commit is contained in:
@@ -287,21 +287,27 @@ func (r *Repository) GetRebateConfig(ctx context.Context, siteID uint64) (*model
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var cfg model.InvitationRebateConfig
|
||||
if siteID > 0 {
|
||||
err := r.db.WithContext(ctx).Where("distribution_site_id = ?", siteID).First(&cfg).Error
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND distribution_site_id = ?", tenantID, siteID).
|
||||
First(&cfg).Error
|
||||
if err == nil {
|
||||
return &cfg, nil
|
||||
}
|
||||
}
|
||||
// 回退主站配置 (distribution_site_id = 0)
|
||||
err := r.db.WithContext(ctx).Where("distribution_site_id = 0").First(&cfg).Error
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND distribution_site_id = 0", tenantID).
|
||||
First(&cfg).Error
|
||||
if err == nil {
|
||||
return &cfg, nil
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// 返回默认硬编码
|
||||
return &model.InvitationRebateConfig{
|
||||
TenantID: tenantID,
|
||||
DistributionSiteID: 0,
|
||||
FirstMonthRate: DefaultFirstMonthRate,
|
||||
SecondMonthRate: DefaultSecondMonthRate,
|
||||
@@ -317,13 +323,17 @@ func (r *Repository) UpdateRebateConfig(ctx context.Context, siteID uint64, req
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var cfg model.InvitationRebateConfig
|
||||
err := r.db.WithContext(ctx).Where("distribution_site_id = ?", siteID).First(&cfg).Error
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND distribution_site_id = ?", tenantID, siteID).
|
||||
First(&cfg).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
cfg = model.InvitationRebateConfig{
|
||||
TenantID: tenantID,
|
||||
DistributionSiteID: siteID,
|
||||
FirstMonthRate: req.FirstMonthRate,
|
||||
SecondMonthRate: req.SecondMonthRate,
|
||||
@@ -333,7 +343,7 @@ func (r *Repository) UpdateRebateConfig(ctx context.Context, siteID uint64, req
|
||||
if req.Enabled != nil {
|
||||
cfg.Enabled = *req.Enabled
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Select("DistributionSiteID", "FirstMonthRate", "SecondMonthRate", "SubsequentRate", "Enabled").Create(&cfg).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Select("TenantID", "DistributionSiteID", "FirstMonthRate", "SecondMonthRate", "SubsequentRate", "Enabled").Create(&cfg).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cfg, nil
|
||||
@@ -619,7 +629,7 @@ func AwardSellerSideRebateTx(tx *gorm.DB, in SellerSideRebateInput) (int64, erro
|
||||
if tx == nil || in.SellerID == 0 || in.SourceID == 0 || in.BasisCent <= 0 {
|
||||
return 0, nil
|
||||
}
|
||||
ctx := context.Background()
|
||||
ctx := invitationTenantContextForUser(tx, in.SellerID)
|
||||
|
||||
var invitation model.UserInvitation
|
||||
if err := tx.WithContext(ctx).
|
||||
@@ -1334,7 +1344,7 @@ func ProcessOrderInvitationRebateWithOptionsTx(tx *gorm.DB, orderID uint64, opts
|
||||
return nil
|
||||
}
|
||||
repo := NewRepository(tx)
|
||||
return repo.processOrderInvitationRebate(context.Background(), tx, orderID, opts)
|
||||
return repo.processOrderInvitationRebate(invitationTenantContextForOrder(tx, orderID), tx, orderID, opts)
|
||||
}
|
||||
|
||||
// OnUserRealnameVerifiedTx 便捷事务入口,供实名认证通过时调用
|
||||
@@ -1343,5 +1353,25 @@ func OnUserRealnameVerifiedTx(tx *gorm.DB, userID uint64) error {
|
||||
return nil
|
||||
}
|
||||
repo := NewRepository(tx)
|
||||
return repo.OnUserRealnameVerified(context.Background(), userID)
|
||||
return repo.OnUserRealnameVerified(invitationTenantContextForUser(tx, userID), userID)
|
||||
}
|
||||
|
||||
func invitationTenantContextForOrder(tx *gorm.DB, orderID uint64) context.Context {
|
||||
ctx := context.Background()
|
||||
var tenantID uint64
|
||||
_ = tx.Table("rental_orders").Select("buyer_tenant_id").Where("id = ?", orderID).Scan(&tenantID).Error
|
||||
if tenantID > 0 {
|
||||
ctx = tenancy.WithScope(ctx, tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func invitationTenantContextForUser(tx *gorm.DB, userID uint64) context.Context {
|
||||
ctx := context.Background()
|
||||
var tenantID uint64
|
||||
_ = tx.Model(&model.User{}).Select("tenant_id").Where("id = ?", userID).Scan(&tenantID).Error
|
||||
if tenantID > 0 {
|
||||
ctx = tenancy.WithScope(ctx, tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package invitation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
func TestRebateConfigIsScopedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
if err := database.AutoMigrateIfMissing(db, &model.InvitationRebateConfig{}); err != nil {
|
||||
t.Fatalf("migrate rebate config: %v", err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101, TenantCode: "tenant_a"})
|
||||
ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202, TenantCode: "tenant_b"})
|
||||
enabled := true
|
||||
|
||||
if _, err := repo.UpdateRebateConfig(ctxA, 0, UpdateConfigRequest{
|
||||
FirstMonthRate: 0.50,
|
||||
SecondMonthRate: 0.25,
|
||||
SubsequentRate: 0.10,
|
||||
Enabled: &enabled,
|
||||
}); err != nil {
|
||||
t.Fatalf("update tenant A config: %v", err)
|
||||
}
|
||||
if _, err := repo.UpdateRebateConfig(ctxB, 0, UpdateConfigRequest{
|
||||
FirstMonthRate: 0.70,
|
||||
SecondMonthRate: 0.35,
|
||||
SubsequentRate: 0.20,
|
||||
Enabled: &enabled,
|
||||
}); err != nil {
|
||||
t.Fatalf("update tenant B config: %v", err)
|
||||
}
|
||||
|
||||
cfgA, err := repo.GetRebateConfig(ctxA, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("get tenant A config: %v", err)
|
||||
}
|
||||
cfgB, err := repo.GetRebateConfig(ctxB, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("get tenant B config: %v", err)
|
||||
}
|
||||
if cfgA.TenantID != 101 || cfgA.FirstMonthRate != 0.50 {
|
||||
t.Fatalf("tenant A config = %+v", cfgA)
|
||||
}
|
||||
if cfgB.TenantID != 202 || cfgB.FirstMonthRate != 0.70 {
|
||||
t.Fatalf("tenant B config = %+v", cfgB)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user