diff --git a/.env b/.env index 858685b0..5c1207eb 100644 --- a/.env +++ b/.env @@ -17,8 +17,8 @@ DATA_SERVICE_PORT=2001 WORKER_COUNT=1 WORKER_CONCURRENCY=2 -WEBSHARE_API_KEY=y8ay534rcbybdkk3evnzmt640xxfhy7252ce2t98 -WEBSHARE_ROTATING_PROXY_URL=http://doimvbnb-rotate:w5fpiwrb9895@p.webshare.io:80/ +WEBSHARE_API_KEY= +WEBSHARE_ROTATING_PROXY_URL= WEBSHARE_API_URL=https://proxy.webshare.io/api/v2/ # =========================================== diff --git a/apps/stock/config/config/default.json b/apps/stock/config/config/default.json index e34d7aca..83afa46a 100644 --- a/apps/stock/config/config/default.json +++ b/apps/stock/config/config/default.json @@ -114,7 +114,7 @@ "cachePrefix": "proxy:", "ttl": 3600, "webshare": { - "apiKey": "y8ay534rcbybdkk3evnzmt640xxfhy7252ce2t98", + "apiKey": "", "apiUrl": "https://proxy.webshare.io/api/v2/" } }, @@ -150,7 +150,7 @@ "name": "eod", "enabled": false, "priority": 4, - "apiKey": "657fe003583a32.85708911", + "apiKey": "", "baseUrl": "https://eodhistoricaldata.com/api", "tier": "free" } diff --git a/apps/stock/config/src/env-safety.ts b/apps/stock/config/src/env-safety.ts index 4e5c95a8..f45c0150 100644 --- a/apps/stock/config/src/env-safety.ts +++ b/apps/stock/config/src/env-safety.ts @@ -1,3 +1,4 @@ +import { hostname as osHostname, networkInterfaces } from 'node:os'; import type { StockAppConfig } from './schemas'; export type StockServiceName = 'dataIngestion' | 'dataPipeline' | 'webApi' | 'orchestrator'; @@ -532,9 +533,64 @@ function parseMongoTarget(uri?: string): ParsedMongoTarget { }; } +function getLocalMachineHosts(): Set { + const hosts = new Set(); + const rawHostname = osHostname().trim().toLowerCase().replace(/\.+$/, ''); + if (rawHostname) { + hosts.add(rawHostname); + const shortName = rawHostname.split('.')[0]; + if (shortName) { + hosts.add(shortName); + hosts.add(`${shortName}.local`); + } + } + const ifaces = networkInterfaces(); + for (const entries of Object.values(ifaces)) { + if (!entries) { + continue; + } + for (const entry of entries) { + if (entry.address) { + const addr = entry.address.split('%')[0]?.trim().toLowerCase(); + if (addr) { + hosts.add(addr); + } + } + } + } + return hosts; +} + +function isLocalMachineHost(host?: string): boolean { + if (!host) { + return false; + } + if (isLoopbackHost(host)) { + return true; + } + const normalized = host + .trim() + .replace(/^\[|\]$/g, '') + .replace(/\.+$/, '') + .toLowerCase(); + const localHosts = getLocalMachineHosts(); + if (localHosts.has(normalized)) { + return true; + } + // Also normalize numeric/decimal IPv4 spellings (e.g. hex or leading-zero LAN IP) + const numericOctets = parseNumericIpv4Octets(normalized) ?? parseDecimalIpv4Octets(normalized); + if (numericOctets !== null) { + const dotted = numericOctets.join('.'); + if (localHosts.has(dotted)) { + return true; + } + } + return false; +} + function isQaMongoTargetHosts(target: ParsedMongoTarget): boolean { return target.hosts.some( - entry => isLoopbackHost(entry.host) && (entry.port === undefined || entry.port === 27017) + entry => isLocalMachineHost(entry.host) && (entry.port === undefined || entry.port === 27017) ); } @@ -543,7 +599,7 @@ function isQaMongoTarget(uri: string | undefined): boolean { } function isQaPostgresTarget(pg: StockAppConfig['database']['postgres']): boolean { - return isLoopbackHost(pg.host) && pg.port === 5432 && pg.database === 'trading_bot'; + return isLocalMachineHost(pg.host) && pg.port === 5432 && pg.database === 'trading_bot'; } /** diff --git a/apps/stock/data-ingestion/src/handlers/eod/shared/config.ts b/apps/stock/data-ingestion/src/handlers/eod/shared/config.ts index a9644bfb..bd0f46fd 100644 --- a/apps/stock/data-ingestion/src/handlers/eod/shared/config.ts +++ b/apps/stock/data-ingestion/src/handlers/eod/shared/config.ts @@ -1,5 +1,5 @@ export const EOD_CONFIG = { // API configuration API_BASE_URL: 'https://eodhd.com/api/', - API_TOKEN: '657fe003583a32.85708911', + API_TOKEN: process.env.EOD_API_KEY ?? '', }; \ No newline at end of file diff --git a/libs/core/handlers/src/decorators/decorators.ts b/libs/core/handlers/src/decorators/decorators.ts index 1c807b93..a0026b77 100644 --- a/libs/core/handlers/src/decorators/decorators.ts +++ b/libs/core/handlers/src/decorators/decorators.ts @@ -44,11 +44,21 @@ export function Operation( if (!constructor.__operations) { constructor.__operations = []; } - constructor.__operations.push({ - name, - method: String(methodName), - batch: options?.batch, - }); + const methodKey = String(methodName); + const existing = constructor.__operations.find( + op => op.name === name && op.method === methodKey + ); + if (existing) { + if (options?.batch !== undefined && existing.batch === undefined) { + existing.batch = options.batch; + } + } else { + constructor.__operations.push({ + name, + method: methodKey, + batch: options?.batch, + }); + } return descriptor; }; @@ -82,11 +92,17 @@ export function QueueSchedule( if (!constructor.__schedules) { constructor.__schedules = []; } - constructor.__schedules.push({ - operation: String(methodName), - cronPattern, - ...options, - }); + const operationKey = String(methodName); + const existingSchedule = constructor.__schedules.find( + s => s.operation === operationKey && s.cronPattern === cronPattern + ); + if (!existingSchedule) { + constructor.__schedules.push({ + operation: operationKey, + cronPattern, + ...options, + }); + } return descriptor; }; diff --git a/libs/core/handlers/test/decorators.test.ts b/libs/core/handlers/test/decorators.test.ts index aed78471..460ced8c 100644 --- a/libs/core/handlers/test/decorators.test.ts +++ b/libs/core/handlers/test/decorators.test.ts @@ -267,6 +267,26 @@ describe('Handler Decorators', () => { expect(constructor.__schedules[1]).toMatchObject({ cronPattern: '0 0 * * *' }); expect(constructor.__schedules[2]).toMatchObject({ cronPattern: '0 0 * * 0' }); }); + + it('should not register duplicate operation metadata when @Operation and @ScheduledOperation are both applied (T-17)', () => { + class DeduplicatedHandler { + @Operation('sync-all-exchanges') + @ScheduledOperation('sync-all-exchanges', '0 0 * * 0', { + priority: 10, + batch: { enabled: true, size: 25 }, + }) + async syncAllExchanges() {} + } + + const constructor = DeduplicatedHandler as any; + expect(constructor.__operations).toHaveLength(1); + expect(constructor.__operations[0]).toEqual({ + name: 'sync-all-exchanges', + method: 'syncAllExchanges', + batch: { enabled: true, size: 25 }, + }); + expect(constructor.__schedules).toHaveLength(1); + }); }); describe('decorator composition', () => { diff --git a/test/unit/environment-safety.test.ts b/test/unit/environment-safety.test.ts index ae059eb2..fc0cb49e 100644 --- a/test/unit/environment-safety.test.ts +++ b/test/unit/environment-safety.test.ts @@ -1,3 +1,4 @@ +import { hostname as osHostname, networkInterfaces } from 'node:os'; import { describe, expect, test } from 'bun:test'; import { ConfigManager } from '@stock-bot/config'; import { ServiceContainerBuilder } from '@stock-bot/di'; @@ -299,6 +300,44 @@ describe('Environment safety & multi-env isolation', () => { ); }); + test('blocks QA MongoDB and PostgreSQL when targeted via machine hostname or local LAN IP (T-15)', () => { + const localHost = osHostname().trim(); + if (localHost) { + const mongoByHostname = makeConfig({ + environment: 'development', + mongoUri: `mongodb://trading_admin:pass@${localHost}:27017/stock`, + mongoDatabase: 'stock', + }); + expect(() => assertEnvironmentSafety(mongoByHostname, 'dataIngestion', {})).toThrow( + /cannot open a writable MongoDB connection to QA MongoDB/ + ); + + const pgByHostname = makeConfig({ + environment: 'development', + pgHost: localHost, + pgPort: 5432, + pgDatabase: 'trading_bot', + }); + expect(() => assertEnvironmentSafety(pgByHostname, 'dataPipeline', {})).toThrow( + /cannot target QA PostgreSQL/ + ); + } + + const lanIpv4 = Object.values(networkInterfaces()) + .flatMap(list => list ?? []) + .find(iface => iface.family === 'IPv4' && !iface.internal)?.address; + if (lanIpv4) { + const mongoByLanIp = makeConfig({ + environment: 'development', + mongoUri: `mongodb://trading_admin:pass@${lanIpv4}:27017/stock`, + mongoDatabase: 'stock', + }); + expect(() => assertEnvironmentSafety(mongoByLanIp, 'dataIngestion', {})).toThrow( + /cannot open a writable MongoDB connection to QA MongoDB/ + ); + } + }); + test('blocks multi-host MongoDB seed lists targeting QA loopback :27017 for dataIngestion and dataPipeline', () => { const qaFirstIngestion = makeConfig({ environment: 'development',