import { getLogger } from '@stock-bot/logger'; import { handlerRegistry, createJobHandler, type HandlerConfig, type ScheduledJobConfig } from '@stock-bot/queue'; import type { ServiceContainer } from '@stock-bot/di'; import { exchangeOperations } from './operations'; const logger = getLogger('exchanges-handler'); const HANDLER_NAME = 'exchanges'; const exchangesHandlerConfig: HandlerConfig = { concurrency: 1, maxAttempts: 3, scheduledJobs: [ { operation: 'sync-all-exchanges', cronPattern: '0 0 * * 0', // Weekly on Sunday at midnight payload: { clearFirst: true }, priority: 10, immediately: false, } as ScheduledJobConfig, { operation: 'sync-qm-exchanges', cronPattern: '0 1 * * *', // Daily at 1 AM payload: {}, priority: 5, immediately: false, } as ScheduledJobConfig, { operation: 'sync-ib-exchanges', cronPattern: '0 3 * * *', // Daily at 3 AM payload: {}, priority: 3, immediately: false, } as ScheduledJobConfig, { operation: 'sync-qm-provider-mappings', cronPattern: '0 3 * * *', // Daily at 3 AM payload: {}, priority: 7, immediately: false, } as ScheduledJobConfig, ], operations: { 'sync-all-exchanges': exchangeOperations.syncAllExchanges, 'sync-qm-exchanges': exchangeOperations.syncQMExchanges, 'sync-ib-exchanges': exchangeOperations.syncIBExchanges, 'sync-qm-provider-mappings': exchangeOperations.syncQMProviderMappings, 'clear-postgresql-data': exchangeOperations.clearPostgreSQLData, 'get-exchange-stats': exchangeOperations.getExchangeStats, 'get-provider-mapping-stats': exchangeOperations.getProviderMappingStats, 'enhanced-sync-status': exchangeOperations['enhanced-sync-status'], }, }; export function initializeExchangesHandler(container: ServiceContainer) { logger.info('Registering exchanges handler...'); // Update operations to use container const containerAwareOperations = Object.entries(exchangeOperations).reduce((acc, [key, operation]) => { acc[key] = createJobHandler(async (payload: any) => { return operation(payload, container); }); return acc; }, {} as Record); const exchangesHandlerConfigWithContainer: HandlerConfig = { ...exchangesHandlerConfig, operations: containerAwareOperations, }; handlerRegistry.register(HANDLER_NAME, exchangesHandlerConfigWithContainer); logger.info('Exchanges handler registered successfully'); }