feat(server/codes): GrantRewardTx 复用 applySubscription 发奖励会员天数

This commit is contained in:
wangjia
2026-07-13 07:11:17 +08:00
parent 52b5e90d92
commit 0695c7958b
2 changed files with 66 additions and 0 deletions
+18
View File
@@ -29,3 +29,21 @@ func (svc *Service) GrantPaidSubscriptionTx(
_ = svc.store.WriteAuditLog(ctx, tx, fmt.Sprintf("user:%d", userID), "pay_grant", ref, string(meta))
return subID, expiresAt, nil
}
// GrantRewardTx 发放奖励会员天数(Pro,source∈{invite,task}),与付费/兑换码同一条
// applySubscription 延时逻辑(max(到期,now)+days;已有活跃 pro 则原地顺延)。审计走 audit_log。
func (svc *Service) GrantRewardTx(
ctx context.Context, tx *sql.Tx, userID int64, days int, source, auditAction, ref string,
) (int64, time.Time, error) {
planID, err := svc.store.GetPlanIDTx(ctx, tx, PlanPro)
if err != nil {
return 0, time.Time{}, err
}
subID, expiresAt, err := svc.applySubscription(ctx, tx, userID, planID, days, source)
if err != nil {
return 0, time.Time{}, err
}
meta, _ := json.Marshal(map[string]any{"days": days, "source": source, "sub_id": subID})
_ = svc.store.WriteAuditLog(ctx, tx, fmt.Sprintf("user:%d", userID), auditAction, ref, string(meta))
return subID, expiresAt, nil
}
@@ -0,0 +1,48 @@
package codes_test
import (
"context"
"testing"
"time"
"github.com/wangjia/pangolin/server/internal/codes"
)
// 无活跃 pro 的新用户:GrantRewardTx 新建一行 source='invite',并写一条 audit_log。
func TestGrantRewardTx_FreshUserCreatesInviteSub(t *testing.T) {
db := openMigratedSQLite(t)
seedUser(t, db, 7)
store := codes.NewStore(db)
svc := codes.NewService(store, nil, 5, time.Hour)
ctx := context.Background()
tx, err := store.BeginTx(ctx)
if err != nil {
t.Fatalf("BeginTx: %v", err)
}
subID, exp, err := svc.GrantRewardTx(ctx, tx, 7, 3, "invite", "invite_reward", "ref-1")
if err != nil {
t.Fatalf("grant: %v", err)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
if subID == 0 || exp.Before(time.Now()) {
t.Fatalf("bad sub %d exp %v", subID, exp)
}
var src string
if err := db.QueryRow(`SELECT source FROM subscriptions WHERE id=?`, subID).Scan(&src); err != nil {
t.Fatalf("query source: %v", err)
}
if src != "invite" {
t.Fatalf("source = %q, want invite", src)
}
var n int
if err := db.QueryRow(`SELECT COUNT(*) FROM audit_log WHERE action='invite_reward' AND target='ref-1'`).Scan(&n); err != nil {
t.Fatalf("query audit_log: %v", err)
}
if n != 1 {
t.Fatalf("audit rows = %d, want 1", n)
}
}