Files
dacli/src/commands/msg/noticeupdate.ts
T

134 lines
5.7 KiB
TypeScript

/**
* commands/msg/noticeupdate.ts — 通知公告修改 (v0.11e+ Phase A2+A1 wire up)
*
* v0.11+ Phase 2: webapi 化 (loadNotice.getInfo + update)
* 替代 v0.10 直接 c.get/c.putJson
*
* v0.11e+ Phase A2: dirty check + Phase A1: enhanceFromBackend
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadNotice } from '../../webapi/message.ts'
import { dirtyCheck, dirtyCheckFailAndExit, DirtyCheckError, logDirtyCheckSkip, buildDirtyCheckSkipFix } from '../_dirty_check.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
interface Opts {
id: string
fullName?: string
enCode?: string
body?: string
category?: string
expectedEnCode?: string
expectedState?: string
force?: boolean
json?: boolean
/** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */
verbose?: boolean
}
export function buildMsgNoticeUpdateCommand(): Command {
const cmd = new Command('noticeUpdate')
.alias('notice-update')
.description('通知公告修改 (PUT 语义, v0.11+ webapi + v0.11e+ dirty check)')
.requiredOption('--id <id>', '公告 id')
.option('--fullName <n>', '公告名')
.option('--enCode <e>', '英文编码')
.option('--body <text>', '公告内容')
.option('--category <c>', '分类 (1=系统 2=业务)')
.option('--expected-enCode <e>', 'v0.11e+ Phase A2: 校验 enCode 未变')
.option('--force', 'v0.11e+ Phase A2: 跳过 dirty check', false)
addOutputOptions(cmd)
.action(async (opts: Opts) => {
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
await wrapCommand({
command: 'msg noticeUpdate',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
await initWebapi(c)
const orig = (await loadNotice.getInfo(opts.id)) as Record<string, unknown>
if (!orig) {
const enhanced = enhanceFromBackend('拉原公告失败, id 不存在', 'id', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })}`)
return ctx.fail(`msg noticeUpdate 失败: 拉原公告 id=${opts.id} 失败`, { id: opts.id })
}
// v0.11e+ Phase A2: dirty check
const dirtyResult = dirtyCheck({
expectedEnCode: opts.expectedEnCode,
force: opts.force,
current: {
enCode: orig.enCode as string | undefined,
state: orig.enabledMark as string | number | undefined,
fullName: orig.fullName as string | undefined,
type: 'di',
},
id: opts.id,
})
if (!dirtyResult.ok) {
// v2.2+ (2026-07-21): dirtyCheckFailAndExit 改 throw (DirtyCheckError), 下方 catch → ctx.fail
try {
dirtyCheckFailAndExit(dirtyResult, { type: 'di', id: opts.id, current: { enCode: orig.enCode as string | undefined, fullName: orig.fullName as string | undefined } }, log.error)
} catch (e) {
if (e instanceof DirtyCheckError) {
return ctx.fail(e.message, { id: opts.id, type: 'msg', errorCount: e.errorCount })
}
throw e
}
} else if (dirtyResult.skipped) {
logDirtyCheckSkip('msg', opts.id, opts.expectedEnCode, opts.expectedState)
ctx.addFix(buildDirtyCheckSkipFix('msg', opts.id))
}
const body: Record<string, unknown> = { ...orig, id: opts.id }
if (opts.fullName) body.fullName = opts.fullName
if (opts.enCode) body.enCode = opts.enCode
if (opts.body) body.body = opts.body
if (opts.category) body.category = opts.category
log.info(`loadNotice.update (id=${opts.id})`)
const r = (await loadNotice.update(body)) as { code?: number; msg?: string; data?: unknown }
if (r.code !== 200 && r.code !== undefined) {
const enhanced = enhanceFromBackend(r.msg || '', 'msg', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
if (opts.json) printJson(r)
return ctx.fail(`msg noticeUpdate 失败 (code=${r.code}): ${r.msg}`, { id: opts.id, code: r.code })
}
log.ok(`✓ 公告更新成功 (id=${opts.id})`)
if (opts.json) printJson(r.data)
ctx.success({ id: opts.id })
} catch (e) {
ctx.fail(`msg noticeUpdate 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
})
installHelpHook(cmd, {
purpose: '通知公告修改. 改 fullName/enCode/body/category, PUT 语义 + v0.11e+ dirty check.',
required: { '--id': '公告 id' },
optional: {
'--fullName': '公告名',
'--enCode': '英文编码',
'--body': '公告内容',
'--category': '分类',
'--expected-enCode': '防 AI 误覆盖 (A2)',
'--force': '跳过 dirty check (A2)',
},
example: 'dacli msg noticeUpdate --id=123 --body="新内容"',
source: { api: 'PUT /api/message/Notice/{id}', ref: 'web/.../api/msgCenter/notice.ts update()' },
notes: [
'v0.11+ 改用 webapi 镜像 (loadNotice.update)',
'v0.11e+ Phase A2: dirty check --expected-enCode (防 AI session 误覆盖)',
'v0.11e+ Phase A1: catch enhanceFromBackend (错误码自动分类)',
],
})
return cmd
}