feat(datalink): add record get, toggle, edit, and recompute actions and fix active display

This commit is contained in:
gitea committed 2026-09-09 15:55:12 +08:00
1 parent 09c4e7df4a
commit 97b67c71da
9 files changed
+655 -7

No files matched your search

+5 -1
View File
@@ -31,10 +31,14 @@ hose-use platform get <应用 ID|名称>
hose-use platform entities <应用 ID|名称>
hose-use platform schema <应用 ID|名称> [对象 ID|名称] [--keyword <字段关键词>]
hose-use platform records <应用 ID|名称> <对象 ID|名称> [--keyword <关键词>] [--filter <表达式>] [--fields <字段列表>]
hose-use platform record get <应用> <业务对象> <记录ID>
hose-use platform record toggle <应用> <业务对象> <记录ID> [--state=true|false]
hose-use platform record edit <应用> <业务对象> <记录ID> --data='{"field": "val"}'
hose-use platform record recompute <应用> <业务对象> <记录ID>
hose-use platform permissions <应用 ID|名称> [对象 ID|名称]
hose-use platform imports <应用 ID|名称> <对象 ID|名称>
```
也可以使用 `datalink` / `data-link` 作为同义命令域。
写入、编辑、启停用和 Excel 上传暂不在本域开放。对象元数据中的操作标识只代表前端声明的能力,不能直接当作已确认的写入接口。
单条业务对象记录支持通过 `record get`(详情)、`record toggle`(启停用)、`record edit`(字段编辑更新)以及 `record recompute`(台账重算)进行维护。Excel 批量上传与流程引擎触发暂不在本域开放。
+102
View File
@@ -7,6 +7,8 @@ import {
type DatalinkImportJob,
type DatalinkPlatformDetail,
type DatalinkRecord,
type DatalinkRecordDetail,
type DatalinkRecordEditPayload,
type DatalinkRecordPage,
type DatalinkRecordQuery,
} from "./types.js";
@@ -176,3 +178,103 @@ export async function getImportResult(
});
return asRecord(await response.json());
}
/** 切换业务对象记录的停启用状态 (active: true <-> false)。若未指定 state 则自动反转当前状态。 */
export async function toggleRecordState(
client: KyInstance,
corpId: string,
recordId: string,
state?: boolean
): Promise<{ recordId: string; active: boolean }> {
const normalizedId = recordId.startsWith("$") ? recordId : `$${recordId}`;
const bareId = recordId.startsWith("$") ? recordId.slice(1) : recordId;
let targetState = state;
if (targetState === undefined) {
const detail = await getRecordDetail(client, corpId, recordId);
const record = ((detail.dataLink ?? (detail as any).data?.dataLink ?? detail) as Record<string, unknown>);
targetState = !(record.active ?? true);
}
await client.put(`api/supplier/v3/contract/${normalizedId}/state`, {
searchParams: { corpId },
json: { id: bareId, state: targetState },
});
return { recordId: bareId, active: targetState };
}
/** 获取单条业务对象记录的完整详情(包含展示模板、字段值、可见性及审批历史等)。 */
export async function getRecordDetail(
client: KyInstance,
corpId: string,
recordId: string
): Promise<DatalinkRecordDetail> {
const normalizedId = recordId.startsWith("$") ? recordId : `$${recordId}`;
const bareId = recordId.startsWith("$") ? recordId.slice(1) : recordId;
const response = await client.get(`api/v2/datalink/detail/${normalizedId}/$DETAIL`, {
searchParams: {
dataLinkId: bareId,
type: "DETAIL",
corpId,
},
});
const json = asRecord(await response.json());
return (json.value ?? json.data ?? json) as DatalinkRecordDetail;
}
/** 获取业务对象编辑表单的字段结构定义。 */
export async function getRecordFormSchema(
client: KyInstance,
corpId: string,
entityId: string
): Promise<Record<string, unknown>> {
const normalizedId = entityId.startsWith("$") ? entityId : `$${entityId}`;
const bareId = entityId.startsWith("$") ? entityId.slice(1) : entityId;
const response = await client.get(`api/v2/datalink/entityEdit/entityForm/${normalizedId}/$true`, {
searchParams: {
entityId: bareId,
isInsert: "true",
needFormula: "true",
corpId,
},
});
return asRecord(await response.json());
}
/** 编辑更新业务对象记录字段。 */
export async function editRecord(
client: KyInstance,
corpId: string,
payload: DatalinkRecordEditPayload
): Promise<Record<string, unknown>> {
const bareEntityId = payload.dataLinkEntityId.startsWith("$")
? payload.dataLinkEntityId.slice(1)
: payload.dataLinkEntityId;
const bareRecordId = payload.dataLinkId.startsWith("$")
? payload.dataLinkId.slice(1)
: payload.dataLinkId;
const response = await client.post("api/v2/datalink/entityEdit/edit", {
searchParams: { corpId },
json: {
...payload,
dataLinkEntityId: bareEntityId,
dataLinkId: bareRecordId,
},
});
const json = asRecord(await response.json());
return (json.value ?? json.data ?? json) as Record<string, unknown>;
}
/** 重算业务对象记录下的台账信息。 */
export async function recomputeRecordLedger(
client: KyInstance,
corpId: string,
recordId: string
): Promise<Record<string, unknown>> {
const normalizedId = recordId.startsWith("$") ? recordId : `$${recordId}`;
const response = await client.post(`api/v2/ledger/recompute/${normalizedId}`, {
searchParams: { corpId },
});
return asRecord(await response.json());
}
+84 -1
View File
@@ -1,5 +1,14 @@
import { describe, expect, test } from "bun:test";
import { getPlatform, listPlatformEntities, listPlatforms, searchEntityRecords } from "./api.js";
import {
editRecord,
getPlatform,
getRecordDetail,
listPlatformEntities,
listPlatforms,
recomputeRecordLedger,
searchEntityRecords,
toggleRecordState,
} from "./api.js";
import { buildKeywordFilter, selectRecordFields } from "./services.js";
const platform = {
@@ -85,4 +94,78 @@ describe("DataLink Domain - 应用、业务对象与记录查询", () => {
expect(buildKeywordFilter('A"1')).toContain('A\\"1');
expect(selectRecordFields({ id: "r-1", dataLink: { code: "A-1" } }, ["编码"], [{ name: "code", label: "编码", type: "text" }])).toEqual({ id: "r-1", dataLink: { 编码: "A-1" } });
});
test("切换记录停启用状态调用 PUT contract state 接口", async () => {
let calledUrl = "";
let calledParams: any = null;
let calledJson: any = null;
const client = {
get: async () => ({
json: async () => ({ value: { id: "record-1", active: true } }),
}),
put: async (url: string, options: any) => {
calledUrl = url;
calledParams = options.searchParams;
calledJson = options.json;
return { status: 204 };
},
} as any;
const res = await toggleRecordState(client, "corp-1", "record-1");
expect(calledUrl).toBe("api/supplier/v3/contract/$record-1/state");
expect(calledParams).toEqual({ corpId: "corp-1" });
expect(calledJson).toEqual({ id: "record-1", state: false });
expect(res).toEqual({ recordId: "record-1", active: false });
});
test("获取记录详情调用 GET datalink detail 接口", async () => {
const client = {
get: async (url: string, options: any) => {
expect(url).toBe("api/v2/datalink/detail/$record-1/$DETAIL");
expect(options.searchParams).toEqual({
dataLinkId: "record-1",
type: "DETAIL",
corpId: "corp-1",
});
return { json: async () => ({ value: { id: "record-1", name: "记录1" } }) };
},
} as any;
const result = await getRecordDetail(client, "corp-1", "record-1");
expect((result as any).name).toBe("记录1");
});
test("编辑业务对象记录调用 POST entityEdit edit 接口", async () => {
const client = {
post: async (url: string, options: any) => {
expect(url).toBe("api/v2/datalink/entityEdit/edit");
expect(options.searchParams).toEqual({ corpId: "corp-1" });
expect(options.json.dataLinkEntityId).toBe("entity-1");
expect(options.json.dataLinkId).toBe("record-1");
expect(options.json.form).toEqual({ name: "新名称" });
return { json: async () => ({ value: { id: "record-1", name: "新名称" } }) };
},
} as any;
const result = await editRecord(client, "corp-1", {
dataLinkEntityId: "$entity-1",
dataLinkId: "$record-1",
form: { name: "新名称" },
});
expect((result as any).name).toBe("新名称");
});
test("重算台账调用 POST ledger recompute 接口", async () => {
const client = {
post: async (url: string, options: any) => {
expect(url).toBe("api/v2/ledger/recompute/$record-1");
expect(options.searchParams).toEqual({ corpId: "corp-1" });
return { json: async () => ({ id: "ok" }) };
},
} as any;
const result = await recomputeRecordLedger(client, "corp-1", "record-1");
expect(result.id).toBe("ok");
});
});
+65
View File
@@ -66,6 +66,71 @@ export const datalinkTools = [
return { jobs, latestResult: result };
},
},
{
name: "datalink_record_get",
description: "获取单条业务对象记录的完整详情(模板、字段值、可见性及历史等)",
parameters: z.object({
recordId: z.string().describe("业务对象记录 ID (dataLinkId)"),
}),
handler: async (clients: unknown, params: { recordId: string }) => {
const { corp, ekbClient } = clientSchema.parse(clients);
return api.getRecordDetail(ekbClient, corp.credentials.corpId, params.recordId);
},
},
{
name: "datalink_record_toggle",
description: "切换业务对象记录的停启用状态 (active: true <-> false)",
parameters: z.object({
recordId: z.string().describe("业务对象记录 ID (dataLinkId)"),
state: z.boolean().optional().describe("目标状态 (true 为启用,false 为停用,不传则自动翻转)"),
}),
handler: async (clients: unknown, params: { recordId: string; state?: boolean }) => {
const { corp, ekbClient } = clientSchema.parse(clients);
const result = await api.toggleRecordState(ekbClient, corp.credentials.corpId, params.recordId, params.state);
return { success: true, ...result };
},
},
{
name: "datalink_record_edit",
description: "编辑更新业务对象记录的字段值",
parameters: z.object({
entityId: z.string().describe("业务对象 ID"),
recordId: z.string().describe("业务对象记录 ID (dataLinkId)"),
form: z.record(z.string(), z.unknown()).describe("待更新的表单字段键值对"),
active: z.boolean().optional().describe("停启用状态"),
visibility: z.record(z.string(), z.unknown()).optional().describe("可见性配置"),
}),
handler: async (
clients: unknown,
params: {
entityId: string;
recordId: string;
form: Record<string, unknown>;
active?: boolean;
visibility?: Record<string, unknown>;
}
) => {
const { corp, ekbClient } = clientSchema.parse(clients);
return api.editRecord(ekbClient, corp.credentials.corpId, {
dataLinkEntityId: params.entityId,
dataLinkId: params.recordId,
form: params.form,
active: params.active,
visibility: params.visibility,
});
},
},
{
name: "datalink_record_recompute",
description: "重新计算指定业务对象记录下的全部台账信息",
parameters: z.object({
recordId: z.string().describe("业务对象记录 ID (dataLinkId)"),
}),
handler: async (clients: unknown, params: { recordId: string }) => {
const { corp, ekbClient } = clientSchema.parse(clients);
return api.recomputeRecordLedger(ekbClient, corp.credentials.corpId, params.recordId);
},
},
];
export type DatalinkTool = (typeof datalinkTools)[number];
+27
View File
@@ -92,3 +92,30 @@ export interface DatalinkPermissionSnapshot {
platform: DatalinkPlatformDetail;
entity?: DatalinkEntity;
}
export interface DatalinkRecordDetail {
dataLink?: DatalinkRecord;
template?: unknown;
entity?: unknown;
actions?: unknown[];
flowCounts?: Record<string, unknown>;
ledger?: Record<string, unknown>;
ledgers?: unknown[];
[key: string]: unknown;
}
export interface DatalinkRecordEditPayload {
dataLinkEntityId: string;
dataLinkId: string;
active?: boolean;
form: Record<string, unknown>;
visibility?: {
fullVisible?: boolean;
staffs?: string[];
roles?: string[];
departments?: string[];
departmentsIncludeChildren?: boolean;
[key: string]: unknown;
};
[key: string]: unknown;
}