diff --git a/backend/services/planting-service/src/pre-planting/infrastructure/external/pre-planting-referral.client.ts b/backend/services/planting-service/src/pre-planting/infrastructure/external/pre-planting-referral.client.ts index 451eb0e4..1ee9e462 100644 --- a/backend/services/planting-service/src/pre-planting/infrastructure/external/pre-planting-referral.client.ts +++ b/backend/services/planting-service/src/pre-planting/infrastructure/external/pre-planting-referral.client.ts @@ -11,6 +11,17 @@ export interface ReferralChainInfo { } | null; } +/** + * referral-service /api/v1/referral/chain/:accountSequence 的实际返回格式 + */ +interface ReferralChainApiResponse { + ancestors: Array<{ + userId: string; + accountSequence: string; + hasPlanted: boolean; + }>; +} + @Injectable() export class PrePlantingReferralClient { private readonly logger = new Logger(PrePlantingReferralClient.name); @@ -27,18 +38,43 @@ export class PrePlantingReferralClient { /** * 获取直接推荐人信息 + * + * [2026-03-01] 修复: + * 1. URL 路径修正:/api/v1/referral/chain/:accountSequence(之前错误地用了 /referrals/:accountSequence/chain) + * 2. 返回格式修正:referral-service 返回 { ancestors: [{userId, accountSequence, hasPlanted}] }, + * 之前错误地期望 { directReferrer: {accountSequence, hasPlanted} } + * 3. ancestors[0] 即为直推人(直接推荐人) */ async getReferralChain(accountSequence: string): Promise { try { const response = await firstValueFrom( - this.httpService.get( - `${this.baseUrl}/api/v1/referrals/${accountSequence}/chain`, + this.httpService.get( + `${this.baseUrl}/api/v1/referral/chain/${accountSequence}`, ), ); - return response.data; + + const ancestors = response.data.ancestors || []; + if (ancestors.length === 0) { + this.logger.log( + `No referrer found for ${accountSequence} (empty ancestors)`, + ); + return { accountSequence, directReferrer: null }; + } + + // ancestors[0] 为直推人 + const directReferrer = ancestors[0]; + this.logger.log( + `Referrer for ${accountSequence}: ${directReferrer.accountSequence} (hasPlanted=${directReferrer.hasPlanted})`, + ); + + return { + accountSequence, + directReferrer: { + accountSequence: directReferrer.accountSequence, + hasPlanted: directReferrer.hasPlanted ?? false, + }, + }; } catch (error) { - // 404 = 用户无推荐关系,属于正常情况(如测试账号、直接注册用户) - // 返回空推荐链而非抛错,避免阻断购买流程 if (error?.response?.status === 404) { this.logger.warn( `No referral chain found for ${accountSequence}, proceeding without referrer`,