import { createSessionClient, getUnityClient } from "../../core/http/index.js"; import type { CorpConfig } from "../../core/config/types.js"; import { listOpenApiKeys, getOpenConfig, listClientGoods, searchStoreGoods, getGoodsDetail, } from "./api.js"; import type { OpenApiKey, OpenConfig, ClientGoods, StoreGoodsItem, GoodsDetail, } from "./types.js"; export async function fetchOpenApiKeys(corp: CorpConfig): Promise { const sessionClient = await createSessionClient(corp); const corpId = corp.corpId || corp.credentials.corpId; return listOpenApiKeys(sessionClient, corpId); } export async function fetchOpenConfig(corp: CorpConfig): Promise { const sessionClient = await createSessionClient(corp); const corpId = corp.corpId || corp.credentials.corpId; return getOpenConfig(sessionClient, corpId); } export async function fetchClientGoods( corp: CorpConfig, options: { keyword?: string; isAll?: boolean } = {} ): Promise { const { client: unityClient } = await getUnityClient(corp); const corpId = corp.corpId || corp.credentials.corpId; const items = await listClientGoods(unityClient, corpId, { name: options.keyword, isAll: options.isAll, }); if (options.keyword && options.keyword.trim()) { const kw = options.keyword.trim().toLowerCase(); return items.filter( (item) => item.name.toLowerCase().includes(kw) || (item.summary && item.summary.toLowerCase().includes(kw)) ); } return items; } /** * 应用是否处于「已开通」状态。 * * 只看 `status`,不看 `onLineStatus` —— 后者表示应用是否在架, * 已购买但已下架的应用仍然可以正常调用其开放接口。 */ export function isClientGoodOpened(item: { status?: string | null; onLineStatus?: string | null }): boolean { return item.status?.toUpperCase() === "OPENED"; } /** * 汇总当前企业**已开通**应用所提供的权限代码(统一小写,便于大小写不敏感比对)。 * * 用于在保存工作流时判断「节点要调的接口,企业到底有没有买对应的应用」。 * 典型场景:调用 `/api/openapi/v1/backlog/data/*`(单据审批)需要 * `TICKET_AUDIT_switch`,未开通时运行期只会返回 403「未开通功能,无权访问」。 */ export async function fetchOpenedPowerCodes(corp: CorpConfig): Promise> { const goods = await fetchClientGoods(corp, { isAll: true }); const codes = new Set(); for (const item of goods) { if (!isClientGoodOpened(item)) continue; for (const power of item.powers ?? []) { if (typeof power === "string") { if (power.trim()) codes.add(power.trim().toLowerCase()); continue; } const code = power?.powerCode; if (code && code.trim()) codes.add(code.trim().toLowerCase()); } } return codes; } export async function searchGoods( corp: CorpConfig, options: { keyword?: string; start?: number; count?: number } = {} ): Promise<{ total: number; items: StoreGoodsItem[] }> { const { client: unityClient } = await getUnityClient(corp); const corpId = corp.corpId || corp.credentials.corpId; return searchStoreGoods(unityClient, corpId, { name: options.keyword, start: options.start, count: options.count, }); } export async function fetchGoodsDetail( corp: CorpConfig, goodsId: string ): Promise { const { client: unityClient } = await getUnityClient(corp); const corpId = corp.corpId || corp.credentials.corpId; return getGoodsDetail(unityClient, corpId, goodsId); }