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

user/update.ts:
- import dirtyCheck + dirtyCheckFailAndExit + enhanceFromBackend/formatErrorLine
- 加 --expected-account/--force 选项 (A2 dirty check)
- catch 后端错时 enhanceFromBackend (A1 错误码分类)
This commit is contained in:
ssw committed 2026-06-27 03:48:30 +08:00
1 parent 249ef1b08c
commit 7632aa50fa
1 file changed
+33 -4
+33 -4
View File
@@ -1,23 +1,31 @@
/**
* commands/user/update.ts — admin 编辑用户
* commands/user/update.ts — admin 编辑用户 (v0.11e+ Phase A2 wire up)
*
* API: PUT /api/permission/Users/{id}
*
* v0.11e+ Phase A2: dirty check (--expected-account) + enhanceFromBackend
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log } from '../../core/log.ts'
import { dirtyCheck, dirtyCheckFailAndExit } from '../_dirty_check.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
interface UpdateOpts {
id: string
fields: string
expectedAccount?: string
force?: boolean
}
export function buildUserUpdateCommand(): Command {
const cmd = new Command('update')
.description('编辑用户 (admin 操作, 部分字段更新)')
.description('编辑用户 (admin 操作, 部分字段更新 + v0.11e+ dirty check)')
.requiredOption('--id <uid>', '用户 id')
.requiredOption('--fields <json>', '要更新的字段 JSON, 如 {"realName":"新名","email":"x@y"}')
.option('--expected-account <a>', 'v0.11e+ Phase A2: 校验 account 未变 (防 AI 误覆盖)')
.option('--force', 'v0.11e+ Phase A2: 跳过 dirty check', false)
.action(async (opts: UpdateOpts) => {
let fields: Record<string, unknown>
try {
@@ -26,17 +34,36 @@ export function buildUserUpdateCommand(): Command {
log.error(`JSON 解析失败: ${(e as Error).message}`)
process.exit(1)
}
// v0.11e+ Phase A2: dirty check
const dirtyResult = dirtyCheck({
expectedEnCode: opts.expectedAccount,
force: opts.force,
current: {
enCode: fields.account as string | undefined,
type: 'role', // 借用 type union (user 不在原 3 类型中, role 接近)
},
id: opts.id,
})
if (!dirtyResult.ok) {
dirtyCheckFailAndExit(dirtyResult, { type: 'role', id: opts.id, current: { enCode: fields.account as string | undefined } }, log.error)
} else if (dirtyResult.skipped) {
log.info(`⚠ dirty check 已 --force 跳过`)
}
const c = await JnpfClient.fromToken()
const r = await c.putJson(`/permission/Users/${opts.id}`, fields)
if (r.code !== 200) {
log.error(`user update 失败 (code=${r.code}): ${r.msg}`)
const enhanced = enhanceFromBackend(r.msg || '', 'user', { entity: 'user' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
process.exit(1)
}
log.info(`✓ 用户 id=${opts.id} 已更新: ${Object.keys(fields).join(', ')}`)
})
installAiHelpHook(cmd, {
purpose: '编辑用户 (admin 操作, PUT /Users/{id})',
purpose: '编辑用户 (admin 操作, PUT /Users/{id} + v0.11e+ dirty check)',
required: { '--id': '用户 id', '--fields': '字段 JSON (realName/email/mobilePhone/enabledMark/sortCode/...)' },
optional: { '--expected-account': '防 AI 误覆盖 (A2)', '--force': '跳过 dirty check (A2)' },
example: 'dacli user update --id=1234567890 --fields=\'{"realName":"新名","email":"[email protected]"}\'',
source: {
web: 'web/apps/jnpf-web-apps-main/src/views/permission/user/Form.vue',
@@ -45,6 +72,8 @@ export function buildUserUpdateCommand(): Command {
notes: [
'**决策 7**: organizeId 可通过 --fields 改 (用户改所属组织)',
'**JSON**: --fields 必须合法 JSON, 失败会 exit(1)',
'v0.11e+ Phase A2: dirty check --expected-account (防 AI session 误覆盖)',
'v0.11e+ Phase A1: catch enhanceFromBackend (错误码自动分类)',
],
})
return cmd