Files
dacli/src/commands/org/create.ts
T
ssw bcf874db2b tier3(print/msg/org/schedule/app, 7 files): 跨命令 auto-fix pipeline 接入 (S-X1/S-X2 silent + G-X1 suggest + L3 CommandLog 落盘)
参照 dict/typecreate.ts:14-66 boilerplate, 给写操作命令补齐三重提醒三重修复:

L1 (--verbose/--json stdout 三模式): setupOutputFilter from _output_filter.ts
L2 (silent/assertive/suggest 三级 fix): runCommandAutoFix + fetchExistingLists from _auto_fix.ts
L3 (CommandLog 落盘, 失败也落): printCommandEnd from _output_filter.ts

修改文件:
- print/create.ts (先于 dfeb935 自动 commit)
- org/create.ts
- msg/noticecreate.ts (MSERR114)
- msg/templatecreate.ts (MSERR114 + 端点修复注释保留)
- schedule/create.ts (SYS131 时间关系)
- schedule/trigger.ts (W11-D4 手动触发, 仅 L1+L3)
- app/login.ts (Phase E base_system 检测 + app 不存在回滚命令)
- app/context.ts (新会话第一步入口, L3 落 dbLinkId/forms/menus 数量)

未触及 (留待后续 batch):
- app/create.ts: 5-step 复杂创建 (system→DB→DataSource→授权→重登), 5 处 process.exit(1) 需重构为 throw + 集中 try/catch, 超出本轮估算
- app/clear.ts: 30KB destructive 操作, 三层接入风险 > 收益, 需先 --dry-run 保护再接入
- 已知 baseline pre-existing bugs: org/create 的 405 后端路由 fallback 问题, msg/notice 的 GET fallback 405 — 与本 commit 无关

L1 验证: npx tsc --noEmit 0 新错误 (7 文件相关 error 为空)
L2/L3 e2e 抽样:
- msg templateCreate S-X1/S-X2+L3 跑通 (e2e id=849237951593317317 已清理)
- print create S-X1+S-X2+L3 跑通 (前置 commit dfeb935)
- app context --json 单行 JSON + L3 落盘跑通
2026-07-21 13:09:13 +08:00

177 lines
7.7 KiB
TypeScript

