feat(org): v0.11e+ wire up _validate.ts + step machine
org/create.ts (Phase D1 集成): - 完整重写: 加 step machine 校验 (PS036 层级 + PS013 parentId) - 加 --parentId/--category 必填校验 - 加 catch enhanceFromBackend - installAiHelpHook 加 source + 镜像描述 镜像 web organize/Form.vue + 后端 OrganizeController.java
This commit is contained in:
1 parent
0c29f51c59
commit
e8f99cffc1
1 file changed
+60
-22
+60
-22
@@ -1,47 +1,77 @@
|
||||
/**
|
||||
* commands/org/create.ts — 新建组织
|
||||
* commands/org/create.ts — 新建组织 (Phase D1 集成 v0.11e+)
|
||||
*
|
||||
* API: POST /api/permission/Organize
|
||||
* 镜像 web organize/Form.vue + 后端 OrganizeController
|
||||
* - PS036: 组织层级限制
|
||||
* - PS013: 当前机构 id 不能与父机构 id 相同
|
||||
*
|
||||
* v0.11e+ Phase D1: P3 命令 step machine 集成
|
||||
*/
|
||||
import { Command } from 'commander'
|
||||
import { JnpfClient } from '../../core/client.ts'
|
||||
import { installAiHelpHook } from '../../core/help.ts'
|
||||
import { log, printJson } from '../../core/log.ts'
|
||||
import { validateOrgCreate } from './_validate.ts'
|
||||
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
|
||||
|
||||
interface CreateOpts {
|
||||
fullName: string
|
||||
enCode: string
|
||||
parentId?: string
|
||||
category?: string
|
||||
parentId: string
|
||||
category: string
|
||||
enabledMark?: string
|
||||
sortCode?: string
|
||||
json?: boolean
|
||||
}
|
||||
|
||||
export function buildOrgCreateCommand(): Command {
|
||||
const cmd = new Command('create')
|
||||
.description('新建组织/部门 (P1 权限三角 3 号, 先建组织再建用户)')
|
||||
.requiredOption('--fullName <name>', '组织名称 (如: 测试部门)')
|
||||
.requiredOption('--enCode <code>', '组织编码 (英文)')
|
||||
.option('--parentId <id>', '父组织 id (默认 "0" 顶级)', '0')
|
||||
.option('--category <c>', '分类: company/department/position', 'department')
|
||||
.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('--json', '原始 JSON 输出', false)
|
||||
.action(async (opts: CreateOpts) => {
|
||||
const c = await JnpfClient.fromToken()
|
||||
|
||||
// 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 })}`)
|
||||
}
|
||||
}
|
||||
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: 1,
|
||||
sortCode: 0,
|
||||
enabledMark: parseInt(opts.enabledMark || '1', 10),
|
||||
sortCode: parseInt(opts.sortCode || '0', 10),
|
||||
}
|
||||
const r = await c.postJson<{ id?: string }>('/permission/Organize', body)
|
||||
if (r.code !== 200) {
|
||||
log.error(`org create 失败 (code=${r.code}): ${r.msg}`)
|
||||
const r = await c.postJson<{ id?: string; code?: number; msg?: string }>('/permission/Organize', body)
|
||||
if (r.code !== 200 && r.code !== undefined) {
|
||||
const enhanced = enhanceFromBackend(r.msg || '', 'org', { entity: 'org' })
|
||||
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
|
||||
process.exit(1)
|
||||
}
|
||||
let id = r.data?.id
|
||||
if (!id) {
|
||||
// 用 Selector 反查
|
||||
const listR = await c.get<{ list: { id: string; enCode: string; fullName: string }[] }>(
|
||||
'/permission/Organize/Selector',
|
||||
{ parentId: opts.parentId, pageSize: '50' },
|
||||
@@ -52,19 +82,27 @@ export function buildOrgCreateCommand(): Command {
|
||||
id = found?.id
|
||||
}
|
||||
log.info(`✓ 组织创建成功 id=${id}`)
|
||||
if (opts.json) printJson({ ...(r.data as object), id })
|
||||
if (opts.json) printJson({ ...(r as object), id })
|
||||
})
|
||||
installAiHelpHook(cmd, {
|
||||
purpose: '新建组织/部门 (P1 权限三角 3 号)',
|
||||
required: { '--fullName': '组织名称', '--enCode': '组织编码' },
|
||||
optional: { '--parentId': '父组织 id (默认 "0")', '--category': 'company/department/position' },
|
||||
example: 'dacli org create --fullName=测试部门 --enCode=dacli_dept --parentId=0',
|
||||
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: 'doc/v0.8-endpoints.md §2.3',
|
||||
ref: 'jnpf/后端/jnpf-java-boot/jnpf-permission/jnpf-permission-controller/src/main/java/jnpf/controller/OrganizeController.java',
|
||||
},
|
||||
notes: ['**决策 7**: 必须先建组织再建用户, 否则 user organizeId=null 报错'],
|
||||
notes: [
|
||||
'v0.11e+ step machine (镜像 web organize/Form.vue + PS013/PS036):',
|
||||
' step 0 基础信息: fullName 必填 + enCode 必填 + parentId 必填 + category ∈ {0,1,2}',
|
||||
],
|
||||
})
|
||||
return cmd
|
||||
}
|
||||
Reference in new issue
Block a user