feat(msg-noticeupdate): v0.11e+ dirty check + enhanceFromBackend (Phase A2+A1)

msg/noticeupdate.ts:
- import dirtyCheck + dirtyCheckFailAndExit + enhanceFromBackend/formatErrorLine
- 加 --expected-enCode/--force 选项 (A2 dirty check)
- 配置拉取失败 catch + 后端更新失败 catch 都用 enhanceFromBackend (A1)
This commit is contained in:
ssw committed 2026-06-27 03:49:53 +08:00
1 parent 3c2b5bf3d4
commit df29f18829
1 file changed
+47 -7
+47 -7
View File
@@ -1,14 +1,18 @@
/**
* commands/msg/noticeupdate.ts — 通知公告修改
* 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 { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadNotice } from '../../webapi/message.ts'
import { dirtyCheck, dirtyCheckFailAndExit } from '../_dirty_check.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
interface Opts {
id: string
@@ -16,27 +20,51 @@ interface Opts {
enCode?: string
body?: string
category?: string
expectedEnCode?: string
force?: boolean
json?: boolean
}
export function buildMsgNoticeUpdateCommand(): Command {
const cmd = new Command('noticeUpdate')
.alias('notice-update')
.description('通知公告修改 (PUT 语义, v0.11+ webapi)')
.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)
.option('--json', '原始 JSON 输出', false)
.action(async (opts: Opts) => {
const c = await JnpfClient.fromToken()
await initWebapi(c)
const orig = (await loadNotice.getInfo(opts.id)) as Record<string, unknown>
if (!orig) {
log.error(`msg noticeUpdate 失败: 拉原公告 id=${opts.id} 失败`)
const enhanced = enhanceFromBackend('拉原公告失败, id 不存在', 'id', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })}`)
process.exit(1)
}
// 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) {
dirtyCheckFailAndExit(dirtyResult, { type: 'di', id: opts.id, current: { enCode: orig.enCode as string | undefined, fullName: orig.fullName as string | undefined } }, log.error)
} else if (dirtyResult.skipped) {
log.info(`⚠ dirty check 已 --force 跳过`)
}
const body: Record<string, unknown> = { ...orig, id: opts.id }
if (opts.fullName) body.fullName = opts.fullName
if (opts.enCode) body.enCode = opts.enCode
@@ -45,7 +73,8 @@ export function buildMsgNoticeUpdateCommand(): Command {
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) {
log.error(`msg noticeUpdate 失败 (code=${r.code}): ${r.msg}`)
const enhanced = enhanceFromBackend(r.msg || '', 'msg', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
if (opts.json) printJson(r)
process.exit(1)
}
@@ -53,12 +82,23 @@ export function buildMsgNoticeUpdateCommand(): Command {
if (opts.json) printJson(r.data)
})
installAiHelpHook(cmd, {
purpose: '通知公告修改. 改 fullName/enCode/body/category, PUT 语义.',
purpose: '通知公告修改. 改 fullName/enCode/body/category, PUT 语义 + v0.11e+ dirty check.',
required: { '--id': '公告 id' },
optional: { '--fullName': '公告名', '--enCode': '英文编码', '--body': '公告内容', '--category': '分类' },
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)'],
notes: [
'v0.11+ 改用 webapi 镜像 (loadNotice.update)',
'v0.11e+ Phase A2: dirty check --expected-enCode (防 AI session 误覆盖)',
'v0.11e+ Phase A1: catch enhanceFromBackend (错误码自动分类)',
],
})
return cmd
}