import type { ServiceContainer } from '@stock-bot/di'; import { getLogger } from '@stock-bot/logger'; import type { IHandler, ExecutionContext } from '../types/types'; /** * Abstract base class for all handlers * Provides common functionality and structure for queue/event operations */ export abstract class BaseHandler implements IHandler { protected readonly logger; constructor(protected readonly container: ServiceContainer) { this.logger = getLogger(this.constructor.name); } /** * Main execution method - must be implemented by subclasses * Works with queue (events commented for future) */ abstract execute(operation: string, input: unknown, context: ExecutionContext): Promise; /** * Queue helper methods */ protected async scheduleOperation(operation: string, payload: unknown, delay?: number): Promise { const queue = await this.container.resolveAsync('queue'); await queue.add(operation, payload, { delay }); } /** * Get a service from the container */ protected async getService(serviceName: string): Promise { return await this.container.resolveAsync(serviceName); } /** * Event methods - commented for future */ // protected async publishEvent(eventName: string, payload: unknown): Promise { // const eventBus = await this.container.resolveAsync('eventBus'); // await eventBus.publish(eventName, payload); // } /** * Lifecycle hooks - can be overridden by subclasses */ async onInit?(): Promise; async onStart?(): Promise; async onStop?(): Promise; async onDispose?(): Promise; } /** * Specialized handler for operations that have scheduled jobs */ export abstract class ScheduledHandler extends BaseHandler { /** * Get scheduled job configurations for this handler * Override in subclasses to define schedules */ getScheduledJobs?(): Array<{ operation: string; cronPattern: string; priority?: number; immediately?: boolean; description?: string; }>; }