fix(pre-planting): 修复推荐链 API 调用的 URL 路径和返回格式解析

1. URL: /referrals/:id/chain → /referral/chain/:id(与正常认种对齐)
2. 返回格式: 正确解析 { ancestors: [{accountSequence, hasPlanted}] }
   之前错误期望 { directReferrer: {...} },导致有推荐人也被当成无推荐人

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-02-28 21:39:07 -08:00
parent 545e897c1f
commit f32748c1d5
1 changed files with 41 additions and 5 deletions

View File

@ -11,6 +11,17 @@ export interface ReferralChainInfo {
} | null; } | null;
} }
/**
* referral-service /api/v1/referral/chain/:accountSequence
*/
interface ReferralChainApiResponse {
ancestors: Array<{
userId: string;
accountSequence: string;
hasPlanted: boolean;
}>;
}
@Injectable() @Injectable()
export class PrePlantingReferralClient { export class PrePlantingReferralClient {
private readonly logger = new Logger(PrePlantingReferralClient.name); 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<ReferralChainInfo> { async getReferralChain(accountSequence: string): Promise<ReferralChainInfo> {
try { try {
const response = await firstValueFrom( const response = await firstValueFrom(
this.httpService.get<ReferralChainInfo>( this.httpService.get<ReferralChainApiResponse>(
`${this.baseUrl}/api/v1/referrals/${accountSequence}/chain`, `${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) { } catch (error) {
// 404 = 用户无推荐关系,属于正常情况(如测试账号、直接注册用户)
// 返回空推荐链而非抛错,避免阻断购买流程
if (error?.response?.status === 404) { if (error?.response?.status === 404) {
this.logger.warn( this.logger.warn(
`No referral chain found for ${accountSequence}, proceeding without referrer`, `No referral chain found for ${accountSequence}, proceeding without referrer`,