fix(simple): refresh user keys for profile sync

This commit is contained in:
root 2026-05-06 06:35:07 +00:00
parent e01f38c883
commit b690f26a28

View File

@ -18,7 +18,7 @@ import { observabilityService } from './services/observabilityService.js';
import { tradingTelemetry } from './services/tradingTelemetry.js';
import { reconciliationService } from './services/reconciliationService.js';
import { reconciliationWatchdogAutoResumeService } from './services/reconciliationWatchdogAutoResumeService.js';
import { listActiveTradeProfiles } from './services/profileRepository.js';
import { getCurrentUserProfile, listActiveTradeProfiles } from './services/profileRepository.js';
import { listActiveTradingUsers } from './services/userRepository.js';
import * as runtimeOrderRepository from './services/runtimeOrderRepository.js';
import { listManualEntries, saveManualEntryForUser, type ManualEntryRecord } from './services/manualEntryRepository.js';
@ -424,12 +424,40 @@ async function main() {
// --- Helper: Build a UserContext from a profile ---
async function buildProfileContext(profile: any): Promise<UserContext | null> {
const user = users.find(u => u.user_id === profile.user_id);
if (!user) {
const snapshotUser = users.find(u => u.user_id === profile.user_id);
const currentProfile = await getCurrentUserProfile(String(profile.user_id || '').trim(), snapshotUser || {
user_id: String(profile.user_id || '').trim(),
trade_enable: true,
});
if (!currentProfile) {
logger.warn(`⚠️ Profile ${profile.name} owner not found in active users. Skipping.`);
return null;
}
const user = {
user_id: String(currentProfile.user_id || ''),
first_name: String(currentProfile.first_name || ''),
last_name: String(currentProfile.last_name || ''),
email: String(currentProfile.email || ''),
FMP_API_KEY: String(currentProfile.FMP_API_KEY || ''),
ALPACA_API_KEY: String(currentProfile.ALPACA_API_KEY || ''),
ALPACA_SECRET_KEY: String(currentProfile.ALPACA_SECRET_KEY || ''),
REAL_ALPACA_API_KEY: String(currentProfile.REAL_ALPACA_API_KEY || ''),
REAL_ALPACA_SECRET_KEY: String(currentProfile.REAL_ALPACA_SECRET_KEY || ''),
role: String(currentProfile.role || 'member'),
trade_enable: Boolean(currentProfile.trade_enable),
drop_threshold_for_buy: Number(currentProfile.drop_threshold_for_buy || 0),
gain_threshold_for_sell: Number(currentProfile.gain_threshold_for_sell || 0),
market_poll_interval_in_seconds: Number(currentProfile.market_poll_interval_in_seconds || 0),
};
const existingUserIndex = users.findIndex((candidate) => candidate.user_id === user.user_id);
if (existingUserIndex >= 0) {
users[existingUserIndex] = user;
} else {
users.push(user);
}
const userKey = config.PAPER_TRADING ? user.ALPACA_API_KEY : user.REAL_ALPACA_API_KEY;
const userSecret = config.PAPER_TRADING ? user.ALPACA_SECRET_KEY : user.REAL_ALPACA_SECRET_KEY;