修复(#130): 统一家庭成员促销资格口径
Build docker and publish / build (20.15.1) (push) Failing after 22m15s
Build docker and publish / build (20.15.1) (pull_request) Failing after 18m6s

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
2026-05-30 01:58:15 -07:00
parent 3644e9ce3f
commit c2d1b5a0d8
2 changed files with 68 additions and 4 deletions
+9 -4
View File
@@ -41,6 +41,15 @@ func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subs
}
userInfo, _ := ctx.Value(constant.CtxKeyUser).(*user.User)
userID := int64(0)
if userInfo != nil {
entitlement, err := commonLogic.ResolveEntitlementUser(ctx, svcCtx.DB, userInfo.Id)
if err != nil {
return nil, err
}
userID = entitlement.EffectiveUserID
}
candidates, err := querySubscribePromoCandidates(ctx, svcCtx, subscribeIDs, userInfo != nil)
if err != nil {
if isMissingPromoTableError(err) {
@@ -49,10 +58,6 @@ func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subs
return nil, err
}
userID := int64(0)
if userInfo != nil {
userID = userInfo.Id
}
for _, candidate := range candidates {
if candidate.Quantity <= 0 {
continue
@@ -9,8 +9,10 @@ import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/perfect-panel/server/internal/model/promo"
"github.com/perfect-panel/server/internal/model/user"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/constant"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
@@ -88,6 +90,63 @@ func TestLoadSubscribePromoMapUsesCommonPromoEvaluation(t *testing.T) {
}
}
func TestLoadSubscribePromoMapUsesFamilyOwnerForInactivePromo(t *testing.T) {
db, mock, cleanup := newSubscribePromoTestDB(t)
defer cleanup()
memberUserID := int64(51637)
ownerUserID := int64(510)
subscribeID := int64(11)
quantity := int64(30)
now := time.Now()
end := now.Add(24 * time.Hour)
mock.ExpectQuery("FROM `user_family_member`").
WithArgs(memberUserID, user.FamilyMemberActive, 1).
WillReturnRows(sqlmock.NewRows([]string{"role", "family_status", "owner_user_id"}).
AddRow(user.FamilyRoleMember, user.FamilyStatusActive, ownerUserID))
mock.ExpectQuery("FROM subscribe_promo AS sp").
WillReturnRows(sqlmock.NewRows([]string{
"subscribe_id", "quantity", "rule_name", "rule_type", "promo_price", "params", "start_time", "end_time",
}).AddRow(subscribeID, quantity, "回归用户01", promoRuleTypeInactiveUser, 100, `{"inactive_months":1}`, nil, end))
mock.ExpectQuery("FROM `user_subscribe`").
WithArgs(ownerUserID, time.UnixMilli(0), 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "user_id", "subscribe_id", "expire_time"}).
AddRow(131, ownerUserID, 1, now.AddDate(0, 1, 0)))
ctx := context.WithValue(context.Background(), constant.CtxKeyUser, &user.User{Id: memberUserID})
promoModel := &fakeSubscribePromoModel{rules: []*promo.RuleWithPrice{
{
Rule: promo.Rule{
Id: 8,
Name: "回归用户01",
Type: promo.RuleTypeInactiveUser,
Enabled: true,
Params: `{"inactive_months":1}`,
EndTime: &end,
},
PromoPrice: 100,
},
}}
got, err := loadSubscribePromoMap(ctx, &svc.ServiceContext{DB: db, PromoModel: promoModel}, []int64{subscribeID})
if err != nil {
t.Fatalf("loadSubscribePromoMap returned error: %v", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("sql expectations: %v", err)
}
if promoModel.lastSubscribeID != subscribeID {
t.Fatalf("promo subscribe id = %d, want %d", promoModel.lastSubscribeID, subscribeID)
}
if promoModel.lastQuantity != quantity {
t.Fatalf("promo quantity = %d, want %d", promoModel.lastQuantity, quantity)
}
if got[subscribeID][quantity] != nil {
t.Fatalf("family member should not receive inactive promo when owner has active subscription, got %+v", got[subscribeID][quantity])
}
}
type fakeSubscribePromoModel struct {
rules []*promo.RuleWithPrice
lastSubscribeID int64