From 406fb80661c4ee60f417719b0c70b291eeee5732 Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Wed, 27 May 2026 02:01:46 -0700 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D(#75):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=B0=B8=E4=B9=85=E8=AE=A2=E9=98=85=E8=AF=AF=E5=88=A4=E5=9B=9E?= =?UTF-8?q?=E5=BD=92=E4=BC=98=E6=83=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: multica-agent --- internal/logic/common/promoEligibility.go | 10 +++- .../logic/common/promoEligibility_test.go | 46 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 internal/logic/common/promoEligibility_test.go 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) + } + }) + } +}