Commit Graph

595 Commits

Author SHA1 Message Date
hailin fce8a919ff fix(scripts): 修改热钱包初始化脚本直接调用mpc-system
- 改为直接调用 mpc-system account-service (端口 4000)
- 使用 snake_case API 格式 (threshold_n, threshold_t, require_delegate)
- 系统热钱包设置 require_delegate: false (不需要用户持有share)
- 更新状态查询路径为 /api/v1/mpc/sessions/{sessionId}

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 08:44:45 -08:00
hailin 9531405c9b fix(scripts): 修复热钱包初始化脚本默认端口为3006
mpc-service 实际运行在 3006 端口,将脚本默认值从 3013 改为 3006

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 08:20:48 -08:00
hailin ad22c23656 refactor(scripts): 完善热钱包MPC初始化脚本
主要改进:
- 修复API路径: /mpc/keygen -> /api/v1/mpc/keygen
- 添加彩色日志输出 (info/success/warn/error/debug)
- 添加依赖检查 (curl, jq)
- 添加服务连通性检查 (health endpoint)
- 添加参数验证
- 添加详细模式 (-v/--verbose)
- 改进等待进度显示 (spinner动画)
- 支持多种地址派生方式 (Node.js ethers / Python eth_keys)
- 改进错误处理和用户提示
- 添加使用示例
- 完成后提供一键复制的环境变量配置命令

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 08:17:40 -08:00
hailin 9cac91b5f0 feat(blockchain): 将提现转账从私钥签名改为 MPC 签名
背景:
- 原实现使用 HOT_WALLET_PRIVATE_KEY 进行热钱包签名
- 私钥直接存储存在安全风险
- 系统已有 MPC 基础设施,应该复用

改动内容:

1. 新增 MPC 签名客户端
   - infrastructure/mpc/mpc-signing.client.ts: 调用 mpc-service 的签名 API
   - 支持创建签名会话、轮询等待、获取签名结果

2. 重构 ERC20 转账服务
   - domain/services/erc20-transfer.service.ts: 从私钥签名改为 MPC 签名
   - 移除 Wallet 依赖,改用 Transaction 手动构建交易
   - 使用 MPC 签名后广播已签名交易

3. 新增初始化服务
   - mpc-transfer-initializer.service.ts: 启动时注入 MPC 客户端
   - 解决 Domain 层和 Infrastructure 层的循环依赖

4. 新增热钱包初始化脚本
   - scripts/init-hot-wallet.sh: 便捷创建系统热钱包的 MPC 密钥
   - 支持配置门限值、用户名等参数

5. 更新配置
   - 移除 HOT_WALLET_PRIVATE_KEY 依赖
   - 新增 MPC_SERVICE_URL, HOT_WALLET_USERNAME, HOT_WALLET_ADDRESS
   - 更新 docker-compose.yml 和 .env.example

部署前需要:
1. 运行 init-hot-wallet.sh 初始化热钱包
2. 配置 HOT_WALLET_USERNAME 和 HOT_WALLET_ADDRESS
3. 向热钱包充值 USDT 和原生币(gas)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 08:04:17 -08:00
hailin 28c44d7219 fix(wallet): 提现失败解冻时优先使用 accountSequence 查找钱包
问题背景:
- 原实现使用 userId 查找钱包进行解冻操作
- userId 来自外部 identity-service,存在变化风险
- 如果 userId 发生变化,可能导致解冻到错误的钱包

解决方案:
- 优先使用 accountSequence 查找钱包(wallet-service 内部主键,稳定可靠)
- 保留 userId 作为兜底查找方式,确保向后兼容
- 增加钱包找不到时的详细错误日志

改动点:
- withdrawal-status.handler.ts: handleWithdrawalFailed() 方法
- 与认种(planting)的钱包查找逻辑保持一致

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 07:37:36 -08:00
hailin 80b74e9877 feat(sentry): 集成 Sentry 自建崩溃收集与错误追踪系统
Backend (infrastructure/sentry):
- 添加 Sentry 自建部署 Docker Compose 配置
- 包含 PostgreSQL, Redis, Kafka, ClickHouse, Snuba 等组件
- 添加 Relay (事件网关) 和 Symbolicator (符号化服务) 配置
- 添加部署脚本 deploy.sh 和配置文件
- 更新 infrastructure README 文档

Frontend (mobile-app):
- 添加 sentry_flutter SDK 依赖
- 创建 SentryService 封装类,统一管理崩溃收集
- 创建 SentryConfig 配置类,支持开发/生产环境配置
- 创建 SentryNavigationObserver 自动追踪页面导航
- 创建 SentryDioInterceptor 自动追踪 HTTP 请求
- 在 bootstrap.dart 中集成 Sentry 初始化
- 支持 Flutter 错误和异步错误捕获
- 自动过滤敏感信息 (密码、助记词、私钥等)
- 在登录/登出/切换账号时同步 Sentry 用户信息

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 07:15:13 -08:00
hailin cc06638e0e feat(telemetry): 将userId改为userSerialNum字符串格式并完善遥测追踪
Backend (presence-service):
- 将EventLog.userId从BigInt改为String类型,存储userSerialNum(如D25121400005)
- 更新Prisma schema,userId字段改为VarChar(20)并添加索引
- 更新心跳相关命令和事件,统一使用userSerialNum字符串
- 添加数据库迁移文件
- 更新相关单元测试和集成测试

Frontend (mobile-app):
- TelemetryEvent新增toServerJson()方法,格式化为后端API期望的格式
- AccountService登录/恢复时设置TelemetryService的userId
- MultiAccountService切换账号时同步更新TelemetryService的userId
- 退出登录时清除TelemetryService的userId
- AuthProvider初始化时设置userId

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 06:55:25 -08:00
hailin 579e8c241e fix(backend): 修复构建错误并添加 TOTP 数据库迁移
修复内容:
- identity-service: 添加 CurrentUserPayload 接口定义
- identity-service: 添加 user_totp 表的数据库迁移文件
- wallet-service: 安装 axios 依赖
- wallet-service: 修复 withdrawal-status.handler.ts 中的类型错误
  - userId.toString() → userId.value (BigInt 类型)
  - unfreezeUsdt() → unfreeze() (正确的方法名)
  - amount.asNumber → amount.value (Money 值对象属性)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 05:29:29 -08:00
