Skip to content
Shell
2.7 KB
Raw Blame
/**
 * Alert System
 * Unified interface for Slack and Email alerts
 */

export * from './slack.js';
export * from './email.js';

import { SlackAlerts, createSlackAlerts } from './slack.js';
import { EmailAlerts, createEmailAlerts } from './email.js';
import type { GoldmineScore, MarketInsight } from '../types.js';

/**
 * Unified Alert Manager
 * Sends alerts to all configured channels
 */
export class AlertManager {
  private slack: SlackAlerts | null;
  private email: EmailAlerts | null;
  private verbose: boolean;

  constructor(options: { verbose?: boolean } = {}) {
    this.slack = createSlackAlerts();
    this.email = createEmailAlerts();
    this.verbose = options.verbose ?? false;

    if (this.verbose) {
      console.log(`Alerts configured: Slack=${!!this.slack}, Email=${!!this.email}`);
    }
  }

  /**
   * Check if any alerts are configured
   */
  isConfigured(): boolean {
    return this.slack !== null || this.email !== null;
  }

  /**
   * Alert on high-value thread discovery
   */
  async alertThread(thread: GoldmineScore): Promise<void> {
    const promises: Promise<boolean>[] = [];

    if (this.slack) {
      promises.push(this.slack.alertThread(thread));
    }

    if (this.email) {
      promises.push(this.email.alertThread(thread));
    }

    if (promises.length > 0) {
      const results = await Promise.allSettled(promises);
      if (this.verbose) {
        const succeeded = results.filter(r => r.status === 'fulfilled' && r.value).length;
        console.log(`Thread alert sent to ${succeeded}/${promises.length} channels`);
      }
    }
  }

  /**
   * Alert on new market insights
   */
  async alertInsights(insight: MarketInsight, threadTitle: string): Promise<void> {
    const promises: Promise<boolean>[] = [];

    if (this.slack) {
      promises.push(this.slack.alertInsights(insight, threadTitle));
    }

    if (this.email) {
      promises.push(this.email.alertInsights(insight, threadTitle));
    }

    if (promises.length > 0) {
      const results = await Promise.allSettled(promises);
      if (this.verbose) {
        const succeeded = results.filter(r => r.status === 'fulfilled' && r.value).length;
        console.log(`Insights alert sent to ${succeeded}/${promises.length} channels`);
      }
    }
  }

  /**
   * Alert scan summary (Slack only)
   */
  async alertScanSummary(stats: {
    threadsScanned: number;
    insightsFound: number;
    topThreads: GoldmineScore[];
    duration: number;
  }): Promise<void> {
    if (this.slack) {
      await this.slack.alertScanSummary(stats);
    }
  }
}

/**
 * Create alert manager from environment
 */
export function createAlertManager(options: { verbose?: boolean } = {}): AlertManager {
  return new AlertManager(options);
}