diff --git a/internal/logic/common/promoEligibility.go b/internal/logic/common/promoEligibility.go index 706dcfc..6270bfe 100644 --- a/internal/logic/common/promoEligibility.go +++ b/internal/logic/common/promoEligibility.go @@ -164,8 +164,16 @@ func evaluateInactiveUserPromo( return false, time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo inactive user subscription failed") } + return evaluateInactiveUserExpire(lastSub.ExpireTime, params, now), ruleExpiresAt, nil +} + +func evaluateInactiveUserExpire(lastExpire time.Time, params promoRuleParams, now time.Time) bool { + if lastExpire.Equal(time.UnixMilli(0)) { + return false + } + threshold := now.AddDate(0, -params.InactiveMonths, 0) - return lastSub.ExpireTime.Before(threshold) || lastSub.ExpireTime.Equal(threshold), ruleExpiresAt, nil + return lastExpire.Before(threshold) || lastExpire.Equal(threshold) } func promoRuleExpiresAt(rule *promo.RuleWithPrice) time.Time { diff --git a/internal/logic/common/promoEligibility_test.go b/internal/logic/common/promoEligibility_test.go new file mode 100644 index 0000000..d583c06 --- /dev/null +++ b/internal/logic/common/promoEligibility_test.go @@ -0,0 +1,46 @@ +package common + +import ( + "testing" + "time" +) + +func TestEvaluateInactiveUserExpire(t *testing.T) { + now := time.Date(2026, 5, 27, 8, 0, 0, 0, time.UTC) + params := promoRuleParams{InactiveMonths: 3} + + tests := []struct { + name string + lastExpire time.Time + want bool + }{ + { + name: "permanent subscription is not inactive", + lastExpire: time.UnixMilli(0), + want: false, + }, + { + name: "active subscription is not inactive", + lastExpire: now.Add(time.Hour), + want: false, + }, + { + name: "expire at threshold is inactive", + lastExpire: now.AddDate(0, -3, 0), + want: true, + }, + { + name: "expire before threshold is inactive", + lastExpire: now.AddDate(0, -3, -1), + want: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := evaluateInactiveUserExpire(tt.lastExpire, params, now); got != tt.want { + t.Fatalf("evaluateInactiveUserExpire() = %v, want %v", got, tt.want) + } + }) + } +}