msg/noticecreate.ts (Phase D1 集成): - import validateMsgCreate + enhanceFromBackend/formatErrorLine - 跑 step machine (fullName/enCode 必填 + MSERR114 系统编码规则禁止 sys_/system_ 开头) - catch 后端错时 enhanceFromBackend
88 lines
3.1 KiB
TypeScript
88 lines
3.1 KiB
TypeScript
/**
|
|
* commands/msg/noticecreate.ts — 新建通知公告
|
|
*
|
|
* API: POST /api/message/Notice
|
|
*/
|
|
import { Command } from 'commander'
|
|
import { JnpfClient } from '../../core/client.ts'
|
|
import { installAiHelpHook } from '../../core/help.ts'
|
|
import { log, printJson } from '../../core/log.ts'
|
|
import { validateMsgCreate } from './_validate.ts'
|
|
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
|
|
|
|
interface CreateOpts {
|
|
fullName: string
|
|
enCode: string
|
|
body?: string
|
|
json?: boolean
|
|
}
|
|
|
|
export function buildMsgNoticeCreateCommand(): Command {
|
|
const cmd = new Command('noticeCreate')
|
|
.alias('notice-create')
|
|
.description('新建通知公告 (草稿, 未发布)')
|
|
.requiredOption('--fullName <name>', '通知标题')
|
|
.requiredOption('--enCode <code>', '通知编码')
|
|
.option('--body <text>', '通知正文 (可选)')
|
|
.option('--json', '原始 JSON 输出', false)
|
|
.action(async (opts: CreateOpts) => {
|
|
const c = await JnpfClient.fromToken()
|
|
|
|
// v0.11e+ Phase D1: step machine 校验 (MSERR114 系统编码规则)
|
|
const validation = validateMsgCreate({
|
|
fullName: opts.fullName,
|
|
enCode: opts.enCode,
|
|
})
|
|
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,
|
|
title: opts.fullName,
|
|
body: opts.body || '通知内容',
|
|
type: 1, // 1=系统公告 2=待办
|
|
enabledMark: 1,
|
|
sortCode: 0,
|
|
}
|
|
const r = await c.postJson<{ id?: string }>('/message/Notice', body)
|
|
if (r.code !== 200) {
|
|
const enhanced = enhanceFromBackend(r.msg || '', 'msg', { entity: 'msg' })
|
|
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
|
|
process.exit(1)
|
|
}
|
|
let id = r.data?.id
|
|
if (!id) {
|
|
const listR = await c.get<{ list: { id: string; enCode: string; fullName: string }[] }>(
|
|
'/message/Notice',
|
|
{ keyword: opts.enCode, pageSize: '50' },
|
|
)
|
|
const found = (listR.data?.list || []).find(
|
|
(d) => d.enCode === opts.enCode || d.fullName === opts.fullName,
|
|
)
|
|
id = found?.id
|
|
}
|
|
log.info(`✓ 通知公告创建成功 id=${id}`)
|
|
if (opts.json) printJson({ ...(r.data as object), id })
|
|
})
|
|
installAiHelpHook(cmd, {
|
|
purpose: '新建通知公告 (草稿, 用 notice-release 发布)',
|
|
required: { '--fullName': '通知标题', '--enCode': '通知编码' },
|
|
optional: { '--body': '通知正文' },
|
|
example: 'dacli msg noticecreate --fullName=系统升级 --enCode=sys_upgrade_2026 --body=今晚 10 点维护',
|
|
source: {
|
|
web: 'web/apps/jnpf-web-apps-main/src/views/msgCenter/notice/Form.vue',
|
|
api: 'POST /api/message/Notice',
|
|
ref: 'doc/v0.8-endpoints.md §2.4',
|
|
},
|
|
})
|
|
return cmd
|
|
}
|