diff --git a/src/commands/msg/noticeupdate.ts b/src/commands/msg/noticeupdate.ts index 26e4a91..bf48efa 100644 --- a/src/commands/msg/noticeupdate.ts +++ b/src/commands/msg/noticeupdate.ts @@ -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') .option('--fullName ', '公告名') .option('--enCode ', '英文编码') .option('--body ', '公告内容') .option('--category ', '分类 (1=系统 2=业务)') + .option('--expected-enCode ', '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 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 = { ...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 }