From b690f26a28523b940a7ee13ec5841ee986b4b0d6 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 6 May 2026 06:35:07 +0000 Subject: [PATCH] fix(simple): refresh user keys for profile sync --- backend/src/index.ts | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/backend/src/index.ts b/backend/src/index.ts index dfabd52..1a1458f 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -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 { - 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;