修复(#128): 按规则类型决定促销资格,让 InactiveUser/Campaign 对老用户生效
此前 calculatePurchasePrice 把 allowPromo 绑死在 orderType == 1,导致只要用户
有任何过往付费订阅(含已过期),就会被 paidSubscriptionQuery 路由为续费
(orderType=2),跳过所有促销评估。后果:
- InactiveUser 召回促销永远无法触发(其目标人群恰好就是有过期订阅的用户)
- Campaign 全员活动对老用户 / 升级加购场景完全失效
- 套餐列表(loadSubscribePromoMap)直接调 EvaluatePromo 不感知 orderType,
可能显示促销价但下单时却拿到原价
修复方式:把 isFirstPurchase 下放给 EvaluatePromo,由规则类型决定 gating:
- NewUser 要求 isFirstPurchase=true(保留首购语义)
- InactiveUser 由规则自身的"上次订阅过期 N 月以上"条件判定
- Campaign 时间窗内对任意用户生效
新增 common.HasPaidSubscription 助手,套餐列表与下单走同一份 isFirstPurchase
判定,确保展示价与实际下单价口径一致。
测试:补充 EvaluatePromo / calculatePurchasePrice 的 NewUser 屏蔽 + Campaign
放开用例;更新 loadSubscribePromoMap 测试覆盖新增 HasPaidSubscription 查询。
注:renewalLogic.go 仍未接入促销(属于方向 B 的彻底统一,本次未涵盖)。
This commit is contained in:
@@ -65,7 +65,7 @@ func TestEvaluatePromoAllowsAnonymousCampaign(t *testing.T) {
|
||||
},
|
||||
}}
|
||||
|
||||
got, err := EvaluatePromo(context.Background(), &svc.ServiceContext{DB: &gorm.DB{}, PromoModel: model}, 0, 7, 12)
|
||||
got, err := EvaluatePromo(context.Background(), &svc.ServiceContext{DB: &gorm.DB{}, PromoModel: model}, 0, 7, 12, true)
|
||||
if err != nil {
|
||||
t.Fatalf("EvaluatePromo returned error: %v", err)
|
||||
}
|
||||
@@ -86,6 +86,54 @@ func TestEvaluatePromoAllowsAnonymousCampaign(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluatePromoCampaignAppliesToReturningUsers(t *testing.T) {
|
||||
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
|
||||
{
|
||||
Rule: promo.Rule{
|
||||
Id: 4,
|
||||
Name: "campaign",
|
||||
Type: promo.RuleTypeCampaign,
|
||||
Enabled: true,
|
||||
},
|
||||
PromoPrice: 299,
|
||||
},
|
||||
}}
|
||||
|
||||
got, err := EvaluatePromo(context.Background(), &svc.ServiceContext{DB: &gorm.DB{}, PromoModel: model}, 42, 7, 1, false)
|
||||
if err != nil {
|
||||
t.Fatalf("EvaluatePromo returned error: %v", err)
|
||||
}
|
||||
if !got.Eligible {
|
||||
t.Fatal("campaign promo should remain eligible for returning users (isFirstPurchase=false)")
|
||||
}
|
||||
if got.PromoPrice != 299 {
|
||||
t.Fatalf("PromoPrice = %d, want 299", got.PromoPrice)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluatePromoNewUserRequiresFirstPurchase(t *testing.T) {
|
||||
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
|
||||
{
|
||||
Rule: promo.Rule{
|
||||
Id: 5,
|
||||
Name: "new user",
|
||||
Type: promo.RuleTypeNewUser,
|
||||
Enabled: true,
|
||||
Params: `{"window_hours": 72}`,
|
||||
},
|
||||
PromoPrice: 99,
|
||||
},
|
||||
}}
|
||||
|
||||
got, err := EvaluatePromo(context.Background(), &svc.ServiceContext{DB: &gorm.DB{}, PromoModel: model}, 42, 7, 1, false)
|
||||
if err != nil {
|
||||
t.Fatalf("EvaluatePromo returned error: %v", err)
|
||||
}
|
||||
if got.Eligible {
|
||||
t.Fatal("new-user promo must be gated out when isFirstPurchase=false (user already has paid subscriptions)")
|
||||
}
|
||||
}
|
||||
|
||||
type fakePromoModel struct {
|
||||
rules []*promo.RuleWithPrice
|
||||
lastSubscribeID int64
|
||||
|
||||
Reference in New Issue
Block a user