修复(#129): 新注册无历史用户不再误命中沉默促销
evaluateInactiveUserPromo 在 ErrRecordNotFound 时之前 return true,导致
新注册无任何订阅历史的用户被错误判定为"沉默用户"命中 inactive_user 规则。
改为 return false 保持判定语义一致:没有历史订阅 ≠ 沉默用户。
Squash from origin/fix/129-新注册误命中沉默促销 (77377ed)
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -175,7 +175,7 @@ func evaluateInactiveUserPromo(
|
|||||||
Take(&lastSub).Error
|
Take(&lastSub).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return true, ruleExpiresAt, nil
|
return false, time.Time{}, nil
|
||||||
}
|
}
|
||||||
return false, time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo inactive user subscription failed")
|
return false, time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo inactive user subscription failed")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,13 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
"gorm.io/driver/mysql"
|
||||||
|
|
||||||
"github.com/perfect-panel/server/internal/model/promo"
|
"github.com/perfect-panel/server/internal/model/promo"
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
"github.com/perfect-panel/server/internal/svc"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -134,6 +138,42 @@ func TestEvaluatePromoNewUserRequiresFirstPurchase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEvaluatePromoRejectsInactiveRuleWhenUserHasNoSubscription(t *testing.T) {
|
||||||
|
db, mock, cleanup := newCommonPromoTestDB(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
|
||||||
|
{
|
||||||
|
Rule: promo.Rule{
|
||||||
|
Id: 9,
|
||||||
|
Name: "inactive",
|
||||||
|
Type: promo.RuleTypeInactiveUser,
|
||||||
|
Params: `{"inactive_months":3}`,
|
||||||
|
Enabled: true,
|
||||||
|
},
|
||||||
|
PromoPrice: 100,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
|
||||||
|
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `user_subscribe` WHERE user_id = ? ORDER BY CASE WHEN expire_time = ? THEN 0 ELSE 1 END, expire_time DESC LIMIT ?")).
|
||||||
|
WithArgs(int64(51640), time.UnixMilli(0), 1).
|
||||||
|
WillReturnError(gorm.ErrRecordNotFound)
|
||||||
|
|
||||||
|
got, err := EvaluatePromo(context.Background(), &svc.ServiceContext{DB: db, PromoModel: model}, 51640, 1, 30, true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("EvaluatePromo returned error: %v", err)
|
||||||
|
}
|
||||||
|
if got.Eligible {
|
||||||
|
t.Fatal("new user without subscription history should not be eligible for inactive promo")
|
||||||
|
}
|
||||||
|
if got.RuleID != 0 || got.PromoPrice != 0 {
|
||||||
|
t.Fatalf("promo fields = (%d, %d), want zero values", got.RuleID, got.PromoPrice)
|
||||||
|
}
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Fatalf("unmet db expectations: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type fakePromoModel struct {
|
type fakePromoModel struct {
|
||||||
rules []*promo.RuleWithPrice
|
rules []*promo.RuleWithPrice
|
||||||
lastSubscribeID int64
|
lastSubscribeID int64
|
||||||
@@ -193,3 +233,22 @@ func (m *fakePromoModel) QueryUsageList(context.Context, promo.UsageFilter) (int
|
|||||||
func (m *fakePromoModel) Transaction(context.Context, func(*gorm.DB) error) error {
|
func (m *fakePromoModel) Transaction(context.Context, func(*gorm.DB) error) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newCommonPromoTestDB(t *testing.T) (*gorm.DB, sqlmock.Sqlmock, func()) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
sqlDB, mock, err := sqlmock.New()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("create sqlmock: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := gorm.Open(mysql.New(mysql.Config{Conn: sqlDB, SkipInitializeWithVersion: true}), &gorm.Config{})
|
||||||
|
if err != nil {
|
||||||
|
_ = sqlDB.Close()
|
||||||
|
t.Fatalf("open gorm db: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, mock, func() {
|
||||||
|
_ = sqlDB.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user