@@ -30,7 +30,7 @@ type promoRuleParams struct {
|
||||
|
||||
func EvaluatePromo(ctx context.Context, svcCtx *svc.ServiceContext, userID int64, subscribeID int64, quantity int64) (*PromoResult, error) {
|
||||
result := &PromoResult{}
|
||||
if svcCtx == nil || svcCtx.PromoModel == nil || svcCtx.DB == nil || userID <= 0 || subscribeID <= 0 || quantity <= 0 {
|
||||
if svcCtx == nil || svcCtx.PromoModel == nil || svcCtx.DB == nil || subscribeID <= 0 || quantity <= 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -101,8 +101,14 @@ func evaluatePromoRule(
|
||||
) (bool, time.Time, error) {
|
||||
switch rule.Type {
|
||||
case promo.RuleTypeNewUser:
|
||||
if userID <= 0 {
|
||||
return false, time.Time{}, nil
|
||||
}
|
||||
return evaluateNewUserPromo(ctx, db, params, userID, currentUser, now)
|
||||
case promo.RuleTypeInactiveUser:
|
||||
if userID <= 0 {
|
||||
return false, time.Time{}, nil
|
||||
}
|
||||
return evaluateInactiveUserPromo(ctx, db, params, userID, promoRuleExpiresAt(rule), now)
|
||||
case promo.RuleTypeCampaign:
|
||||
return true, promoRuleExpiresAt(rule), nil
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/internal/model/promo"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestEvaluateInactiveUserExpire(t *testing.T) {
|
||||
@@ -44,3 +49,99 @@ func TestEvaluateInactiveUserExpire(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluatePromoAllowsAnonymousCampaign(t *testing.T) {
|
||||
end := time.Now().Add(time.Hour)
|
||||
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
|
||||
{
|
||||
Rule: promo.Rule{
|
||||
Id: 3,
|
||||
Name: "campaign",
|
||||
Type: promo.RuleTypeCampaign,
|
||||
Enabled: true,
|
||||
EndTime: &end,
|
||||
},
|
||||
PromoPrice: 199,
|
||||
},
|
||||
}}
|
||||
|
||||
got, err := EvaluatePromo(context.Background(), &svc.ServiceContext{DB: &gorm.DB{}, PromoModel: model}, 0, 7, 12)
|
||||
if err != nil {
|
||||
t.Fatalf("EvaluatePromo returned error: %v", err)
|
||||
}
|
||||
if !got.Eligible {
|
||||
t.Fatal("anonymous campaign promo should be eligible")
|
||||
}
|
||||
if got.RuleID != 3 {
|
||||
t.Fatalf("RuleID = %d, want 3", got.RuleID)
|
||||
}
|
||||
if got.PromoPrice != 199 {
|
||||
t.Fatalf("PromoPrice = %d, want 199", got.PromoPrice)
|
||||
}
|
||||
if model.lastSubscribeID != 7 {
|
||||
t.Fatalf("lastSubscribeID = %d, want 7", model.lastSubscribeID)
|
||||
}
|
||||
if model.lastQuantity != 12 {
|
||||
t.Fatalf("lastQuantity = %d, want 12", model.lastQuantity)
|
||||
}
|
||||
}
|
||||
|
||||
type fakePromoModel struct {
|
||||
rules []*promo.RuleWithPrice
|
||||
lastSubscribeID int64
|
||||
lastQuantity int64
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) QueryEligibleRules(_ context.Context, subscribeID int64, quantity int64) ([]*promo.RuleWithPrice, error) {
|
||||
m.lastSubscribeID = subscribeID
|
||||
m.lastQuantity = quantity
|
||||
return m.rules, nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) InsertUsage(context.Context, *promo.Usage, ...*gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) InsertRule(context.Context, *promo.Rule) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) FindRule(context.Context, int64) (*promo.Rule, error) {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) UpdateRule(context.Context, *promo.Rule) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) DeleteRule(context.Context, int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) QueryRuleList(context.Context, int, int, string, *bool, string) (int64, []*promo.Rule, error) {
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) UpsertPrices(context.Context, int64, []*promo.SubscribePromo) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) FindPrice(context.Context, int64) (*promo.SubscribePromo, error) {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) DeletePrice(context.Context, int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) QueryPriceList(context.Context, promo.PriceFilter) (int64, []*promo.SubscribePromo, error) {
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) QueryUsageList(context.Context, promo.UsageFilter) (int64, []*promo.Usage, error) {
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
func (m *fakePromoModel) Transaction(context.Context, func(*gorm.DB) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user