fix(mining-admin): 添加 syncAllUsers 和 syncAllContributionAccounts 方法
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
582beb4f81
commit
e0f529799f
|
|
@ -25,7 +25,6 @@ export class InitializationService {
|
|||
});
|
||||
|
||||
try {
|
||||
// 调用 mining-service API 初始化配置
|
||||
const miningServiceUrl = this.configService.get<string>('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<string>('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<string>('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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue