From e0f529799f6fa09f39b3477454c6edd6abd79add Mon Sep 17 00:00:00 2001 From: hailin Date: Sun, 11 Jan 2026 20:24:48 -0800 Subject: [PATCH] =?UTF-8?q?fix(mining-admin):=20=E6=B7=BB=E5=8A=A0=20syncA?= =?UTF-8?q?llUsers=20=E5=92=8C=20syncAllContributionAccounts=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.5 --- .../services/initialization.service.ts | 104 +++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/backend/services/mining-admin-service/src/application/services/initialization.service.ts b/backend/services/mining-admin-service/src/application/services/initialization.service.ts index c3ed5a4f..5dff6095 100644 --- a/backend/services/mining-admin-service/src/application/services/initialization.service.ts +++ b/backend/services/mining-admin-service/src/application/services/initialization.service.ts @@ -25,7 +25,6 @@ export class InitializationService { }); try { - // 调用 mining-service API 初始化配置 const miningServiceUrl = this.configService.get('MINING_SERVICE_URL', 'http://localhost:3021'); const response = await fetch(`${miningServiceUrl}/api/v1/admin/initialize`, { method: 'POST', @@ -97,4 +96,107 @@ export class InitializationService { return { success: false, message: error.message }; } } + + async syncAllUsers(adminId: string): Promise<{ success: boolean; message: string; syncedCount?: number }> { + try { + const authServiceUrl = this.configService.get('AUTH_SERVICE_URL', 'http://localhost:3024'); + const response = await fetch(`${authServiceUrl}/api/v2/admin/users/sync`); + + if (!response.ok) { + throw new Error(`Failed to fetch users: ${response.statusText}`); + } + + const { users } = await response.json(); + let syncedCount = 0; + + for (const user of users) { + try { + await this.prisma.syncedUser.upsert({ + where: { accountSequence: user.accountSequence }, + create: { + originalUserId: user.id || user.accountSequence, + accountSequence: user.accountSequence, + phone: user.phone, + status: user.status || 'ACTIVE', + kycStatus: user.kycStatus || 'PENDING', + realName: user.realName || null, + isLegacyUser: user.isLegacyUser || false, + createdAt: new Date(user.createdAt), + }, + update: { + phone: user.phone, + status: user.status || 'ACTIVE', + kycStatus: user.kycStatus || 'PENDING', + realName: user.realName || null, + }, + }); + syncedCount++; + } catch (err) { + this.logger.warn(`Failed to sync user ${user.accountSequence}: ${err}`); + } + } + + await this.prisma.auditLog.create({ + data: { adminId, action: 'SYNC', resource: 'USER', newValue: { syncedCount } }, + }); + + return { success: true, message: `Synced ${syncedCount} users`, syncedCount }; + } catch (error: any) { + return { success: false, message: error.message }; + } + } + + async syncAllContributionAccounts(adminId: string): Promise<{ success: boolean; message: string; syncedCount?: number }> { + try { + const contributionServiceUrl = this.configService.get('CONTRIBUTION_SERVICE_URL', 'http://localhost:3020'); + const response = await fetch(`${contributionServiceUrl}/api/v2/admin/accounts/sync`); + + if (!response.ok) { + throw new Error(`Failed to fetch accounts: ${response.statusText}`); + } + + const { accounts } = await response.json(); + let syncedCount = 0; + + for (const account of accounts) { + try { + await this.prisma.syncedContributionAccount.upsert({ + where: { accountSequence: account.accountSequence }, + create: { + accountSequence: account.accountSequence, + personalContribution: account.personalContribution || 0, + teamLevelContribution: account.teamLevelContribution || 0, + teamBonusContribution: account.teamBonusContribution || 0, + totalContribution: account.totalContribution || 0, + effectiveContribution: account.effectiveContribution || 0, + hasAdopted: account.hasAdopted || false, + directReferralCount: account.directReferralAdoptedCount || 0, + unlockedLevelDepth: account.unlockedLevelDepth || 0, + }, + update: { + personalContribution: account.personalContribution, + teamLevelContribution: account.teamLevelContribution, + teamBonusContribution: account.teamBonusContribution, + totalContribution: account.totalContribution, + effectiveContribution: account.effectiveContribution, + hasAdopted: account.hasAdopted, + directReferralCount: account.directReferralAdoptedCount, + unlockedLevelDepth: account.unlockedLevelDepth, + }, + }); + syncedCount++; + } catch (err) { + this.logger.warn(`Failed to sync account ${account.accountSequence}: ${err}`); + } + } + + await this.prisma.auditLog.create({ + data: { adminId, action: 'SYNC', resource: 'CONTRIBUTION_ACCOUNT', newValue: { syncedCount } }, + }); + + return { success: true, message: `Synced ${syncedCount} accounts`, syncedCount }; + } catch (error: any) { + return { success: false, message: error.message }; + } + } }