diff --git a/backend/services/referral-service/src/api/controllers/internal-referral-chain.controller.ts b/backend/services/referral-service/src/api/controllers/internal-referral-chain.controller.ts index f139f19e..3338bfcc 100644 --- a/backend/services/referral-service/src/api/controllers/internal-referral-chain.controller.ts +++ b/backend/services/referral-service/src/api/controllers/internal-referral-chain.controller.ts @@ -58,8 +58,21 @@ export class InternalReferralChainController { }; } - // ancestorPath 存储的是 userId (bigint),需要转换为字符串 - const ancestorPath = relationship.referralChain.map((id) => id.toString()); + // referralChain 存储的是 userId (bigint),需要转换为 accountSequence + // 因为 authorization-service 使用 accountSequence 来查询授权记录 + const ancestorUserIds = relationship.referralChain; + const ancestorPath: string[] = []; + + for (const ancestorUserId of ancestorUserIds) { + const ancestorRelationship = await this.referralRepo.findByUserId(ancestorUserId); + if (ancestorRelationship) { + ancestorPath.push(ancestorRelationship.accountSequence); + } else { + this.logger.warn(`[INTERNAL] Ancestor userId ${ancestorUserId} not found`); + } + } + + this.logger.debug(`[INTERNAL] getReferralChain result: ${ancestorPath.length} ancestors`); return { accountSequence: relationship.accountSequence, @@ -88,9 +101,18 @@ export class InternalReferralChainController { for (const seq of sequences) { const relationship = await this.referralRepo.findByAccountSequence(seq); if (relationship) { + // 转换 userId 为 accountSequence + const ancestorPath: string[] = []; + for (const ancestorUserId of relationship.referralChain) { + const ancestorRelationship = await this.referralRepo.findByUserId(ancestorUserId); + if (ancestorRelationship) { + ancestorPath.push(ancestorRelationship.accountSequence); + } + } + results[seq] = { userId: relationship.userId.toString(), - ancestorPath: relationship.referralChain.map((id) => id.toString()), + ancestorPath, referrerId: relationship.referrerId?.toString() ?? null, }; } else {