|
|
const logger = require('./logger') |
|
|
const webhookService = require('../services/webhookService') |
|
|
const { getISOStringWithTimezone } = require('./dateHelper') |
|
|
|
|
|
class WebhookNotifier { |
|
|
constructor() { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async sendAccountAnomalyNotification(notification) { |
|
|
try { |
|
|
|
|
|
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) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async sendAccountEvent(eventType, data) { |
|
|
try { |
|
|
|
|
|
await webhookService.sendNotification('accountEvent', { |
|
|
eventType, |
|
|
...data, |
|
|
timestamp: data.timestamp || getISOStringWithTimezone(new Date()) |
|
|
}) |
|
|
} catch (error) { |
|
|
logger.error(`Failed to send account event (${eventType}):`, error) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_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() |
|
|
|