Skip to content

feat(points): P1 points rules — daily gift/expiry/gift-first deduction/admin top-up - #75

Merged
argszero merged 1 commit into
mainfrom
feat/backend-p1-points-rules
Aug 18, 2026
Merged

argszero merged 1 commit into
mainfrom
feat/backend-p1-points-rules

Conversation

@argszero

Copy link
Copy Markdown
Owner

Summary

Backend P1 — points rules refinement (rant 2026-08-18T11:03:02.350807, "后端实现 P1 — 点数规则细化(赠送/有效期/扣减顺序/管理员充值)"). Implements the ledger rules from user-stories v1.7:

  1. Points account split (migration v3, idempotent; SCHEMA_VERSION 2→3):
    • quotas.gift_balance column (currently-valid gift points); existing balance = permanent points (sharing earnings / admin top-up)
    • New gift_grants table (id/user_id/amount/granted_at/expires_at/status) for audit
  2. New-user daily gift (src/gift.rs): 1 point/day for 10 consecutive days from registration, valid until day's 23:59:59; lazy ensure_daily_gift before wallet/dashboard/settle; lazy expiry cleanup (no cron)
  3. Deduction order (settle): gift points with earliest expiry first, then permanent balance; gateway precheck (stream + non-stream) uses available = gift + permanent
  4. Admin recharge API (src/routes/admin.rs): POST /api/admin/credits (role=admin, else 403) + GET /api/admin/users + GET /api/admin/usage; seed admin account admin@aitokenpool.local / admin1234
  5. Wallet returns gift_balance; transactions type filter now supports topup

Tests

  • 61/61 pass (49 → 61, +12: daily gift grant/no-duplicate/window-exit/lazy-expiry, gift-first + oldest-expiry deduction, mixed gift+permanent settle, expired-gift-not-consumed, admin 403, top-up ledger, users/usage lists, wallet gift display)
  • cargo build + cargo test + cargo fmt --check + cargo clippy --all-targets -- -D warnings all clean
  • Live smoke: demo wallet shows gift_balance=1; admin top-up +50 credited to permanent; non-admin 403; topup transaction written; migrate idempotent on restart

Related

Rant: 2026-08-18T11:03:02.350807 (backend P1 points rules)

- quotas.gift_balance + gift_grants 明细表(迁移 v3,幂等;SCHEMA_VERSION 2→3)
- gift.rs:注册 10 天窗口每日 1 点(当日 23:59:59 过期),懒加载补发 + 惰性过期清理
- billing.rs settle:先扣最早到期赠送,不足再扣永久 balance(可用余额 = gift + permanent)
- 网关预检(stream/非 stream)改用可用余额口径
- routes/admin.rs:POST /api/admin/credits(role=admin 403 保护)+ GET users/usage;
  seed 管理员 admin@aitokenpool.local / admin1234
- wallet 返回 gift_balance;transactions type 过滤支持 topup
- 测试 49→61(+12:赠送/跨天扣减顺序/过期/混合结算/管理员 403/充值/列表/钱包赠送)
@argszero
argszero merged commit 889532d into main Aug 18, 2026
1 check passed
argszero added a commit that referenced this pull request Aug 18, 2026
- §4.1 网关层标注已实现:OpenAI/Anthropic 双协议 + SSE 流式 + 余额预检(可用余额口径)+ 完整 API 一览
- §4.2 补上游 key AES-256-GCM 加密(v1:<nonce>:<cipher>,ATP_MASTER_KEY → config.master_key → dev 随机兜底)
- §4.3/4.4 计量与账本标注已实现:点数公式、事务性 settle、双余额账户、每日赠送/惰性过期清理、先赠后永扣减、SHARE_RATIO=0.9、管理员充值 topup
- §4.6 管理台补管理员 API(role 判定/403、种子账号)
- §5 数据库设计改为实表(gift_grants/schema_version/quotas 双列),§6 补配置说明
- §7 路线勾掉 P0/P1(v0.1.0→v0.3.0,PR #71–#75)

Co-authored-by: argszero <argszero@argszerodeMac-mini.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant