157 lines
5.2 KiB
TypeScript
157 lines
5.2 KiB
TypeScript
import type { KyInstance } from "ky";
|
|
import { validationError } from "../../../core/errors.js";
|
|
import type { CorpConfig } from "../../../core/config/types.js";
|
|
import { createHoseCloseClient } from "./client.js";
|
|
|
|
/**
|
|
* A single row of the CloseAPI outbound message log. The `request` payload is
|
|
* the EBot push body, so its fields mirror Seal's `/api/hosecloud/outbound`
|
|
* contract (flowId / nodeId / messageId / rejectionNum / corporationId).
|
|
*/
|
|
export interface HoseOutboundLogItem {
|
|
id: string;
|
|
name: string;
|
|
type: "INVOKING" | "CALLBACK" | string;
|
|
state: string;
|
|
flowId?: {
|
|
id: string;
|
|
form?: {
|
|
code?: string;
|
|
};
|
|
};
|
|
nodeName?: string;
|
|
createTime: number;
|
|
messageType?: string;
|
|
request?: {
|
|
flowId?: string;
|
|
nodeId?: string;
|
|
messageId?: string;
|
|
rejectionNum?: number;
|
|
corporationId?: string;
|
|
action?: string;
|
|
comment?: string;
|
|
signKey?: string;
|
|
[key: string]: unknown;
|
|
};
|
|
response?: unknown;
|
|
}
|
|
|
|
/**
|
|
* A configured outbound message on the Hose side (EBot push or completion
|
|
* callback). Only the fields seal-use surfaces are kept; Hose returns more.
|
|
*/
|
|
export interface HoseOutboundMessageConfig {
|
|
id: string;
|
|
messageName?: string;
|
|
messageType?: string;
|
|
enable?: boolean;
|
|
signKey?: string;
|
|
callbackUrl?: string;
|
|
corporationId?: string;
|
|
actionEvent?: string[];
|
|
formFields?: string[];
|
|
remark?: string;
|
|
createTime?: number;
|
|
updateTime?: number;
|
|
}
|
|
|
|
function parseOutboundMessage(item: unknown): HoseOutboundMessageConfig | undefined {
|
|
if (!item || typeof item !== "object" || Array.isArray(item)) return undefined;
|
|
const raw = item as Record<string, unknown>;
|
|
if (typeof raw.id !== "string" || !raw.id) return undefined;
|
|
const stringList = (value: unknown): string[] | undefined =>
|
|
Array.isArray(value)
|
|
? value.filter((entry): entry is string => typeof entry === "string")
|
|
: undefined;
|
|
return {
|
|
id: raw.id,
|
|
messageName: typeof raw.messageName === "string" ? raw.messageName : undefined,
|
|
messageType: typeof raw.messageType === "string" ? raw.messageType : undefined,
|
|
enable: typeof raw.enable === "boolean" ? raw.enable : undefined,
|
|
signKey: typeof raw.signKey === "string" ? raw.signKey : undefined,
|
|
callbackUrl: typeof raw.callbackUrl === "string" ? raw.callbackUrl : undefined,
|
|
corporationId: typeof raw.corporationId === "string" ? raw.corporationId : undefined,
|
|
actionEvent: stringList(raw.actionEvent),
|
|
formFields: stringList(raw.formFields),
|
|
remark: typeof raw.remark === "string" ? raw.remark : undefined,
|
|
createTime: typeof raw.createTime === "number" ? raw.createTime : undefined,
|
|
updateTime: typeof raw.updateTime === "number" ? raw.updateTime : undefined
|
|
};
|
|
}
|
|
|
|
/**
|
|
* List every configured outbound message for the tenant via the read-only
|
|
* CloseAPI `GET api/outbound/v1/message`. Each row carries the `signKey` that
|
|
* the EBot callback URL embeds as `config_id`, which is how operators find
|
|
* which signature key belongs to which message.
|
|
*/
|
|
export async function fetchHoseOutboundMessages(
|
|
sourceCorp: CorpConfig
|
|
): Promise<HoseOutboundMessageConfig[]> {
|
|
if (sourceCorp.source.type !== "hose") {
|
|
throw validationError(
|
|
`企业「${sourceCorp.name}」非合思(hose)租户,无法查询合思出站消息配置。`
|
|
);
|
|
}
|
|
|
|
const client = await createHoseCloseClient(sourceCorp);
|
|
const response = (await client
|
|
.get("api/outbound/v1/message", {
|
|
searchParams: { corpId: sourceCorp.source.corpId }
|
|
})
|
|
.json()) as { items?: unknown };
|
|
|
|
if (!Array.isArray(response.items)) {
|
|
throw new Error("合思出站消息接口未返回 items 列表,响应结构与预期不符");
|
|
}
|
|
|
|
return response.items
|
|
.map(parseOutboundMessage)
|
|
.filter((message): message is HoseOutboundMessageConfig => message !== undefined);
|
|
}
|
|
|
|
/**
|
|
* Query CloseAPI's outbound message log for one document SN (flowId.form.code).
|
|
*/
|
|
export async function searchOutboundMessageLogs(
|
|
client: KyInstance,
|
|
corpId: string,
|
|
sn: string,
|
|
options?: { limit?: number }
|
|
): Promise<HoseOutboundLogItem[]> {
|
|
const response = (await client
|
|
.post("api/outbound/v1/message/messageLog", {
|
|
searchParams: {
|
|
messageType: "flow",
|
|
corpId
|
|
},
|
|
json: {
|
|
limit: { start: 0, count: options?.limit ?? 20 },
|
|
orderBy: [{ value: "createTime", order: "DESC" }],
|
|
select: "id,name,flowId(id, form(code)),nodeName,type,request,response,state,createTime,messageType",
|
|
filterBy: `(flowId.form.code==("${sn.replace(/"/g, '\\"')}"))`
|
|
}
|
|
})
|
|
.json()) as { items?: HoseOutboundLogItem[] };
|
|
|
|
return response.items ?? [];
|
|
}
|
|
|
|
/**
|
|
* Fetch historical outbound message logs from Hosecloud's CloseAPI for a given document SN.
|
|
*/
|
|
export async function fetchHoseOutboundLogs(
|
|
sourceCorp: CorpConfig,
|
|
sn: string,
|
|
options?: { limit?: number }
|
|
): Promise<HoseOutboundLogItem[]> {
|
|
if (sourceCorp.source.type !== "hose") {
|
|
throw validationError(
|
|
`源企业「${sourceCorp.name}」非合思(hose)租户,无法通过合思 CloseAPI 查询出站消息日志。`
|
|
);
|
|
}
|
|
|
|
const client = await createHoseCloseClient(sourceCorp);
|
|
return searchOutboundMessageLogs(client, sourceCorp.source.corpId, sn, options);
|
|
}
|