From dbf97ae487bead59fa16e49e3e67cef0499e117f Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 13 Jan 2026 19:48:34 -0800 Subject: [PATCH] fix(contribution-service): filter adoptions by MINING_ENABLED status Only process adoptions with MINING_ENABLED status for contribution calculation. This fixes the bug where non-final adoption records (PENDING, PAID, etc.) were incorrectly being processed, causing duplicate contribution records. Affected methods: - findUndistributedAdoptions: only process MINING_ENABLED adoptions - getDirectReferralAdoptedCount: only count users with MINING_ENABLED adoptions - getTotalTreesByAccountSequence: only sum trees from MINING_ENABLED adoptions - getTeamTreesByLevel: only count MINING_ENABLED adoptions - countUndistributedAdoptions: only count MINING_ENABLED adoptions Co-Authored-By: Claude Opus 4.5 --- .../repositories/synced-data.repository.ts | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/backend/services/contribution-service/src/infrastructure/persistence/repositories/synced-data.repository.ts b/backend/services/contribution-service/src/infrastructure/persistence/repositories/synced-data.repository.ts index 331fe91b..c8f986f1 100644 --- a/backend/services/contribution-service/src/infrastructure/persistence/repositories/synced-data.repository.ts +++ b/backend/services/contribution-service/src/infrastructure/persistence/repositories/synced-data.repository.ts @@ -136,7 +136,10 @@ export class SyncedDataRepository implements ISyncedDataRepository { async findUndistributedAdoptions(limit: number = 100): Promise { const records = await this.client.syncedAdoption.findMany({ - where: { contributionDistributed: false }, + where: { + contributionDistributed: false, + status: 'MINING_ENABLED', // 只处理最终成功的认种订单 + }, orderBy: { adoptionDate: 'asc' }, take: limit, }); @@ -171,7 +174,10 @@ export class SyncedDataRepository implements ISyncedDataRepository { async getTotalTreesByAccountSequence(accountSequence: string): Promise { const result = await this.client.syncedAdoption.aggregate({ - where: { accountSequence }, + where: { + accountSequence, + status: 'MINING_ENABLED', // 只统计最终成功的认种订单 + }, _sum: { treeCount: true }, }); return result._sum.treeCount ?? 0; @@ -285,8 +291,12 @@ export class SyncedDataRepository implements ISyncedDataRepository { const accountSequences = directReferrals.map((r) => r.accountSequence); + // 只统计有 MINING_ENABLED 状态认种记录的直推用户数 const adoptedCount = await this.client.syncedAdoption.findMany({ - where: { accountSequence: { in: accountSequences } }, + where: { + accountSequence: { in: accountSequences }, + status: 'MINING_ENABLED', // 只统计最终成功的认种订单 + }, distinct: ['accountSequence'], }); @@ -308,7 +318,10 @@ export class SyncedDataRepository implements ISyncedDataRepository { const adoptions = await this.client.syncedAdoption.groupBy({ by: ['accountSequence'], - where: { accountSequence: { in: sequences } }, + where: { + accountSequence: { in: sequences }, + status: 'MINING_ENABLED', // 只统计最终成功的认种订单 + }, _sum: { treeCount: true }, }); @@ -358,7 +371,10 @@ export class SyncedDataRepository implements ISyncedDataRepository { async countUndistributedAdoptions(): Promise { return this.client.syncedAdoption.count({ - where: { contributionDistributed: false }, + where: { + contributionDistributed: false, + status: 'MINING_ENABLED', // 只统计最终成功的认种订单 + }, }); }