File size: 3,719 Bytes
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const logger = require('./logger')
const webhookService = require('../services/webhookService')
const { getISOStringWithTimezone } = require('./dateHelper')

class WebhookNotifier {
  constructor() {
    // 保留此类用于兼容性,实际功能委托给webhookService
  }

  /**
   * 发送账号异常通知
   * @param {Object} notification - 通知内容
   * @param {string} notification.accountId - 账号ID
   * @param {string} notification.accountName - 账号名称
   * @param {string} notification.platform - 平台类型 (claude-oauth, claude-console, gemini)
   * @param {string} notification.status - 异常状态 (unauthorized, blocked, error)
   * @param {string} notification.errorCode - 异常代码
   * @param {string} notification.reason - 异常原因
   * @param {string} notification.timestamp - 时间戳
   */
  async sendAccountAnomalyNotification(notification) {
    try {
      // 使用新的webhookService发送通知
      await webhookService.sendNotification('accountAnomaly', {
        accountId: notification.accountId,
        accountName: notification.accountName,
        platform: notification.platform,
        status: notification.status,
        errorCode:
          notification.errorCode || this._getErrorCode(notification.platform, notification.status),
        reason: notification.reason,
        timestamp: notification.timestamp || getISOStringWithTimezone(new Date())
      })
    } catch (error) {
      logger.error('Failed to send account anomaly notification:', error)
    }
  }

  /**
   * 测试Webhook连通性(兼容旧接口)
   * @param {string} url - Webhook URL
   * @param {string} type - 平台类型(可选)
   */
  async testWebhook(url, type = 'custom') {
    try {
      // 创建临时平台配置
      const platform = {
        type,
        url,
        enabled: true,
        timeout: 10000
      }

      const result = await webhookService.testWebhook(platform)
      return result
    } catch (error) {
      return { success: false, error: error.message }
    }
  }

  /**
   * 发送账号事件通知
   * @param {string} eventType - 事件类型 (account.created, account.updated, account.deleted, account.status_changed)
   * @param {Object} data - 事件数据
   */
  async sendAccountEvent(eventType, data) {
    try {
      // 使用webhookService发送通知
      await webhookService.sendNotification('accountEvent', {
        eventType,
        ...data,
        timestamp: data.timestamp || getISOStringWithTimezone(new Date())
      })
    } catch (error) {
      logger.error(`Failed to send account event (${eventType}):`, error)
    }
  }

  /**
   * 获取错误代码映射
   * @param {string} platform - 平台类型
   * @param {string} status - 状态
   * @param {string} _reason - 原因 (未使用)
   */
  _getErrorCode(platform, status, _reason) {
    const errorCodes = {
      'claude-oauth': {
        unauthorized: 'CLAUDE_OAUTH_UNAUTHORIZED',
        blocked: 'CLAUDE_OAUTH_BLOCKED',
        error: 'CLAUDE_OAUTH_ERROR',
        disabled: 'CLAUDE_OAUTH_MANUALLY_DISABLED'
      },
      'claude-console': {
        blocked: 'CLAUDE_CONSOLE_BLOCKED',
        error: 'CLAUDE_CONSOLE_ERROR',
        disabled: 'CLAUDE_CONSOLE_MANUALLY_DISABLED'
      },
      gemini: {
        error: 'GEMINI_ERROR',
        unauthorized: 'GEMINI_UNAUTHORIZED',
        disabled: 'GEMINI_MANUALLY_DISABLED'
      },
      openai: {
        error: 'OPENAI_ERROR',
        unauthorized: 'OPENAI_UNAUTHORIZED',
        blocked: 'OPENAI_RATE_LIMITED',
        disabled: 'OPENAI_MANUALLY_DISABLED'
      }
    }

    return errorCodes[platform]?.[status] || 'UNKNOWN_ERROR'
  }
}

module.exports = new WebhookNotifier()