From f32748c1d5640d1dd40602f6e0cffcbac9fc1bae Mon Sep 17 00:00:00 2001 From: hailin Date: Sat, 28 Feb 2026 21:39:07 -0800 Subject: [PATCH] =?UTF-8?q?fix(pre-planting):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=8E=A8=E8=8D=90=E9=93=BE=20API=20=E8=B0=83=E7=94=A8=E7=9A=84?= =?UTF-8?q?=20URL=20=E8=B7=AF=E5=BE=84=E5=92=8C=E8=BF=94=E5=9B=9E=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. URL: /referrals/:id/chain → /referral/chain/:id(与正常认种对齐) 2. 返回格式: 正确解析 { ancestors: [{accountSequence, hasPlanted}] } 之前错误期望 { directReferrer: {...} },导致有推荐人也被当成无推荐人 Co-Authored-By: Claude Opus 4.6 --- .../external/pre-planting-referral.client.ts | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) 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`,