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 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-13 19:48:34 -08:00
parent fdfc2d6700
commit dbf97ae487
1 changed files with 21 additions and 5 deletions

View File

@ -136,7 +136,10 @@ export class SyncedDataRepository implements ISyncedDataRepository {
async findUndistributedAdoptions(limit: number = 100): Promise<SyncedAdoption[]> { async findUndistributedAdoptions(limit: number = 100): Promise<SyncedAdoption[]> {
const records = await this.client.syncedAdoption.findMany({ const records = await this.client.syncedAdoption.findMany({
where: { contributionDistributed: false }, where: {
contributionDistributed: false,
status: 'MINING_ENABLED', // 只处理最终成功的认种订单
},
orderBy: { adoptionDate: 'asc' }, orderBy: { adoptionDate: 'asc' },
take: limit, take: limit,
}); });
@ -171,7 +174,10 @@ export class SyncedDataRepository implements ISyncedDataRepository {
async getTotalTreesByAccountSequence(accountSequence: string): Promise<number> { async getTotalTreesByAccountSequence(accountSequence: string): Promise<number> {
const result = await this.client.syncedAdoption.aggregate({ const result = await this.client.syncedAdoption.aggregate({
where: { accountSequence }, where: {
accountSequence,
status: 'MINING_ENABLED', // 只统计最终成功的认种订单
},
_sum: { treeCount: true }, _sum: { treeCount: true },
}); });
return result._sum.treeCount ?? 0; return result._sum.treeCount ?? 0;
@ -285,8 +291,12 @@ export class SyncedDataRepository implements ISyncedDataRepository {
const accountSequences = directReferrals.map((r) => r.accountSequence); const accountSequences = directReferrals.map((r) => r.accountSequence);
// 只统计有 MINING_ENABLED 状态认种记录的直推用户数
const adoptedCount = await this.client.syncedAdoption.findMany({ const adoptedCount = await this.client.syncedAdoption.findMany({
where: { accountSequence: { in: accountSequences } }, where: {
accountSequence: { in: accountSequences },
status: 'MINING_ENABLED', // 只统计最终成功的认种订单
},
distinct: ['accountSequence'], distinct: ['accountSequence'],
}); });
@ -308,7 +318,10 @@ export class SyncedDataRepository implements ISyncedDataRepository {
const adoptions = await this.client.syncedAdoption.groupBy({ const adoptions = await this.client.syncedAdoption.groupBy({
by: ['accountSequence'], by: ['accountSequence'],
where: { accountSequence: { in: sequences } }, where: {
accountSequence: { in: sequences },
status: 'MINING_ENABLED', // 只统计最终成功的认种订单
},
_sum: { treeCount: true }, _sum: { treeCount: true },
}); });
@ -358,7 +371,10 @@ export class SyncedDataRepository implements ISyncedDataRepository {
async countUndistributedAdoptions(): Promise<number> { async countUndistributedAdoptions(): Promise<number> {
return this.client.syncedAdoption.count({ return this.client.syncedAdoption.count({
where: { contributionDistributed: false }, where: {
contributionDistributed: false,
status: 'MINING_ENABLED', // 只统计最终成功的认种订单
},
}); });
} }