105 lines
4.3 KiB
TypeScript
105 lines
4.3 KiB
TypeScript
/**
|
|
* commands/integration/emailtest.ts — 邮件测试发送
|
|
*
|
|
* 走 JNPF AccountConfig 端点 (web 真实端点, v0.11+ P0 修复)
|
|
*
|
|
* 修复历史:
|
|
* - v0.10 错误端点: /message/SendMessageConfig/testSendConfig (假 PASS)
|
|
* - v0.11+ 正确端点: /message/AccountConfig/testSendMail (web Test.vue 验证)
|
|
*/
|
|
import { Command } from 'commander'
|
|
import { JnpfClient } from '../../core/client.ts'
|
|
import { installHelpHook } from '../../core/help.ts'
|
|
import { log } from '../../core/log.ts'
|
|
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
|
|
import { setupOutputFilter, addOutputOptions, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘
|
|
|
|
interface EmailTestOpts {
|
|
configId: string
|
|
email?: string
|
|
title?: string
|
|
content?: string
|
|
/** v2.1+ (2026-07-21): --verbose / --json (CommandLog 落盘对应字段) */
|
|
verbose?: boolean
|
|
json?: boolean
|
|
}
|
|
|
|
export function buildIntegrationEmailTestCommand(): Command {
|
|
const cmd = new Command('emailTest')
|
|
.alias('email-test')
|
|
.description('邮件测试发送 (走 AccountConfig/testSendMail)')
|
|
.requiredOption('--configId <id>', 'AccountConfig id (从 integration configList 获取)')
|
|
.option('--email <e>', '目标邮箱 (必填, 必填收件人)', '[email protected]')
|
|
.option('--title <t>', '测试邮件标题', 'Test from dacli')
|
|
.option('--content <c>', '测试邮件内容', 'This is a test email sent by dacli.')
|
|
addOutputOptions(cmd)
|
|
.action(async (opts: EmailTestOpts) => {
|
|
// 输出过滤: 默认静默
|
|
setupOutputFilter(opts)
|
|
const c = await JnpfClient.fromToken()
|
|
const startTime = Date.now()
|
|
let cmdError: string | undefined
|
|
let cmdSummary: Record<string, unknown> = {}
|
|
|
|
try {
|
|
// 1. 拉取 config 详情 (AccountConfig/testSendMail 需要 config dataForm)
|
|
const configR = await c.get<Record<string, unknown>>(`/message/AccountConfig/${opts.configId}`)
|
|
if (configR.code !== 200 || !configR.data) {
|
|
const enhanced = enhanceFromBackend(configR.msg || 'id 不存在', 'configId', { entity: 'integration' })
|
|
throw new Error(formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' }))
|
|
}
|
|
const config = configR.data
|
|
|
|
// 2. 拼装 test 请求 (web Test.vue 模式: {...config, testSendEmail, testEmailTitle, testEmailContent, testType})
|
|
const body = {
|
|
...config,
|
|
testSendEmail: [opts.email],
|
|
testEmailTitle: opts.title,
|
|
testEmailContent: opts.content,
|
|
testType: 'testSendMail',
|
|
}
|
|
|
|
// 3. 走 AccountConfig/testSendMail (web 端真实端点)
|
|
const r = await c.postJson('/message/AccountConfig/testSendMail', body)
|
|
if (r.code !== 200) {
|
|
const enhanced = enhanceFromBackend(r.msg || '', 'email', { entity: 'integration' })
|
|
throw new Error(formatErrorLine(enhanced, { step: 1, stepName: '后端API' }))
|
|
}
|
|
log.info(`✓ 邮件测试发送 OK (configId=${opts.configId}, to=${opts.email})`)
|
|
cmdSummary = { configId: opts.configId, email: opts.email, ok: true }
|
|
} catch (e) {
|
|
cmdError = (e as Error).message?.slice(0, 200)
|
|
log.error(`email test 失败: ${cmdError}`)
|
|
} finally {
|
|
// v2.1+ (2026-07-21): 落 CommandLog (L3)
|
|
printCommandEnd({
|
|
command: 'integration email-test',
|
|
appCode: c.appCode,
|
|
result: cmdError ? 'failure' : 'success',
|
|
error: cmdError,
|
|
summary: cmdSummary,
|
|
duration_ms: Date.now() - startTime,
|
|
verbose: opts.verbose,
|
|
json: opts.json,
|
|
})
|
|
if (cmdError) process.exit(1)
|
|
}
|
|
})
|
|
installHelpHook(cmd, {
|
|
purpose: '邮件测试发送 (修复 v0.10 假 PASS 端点)',
|
|
required: { '--configId': 'AccountConfig id' },
|
|
optional: {
|
|
'--email': '目标邮箱',
|
|
'--title': '测试邮件标题',
|
|
'--content': '测试邮件内容',
|
|
},
|
|
example: 'dacli integration emailtest --configId=123 [email protected]',
|
|
source: {
|
|
web: 'web/apps/jnpf-web-apps-main/src/views/integrationCenter/email/Test.vue',
|
|
api: 'POST /api/message/AccountConfig/testSendMail',
|
|
ref: 'web/.../api/msgCenter/accountConfig.ts:29 testConfig()',
|
|
},
|
|
})
|
|
return cmd
|
|
}
|