hailin b01277ab7d feat(withdraw): 实现完整的提取功能 - TOTP验证和KAVA转账
功能实现:
- identity-service: 添加TOTP二次验证功能
  - 新增 UserTotp 数据模型
  - 实现 TotpService (生成/验证/启用/禁用)
  - 添加 /totp/* API端点

- wallet-service: 提取API添加TOTP验证
  - withdrawal.dto 添加可选 totpCode 字段
  - 添加 IdentityClientService 调用identity-service验证TOTP
  - 添加 WithdrawalStatusHandler 处理区块链确认/失败事件

- blockchain-service: 实现真实KAVA ERC20转账
  - 新增 Erc20TransferService (热钱包USDT转账)
  - 更新 withdrawal-requested.handler 执行真实转账
  - 发布确认/失败事件回wallet-service

- 前端: 对接真实提取API
  - withdraw_confirm_page 调用 walletService.withdrawUsdt

环境变量配置:
- TOTP_ENCRYPTION_KEY (identity-service)
- IDENTITY_SERVICE_URL (wallet-service)
- HOT_WALLET_PRIVATE_KEY (blockchain-service)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 05:16:42 -08:00
hailin 09e66cef10 refactor: 多项UI优化和品牌更名
- 品牌更名: 将"榴莲女皇"全部替换为"榴莲皇后"(前端+后端共15处)
- 导航优化: 将"交易"Tab改名为"兑换"
- 创建钱包页: 添加返回按钮,可返回向导页第5页(仅账号未创建时显示)
- 兑换页面: 禁用"一键结算"和"卖出DST"按钮,提款按钮在余额为0时禁用

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 01:43:47 -08:00
hailin db37fbf860 feat(notification): 添加通知中心功能
后端 (admin-service):
- 新增 Notification 和 NotificationRead 数据模型
- 支持通知类型: 系统/活动/收益/升级/公告
- 实现管理端 API: 创建/更新/删除/列表
- 实现移动端 API: 获取通知列表/未读数量/标记已读

前端 (mobile-app):
- 新增 NotificationService 和 Provider
- 新增通知中心页面 (NotificationInboxPage)
- 在"我的"页面右上角添加通知图标(带未读角标)
- 支持查看通知详情、标记已读、全部已读

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 20:45:03 -08:00
hailin 2e44263834 feat(profile): 添加我的伞下功能 - 展示下级用户树形结构
- 后端新增 GET /referral/user/:accountSequence/direct-referrals API
- 前端新增伞下树组件,支持懒加载、缓存、展开/收起
- 使用 CustomPaint 绘制父子节点连接线
- 超出屏幕宽度时显示省略号,点击弹出底部列表

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 10:34:56 -08:00
hailin ed47e7ab2d fix(authorization): 修复下级团队认种数重复减去自己认种数的 BUG
问题描述:
- 社区/省公司/市公司的初始考核判断错误
- 原因: referral-service 返回的 totalTeamPlantingCount 已经是"下级团队认种数"(不含自己)
  但 authorization-service 又减了一次 selfPlantingCount,导致结果偏小

示例:
- D25121400003: 自己认种12棵,下级认种14棵
- 修复前: subordinateTeamPlantingCount = 14 - 12 = 2 (错误!)
- 修复后: subordinateTeamPlantingCount = 14 (正确)

影响范围:
- 社区权益初始考核 (10棵门槛)
- 省公司权益初始考核
- 市公司权益初始考核 (100棵门槛)
- 所有使用 subordinateTeamPlantingCount 的业务逻辑

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 09:15:00 -08:00
hailin 090c8b747e fix(reward): 修复 accountSequence 转 userId 时字母前缀导致的 BigInt 转换失败
问题描述:
- 社区权益激活后,用户收不到奖励
- 错误: SyntaxError: Cannot convert D25121400002 to a BigInt
- 原因: accountSequence 格式为 D25121400002 (D+日期+序号),直接 BigInt() 转换失败

修改方案:
- 新增 parseAccountSequenceToUserId() 辅助方法
- 如果 accountSequence 以字母开头,去掉第一个字符后再转 BigInt
- 影响 5 个方法: calculateProvinceTeamRight, calculateProvinceAreaRight,
  calculateCityTeamRight, calculateCityAreaRight, calculateCommunityRight

技术背景:
- accountSequence 格式: D25121400002 (用户) / S0000000001 (固定系统账户) / 9440000 (省系统)
- 省市系统账户在 authorization-service 动态创建,identity-service 中不存在
- reward-service 的 userId 字段实际业务中不被使用 (查询用 accountSequence)

潜在隐患:
1. userId 字段存储的不是 identity-service 中的真实 userId
   - D25121400002 -> 25121400002 (不是真实的自增 userId)
   - S0000000001 -> 1 (恰好匹配固定系统账户)
   - 9440000 -> 9440000 (省系统账户,本无字母前缀)
2. 如果未来 reward-service 需要与 identity-service 关联查询,可能出现问题
3. 建议后续考虑: 将 userId 改为可选字段,或完全移除该字段依赖

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 08:18:23 -08:00
hailin c7d54da49f fix(reward): 修复调用 authorization-service 时传递 userId 而非 accountSequence 的问题
- IAuthorizationServiceClient 接口参数类型从 userId: bigint 改为 accountSequence: string
- authorization-service.client.ts 调用时使用正确的 accountSequence 格式
- calculateCommunityRight/calculateProvinceTeamRight/calculateCityTeamRight 方法增加 sourceAccountSequence 参数

此修复确保 reward-service 调用 authorization-service 时传递正确的用户标识格式(如 D25121400006),
而不是数字形式的 userId(如 7),使 authorization-service 能正确查找社区授权记录。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 06:48:58 -08:00
hailin c9f944355c fix(referral): 修复 getReferralChain API 返回 accountSequence 而非 userId
- 修改 getReferralChain 方法返回的 ancestorPath 为 accountSequence 格式
- 修改 getBatchReferralChains 方法同样返回 accountSequence 格式
- 这修复了 authorization-service 查询社区层级时使用错误格式的问题
- 之前返回 userId (如 "25121400000"),现在返回 accountSequence (如 "D25121400000")

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 06:10:41 -08:00
hailin 367e34f3a6 fix(reward): 待领取奖励转可结算使用accountSequence
- 添加 claimPendingRewardsForAccountSequence 方法
- event-consumer 改用 accountSequence 查找待领取奖励
- 修复上家认种后待领取奖励不能转为可结算的问题

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 05:58:19 -08:00
hailin 7d95a35204 fix(referral): 团队统计使用accountSequence替代userId
- UpdateTeamStatisticsCommand 改用 accountSequence
- PlantingCreatedHandler 传递 accountSequence
- TeamStatisticsService 使用 findByAccountSequence 查询
- 修复上家团队认种数不正确的问题

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 05:46:11 -08:00
hailin a7c36b5ee1 fix(referral): 使用JWT中的accountSequence替代userId查询
- JwtAuthGuard解析accountSequence字段放入request.user
- getMyReferralInfo和getMyDirectReferrals改用accountSequence
- 修复referral-service与identity-service用户ID不匹配问题

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 05:27:22 -08:00
hailin 6ef8824ef0 fix(referral): 修复 getMyReferralInfo 使用 userId 而不是 accountSequence 的问题
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 05:05:03 -08:00
hailin 1a97a9df54 fix(reward): add WALLET_SERVICE_URL to reward-service
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 04:52:24 -08:00
hailin d6055099c7 fix(planting): 支付完成后直接开启挖矿,跳过底池注入流程
- enableMining() 允许从 PAID 状态直接开启
- addPlanting() 树直接进入 effectiveTreeCount
- payOrder() 删除底池批次逻辑,直接调用 enableMining()

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 04:25:31 -08:00
hailin 17c3f61657 fix(identity): 优化默认昵称格式为简洁序号
修改默认昵称生成逻辑:
- 从完整序列号中提取后5位数字
- 去掉前导零后组合
- 示例: D24121400001 -> 榴莲女皇1号

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 03:33:40 -08:00
hailin 7c72be3ba0 fix(docker): 修复 referral-service Dockerfile 健康检查 URL
修复 referral-service 的健康检查端点配置:
- referral-service: /health -> /api/v1/health

注:backup-service 使用 /health,leaderboard-service 使用 /api/health(这是服务本身实现的端点)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 03:05:18 -08:00
hailin cd742856c0 fix(identity): 优化默认昵称生成格式
将新用户默认昵称从「用户D2512140001」改为「用户1」,
使用 accountSequence.dailySequence 提取当日序号并去除前导零。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 00:59:01 -08:00
hailin 35f5d54a0c fix(planting): 无推荐人时分享权益进入分享权益池
- 将无推荐人时的分享权益目标从 SYSTEM_HEADQUARTERS_COMMUNITY 改为 SYSTEM_SHARE_RIGHT_POOL
- 将 SYSTEM:OPERATION_ACCOUNT 改为 SYSTEM:SHARE_RIGHT_POOL

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 00:18:32 -08:00
hailin 4478351f89 fix(authorization): 修复 grantProvinceCompany 业务验证逻辑
- 添加省区域/市区域互斥检查:同一用户不能同时拥有两种身份
- 添加省区域全局唯一性检查:整个系统只允许一个省区域角色被授权
- 添加 findAnyProvinceCompany 仓储方法用于全局唯一性校验
- 移除错误的 validateAuthorizationRequest 调用(该方法只适用于团队角色)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 00:15:09 -08:00
hailin 298ce52fc7 fix(authorization): 修复 grantCityCompany 业务验证逻辑
- 添加市区域/省区域互斥检查:同一用户不能同时拥有两种身份
- 添加用户市区域唯一性检查:一个用户只能有一个市区域角色
- 添加城市全局唯一性检查:同一城市只允许一个市区域角色
- 移除错误的 validateAuthorizationRequest 调用(该方法只适用于团队角色)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 00:11:13 -08:00
hailin 10ce981111 fix(authorization-service): 社区授权唯一性约束
- 一个用户只能拥有一个社区角色
- 社区名称全局唯一,不允许重复
- 添加 findCommunityByName repository 方法

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 00:04:24 -08:00
hailin 95e1cdffba feat(authorization-service): 正式省公司(PROVINCE_COMPANY)阶梯考核及月度考核
- 添加 PROVINCE_COMPANY_LADDER 阶梯目标配置 (150→300→600→1200→2400→4800→9600→19200→11750)
- 修改 getProvinceAreaRewardDistribution 使用阶梯目标判断激活条件
- 添加 processExpiredProvinceCompanyBenefits 月度考核方法
- 添加每月最后一天23:59定时任务执行省公司月度考核
- 添加月度数据存档方法 archiveAndResetProvinceCompanyMonthlyTreeCounts

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 23:43:41 -08:00
hailin 1896dd6b4c feat(authorization-service): 正式市公司(CITY_COMPANY)阶梯考核及月度考核
- 添加 CITY_COMPANY_LADDER 阶梯目标配置 (30→60→120→240→480→960→1920→3840→2350)
- 修改 getCityAreaRewardDistribution 使用阶梯目标激活权益
- 添加 processExpiredCityCompanyBenefits 月度考核处理方法
- 添加月末23:59考核定时任务 (判断是否当月最后一天)
- 添加正式市公司月度数据存档定时任务

业务规则:
- 第一个月达标30棵树即可激活权益
- 每月最后一天23:59执行考核
- 达标续期并递增月份索引
- 不达标停用权益

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 23:34:55 -08:00
hailin 4d58d4be6c feat(authorization-service): 省团队授权级联激活/停用及月度考核
- 添加 cascadeActivateParentAuthProvinces: 当省团队授权激活时级联激活上级省团队
- 添加 cascadeDeactivateAuthProvinceBenefits: 月度考核失败时级联停用上级省团队
- 添加 processExpiredAuthProvinceBenefits: 月度考核处理(500棵树/月)
- 修改 tryActivateBenefit 支持 AUTH_PROVINCE_COMPANY 级联激活
- 定时任务: 每天凌晨5点检查省团队权益过期
- 定时任务: 每月1号存档并重置省团队月度新增树数

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 22:32:39 -08:00
hailin 049a13c97e feat(authorization-service): 市团队授权级联激活/停用及月度考核
- 添加 cascadeActivateParentAuthCities: 当市团队授权激活时级联激活上级市团队
- 添加 cascadeDeactivateAuthCityBenefits: 月度考核失败时级联停用上级市团队
- 添加 processExpiredAuthCityBenefits: 月度考核处理(100棵树/月)
- 添加 findExpiredActiveByRoleType 仓储方法
- 修改 tryActivateBenefit 支持 AUTH_CITY_COMPANY 级联激活
- 定时任务: 每天凌晨4点检查市团队权益过期
- 定时任务: 每月1号存档并重置市团队月度新增树数

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 22:04:59 -08:00
hailin 4a2a1e3855 feat(authorization-service): 为 getProvinceTeamRewardDistribution 添加 addMonthlyTrees 调用
参考 getCommunityRewardDistribution 的实现,在以下三个场景添加月度新增树数累计:
1. benefitActive=true 时:全部 treeCount 给该省团队,累加 treeCount
2. currentTeamCount >= initialTarget 时(兜底处理):累加 treeCount
3. 跨越考核达标点时:只累加归自己的部分 afterTargetCount

这确保了省团队权益与社区权益的月度考核追踪逻辑一致。
- 考核目标:500棵(省团队)
- 奖励:20 USDT
- 无上级时分配到系统省团队账户

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 21:55:28 -08:00
hailin 8f91a5b985 feat(authorization-service): 为 getCityTeamRewardDistribution 添加 addMonthlyTrees 调用
参考 getCommunityRewardDistribution 的实现,在以下三个场景添加月度新增树数累计:
1. benefitActive=true 时:全部 treeCount 给该市团队,累加 treeCount
2. currentTeamCount >= initialTarget 时(兜底处理):累加 treeCount
3. 跨越考核达标点时:只累加归自己的部分 afterTargetCount

这确保了市团队权益与社区权益的月度考核追踪逻辑一致。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 21:36:37 -08:00
hailin d12bf6df2f fix(authorization-service): 修复权益分配边界逻辑 + 添加 addMonthlyTrees 调用
边界逻辑修复:
- 社区/省/市团队和区域权益分配:第N棵(达标棵)也给上级,第N+1棵起才给自己
- 例如社区:第1-10棵全部给上级的上级/总部,第11棵起才归该社区

addMonthlyTrees 调用:
- 权益已激活时:累加全部树数
- 初始考核达标后:累加归自己的那部分(第11棵起)
- 已达标但未激活时:累加全部树数

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 21:24:16 -08:00
hailin 6bd6c6b5be feat(authorization-service): 实现月度考核数据存档机制(方案B)
- 新增 lastMonthTreesAdded 字段,用于存档上月业绩数据
- 新增 archiveAndResetMonthlyTreeCounts 定时任务:每月1日0:00将当月数据存档后重置
- 新增 getTreesForAssessment() 方法:根据 benefitValidUntil 判断使用当月或上月数据
- 修复月度考核时序问题:数据重置(0:00)在考核(3:00)之前执行

业务规则:
- 严格自然月统计,11月的业绩不计入12月
- 激活当月免考核,考核激活当月的下一个月
- 权益有效期在上月末 → 使用 lastMonthTreesAdded
- 权益有效期在当月末 → 使用 monthlyTreesAdded

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 20:39:16 -08:00
hailin b705c6aa91 feat(wallet-service): 新增分享权益池账户S0000000005
- 新增 S0000000005 分享权益池系统账户 (user_id = -5)
- 修改 reward-service: 无推荐人的分享权益(500 USDT/棵)进入分享权益池
- 与总部社区账户(S0000000001)分离,便于单独追踪无推荐人的分享权益

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 18:53:21 -08:00
hailin 947e81b58d chore(wallet-service): 确保 @nestjs/schedule 依赖正确配置
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 17:13:21 -08:00
hailin 2af44e5854 feat(authorization-service): 实现社区权益月度考核及级联激活/停用功能
- 新增 benefitValidUntil、lastAssessmentMonth、monthlyTreesAdded 字段追踪月度考核
- 实现级联激活:当社区权益激活时,自动激活所有上级社区的权益
- 实现级联停用:当月度考核不达标时,级联停用该社区及所有上级社区
- 新增定时任务:每天凌晨3点检查过期社区权益,每月1号凌晨重置月度树数
- 权益有效期为当前月+下月末

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 07:23:46 -08:00
hailin 846915badc fix(wallet-service): 使用事务确保 settleUserPendingRewards 原子性
- 将 pending_rewards 状态更新和 wallet_accounts 余额更新包装在 Prisma $transaction 中
- 修复 Bug 4: pending_rewards 被标记为 SETTLED 但 settleable_usdt 未更新的问题
- 添加 PrismaService 依赖注入
- 同时减少 pendingUsdt/pendingHashpower,增加 settleableUsdt/settleableHashpower
- 记录 REWARD_TO_SETTLEABLE 类型的流水

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 06:58:07 -08:00
hailin c93f43546e fix(planting-service): PlantingOrderPaidEvent 添加 accountSequence 字段
Bug 3 修复: wallet-service 结算待领取奖励时需要 accountSequence

- PlantingOrderPaidEvent 事件 data 添加 accountSequence 字段
- markAsPaid(accountSequence) 方法接收并传递 accountSequence
- payOrder 调用 markAsPaid 时传入 accountSequence
- 更新相关单元测试

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 06:13:59 -08:00
hailin 28864e6160 fix(wallet-service): 修复待领取奖励显示和结算触发问题
Bug 1: allocateToUserWallet 同步更新 wallet_accounts.pending_usdt
- 写入 pending_rewards 表时同步调用 wallet.addPendingReward()
- 修复前端待领取金额显示为 0 的问题

Bug 2: Kafka 事件类型匹配兼容 planting-service 格式
- 支持 eventName 字段解析 (planting-service 使用)
- 支持 data 字段解析 (planting-service 使用)
- 新增 PlantingOrderPaid 事件类型支持
- 修复用户认种后待领取无法转换为可结算的问题

其他: 定时任务频率改为每分钟一次 (测试用途)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 05:51:28 -08:00
hailin e4389a5733 fix(wallet-service): 优化资金分配逻辑 - 区分直接到账和待领取
- SHARE_RIGHT (分享权益): 写入 pending_rewards 表,24小时待领取
- PROVINCE_TEAM_RIGHT/PROVINCE_AREA_RIGHT/CITY_TEAM_RIGHT/CITY_AREA_RIGHT: 直接到账
- COMMUNITY_RIGHT (社区权益): 进入总部社区账户 S0000000001,直接到账

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 04:51:34 -08:00
hailin 737fc3bb3c fix(wallet-service): allocateToUserWallet 改用 pending_rewards 表
- 移除 wallet.addPendingReward() 调用(旧方案,更新 wallet_accounts.pending_usdt)
- 改用 PendingReward.create() + pendingRewardRepo.save() 写入 pending_rewards 表
- 支持 7+省代码(省团队)和 6+市代码(市团队)账户格式

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 03:53:23 -08:00
hailin 8f81c46d75 feat: 省市团队账户及待领取奖励逐笔追踪
1. authorization-service:
   - 省团队权益分配改用系统省团队账户 (7+provinceCode)
   - 市团队权益分配改用系统市团队账户 (6+cityCode)
   - 无推荐链时不再进总部,而是进对应团队账户

2. wallet-service:
   - 新增 pending_rewards 表支持逐笔追踪待领取奖励
   - ensureRegionAccounts 同时创建区域账户和团队账户
   - 新增 getPendingRewards API 查询待领取列表
   - 新增 settleUserPendingRewards 用户认种后结算
   - 新增 processExpiredRewards 定时处理过期奖励
   - PlantingCreatedHandler 监听认种事件触发结算
   - ExpiredRewardsScheduler 每小时处理过期奖励

账户格式:
- 省区域: 9+provinceCode (如 9440000)
- 市区域: 8+cityCode (如 8440100)
- 省团队: 7+provinceCode (如 7440000)
- 市团队: 6+cityCode (如 6440100)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 03:32:47 -08:00
hailin 4d3290f029 fix(wallet-service): 修复系统账户资金分配功能
问题:
- 认种订单支付后,系统账户(成本费、运营费、总部社区、RWA底池)余额始终为0
- reward-service 正确计算分配,但 wallet-service 未实际执行系统账户的资金转移

根本原因:
1. allocateToSystemAccount() 方法只打印日志,未执行任何数据库操作(遗留的 TODO)
2. UserId 值对象不允许负数,而系统账户 user_id 为负数(-1 到 -4)

修复内容:

1. wallet-application.service.ts - allocateToSystemAccount()
   - 实现完整的系统账户资金分配逻辑
   - 通过 findByAccountSequence() 获取系统账户
   - 调用 addAvailableBalance() 直接增加可用余额
   - 创建 SYSTEM_ALLOCATION 类型的流水记录

2. wallet-account.aggregate.ts
   - 新增 addAvailableBalance(amount: Money) 方法
   - 用于系统账户直接增加余额(无需待领取/过期机制)

3. ledger-entry-type.enum.ts
   - 新增 SYSTEM_ALLOCATION 枚举值,用于系统账户分配流水

4. user-id.vo.ts
   - 移除负数校验,允许系统账户使用负数 user_id
   - 系统账户约定:-1(总部社区)、-2(成本费)、-3(运营费)、-4(RWA底池)

验证结果(认种1棵树=2199 USDT):
- S0000000001 总部社区: 9 USDT ✓
- S0000000002 成本费账户: 400 USDT ✓
- S0000000003 运营费账户: 300 USDT ✓
- S0000000004 RWA底池: 800 USDT ✓

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 02:01:48 -08:00
hailin 98d8bee20d fix: 统一推荐码生成逻辑 - 由 identity-service 单点生成
重要变更:
- identity-service 生成用户推荐码,通过 Kafka 事件传递给 referral-service
- referral-service 不再自己生成推荐码,直接使用事件中的推荐码
- 修复两个服务推荐码不一致的问题

涉及服务:
- identity-service: 事件 payload 添加 referralCode 字段
- referral-service: 接收并存储 identity-service 生成的推荐码
- wallet-service: 添加区域账户动态创建接口
- planting-service: 调用区域账户创建接口

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 01:14:56 -08:00
hailin ebbf2d971a feat: 跨服务使用 accountSequence 查询推荐链 + 系统账户动态创建
1. reward-service 使用 accountSequence 查询推荐链
   - event-consumer.controller.ts: 优先使用 accountSequence 作为用户标识
   - reward-calculation.service.ts: 使用 accountSequence 查询推荐关系
   - referral-service.client.ts: 参数从 userId 改为 accountSequence

2. referral-service 支持 accountSequence 格式的推荐链查询
   - referral.controller.ts: /chain/:identifier 同时支持 userId 和 accountSequence

3. wallet-service 系统账户动态创建
   - wallet-application.service.ts: allocateToUserWallet 使用 getOrCreate
   - 支持省区域(9+code)和市区域(8+code)账户自动创建
   - 新增 migration seed: 4个固定系统账户 (S0000000001-S0000000004)

4. planting-service 事件增强
   - 事件中添加 accountSequence 字段用于跨服务关联

系统账户格式:
- S0000000001: 总部社区 (基础费9U + 兜底权益)
- S0000000002: 成本费账户 (400U)
- S0000000003: 运营费账户 (300U)
- S0000000004: RWAD底池账户 (800U)
- 9+provinceCode: 省区域系统账户 (动态创建)
- 8+cityCode: 市区域系统账户 (动态创建)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 23:22:01 -08:00
hailin d20ff9e9b5 fix: 修复 wallet-service 支持 accountSequence 格式及订单状态机
1. wallet-service/internal-wallet.controller.ts:
   - getBalance: 支持 accountSequence (D开头) 和 userId (纯数字) 两种格式

2. wallet-service/wallet-application.service.ts:
   - freezeForPlanting: 修复 BigInt 转换错误,优先按 accountSequence 查找钱包
   - confirmPlantingDeduction: 同上
   - unfreezeForPlanting: 同上
   - getMyWallet: 支持 userId='0' 的情况(仅通过 accountSequence 查询)

3. planting-service/planting-order.aggregate.ts:
   - schedulePoolInjection: 状态检查从 FUND_ALLOCATED 改为 PAID
   - 原因: 资金分配已移至 reward-service 异步处理

修复问题: "Cannot convert D25121300006 to a BigInt"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 22:35:18 -08:00
hailin ad82e3ee44 fix(referral-service): 从领域层修复 usedReferralCode 存储错误
问题:
- 创建推荐关系时,used_referral_code 字段错误地存储了用户自己的推荐码
- 而不是用户使用的推荐人的推荐码

根本修复(从领域层):
1. ReferralRelationshipProps: 添加 usedReferralCode 字段
2. ReferralRelationship.create(): 新增 referrerReferralCode 参数
3. ReferralRelationship: 添加 _usedReferralCode 私有属性和 getter
4. toPersistence(): 输出 usedReferralCode
5. reconstitute(): 正确重建 usedReferralCode

调用链路修复:
- referral.service.ts: 在查询推荐人时获取其 referralCode,
  并在调用 ReferralRelationship.create() 时传入
- repository: 直接使用 data.usedReferralCode 而不是额外查询

测试更新:
- 更新单元测试和集成测试以支持新的 create() 签名
- 新增 usedReferralCode 相关测试用例

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 19:49:11 -08:00
hailin e557b00c24 fix(reward-service): 修复 targetType 类型断言 2025-12-12 19:16:15 -08:00
hailin 96f031da35 fix(wallet-service): 修复 allocateToUserWallet 使用 accountSequence 查找钱包
- targetId 现在是 accountSequence (如 D2512120001),不再是 userId
- 移除无效的 BigInt(targetId) 转换
- 从 wallet 对象获取 userId 用于流水记录和缓存失效

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 19:15:47 -08:00
hailin 518667e88e fix(reward-service): 修复与 wallet-service 的接口字段不匹配
修复 allocateFunds 接口:
- targetType: 使用 USER/SYSTEM 而不是 rightType
- targetId: 使用 accountSequence 而不是 userId
- allocationType: 新增字段,存储 rightType
- hashpowerPercent: 新增字段,传递算力百分比

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 19:10:39 -08:00
hailin d94badfa53 fix(wallet-service): 修复 account_sequence 类型从 BIGINT 改为 VARCHAR(20)
与其他服务保持一致,accountSequence 格式为 D + YYMMDD + 5位序号
- wallet_accounts.account_sequence
- wallet_ledger_entries.account_sequence
- deposit_orders.account_sequence
- withdrawal_orders.account_sequence

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 18:46:37 -08:00
hailin a5089db288 fix: 修复授权唯一性验证不检查地区的bug
授权验证规则:一条推荐线上同一类型授权只能有一个人,不管地区是什么
- 使用 findByUserIdAndRoleType 替代 findByUserIdRoleTypeAndRegion
- 错误信息中显示已存在授权的地区名称

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 13:20:53 -08:00
hailin 75c49951b7 fix: 修复多个服务的 accountSequence 类型和推荐关系 bug
1. referral-service: 修复 userId 从临时值 0 导致的 "用户ID必须大于0" 错误
   - 从 accountSequence 提取数值部分作为 userId (去掉 "D" 前缀)
   - 避免依赖 identity-service 发送的临时 userId

2. 多服务 migration 修复: accountSequence/inviterSequence 类型从 BIGINT 改为 VARCHAR(12)
   - identity-service: account_sequence, inviter_sequence
   - authorization-service: account_sequence
   - blockchain-service: account_sequence
   - referral-service: account_sequence
   - reward-service: account_sequence
   - backup-service: account_sequence

3. mpc-service 与 backup-service 集成:
   - mpc-service: 添加 BACKUP_SERVICE_URL, BACKUP_SERVICE_ENABLED, SERVICE_JWT_SECRET
   - backup-service: ALLOWED_SERVICES 添加 mpc-service

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 12:29:11 -08:00
hailin 4be9c1fb82 refactor!: 重构账户序列号格式 (BREAKING CHANGE)
将 accountSequence 从数字类型改为字符串类型,新格式为:
- 普通用户: D + YYMMDD + 5位序号 (例: D2512120001)
- 系统账户: S + 10位序号 (例: S0000000001)

主要变更:
- identity-service: AccountSequence 值对象改为字符串类型
- identity-service: 序列号生成器改为按日期重置计数
- 所有服务: Prisma schema 字段类型从 BigInt/Int 改为 String
- 所有服务: DTO、Command、Event 中的类型定义更新
- Flutter 前端: 相关数据模型类型更新

涉及服务:
- identity-service (核心变更)
- referral-service
- authorization-service
- wallet-service
- reward-service
- blockchain-service
- backup-service
- planting-service
- mpc-service
- admin-service
- mobile-app (Flutter)

注意: 此为破坏性变更,需要清空数据库并重新运行 migration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 09:11:18 -08:00
hailin 8148d1d127 fix: 修复权益分配竞态条件和统计数据bug
1. 事件流重构:将 planting.order.paid 事件从 planting-service 移至 referral-service 发送
   - 确保统计数据更新后再触发奖励计算,避免竞态条件
   - planting-service 只发送 planting.planting.created 事件(包含订单信息)
   - referral-service 处理完统计更新后转发 planting.order.paid 给 reward-service

2. 修复 addPersonalPlanting 方法:
   - 原代码错误地更新 _totalTeamCount(团队人数)而非 _teamPlantingCount(团队认种数)
   - 导致 subordinateTeamPlantingCount 计算错误,权益无法正确分配

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 05:35:31 -08:00
hailin 16f891b743 fix(authorization): include target-reaching tree in user's reward
修复达标时收益分配错误:达标的那一棵应该归被授权用户所有。

之前逻辑:目标10棵,当前0棵,本次种10棵
- remaining = 10 - 0 = 10
- 上级得到10棵,用户得到0棵 

修复后:
- remaining = max(0, 10 - 0 - 1) = 9(不包括达标那一棵)
- 上级得到9棵,用户得到1棵 

影响:社区、省团队、市团队、省区域、市区域

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 03:53:59 -08:00
hailin a54e9743ef fix(authorization): correctly distribute rewards when assessment target is exactly reached
修复当认种恰好达标时的收益分配错误:
- 之前:恰好达标时全部收益给上级/总部,被授权用户无法获得任何收益
- 现在:达标时的那一棵收益归被授权用户所有

影响的分配方法:
- getCommunityRewardDistribution (社区)
- getProvinceTeamRewardDistribution (省团队)
- getCityTeamRewardDistribution (市团队)
- getProvinceAreaRewardDistribution (省区域)
- getCityAreaRewardDistribution (市区域)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 03:19:09 -08:00
hailin f60d4eac87 feat(authorization): remove region matching requirement for team rewards
省团队(AUTH_PROVINCE_COMPANY)和市团队(AUTH_CITY_COMPANY)收益分配不再要求
认种者选择的省市与被授权用户的省市一致,只要在推荐链上有授权即可获得收益。

省区域(PROVINCE_COMPANY)和市区域(CITY_COMPANY)收益仍保持省市匹配要求。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-12 03:04:55 -08:00
hailin 4bf4efb1f4 feat(authorization): add assessment rules for province/city area roles
- Province area (PROVINCE_COMPANY): 50,000 trees initial target
- City area (CITY_COMPANY): 10,000 trees initial target
- Apply consistent assessment logic: pre-target rewards go to system account, post-target rewards go to company
- Auto-activate benefit when target is reached

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 21:57:00 -08:00
hailin 905725fc2d feat(authorization): add admin APIs for auth-province and auth-city company
- Add POST /admin/authorizations/auth-province-company for 省团队授权
- Add POST /admin/authorizations/auth-city-company for 市团队授权
- Add team uniqueness validation for all province/city authorization types
- Add domain events: AuthProvinceCompanyGrantedEvent, AuthCityCompanyGrantedEvent
- Add factory methods: createAuthProvinceCompanyByAdmin, createAuthCityCompanyByAdmin

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 21:46:44 -08:00
hailin 582828b8be fix(authorization): activate benefit when assessment target is exactly reached
When tree count + current team count equals initial target exactly,
the benefit was not being activated. Now properly handles three cases:
- afterPlantingCount < target: still in assessment, no activation
- afterPlantingCount === target: reached target, activate benefit
- afterPlantingCount > target: exceeded target, split rewards & activate

Applied to community, province team, and city team reward distribution.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 20:29:15 -08:00
hailin 9819130f98 fix(authorization): use subordinate team count for assessment and auto-activate benefit
1. Assessment calculation now uses subordinate team count (excluding self planting)
   - Added selfPlantingCount to referral-service API response
   - Added subordinateTeamPlantingCount getter to TeamStatistics interface
   - Updated all assessment checks to use subordinateTeamPlantingCount

2. Auto-activate benefit when assessment target is reached
   - Added tryActivateBenefit helper method
   - Community, province team, and city team reward distribution methods
     now automatically activate benefit when target is reached

3. Fixed event consumer to support AUTHORIZED status (admin grants)
   - Previously only checked PENDING status for activation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 19:33:39 -08:00
hailin f94e7283d5 fix(authorization): fix race condition in reward distribution calculation
When calculating reward distribution for community/province/city benefits,
the totalTeamPlantingCount was being read after referral-service had already
updated it with the current planting. This caused incorrect distribution
where benefits were given to the community holder before they completed
their assessment target.

Fix: Subtract treeCount from rawTeamCount to restore the "before planting"
team count, ensuring correct assessment calculation.

Affected methods:
- getCommunityRewardDistribution
- getProvinceTeamRewardDistribution
- getCityTeamRewardDistribution

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 19:01:11 -08:00
hailin fc55afb7b9 fix(authorization): show parent community without requiring benefitActive
Changed findActiveCommunityByAccountSequences to not require benefitActive=true.
This allows communities that haven't passed assessment to still be shown as
parent communities in the UI, while reward distribution logic remains unchanged.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 18:39:22 -08:00
hailin 9378bf7570 feat(authorization): add skipAssessment option to admin grant APIs
Admin grant APIs now default to requiring assessment (benefitActive=false).
Added optional skipAssessment parameter to bypass assessment when needed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 17:48:49 -08:00
hailin 4d01bc5faa chore(reward-service): add detailed debug logs to reward calculation
Add comprehensive logging to RewardCalculationService for easier debugging:
- Log start/end of calculateRewards with order info
- Log each benefit calculation with input parameters
- Log distribution results from authorization-service calls
- Log share right decisions (settleable vs pending)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 07:59:01 -08:00
hailin 16d95999de feat(backend): implement assessment rules for reward distribution
Add proper assessment/考核 logic for all 5 benefit types:
- Community (80 USDT): 10-tree initial assessment, splits rewards between
  current community and parent/headquarters based on progress
- Province Team (20 USDT): 500-tree initial assessment for AUTH_PROVINCE_COMPANY
- Province Area (15 USDT + 1%): 50000-tree target for PROVINCE_COMPANY,
  routes to system account until target reached
- City Team (40 USDT): 100-tree initial assessment for AUTH_CITY_COMPANY
- City Area (35 USDT + 2%): 10000-tree target for CITY_COMPANY,
  routes to system account until target reached

Changes:
- authorization-service: Add 4 new distribution API endpoints and application
  service methods for province/city team/area reward distribution
- authorization-service: Add repository methods for querying authorizations
  including benefitActive=false records
- reward-service: Update client to call new distribution APIs
- reward-service: Modify calculation methods to return multiple reward entries
  based on assessment distribution plan

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 07:54:55 -08:00
hailin c162ccced9 fix(reward): correct response parsing for authorization-service API
The authorization-service uses TransformInterceptor which wraps all API
responses in a standard format: { success, data, timestamp }

Before this fix, the AuthorizationServiceClient was reading:
  data.accountSequence (undefined - wrong level)

After this fix, it correctly reads:
  result.data.accountSequence

This ensures that COMMUNITY_RIGHT is allocated to the correct authorized
community user (e.g., accountSequence=6) instead of falling back to
HEADQUARTERS (accountSequence=1).

Changes in reward-service:
- Added AuthorizationServiceResponse<T> interface
- Added NearestAuthorizationResult interface
- Updated findNearestAuthorizedProvince() to parse wrapped response
- Updated findNearestAuthorizedCity() to parse wrapped response
- Updated findNearestCommunity() to parse wrapped response
- Added debug logging for findNearestCommunity

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 03:33:10 -08:00
hailin d1a7c44f23 fix(referral): correct response parsing for authorization-service API
The authorization-service uses TransformInterceptor which wraps all API
responses in a standard format: { success, data, timestamp }

Before this fix, the AuthorizationServiceClient was reading:
  response.data.accountSequence (undefined)

After this fix, it correctly reads:
  response.data.data.accountSequence

This ensures that nearestCommunity, nearestProvinceAuth, and nearestCityAuth
are properly extracted from the wrapped response, allowing community benefits
to be correctly allocated to authorized users instead of falling back to
SYSTEM_HEADQUARTERS_COMMUNITY.

Changes:
- Added AuthorizationServiceResponse<T> interface to model wrapped response
- Updated findNearestCommunity() to use wrapped response type
- Updated findNearestProvince() to use wrapped response type
- Updated findNearestCity() to use wrapped response type
- Updated catchError handlers to return properly structured fallback data

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 03:08:50 -08:00
hailin ee9265f357 fix(docker): remove cyclic dependency between referral-service and authorization-service
两个服务互相调用,形成循环依赖。移除 depends_on 约束,使用 fallback 机制处理服务暂时不可用的情况。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 02:50:04 -08:00
hailin 1a5925494e feat(referral): integrate authorization-service for community/province/city rights allocation
## Problem
社区/省/市权益分配一直返回 null,导致所有权益都分配给系统账户而非正确的授权用户。
原因:referral-service 的 getReferralContext 接口中 nearestCommunity、nearestProvinceAuth、
nearestCityAuth 三个字段硬编码为 null,注释说"需要后续实现"但一直未实现。

## Solution
1. 新建 AuthorizationServiceClient 调用 authorization-service 的内部 API
   - /api/v1/authorization/nearest-community
   - /api/v1/authorization/nearest-province
   - /api/v1/authorization/nearest-city
2. 修改 InternalReferralController 使用并行查询获取授权信息
3. 添加 fallback 机制:authorization-service 不可用时返回 null(保持现有行为)
4. docker-compose.yml 添加 AUTHORIZATION_SERVICE_URL 环境变量

## Files Changed
- backend/services/referral-service/src/infrastructure/external/authorization-service.client.ts (new)
- backend/services/referral-service/src/api/controllers/referral.controller.ts
- backend/services/referral-service/src/modules/infrastructure.module.ts
- backend/services/docker-compose.yml

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 02:10:33 -08:00
hailin 7d9837fc22 refactor(reward): 前端直接从 reward-service 查询奖励数据
移除 reward-service 和 wallet-service 之间的 Kafka 同步机制:
- 屏蔽 reward-service 的 outbox 发布逻辑
- 屏蔽 wallet-service 的 RewardEventConsumerController 订阅

Flutter 前端改为直接调用 reward-service API:
- 新增 RewardService 调用 /rewards/summary
- profile_page 改用 rewardService.getMyRewardSummary()

这样简化了架构,数据从源头获取,避免同步问题。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 00:36:53 -08:00
hailin a1ca490ff4 fix(wallet): fix message format parsing in RewardEventConsumerController
The OutboxPublisherService sends payload fields directly at the message
root level, not nested under a 'payload' property. Update the consumer
to read fields directly from message instead of message.payload.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 23:49:07 -08:00
hailin 1acb314e7b feat(reward): add outbox event publishing for reward summary updates
Add publishSummaryUpdatesToOutbox method to write reward.summary.updated
events to outbox table for synchronization to wallet-service.

Events are published after:
- distributeRewards: when rewards are distributed to users
- claimPendingRewardsForUser: when pending rewards are claimed
- settleRewards: when rewards are settled
- expireOverdueRewards: when rewards expire

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 23:40:35 -08:00
hailin 538aae4ef0 feat(sync): implement Outbox Pattern for reward-service to wallet-service sync
Add event synchronization infrastructure between reward-service and wallet-service:

reward-service changes:
- Add OutboxEvent model to prisma schema for reliable event publishing
- Add outbox.repository.ts for outbox table CRUD operations
- Add outbox-publisher.service.ts for polling and publishing events to Kafka
- Add event-ack.controller.ts to receive consumer confirmations

wallet-service changes:
- Add ProcessedEvent model to prisma schema for idempotency checking
- Add reward-event-consumer.controller.ts to consume reward.summary.updated events
- Add event-ack.publisher.ts to send ACK to reward-service
- Update kafka.module.ts with Kafka client configuration
- Update main.ts to connect Kafka microservice on startup

Event flow: reward-service -> Kafka (reward.summary.updated) -> wallet-service -> Kafka (reward.events.ack) -> reward-service

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 23:26:49 -08:00
hailin e2b3f25dbc fix(reward): add /api/v1 prefix to internal service API calls
Both referral-service and authorization-service use global prefix /api/v1,
so the client calls need to include this prefix.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 22:42:14 -08:00
hailin 9a823e8871 fix(referral): convert BigInt to string for JSON serialization
BigInt cannot be serialized by JSON.stringify, convert to string
before returning from the API endpoint.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 22:29:17 -08:00
hailin 1afdc2ce17 refactor(reward): remove unused transferExpiredRewardToOperationAccount method
Expired referral rewards are transferred to headquarters community (seq=1),
not operation account. Remove the unused method to avoid confusion.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 21:55:18 -08:00
hailin 6f46a8633f feat(reward): add internal APIs for reward distribution
- Add /referral/chain/{userId} API in referral-service for getting referral chain with hasPlanted status
- Add internal authorization APIs in authorization-service:
  - GET /authorization/nearest-community: find nearest community in referral chain
  - GET /authorization/nearest-province: find nearest province company in referral chain
  - GET /authorization/nearest-city: find nearest city company in referral chain
- Add repository methods for finding active authorizations by accountSequence
- Update reward-service client to use accountSequence parameter

These APIs enable reward-service to correctly distribute:
- 分享权益 (share benefit): to referrer with hasPlanted=true
- 社区权益 (community benefit): to nearest community leader
- 省团队权益 (province team benefit): to nearest province company
- 市团队权益 (city team benefit): to nearest city company

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 21:49:13 -08:00
hailin 9cf8b5305b feat(authorization): add community hierarchy API
- Add internal referral-chain API in referral-service for getting ancestor path and team members
- Extend ReferralServiceClient to call referral-chain API
- Add findActiveCommunityByAccountSequences repository method
- Add getCommunityHierarchy application service method
- Add GET /authorizations/my/community-hierarchy endpoint
- Update frontend with CommunityHierarchy model and getMyCommunityHierarchy method

API returns:
- myCommunity: user's own community authorization (if any)
- parentCommunity: nearest parent community (defaults to 总部社区 if none)
- childCommunities: nearest child communities in user's team

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 18:18:49 -08:00
hailin f8cfb5e597 feat(authorization): add admin API for community authorization
Add new endpoint POST /api/v1/admin/authorizations/community
that allows administrators to directly authorize users as community
managers without requiring the user to self-apply first.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 17:03:34 -08:00
hailin 003bef1c76 fix(reward): add REFERRAL_SERVICE_URL and AUTHORIZATION_SERVICE_URL
reward-service was trying to connect to localhost instead of Docker
network service names, causing connection refused errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 16:48:47 -08:00
hailin b1e6cb5041 chore(authorization): add axios dependency for referral-service HTTP client
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 16:37:01 -08:00
hailin ccef23f5b0 feat(authorization): integrate with referral-service for team statistics
- Add InternalTeamStatisticsController in referral-service for service-to-service API
- Create ReferralServiceClient in authorization-service to fetch real team statistics
- Replace MockTeamStatisticsRepository with real HTTP client implementation
- Configure docker-compose with REFERRAL_SERVICE_URL for authorization-service

This enables authorization-service to get real team planting counts from
referral-service for authorization assessment and activation logic.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 16:30:35 -08:00
hailin 05545dea59 fix(authorization): add missing findByAccountSequence to MockTeamStatisticsRepository
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 16:18:12 -08:00
hailin b6bc10d680 debug(backup-service): log allowed services on init
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 16:00:29 -08:00
hailin 5ecb13974f fix(backup-service): add mpc-service to allowed services
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 15:42:40 -08:00
hailin 660e5ea3a0 fix(mpc-service): add backup service environment variables
Add BACKUP_SERVICE_URL, BACKUP_SERVICE_ENABLED, and SERVICE_JWT_SECRET
to mpc-service configuration to enable backup share storage.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 15:39:52 -08:00
hailin 41af5ef3a3 fix(mpc/backup): add accountSequence to backup share storage
- mpc-service: add accountSequence param to StoreBackupShareParams
- mpc-service: pass accountSequence when calling backup-service
- backup-service: add logging for store request success/failure

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 15:06:00 -08:00
hailin 300fe211c8 fix: add accountSequence to all services and fix compilation errors
- reward-service: add accountSequence to aggregates, services, tests
- authorization-service: fix UserId/AdminUserId to accept accountSequence, add findByAccountSequence to repositories
- referral-service: fix test files for accountSequence changes
- Add migration files for reward-service and authorization-service

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 14:14:06 -08:00
hailin 034fb53674 refactor: use accountSequence as unified user identifier across all services
- planting-service: extract accountSequence from JWT, pass to referral-service
- referral-service: query by accountSequence instead of userId
- reward-service: add accountSequence field to schema and all layers
- wallet-service: prioritize accountSequence lookup over userId
- authorization-service: change userId from String to BigInt, add accountSequence

This change ensures consistent cross-service user identification using
accountSequence (8-digit unique business ID) instead of internal database IDs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 13:55:03 -08:00
hailin 62a21b73a5 fix(referral): use accountSequence instead of userId for user registration events
identity-service sends userId=0 in events (temp value before DB save),
use accountSequence as the unique identifier instead
2025-12-10 13:29:36 -08:00
hailin 31c3c24a12 fix(reward-service): correct healthcheck endpoint path to /api/v1/health 2025-12-10 12:48:26 -08:00
hailin 8532bf6945 fix(blockchain): use amountFormatted to rebuild TokenAmount from DB
Previously used amount (raw) with hardcoded 18 decimals, which caused
incorrect formatting for USDT (6 decimals). Now uses amountFormatted
which is already correctly formatted during deposit detection.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 12:22:32 -08:00
hailin db3e0ba1bd fix(blockchain): fix deposit stuck in CONFIRMING status
The updateConfirmations logic only checked for DETECTED status
when confirming, so deposits that transitioned to CONFIRMING
would never be confirmed even with enough confirmations.

Changed condition to accept both DETECTED and CONFIRMING status.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 12:19:29 -08:00
hailin 5e670e64b3 fix(blockchain): read token decimals from contract for deposit detection
Previously decimals were hardcoded to 18, causing incorrect amount parsing
for USDT which uses 6 decimals. Now reads decimals() from ERC20 contract
when processing deposits.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 11:57:34 -08:00