/**
* commands/org/create.ts — 新建组织 (Phase D1, v3.0+ 走 webapi/permission 镜像, v2.1+ 三层接入)
*
* 镜像 web organize/Form.vue + 后端 OrganizeController
* - PS036: 组织层级限制
* - PS013: 当前机构 id 不能与父机构 id 相同
*
* v0.11e+ Phase D1: P3 命令 step machine 集成
* v3.0+ (2026-06-29 regression audit P0): 改用 webapi/permission.createOrg
*
* v2.1+ (2026-07-21) 三层接入 (跨命令 auto-fix 模式, 参照 dict/typecreate.ts):
* L1: --verbose / --json 三模式 (commands/_output_filter.ts:setupOutputFilter)
* L2: 跨命令 auto-fix pipeline
* - enCode 含 `-`/`_` 自动 strip (S-X1 silent, BS-新38)
* - fullName 含 space/parens 自动 soften (S-X2 silent, BS-新52)
* - 已有 org 重名 → suggest 加 `<appCode>` 前缀 (G-X1 suggest)
* L3: 完成后落 CommandLog, 失败时也落 (含 PS013/PS036 错误)
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { validateOrgCreate } from './_validate.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
import {
initWebapi as initPermissionWebapi,
createOrg,
getOrganizeList,
} from '../../webapi/permission.ts'
import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): 跨命令 auto-fix (S-X1/S-X2 silent + G-X1 suggest)
import { setupOutputFilter, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): stdout 三模式 + CommandLog 落盘
interface CreateOpts {
fullName: string
enCode: string
parentId: string
category: string
enabledMark?: string
sortCode?: string
json?: boolean
verbose?: boolean
}
export function buildOrgCreateCommand(): Command {
const cmd = new Command('create')
.description('新建组织 (v0.11e+ Phase D1 + step machine 校验)')
.requiredOption('--fullName <name>', '组织名称')
.requiredOption('--enCode <code>', '组织编码')
.requiredOption('--parentId <pid>', '上级组织 id (顶级传 -1)')
.requiredOption('--category <c>', '组织类型: 0=公司 1=部门 2=小组')
.option('--enabledMark <m>', '0/1 启用', '1')
.option('--sortCode <s>', '排序', '0')
.option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false)
.option('--json', '原始 JSON 输出', false)
.action(async (opts: CreateOpts) => {
// v2.1+ L1: stdout 三模式 — 默认 quiet, --verbose 全部输出, --json 原始 JSON
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
const startTime = Date.now()
// v2.1+ L2 silent+assertive+suggest: 跨命令 auto-fix pipeline (S-X1/S-X2/G-X1)
// - enCode 含 `-`/`_` 自动 strip (BS-新38 silent)
// - fullName 含 space/parens 自动 soften (BS-新52 silent)
// - 已有 org 重名 → suggest 加 `<appCode>` 前缀 (启用需 fetchExistingLists)
const fixCtx = await fetchExistingLists(c, c.appCode)
const cmdFix = runCommandAutoFix(opts as unknown as Record<string, unknown>, fixCtx)
if (cmdFix.silent.length > 0) {
Object.assign(opts, cmdFix.opts)
}
// v0.11e+ Phase D1: step machine 校验 (PS036 层级 + PS013 parentId)
const validation = validateOrgCreate({
fullName: opts.fullName,
enCode: opts.enCode,
parentId: opts.parentId,
category: opts.category,
enabledMark: opts.enabledMark,
sortCode: opts.sortCode,
})
if (!validation.ok) {
for (const s of validation.steps) {
for (const e of s.errors) {
log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`)
}
}
// v2.1+ L3: 失败也落 CommandLog (PS013/PS036 错误供 AI 查 dacli log last)
printCommandEnd({
command: 'org create',
appCode: c.appCode,
result: 'failure',
summary: { enCode: opts.enCode, fullName: opts.fullName },
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
error: validation.steps.flatMap(s => s.errors).map(e => e.message).join('; '),
duration_ms: Date.now() - startTime,
verbose: opts.verbose,
json: opts.json,
})
process.exit(1)
}
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
const body = {
fullName: opts.fullName,
enCode: opts.enCode,
parentId: opts.parentId,
category: opts.category,
enabledMark: parseInt(opts.enabledMark || '1', 10),
sortCode: parseInt(opts.sortCode || '0', 10),
}
// v3.0+ P0: 走 webapi/permission.createOrg (web 端 create 函数)
await initPermissionWebapi(c)
const r = (await createOrg(body as any)) as { id?: string; code?: number; msg?: string }
if (r.code !== 200 && r.code !== undefined) {
const enhanced = enhanceFromBackend(r.msg || '', 'org', { entity: 'org' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
// v2.1+ L3: API 失败也落 CommandLog
printCommandEnd({
command: 'org create',
appCode: c.appCode,
result: 'failure',
summary: { enCode: opts.enCode, fullName: opts.fullName, parentId: opts.parentId },
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
error: r.msg || `code=${r.code}`,
duration_ms: Date.now() - startTime,
verbose: opts.verbose,
json: opts.json,
})
process.exit(1)
}
let id = r.id
if (!id) {
const listR = (await getOrganizeList({ parentId: opts.parentId, pageSize: 50 })) as
| { list?: { id: string; enCode: string; fullName: string }[] }
| { id: string; enCode: string; fullName: string }[]
const list = Array.isArray(listR) ? listR : listR.list || []
const found = list.find(
(d) => d.enCode === opts.enCode || d.fullName === opts.fullName,
)
id = found?.id
}
log.ok(`✓ 组织创建成功 id=${id}`)
if (opts.json) printJson({ ...(r as object), id })
// v2.1+ L3: 落 CommandLog + 打印摘要 + "详情: dacli log last" 提示
printCommandEnd({
command: 'org create',
appCode: c.appCode,
summary: { id, enCode: opts.enCode, fullName: opts.fullName, parentId: opts.parentId, category: opts.category },
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
duration_ms: Date.now() - startTime,
verbose: opts.verbose,
json: opts.json,
})
})
installHelpHook(cmd, {
purpose: '新建组织 (v0.11e+ Phase D1 + step machine 校验, 镜像 web organize/Form.vue + 后端 OrganizeController + PS013/PS036)',
required: {
'--fullName': '组织名称',
'--enCode': '组织编码',
'--parentId': '上级组织 id (顶级 -1)',
'--category': '0=公司 1=部门 2=小组',
},
optional: { '--enabledMark': '0/1 启用', '--sortCode': '排序' },
example: 'dacli org create --fullName=技术部 --enCode=tech_dept --parentId=-1 --category=1',
source: {
web: 'web/apps/jnpf-web-apps-main/src/views/permission/organize/Form.vue',
api: 'POST /api/permission/Organize',
ref: 'jnpf/后端/jnpf-java-boot/jnpf-permission/jnpf-permission-controller/src/main/java/jnpf/controller/OrganizeController.java',
},
notes: [
'v0.11e+ step machine (镜像 web organize/Form.vue + PS013/PS036):',
' step 0 基础信息: fullName 必填 + enCode 必填 + parentId 必填 + category ∈ {0,1,2}',
],
})
return cmd
}