This repository has been archived on 2026-09-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
BPOS-TMP-REPO/apps/api/src/app/seed-mvp-flow.ts
T
ChaiHaoandClaude Opus 5 b2f5f69d6a 流程包去掉发布机制 —— 严格检查 + 保存即生效,生效由运行时决定
用户要的是运行时机制:新建 + 编辑,存下去就生效,旧任务走完旧版本,
新任务调用时才加载新编排。所以「草稿 → 发布」两态整套去掉。

## 闸从「发布」挪到「保存」

`savePackage` 现在先跑闭合校验,不闭合抛 `NotClosedError`(路由 412 带报告)——
**一个字节都不落库**。存进来的每一版都能起单,所以不需要「已发布」这个标记。
`publish` 用例、路由、`publishPackage` 存储方法一并删掉。

实测:把进 A6 的边映射清空后保存 → 412,逐条点名「边 e-g3-a6 未完成载荷映射;
节点「报价议价」的必填字段未映射:rows」。

## 保存恒新增版本号,服务端说了算

这不是可选项,是「旧任务走完旧版本」的前提:工作流按 (包id, 版本) 去读定义,
就地覆盖 v1 就等于改掉所有在途 v1 任务正在读的编排 —— 它们会中途换了规则却毫不知情。

版本号由用例层算(当前最大 + 1),**不采信入参**。实测:入参故意填 version: 999,
落的是 v2。store 层因此改成**只插不改**:撞键宁可让唯一约束报错,也不静默覆盖。

起单取版从「最新已发布」改为「最新一版」(retired 除外);触发器的 requiredInputs 同口径。
实测:改完描述存成 v2 后,新起的单用 v2,早先那单仍挂 v1。

## 迁移:旧状态值不归一就是一读就炸

状态枚举从 draft/published/retired 收成 active/retired。列上和 definition 这个 jsonb 里
各存着一份 state,两处都要改 —— 不改,`processPackageSchema.parse` 读一行就抛。
这不是开发期小麻烦,任何已部署实例都会中招。加了迁移 0012。

## 界面:恒为 0 的假指标撤掉,不换个说法留着

- 编排面板:「已发布/草稿」芯片 → 「生效中/历史版本」(真正的区别是哪一版会被新起单取到);
  发布按钮撤掉,保存按钮改成「保存并生效(存为 vN)」并在确认卡里说清影响面
- 历史版本只读,但就地指路:「去 v(N-1) 改(生效中)」,不让人自己找
- 蓝图面:「草稿 N · 未闭合 M」两个计数在新机制下**结构上恒为 0**(不闭合存不进来),
  换成「共 N 个版本,保存即生效」
- 驾驶舱:「未闭合草稿」卡整张撤掉 —— 同理恒为 0。天然的第三张是「死路」
  (route-dead-end 事件),但那需要一条按事件类型聚合的路由,没有之前不摆(同死信卡的规矩)

## 顺手修:种子的幂等判断本来是坏的

比对用 `JSON.stringify` 认键序,而 jsonb 从 PG 读回来键是重排过的 —— 永远判不等,
每播一次种就多存一版一模一样的定义(实测一口气堆到 v3)。改成递归按键名排序后比对。
连播三次,只第一次存。

## 契约变更(破坏性,一步到位)

`publish` 删除、`NotPublishableError` → `NotClosedError`、`publishable` → `closed`、
状态枚举收窄。按规矩破坏性变更该走三步弃用 —— 这里一步改完,因为:
① depcruise 反查下游全在仓内(374 模块无违规),同批改完;
② 要弃用的是一个**产品决定不再存在**的机制,留弃用窗口没有意义。
CONTRACT.md 已同步,快照单独说明。

typecheck ✓ lint ✓ depcruise 374 模块 ✓ 495 测试 ✓

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
2026-07-28 12:49:24 +08:00

503 lines
22 KiB
TypeScript

