fix(authorization-service): 使用 accountSequence 替代 userId 查询团队统计

问题:planting-service 发送的 PlantingOrderPaid 事件中的 userId 是
订单表的自增主键(如 15),而不是 referral-service 中的真实 user_id
(如 25122600006)。这导致 handleTreePlanted 方法查询团队统计时
返回 null,社区权益无法被自动激活。

修复:改用事件中的 accountSequence 字段查询团队统计,因为
accountSequence 是跨服务一致的用户标识。

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-25 22:51:32 -08:00
parent 148e197ea1
commit 6e84e370ee
1 changed files with 21 additions and 11 deletions

View File

@ -19,6 +19,7 @@ import { EventAckPublisher } from './event-ack.publisher'
interface TreePlantedPayload {
userId: string
accountSequence?: string
treeCount: number
provinceCode?: string
cityCode?: string
@ -195,31 +196,40 @@ export class EventConsumerController {
*
* 1.
* 2.
*
* planting-service userId referral-service user_id
* 使 accountSequence
*/
private async handleTreePlanted(payload: TreePlantedPayload): Promise<void> {
const { userId, treeCount } = payload
this.logger.log(`[PLANTING] Processing tree planted event: userId=${userId}, count=${treeCount}`)
const { userId, accountSequence, treeCount } = payload
this.logger.log(`[PLANTING] Processing tree planted event: userId=${userId}, accountSequence=${accountSequence}, count=${treeCount}`)
// 优先使用 accountSequence 查询,因为 userId 可能是 planting-service 的订单主键
if (!accountSequence) {
this.logger.warn(`[PLANTING] No accountSequence in payload, skipping. userId=${userId}`)
return
}
try {
// 1. 获取用户团队统计
const teamStats = await this.statsRepository.findByUserId(userId)
// 1. 使用 accountSequence 获取用户团队统计
const teamStats = await this.statsRepository.findByAccountSequence(accountSequence)
if (!teamStats) {
this.logger.warn(`[PLANTING] No team stats found for user ${userId}, skipping`)
this.logger.warn(`[PLANTING] No team stats found for accountSequence ${accountSequence}, skipping`)
return
}
// 使用下级团队认种数(不含自己)进行考核判断
const subordinateTreeCount = teamStats.subordinateTeamPlantingCount
this.logger.debug(`[PLANTING] User ${userId} subordinate team planting count: ${subordinateTreeCount}`)
this.logger.debug(`[PLANTING] User ${accountSequence} subordinate team planting count: ${subordinateTreeCount}`)
// 2. 获取用户所有授权
const authorizations = await this.authorizationRepository.findByUserId(
UserId.create(userId, teamStats.accountSequence),
UserId.create(teamStats.userId, accountSequence),
)
// 3. 处理每个授权(如果有的话)
if (authorizations.length === 0) {
this.logger.debug(`[PLANTING] User ${userId} has no authorizations, skipping individual auth processing`)
this.logger.debug(`[PLANTING] User ${accountSequence} has no authorizations, skipping individual auth processing`)
}
for (const auth of authorizations) {
@ -232,7 +242,7 @@ export class EventConsumerController {
this.logger.debug(`[PLANTING] Checking initial target: ${subordinateTreeCount}/${initialTarget}`)
if (subordinateTreeCount >= initialTarget) {
this.logger.log(`[PLANTING] User ${userId} reached initial target for ${auth.roleType}, activating benefit`)
this.logger.log(`[PLANTING] User ${accountSequence} reached initial target for ${auth.roleType}, activating benefit`)
auth.activateBenefit()
await this.authorizationRepository.save(auth)
await this.eventPublisher.publishAll(auth.domainEvents)
@ -258,9 +268,9 @@ export class EventConsumerController {
// 通过监听 referral.team-statistics.events 事件来触发升级检查
// 这样确保在 referral-service 完成统计更新后再进行检查,避免竞态条件
this.logger.log(`[PLANTING] Completed processing tree planted event for user ${userId}`)
this.logger.log(`[PLANTING] Completed processing tree planted event for accountSequence ${accountSequence}`)
} catch (error) {
this.logger.error(`[PLANTING] Error processing tree planted for user ${userId}:`, error)
this.logger.error(`[PLANTING] Error processing tree planted for accountSequence ${accountSequence}:`, error)
throw error
}
}