修复(#130): 统一家庭成员促销资格口径
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -41,6 +41,15 @@ func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subs
|
|||||||
}
|
}
|
||||||
|
|
||||||
userInfo, _ := ctx.Value(constant.CtxKeyUser).(*user.User)
|
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)
|
candidates, err := querySubscribePromoCandidates(ctx, svcCtx, subscribeIDs, userInfo != nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if isMissingPromoTableError(err) {
|
if isMissingPromoTableError(err) {
|
||||||
@@ -49,10 +58,6 @@ func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subs
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userID := int64(0)
|
|
||||||
if userInfo != nil {
|
|
||||||
userID = userInfo.Id
|
|
||||||
}
|
|
||||||
for _, candidate := range candidates {
|
for _, candidate := range candidates {
|
||||||
if candidate.Quantity <= 0 {
|
if candidate.Quantity <= 0 {
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -9,8 +9,10 @@ import (
|
|||||||
|
|
||||||
"github.com/DATA-DOG/go-sqlmock"
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
"github.com/perfect-panel/server/internal/model/promo"
|
"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/svc"
|
||||||
"github.com/perfect-panel/server/internal/types"
|
"github.com/perfect-panel/server/internal/types"
|
||||||
|
"github.com/perfect-panel/server/pkg/constant"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"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 {
|
type fakeSubscribePromoModel struct {
|
||||||
rules []*promo.RuleWithPrice
|
rules []*promo.RuleWithPrice
|
||||||
lastSubscribeID int64
|
lastSubscribeID int64
|
||||||
|
|||||||
Reference in New Issue
Block a user