124 lines
5.0 KiB
TypeScript
124 lines
5.0 KiB
TypeScript
import { HTTPError } from "ky";
|
||
import type { CorpConfig } from "../../../core/config/types.js";
|
||
import { CliError, validationError } from "../../../core/errors.js";
|
||
import { createHoseOpenClient } from "./client.js";
|
||
|
||
/**
|
||
* EBot approval callback (`POST api/openapi/v1/approval`) — the OpenAPI call an
|
||
* external service makes after processing an outbound message, so the EBot node
|
||
* can advance (accept/refuse) or add approvers. Only EBot-node approvals are
|
||
* supported upstream; everything else comes back as business code 412.
|
||
*/
|
||
|
||
export const EBOT_APPROVAL_ACTIONS = ["accept", "refuse", "preAdd", "aftAdd"] as const;
|
||
export type EbotApprovalAction = (typeof EBOT_APPROVAL_ACTIONS)[number];
|
||
|
||
export const EBOT_RESUBMIT_METHODS = ["FROM_START", "TO_REJECTOR"] as const;
|
||
|
||
export type EbotApprovalCallbackParams = {
|
||
signKey: string;
|
||
flowId: string;
|
||
nodeId: string;
|
||
action: EbotApprovalAction;
|
||
comment: string;
|
||
policy?: "ALL" | "ANY";
|
||
addApproverIds?: string[];
|
||
rejectTo?: string;
|
||
resubmitMethod?: "FROM_START" | "TO_REJECTOR";
|
||
};
|
||
|
||
export type EbotApprovalCallbackResult = {
|
||
code: string;
|
||
message: string;
|
||
};
|
||
|
||
export async function executeEbotApprovalCallback(
|
||
corp: CorpConfig,
|
||
params: EbotApprovalCallbackParams
|
||
): Promise<EbotApprovalCallbackResult> {
|
||
if (corp.source.type !== "hose") {
|
||
throw validationError(
|
||
`企业「${corp.name}」非合思(hose)租户,无法调用合思 OpenAPI 审批回调。`
|
||
);
|
||
}
|
||
|
||
const appKey = corp.source.appKey ?? corp.source.key;
|
||
const appSecurity = corp.source.appSecurity ?? corp.source.password;
|
||
if (!appKey || !appSecurity) {
|
||
throw validationError(
|
||
`缺少合思凭证:企业「${corp.name}」(id: ${corp.id}) 未配置 appKey/appSecurity(或 key/password),无法换取 accessToken 执行审批回调。`
|
||
);
|
||
}
|
||
|
||
if ((params.action === "preAdd" || params.action === "aftAdd") && (!params.addApproverIds || params.addApproverIds.length === 0)) {
|
||
throw validationError(
|
||
`执行${params.action === "preAdd" ? "前加签 (preAdd)" : "后加签 (aftAdd)"}时,必须通过 --add-approver-ids 指定加签审批人 ID 列表。`
|
||
);
|
||
}
|
||
if (params.action === "aftAdd" && (params.addApproverIds?.length ?? 0) > 1 && !params.policy) {
|
||
throw validationError(
|
||
"后加签审批人超过 1 人时,必须指定会签方式 --policy ALL (会签) 或 --policy ANY (或签)。"
|
||
);
|
||
}
|
||
|
||
// The manual requires a token minted right before the callback: accessToken
|
||
// expires quickly and stale reuse is not worth the risk on a flow write.
|
||
const client = await createHoseOpenClient(corp, undefined, { force: true });
|
||
|
||
let response: unknown;
|
||
try {
|
||
response = await client.post("api/openapi/v1/approval", {
|
||
json: {
|
||
signKey: params.signKey,
|
||
flowId: params.flowId,
|
||
nodeId: params.nodeId,
|
||
action: params.action,
|
||
comment: params.comment,
|
||
...(params.policy ? { policy: params.policy } : {}),
|
||
...(params.addApproverIds?.length ? { addApproverIds: params.addApproverIds } : {}),
|
||
...(params.rejectTo ? { rejectTo: params.rejectTo } : {}),
|
||
...(params.resubmitMethod ? { resubmitMethod: params.resubmitMethod } : {})
|
||
}
|
||
}).json();
|
||
} catch (error) {
|
||
// HTTP 400 means our request body was rejected — an input problem, so the
|
||
// caller gets exit 13 with the upstream body; HTTP 401/403 fall through to
|
||
// the shared classifier (exit 11).
|
||
if (error instanceof HTTPError && error.response.status === 400) {
|
||
throw validationError(`合思审批回调参数被上游拒绝 (HTTP 400):${error.message}`);
|
||
}
|
||
throw error;
|
||
}
|
||
|
||
const rawObj = (response && typeof response === "object") ? (response as Record<string, unknown>) : {};
|
||
const valueObj = rawObj.value && typeof rawObj.value === "object" ? (rawObj.value as Record<string, unknown>) : rawObj;
|
||
const rawCode = valueObj.code ?? rawObj.code;
|
||
const code = rawCode !== undefined ? String(rawCode) : undefined;
|
||
const message = (typeof valueObj.message === "string" ? valueObj.message : (typeof rawObj.message === "string" ? rawObj.message : "")) || "";
|
||
|
||
if (code === "204") {
|
||
return { code, message };
|
||
}
|
||
if (code === "401") {
|
||
throw new CliError(
|
||
"合思审批回调失败:签名秘钥 signKey 错误 (401)。" +
|
||
"请核对出站消息配置中的 signKey(seal-use hose-ebot messages --reveal 可查看)。",
|
||
11
|
||
);
|
||
}
|
||
if (code === "400") {
|
||
throw validationError(`合思审批回调参数错误 (400):${message || "请求参数错误"}`);
|
||
}
|
||
if (code === "412") {
|
||
throw new CliError(
|
||
`合思审批回调失败:业务前置条件不满足 (412):${message || "当前节点不可审批或已被处理"}。\n` +
|
||
"👉 建议:运行 seal-use hose-ebot search <单号SN> 核对单据当前节点与出站记录。",
|
||
1
|
||
);
|
||
}
|
||
if (!code) {
|
||
throw new Error(`合思审批回调未返回业务码,响应与预期不符:${JSON.stringify(response)}`);
|
||
}
|
||
throw new Error(`合思审批回调失败 (${code}):${message || "未知错误"}`);
|
||
}
|