/**
* 「统一报价」流程包 —— 语义取自 `docs/MVP示例工作流.html` 的 FIG.1,几何**重排过**。
*
* 语义 15 节点:起点 · A1 · G1 · A2 · G2 · A3 · G2J · A4 · **A5 审批点** · G3 · A6 · G4 · A7 · 双终点。
*
* 与 FIG.1 的两处语义差异:
* · **多一颗 G2J**:G2 的两条互斥分支必须显式汇合 —— FIG.1 把它画成两条线各扎进 A4,
* 语义上那是隐式合并,闭合校验不收(见下 joinNode 注释)。
* · **A5 是内置审批点**(审批不是任务,零开发、不入活动库),而 FIG.1 给它配了作业指导书。
*
* G3「审批结果」曾被我做掉过,理由是「审批点把它吸收了:通过走出边、驳回走 reject 声明」。
* **那是错的** —— 审批点的两个去向本该由图决定(BPMN 的常规,也是用户要的)。
* 现已补回,并且引擎跟着改成**先看图**:`onApprovalVerdict` 把裁决放进载荷,
* 通过与驳回都先试出边,只有没画驳回边的包才退回 `reject` 声明。
* 所以 G3 的两条出边都是**真的会走**的,不是摆设 —— 没有给条件喂常量。
*
* `reject` 声明仍保留:schema 必填,且它是通用机制(不是每个包都把驳回画成边)。
* 它与 `e-g3-a4` 必须字字对齐,有测试守着。
*
* 几何是**重排**,不再逐点等于 SVG:两带换行(受理·选型·寻源 → 定价·审批·议价·成交),
* 每带内 x 随推进次序单调递增,全图零连线交叉、零穿框。回跳仍用**卡点**表示(不画长线),
* 卡点带 `to`,`mergeGraphIntoPackage` 会把「X → 卡点(to: Y)」还原成语义边,编辑器保存不丢回边。
*/
import { processPackageSchema, triggerConfigSchema } from '@contracts';
import type { ProcessPackage } from '@contracts';
import type { Container } from './container.ts';
/** 活动节点:岗位独任、失败即终止 —— 槽位期不谈容忍 */
const activityNode = (
id: string,
label: string,
activityId: string,
position: string,
col: number,
row: number,
x: number,
y: number,
) => ({
id,
kind: 'activity' as const,
label,
sub: position,
activityId,
owner: { position, strategy: 'sole' as const },
failure: { kind: 'terminate' as const },
layout: { row, col, x, y },
});
/** 判断网关:异或分叉,判据人读、条件在出边 */
const gatewayNode = (id: string, label: string, basis: string, col: number, row: number, x: number, y: number) => ({
id,
kind: 'gateway' as const,
label,
gateway: 'exclusive' as const,
mode: 'fork' as const,
basis,
layout: { row, col, x, y },
});
/**
* 异或汇合点 —— 两条互斥分支重新并回一条路。
*
* MVP 图上这里是两条线各自扎进 A4(隐式合并);语义上必须显式:
* 不给汇合点,A4 就有两条同名映射的入边,闭合校验按「不做隐式合并」拦下(实测)。
* 分叉必有汇合是 BPMN 的正解,本仓 v1 的 G2/G2J 即此先例。
*/
const joinNode = (id: string, label: string, col: number, row: number, x: number, y: number) => ({
id,
kind: 'gateway' as const,
label,
gateway: 'exclusive' as const,
mode: 'join' as const,
layout: { row, col, x, y },
});
/** 受理口的两枚去向旗与记录 —— A4 重新入场(回边 / 驳回)时照此装配 */
const REENTER_A4 = [
{ target: 'records', source: { from: 'output-port' as const, port: 'intake', path: 'records' } },
{ target: 'customerCode', source: { from: 'initiation' as const, path: 'customerCode' } },
];
export const MVP_PACKAGE: ProcessPackage = processPackageSchema.parse({
id: 'unified-quote',
name: '统一报价',
version: 1,
state: 'active',
callInterface: {
input: {
type: 'object',
required: ['customerCode', 'rfqRef'],
properties: {
customerCode: { type: 'string' },
rfqRef: { type: 'string' },
subject: { type: 'string' },
body: { type: 'string' },
quotedAt: { type: 'string' },
},
},
output: { type: 'object', properties: { landingRate: { type: 'number' } } },
},
/*
* 版式:**两带换行**,不回形。`col` 用全局最长路径层级(0..13),每一带内 x 随 col 单调递增,
* 所以「左→右、上→下」读出来就是推进次序 —— 图纸不再需要被当成次序的依据
* (`advanceOrder` 早已改按边拓扑排,这里让两者一致只是为了不误导人)。
*
* 带 0(y 60..100) start → A1 → G1 → A2 → G2 → A3 → G2J 受理 · 选型 · 寻源
* 换行车道 y=190 G2J 右探 40 再降下来,横扫回左,落 A4 北口
* 带 1(y 310..350) A4 → A5 → G3 → A6 → G4 → end_won 定价 · 审批 · 议价 · 成交
* 卡点车道 y=250..280 两枚回 A4 的牌子同朝向(向上 = 回退)
* 带 2(y 410..450) A7 → end_arch 未成交闭环
*
* 注:工厂的参数序是 **col 在 row 前**(activityNode(..., col, row, x, y))。
*/
nodes: [
{ id: 'start', kind: 'start', label: '客户RFQ询价', layout: { row: 0, col: 0, x: 30, y: 70 } },
activityNode('A1', '客户RFQ受理', 'rfq-intake', 'BD', 1, 0, 90, 60),
gatewayNode('G1', '是否需要选型', '受理判定:该条是否要先选替代料(needAlternative)', 2, 0, 250, 60),
activityNode('A2', '替代选型', 'alt-selection', 'FAE', 3, 0, 350, 60),
gatewayNode('G2', '是否需要寻源', '受理判定:该条是否要向渠道询底价(needSourcing)', 4, 0, 510, 60),
activityNode('A3', '寻源询价', 'sourcing-inquiry', 'PRM', 5, 0, 610, 60),
joinNode('G2J', '寻源汇合', 6, 0, 770, 60),
activityNode('A4', '成本定价', 'cost-pricing', 'BD', 7, 1, 60, 310),
{
id: 'A5',
kind: 'approval',
label: '定价审批',
sub: 'PM',
approver: { position: 'PM', strategy: 'sole' },
/*
* 驳回走向声明式,不由临场决定:退回 A4 重新定价。
* 图上由 G3「审批结果」的 `verdict = "reject"` 出边**显式表达同一件事**,
* 而引擎现在是**先看图**(见 `onApprovalVerdict`):有这条边就走边,没有才退回本声明。
* 两处必须字字对齐 —— 有测试守着。本声明不能删:schema 必填,
* 且它是通用机制(不是每个流程包都把驳回画成边)。
*/
reject: { kind: 'reopen', nodeId: 'A4', mapping: REENTER_A4 },
nudge: { steps: [{ after: 'PT20M', action: 'nudge' }], deadline: 'PT30M' },
layout: { row: 1, col: 8, x: 240, y: 310 },
},
gatewayNode('G3', '审批结果', 'A5 裁决的去向:通过 / 不通过(verdict)—— 偏离底价 ≤±20% 才放行(底价门禁P-02)', 9, 1, 380, 310),
activityNode('A6', '报价议价', 'quote-negotiation', 'BD', 10, 1, 460, 310),
gatewayNode('G4', '议价结果', 'A6 办结的去向:成交 / 有异议 / 拒绝或超时(outcome)', 11, 1, 620, 310),
activityNode('A7', '情报归档', 'intel-archive', 'BD', 12, 2, 590, 410),
{ id: 'end_won', kind: 'end', label: '转订单履约', layout: { row: 1, col: 12, x: 700, y: 320 } },
{ id: 'end_arch', kind: 'end', label: '归档闭环', layout: { row: 2, col: 13, x: 730, y: 420 } },
],
edges: [
{
id: 'e-start-a1',
from: 'start',
to: 'A1',
mapping: [
{ target: 'customerCode', source: { from: 'initiation', path: 'customerCode' } },
{ target: 'rfqRef', source: { from: 'initiation', path: 'rfqRef' } },
// 主题与正文是归一步唯一的原始素材:只给单号,模型没有可拆解的东西
{ target: 'subject', source: { from: 'initiation', path: 'subject' } },
{ target: 'body', source: { from: 'initiation', path: 'body' } },
{ target: 'quotedAt', source: { from: 'initiation', path: 'quotedAt' } },
],
reopen: false,
},
{ id: 'e-a1-g1', from: 'A1', to: 'G1', mapping: [], label: '统一受理记录', reopen: false },
{
id: 'e-g1-a2',
from: 'G1',
to: 'A2',
condition: 'needAlternative = true',
label: '需要选型',
mapping: [
{ target: 'material', source: { from: 'output-port', port: 'intake', path: 'pn' } },
// 选型完了还要不要寻源,这枚旗得跟着走 —— G2 在本路径上读的是 A2 的载荷
{ target: 'needSourcing', source: { from: 'output-port', port: 'intake', path: 'needSourcing' } },
/*
* 询价单号也要带上:A2 办结时要把 `needSourcing` 回写「受理结论」那一行,
* 而那一行的主键就是询价单号。不带,A2 就只能另起一行(或写出无主键的行,
* 合并进正式版时被非空约束挡下)—— 去向便有了两个出处,G2 读哪个都不对。
*/
{ target: 'rfqRef', source: { from: 'initiation', path: 'rfqRef' } },
],
reopen: false,
},
{ id: 'e-g1-g2', from: 'G1', to: 'G2', condition: 'needAlternative = false', label: '不需要选型', mapping: [], reopen: false },
{ id: 'e-a2-g2', from: 'A2', to: 'G2', mapping: [], label: '替代对标表', reopen: false },
{
id: 'e-g2-a3',
from: 'G2',
to: 'A3',
condition: 'needSourcing = true',
label: '需要寻源',
mapping: [{ target: 'pn', source: { from: 'output-port', port: 'intake', path: 'pn' } }],
reopen: false,
},
{
id: 'e-g2-j',
from: 'G2',
to: 'G2J',
condition: 'needSourcing = false',
label: '不需要寻源',
mapping: [],
reopen: false,
},
{ id: 'e-a3-j', from: 'A3', to: 'G2J', mapping: [], label: '供应商底价表', reopen: false },
{
id: 'e-j-a4',
from: 'G2J',
to: 'A4',
mapping: [
...REENTER_A4,
// 底价只在寻源那条路上有;直通路径取不到即为空,A4 的 floors 非必填
{ target: 'floors', source: { from: 'output-port', port: 'compare', path: 'floors' } },
],
reopen: false,
},
{ id: 'e-a4-a5', from: 'A4', to: 'A5', mapping: [], label: '报价草案提审', reopen: false },
/*
* 审批点 → 网关:**审批的两个去向都由图决定**(BPMN 的常规)。
* 判据 `verdict` 由引擎在裁决时放进载荷(`onApprovalVerdict`),不是这条边喂的常量 ——
* 喂常量能让闸过、让流跑,但那样 G3 的条件恒真、驳回边永不可达,网关就成了摆设。
*/
{ id: 'e-a5-g3', from: 'A5', to: 'G3', mapping: [], reopen: false },
{
id: 'e-g3-a6',
from: 'G3',
to: 'A6',
condition: 'verdict = "pass"',
label: '通过',
tone: 'ok',
// 原 e-a5-a6 的装配原样搬来:draft 口是 A4 落的,一直在 record.ports 里
mapping: [
{ target: 'rows', source: { from: 'output-port', port: 'draft', path: 'rows' } },
{ target: 'quote_no', source: { from: 'output-port', port: 'draft', path: 'quote_no' } },
{ target: 'customerCode', source: { from: 'initiation', path: 'customerCode' } },
],
reopen: false,
},
{
id: 'e-g3-a4',
from: 'G3',
to: 'A4',
condition: 'verdict = "reject"',
label: '不通过',
tone: 'bad',
mapping: REENTER_A4,
/*
* 回边:与 A5 的 `reject` 声明同一去向(回 A4 重开新实例),两处须一致。
* `reopen` 必须为 true —— 标 false 则 A4 有两条**并发**入边(G2J 与 G3),
* records / customerCode 同名双源,闭合校验按「汇合同名字段来源不明」拦下发布;
* 拓扑序也会成环,流程链会读反。
*/
reopen: true,
},
{ id: 'e-a6-g4', from: 'A6', to: 'G4', mapping: [], label: '正式报价单', reopen: false },
{
id: 'e-g4-won',
from: 'G4',
to: 'end_won',
condition: 'outcome = "won"',
label: '成交',
tone: 'ok',
mapping: [],
reopen: false,
},
{
id: 'e-g4-a4',
from: 'G4',
to: 'A4',
condition: 'outcome = "revise"',
label: '有异议',
tone: 'bad',
mapping: REENTER_A4,
// 回边:人裁决后重开新任务实例(同节点、新任务 ID、留痕关联)
reopen: true,
},
{
id: 'e-g4-a7',
from: 'G4',
to: 'A7',
condition: 'outcome = "lost"',
label: '拒绝/超时',
tone: 'bad',
mapping: [
{ target: 'outcome', source: { from: 'output-port', port: 'formal', path: 'outcome' } },
{ target: 'quote_no', source: { from: 'output-port', port: 'formal', path: 'quote_no' } },
{ target: 'customer_code', source: { from: 'output-port', port: 'formal', path: 'customer_code' } },
{ target: 'quotedAt', source: { from: 'initiation', path: 'quotedAt' } },
],
reopen: false,
},
{ id: 'e-a7-end', from: 'A7', to: 'end_arch', mapping: [], label: '归档闭环', reopen: false },
],
variables: [],
/*
* 手绘几何存证 —— 呈现以它为真相(不走自动布局)。
* 几何是重排后的两带换行(见 nodes 上方的版式说明),不再逐点等于 MVP 的 SVG;
* 卡点是回跳的呈现(带 to,不画长线)。
*/
diagram: {
manual: true,
rows: [],
anchors: [],
nodes: [
{ id: 'start', kind: 'start', label: '客户RFQ询价', x: 30, y: 70, lx: 44, ly: 104 },
{ id: 'A1', kind: 'activity', label: '客户RFQ受理', code: 'A1', owner: 'BD', x: 90, y: 60 },
{ id: 'G1', kind: 'gateway', label: '是否需要选型', mode: 'x', x: 250, y: 60, lx: 270, ly: 112 },
{ id: 'A2', kind: 'activity', label: '替代选型', code: 'A2', owner: 'FAE', flavor: 'case', x: 350, y: 60 },
// 旁注左偏:正下方 x=530 有「不需要寻源」的竖段穿过
{ id: 'G2', kind: 'gateway', label: '是否需要寻源', mode: 'x', x: 510, y: 60, lx: 480, ly: 112 },
{ id: 'A3', kind: 'activity', label: '寻源询价', code: 'A3', owner: 'PRM', flavor: 'case', x: 610, y: 60 },
{ id: 'G2J', kind: 'gateway', label: '寻源汇合', mode: 'x', x: 770, y: 60, lx: 750, ly: 112 },
{ id: 'A4', kind: 'activity', label: '成本定价', code: 'A4', owner: 'BD', x: 60, y: 310 },
{ id: 'A5', kind: 'activity', label: '定价审批', code: 'A5', owner: 'PM', flavor: 'approval', x: 240, y: 310 },
{ id: 'G3', kind: 'gateway', label: '审批结果', mode: 'x', x: 380, y: 310, lx: 400, ly: 368 },
{ id: 'A6', kind: 'activity', label: '报价议价', code: 'A6', owner: 'BD', flavor: 'case', x: 460, y: 310 },
// 旁注斜下:G4 四个口全占着线
{ id: 'G4', kind: 'gateway', label: '议价结果', mode: 'x', x: 620, y: 310, lx: 590, ly: 368 },
{ id: 'A7', kind: 'activity', label: '情报归档', code: 'A7', owner: 'BD', x: 590, y: 410 },
{ id: 'end_won', kind: 'end', label: '转订单履约', x: 700, y: 320, lx: 710, ly: 352 },
{ id: 'end_arch', kind: 'end-bad', label: '归档闭环', x: 730, y: 420, lx: 740, ly: 452 },
// 两枚回 A4 的牌子同一条车道、同朝向(向上 = 回退):G3 上方与 G4 上方
{ id: 'c_reject', kind: 'callout', label: '退回调整', to: 'A4', x: 350, y: 250 },
{ id: 'c_revise', kind: 'callout', label: '返回调整', to: 'A4', x: 590, y: 250 },
],
edges: [
{ from: 'start', to: 'A1', points: [{ x: 50, y: 80 }, { x: 90, y: 80 }] },
{ from: 'A1', to: 'G1', label: '统一受理记录', points: [{ x: 190, y: 80 }, { x: 250, y: 80 }] },
{ from: 'G1', to: 'A2', label: '需要选型', points: [{ x: 290, y: 80 }, { x: 350, y: 80 }] },
// 不需要选型:从 G1 北口翻过 A2 头顶(y=20 车道)
{
from: 'G1',
to: 'G2',
label: '不需要选型',
points: [{ x: 270, y: 60 }, { x: 270, y: 20 }, { x: 530, y: 20 }, { x: 530, y: 60 }],
},
{ from: 'A2', to: 'G2', label: '替代对标表', points: [{ x: 450, y: 80 }, { x: 510, y: 80 }] },
{ from: 'G2', to: 'A3', label: '需要寻源', points: [{ x: 550, y: 80 }, { x: 610, y: 80 }] },
// 不需要寻源:从 G2 南口穿过 A3 脚下(y=150 车道)扎进 G2J 南口
{
from: 'G2',
to: 'G2J',
label: '不需要寻源',
points: [{ x: 530, y: 100 }, { x: 530, y: 150 }, { x: 790, y: 150 }, { x: 790, y: 100 }],
},
{ from: 'A3', to: 'G2J', label: '供应商底价表', points: [{ x: 710, y: 80 }, { x: 770, y: 80 }] },
// 换行那一笔:右探 40 → 降到 190 车道 → 横扫回左 → 落 A4 北口
{
from: 'G2J',
to: 'A4',
points: [
{ x: 810, y: 80 },
{ x: 850, y: 80 },
{ x: 850, y: 190 },
{ x: 110, y: 190 },
{ x: 110, y: 310 },
],
},
{ from: 'A4', to: 'A5', label: '报价草案提审', points: [{ x: 160, y: 330 }, { x: 240, y: 330 }] },
{ from: 'A5', to: 'G3', points: [{ x: 340, y: 330 }, { x: 380, y: 330 }] },
{ from: 'G3', to: 'A6', label: '通过', tone: 'ok', points: [{ x: 420, y: 330 }, { x: 460, y: 330 }] },
// 驳回:卡点(不画长线);语义边 e-g3-a4 在包里,存盘会被 mergeGraphIntoPackage 认回
{
from: 'G3',
to: 'c_reject',
label: '不通过',
tone: 'bad',
points: [{ x: 400, y: 310 }, { x: 400, y: 280 }],
lx: 370,
ly: 296,
},
{ from: 'A6', to: 'G4', label: '正式报价单', points: [{ x: 560, y: 330 }, { x: 620, y: 330 }] },
{ from: 'G4', to: 'end_won', label: '成交', tone: 'ok', points: [{ x: 660, y: 330 }, { x: 700, y: 330 }] },
{
from: 'G4',
to: 'c_revise',
label: '有异议',
tone: 'bad',
points: [{ x: 640, y: 310 }, { x: 640, y: 280 }],
lx: 610,
ly: 296,
},
{
from: 'G4',
to: 'A7',
label: '拒绝/超时',
tone: 'bad',
points: [{ x: 640, y: 350 }, { x: 640, y: 410 }],
lx: 670,
ly: 380,
},
{ from: 'A7', to: 'end_arch', label: '归档闭环', points: [{ x: 690, y: 430 }, { x: 730, y: 430 }] },
],
},
});
/** 事件触发器:投进公司询价邮箱且主题含询价/RFQ 的自动起单 */
const TRIGGER_EVENT = triggerConfigSchema.parse({
id: 'trg-rfq',
name: '客户RFQ询价',
enabled: true,
processPackageId: 'unified-quote',
mapping: [
{ target: 'customerCode', source: { from: 'expression', sfeel: 'keys.customerCode' } },
{ target: 'rfqRef', source: { from: 'expression', sfeel: 'keys.rfqRef' } },
{ target: 'subject', source: { from: 'expression', sfeel: 'subject' } },
{ target: 'body', source: { from: 'expression', sfeel: 'body' } },
// 单据日期取入站时刻:`intel.history_price` 的主键是 (pn, quoted_at),归档拿不到日期就写不进去
{ target: 'quotedAt', source: { from: 'expression', sfeel: 'occurredAt' } },
],
// 按**通道**收口:同一封信投到「询价待确认邮箱」不命中,落人工确认那条路
spec: {
kind: 'event',
rule: 'channelId = "ch-rfq-mail" and (contains(subject, "询价") or contains(subject, "RFQ"))',
eventType: 'data',
},
});
/** 登记触发器:人确认后起单,「登记人即来源」—— 表单由调用接口 schema 自动渲染 */
const TRIGGER_REGISTRATION = triggerConfigSchema.parse({
id: 'trg-rfq-manual',
name: '人工登记起单(统一报价)',
enabled: true,
processPackageId: 'unified-quote',
mapping: [],
spec: { kind: 'registration' },
});
export async function seedMvpFlow(container: Container): Promise<void> {
const log = (line: string) => process.stdout.write(line + '\n');
/*
* 保存即生效,且**内容没变就不再存新版**。
*
* 保存恒新增版本(这是「旧任务走完旧版本」的前提),所以种子不能无条件存 ——
* 那样每跑一次播种就多一个版本,库里堆一串一模一样的定义。
* 比对的是语义与呈现,不含 version/state(它们由保存本身决定)。
*/
/*
* 比对要**认内容不认键序**。jsonb 从 PG 读回来时键是重排过的
* (Postgres 对 jsonb 的对象键排序),直接 `JSON.stringify` 永远判不等 ——
* 于是每播一次种就多存一版一模一样的定义(实测:一口气堆到 v3)。
* 所以递归按键名排序后再序列化;version / state 由保存本身决定,不参与比对。
*/
const canonical = (value: unknown): unknown =>
Array.isArray(value)
? value.map(canonical)
: value !== null && typeof value === 'object'
? Object.fromEntries(
Object.entries(value as Record<string, unknown>)
.filter(([k]) => k !== 'version' && k !== 'state')
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
.map(([k, v]) => [k, canonical(v)]),
)
: value;
const stripped = (pkg: unknown) => JSON.stringify(canonical(pkg));
const current = await container.process.getPackage(MVP_PACKAGE.id);
if (current !== undefined && stripped(current) === stripped(MVP_PACKAGE)) {
log(`④ 流程包「统一报价」v${current.version} 已在库,内容未变 —— 不存新版`);
} else {
const saved = await container.process.savePackage(MVP_PACKAGE);
log(
`④ 流程包「统一报价」v${saved.version} 已保存生效 —— ${saved.nodes.length} 节点,` +
`闭合校验通过(不闭合会拒存)${current === undefined ? '' : `;旧版 v${current.version} 原样留着,在途任务照它跑完`}`,
);
}
await container.trigger.upsert(TRIGGER_EVENT);
await container.trigger.upsert(TRIGGER_REGISTRATION);
log(' 触发器已配置:客户RFQ询价(事件)· 人工登记起单(登记)');
/* 邮件通道两条:自动起单的与人工确认的 —— 同一封 RFQ 两条路,起不起单由通道定 */
const { positions } = await container.org.listStaff();
const op = positions.find((p) => p.code === 'OP');
const pm = positions.find((p) => p.code === 'PM');
const extract = [
{ key: 'customerCode', regex: '\\bC\\d{3}\\b', source: 'both' as const },
{ key: 'rfqRef', regex: 'RFQ-[A-Z0-9]+', source: 'both' as const },
];
await container.channel.upsert({
id: 'ch-rfq-mail',
name: '公司询价邮箱',
kind: 'mail',
address: 'rfq@we.com',
enabled: true,
extract: [...extract, { key: 'quoteNo', regex: 'Q\\d{6}', source: 'both' }],
mapping: [],
...(op === undefined ? {} : { fallbackPosition: op.id }),
addressWindowMs: 7 * 86_400_000,
});
await container.channel.upsert({
id: 'ch-rfq-review',
name: '询价待确认邮箱',
kind: 'mail',
address: 'rfq-review@we.com',
enabled: true,
extract,
mapping: [],
...(pm === undefined ? {} : { fallbackPosition: pm.id }),
addressWindowMs: 7 * 86_400_000,
});
log(' 邮件通道已配置:公司询价邮箱(OP 兜底)· 询价待确认邮箱(PM 兜底)');
}