fix(contribution): use setDirectReferralAdoptedCount for accurate count update

Changed updateReferrerUnlockStatus to:
1. Create account if not exists (for full-reset scenarios)
2. Use setDirectReferralAdoptedCount instead of increment loop
3. This ensures the count is always accurate regardless of processing order

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-13 20:29:53 -08:00
parent 5eae4464ef
commit 1fbb88f773
1 changed files with 11 additions and 10 deletions

View File

@ -400,28 +400,29 @@ export class ContributionCalculationService {
* 线
*/
private async updateReferrerUnlockStatus(referrerAccountSequence: string): Promise<void> {
const account = await this.contributionAccountRepository.findByAccountSequence(referrerAccountSequence);
if (!account) return;
let account = await this.contributionAccountRepository.findByAccountSequence(referrerAccountSequence);
// 重新计算直推认种用户数
// 如果账户不存在,先创建一个
if (!account) {
account = ContributionAccountAggregate.create(referrerAccountSequence);
}
// 重新计算直推认种用户数(从数据库实时查询)
const directReferralAdoptedCount = await this.syncedDataRepository.getDirectReferralAdoptedCount(
referrerAccountSequence,
);
// 更新解锁状态
// 直接设置计数值(不是增量更新),确保数据一致性
const currentCount = account.directReferralAdoptedCount;
if (directReferralAdoptedCount > currentCount) {
// 需要增量更新
for (let i = currentCount; i < directReferralAdoptedCount; i++) {
account.incrementDirectReferralAdoptedCount();
}
if (directReferralAdoptedCount !== currentCount) {
account.setDirectReferralAdoptedCount(directReferralAdoptedCount);
await this.contributionAccountRepository.save(account);
// 发布账户更新事件到 outbox用于 CDC 同步到 mining-admin-service
await this.publishContributionAccountUpdatedEvent(account);
this.logger.debug(
`Updated referrer ${referrerAccountSequence} unlock status: level=${account.unlockedLevelDepth}, bonus=${account.unlockedBonusTiers}`,
`Updated referrer ${referrerAccountSequence} unlock status: count=${directReferralAdoptedCount}, level=${account.unlockedLevelDepth}, bonus=${account.unlockedBonusTiers}`,
);
}
}