feat: add CLI task center queries

This commit is contained in:
gitea committed 2026-09-05 13:33:33 +08:00
1 parent 6c0f4dc567
commit d28b0c92d9
11 files changed
+338 -1

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
import type { KyInstance } from "ky";
import type { TaskQueryOptions, TaskView } from "./types.js";
const JOIN = [
"flowId.form.payMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems",
"flowId.form.expenseMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems",
"flowId.form.loanMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems",
"flowId.form.requisitionMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems",
"flowId.id,nodeState,/flow/v2/backlogs/state",
];
function dateFilter(options: TaskQueryOptions): string {
if (options.from === undefined && options.to === undefined) return "";
const from = options.from ?? 0;
const to = options.to ?? 32503680000000;
return `&&((updateTime > ${from}) && (updateTime < ${to}))`;
}
function approvedDateFilter(options: TaskQueryOptions): string {
if (options.from === undefined && options.to === undefined) return "";
const from = options.from ?? 0;
const to = options.to ?? 32503680000000;
return `&&((approvedFlow.updateTime > ${from}) && (approvedFlow.updateTime < ${to}))`;
}
function selectForBacklog(): string {
return "flowId(ownerId(id,name,enName),form(title,code,specificationId(active,id,name,enName,nameSpell,code),submitDate,submitterId(id,name,enName,nickName,code,email,cellphone,note),reviewStatus,expenseMoney,loanMoney,requisitionMoney,payMoney),`...`),`...`";
}
export async function fetchTaskPage(
client: KyInstance,
corpId: string,
view: TaskView,
options: TaskQueryOptions = {}
): Promise<{ count: number; items: Record<string, unknown>[] }> {
const start = options.start ?? 0;
const count = options.count ?? 20;
if (view === "approved") {
const response = await client.post("api/flow/v1/flows/approved", {
searchParams: {
join: "id,nodeState,/flow/v2/backlogs/state",
corpId,
},
json: {
query: {
filterBy: `(approvedFlow.action==\"APPROVED\")&&((formType != \"permit\"))${approvedDateFilter(options)}`,
limit: { start, count },
select: "ownerId(id,name,enName),form(title,code,specificationId(active,id,name,enName,nameSpell,code),submitDate,submitterId(id,name,enName,nickName,code,email,cellphone,note),reviewStatus,expenseMoney,loanMoney,requisitionMoney,payMoney,`...`),`...`",
},
},
});
const json = (await response.json()) as { count?: number; items?: Record<string, unknown>[] };
return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] };
}
const state = view === "todo" ? "APPROVING" : "PROCESSED";
const orderBy = view === "todo"
? [
{ value: "flowId.state", order: "ASC" },
{ value: "isUrgent", order: "DESC" },
{ value: "updateTime", order: "ASC" },
]
: [{ value: "updateTime", order: "DESC" }];
const searchParams = new URLSearchParams({ corpId });
for (const join of JOIN) searchParams.append("join", join);
const response = await client.post("api/flow/v2/backlogs/search", {
searchParams,
json: {
filterBy: `(state.in(\"${state}\"))${dateFilter(options)}`,
limit: { start, count },
select: selectForBacklog(),
orderBy,
...(view === "todo" ? { creditSearch: true } : {}),
},
});
const json = (await response.json()) as { count?: number; items?: Record<string, unknown>[] };
return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] };
}
+31
View File
@@ -0,0 +1,31 @@
import { describe, expect, test } from "bun:test";
import { normalizeTask } from "./tools.js";
describe("Task Domain - 待办任务模型", () => {
test("normalizes backlog and preserves approval actions", () => {
const item = normalizeTask({
id: "backlog-1",
state: "APPROVING",
nodeState: { nodeName: "主管审批" },
actions: { "staff-1": ["freeflow.agree", "freeflow.reject"] },
flowId: {
id: "flow-1",
formType: "expense",
ownerId: { id: "staff-1", name: "张三" },
form: {
code: "BX26000001",
title: "差旅报销",
specificationId: { name: "报销单" },
submitterId: { id: "staff-2", name: "李四", code: "1002" },
expenseMoney: { standard: "120.00", standardStrCode: "CNY", standardSymbol: "¥" },
},
},
});
expect(item.taskId).toBe("backlog-1");
expect(item.flowId).toBe("flow-1");
expect(item.billCode).toBe("BX26000001");
expect(item.submitter?.name).toBe("李四");
expect(item.amount?.value).toBe("120.00");
expect(item.actions).toEqual(["freeflow.agree", "freeflow.reject"]);
});
});
+89
View File
@@ -0,0 +1,89 @@
import type { KyInstance } from "ky";
import { fetchTaskPage } from "./api.js";
import type { TaskItem, TaskPageResult, TaskPerson, TaskQueryOptions, TaskView } from "./types.js";
function record(value: unknown): Record<string, unknown> {
return value && typeof value === "object" ? value as Record<string, unknown> : {};
}
function person(value: unknown): TaskPerson | undefined {
const item = record(value);
if (!Object.keys(item).length) return undefined;
return {
id: typeof item.id === "string" ? item.id : undefined,
name: typeof item.name === "string" ? item.name : undefined,
code: typeof item.code === "string" ? item.code : undefined,
email: typeof item.email === "string" ? item.email : undefined,
cellphone: typeof item.cellphone === "string" ? item.cellphone : undefined,
};
}
function money(value: unknown): TaskItem["amount"] {
const item = record(value);
if (item.standard === undefined) return undefined;
return {
value: String(item.standard),
currency: typeof item.standardStrCode === "string" ? item.standardStrCode : undefined,
symbol: typeof item.standardSymbol === "string" ? item.standardSymbol : undefined,
};
}
export function normalizeTask(raw: Record<string, unknown>): TaskItem {
const flow = record(raw.flowId);
const form = record(flow.form ?? raw.form);
const nodeState = record(raw.nodeState ?? flow.nodeState);
const amount = money(form.expenseMoney ?? form.payMoney ?? form.requisitionMoney ?? form.loanMoney);
const specification = record(form.specificationId);
const submitter = person(form.submitterId);
const owner = person(flow.ownerId ?? raw.ownerId);
const actionsByStaff = record(flow.actions ?? raw.actions);
const actionValues = Object.values(actionsByStaff).flatMap((value) => Array.isArray(value) ? value : []);
const logs = Array.isArray(flow.logs) ? flow.logs : [];
const logIndex = typeof raw.logId === "number" ? raw.logId : -1;
const log = record(logs[logIndex] ?? logs[logs.length - 1]);
const attributes = record(log.attributes);
return {
taskId: String(raw.id ?? flow.id ?? ""),
flowId: typeof flow.id === "string" ? flow.id : typeof raw.id === "string" ? raw.id : undefined,
billId: typeof flow.id === "string" ? flow.id : undefined,
billCode: typeof form.code === "string" ? form.code : undefined,
title: typeof form.title === "string" ? form.title : undefined,
templateName: typeof specification.name === "string" ? specification.name : undefined,
formType: typeof flow.formType === "string" ? flow.formType : typeof raw.type === "string" ? raw.type : undefined,
state: typeof raw.state === "string" ? raw.state : typeof flow.state === "string" ? flow.state : undefined,
action: typeof log.action === "string" ? log.action : typeof raw.action === "string" ? raw.action : undefined,
actionTime: typeof log.time === "number" ? log.time : undefined,
comment: typeof attributes.comment === "string" ? attributes.comment : undefined,
nodeName: typeof nodeState.nodeName === "string" ? nodeState.nodeName : typeof raw.nodeName === "string" ? raw.nodeName : undefined,
submitDate: typeof form.submitDate === "number" ? form.submitDate : undefined,
submitter,
owner,
amount,
actions: actionValues.filter((value): value is string => typeof value === "string"),
updatedAt: typeof flow.updateTime === "number" ? flow.updateTime : typeof raw.updateTime === "number" ? raw.updateTime : undefined,
raw,
};
}
export async function listTasks(
client: KyInstance,
corpId: string,
view: TaskView,
options: TaskQueryOptions = {}
): Promise<TaskPageResult> {
const page = await fetchTaskPage(client, corpId, view, options);
let items = page.items.map(normalizeTask);
if (options.staffId) {
const target = options.staffId.toLowerCase();
items = items.filter((item) => [item.submitter?.id, item.submitter?.code, item.owner?.id, item.owner?.code]
.filter(Boolean)
.some((value) => String(value).toLowerCase().includes(target)));
}
return {
view,
total: page.count,
start: options.start ?? 0,
count: options.count ?? 20,
items,
};
}
+50
View File
@@ -0,0 +1,50 @@
import { z } from "zod";
export const TaskViewSchema = z.enum(["todo", "approved", "processed"]);
export type TaskView = z.infer<typeof TaskViewSchema>;
export interface TaskQueryOptions {
start?: number;
count?: number;
from?: number;
to?: number;
staffId?: string;
}
export interface TaskPerson {
id?: string;
name?: string;
code?: string;
email?: string;
cellphone?: string;
}
export interface TaskItem {
taskId: string;
flowId?: string;
billId?: string;
billCode?: string;
title?: string;
templateName?: string;
formType?: string;
state?: string;
action?: string;
actionTime?: number;
comment?: string;
nodeName?: string;
submitDate?: number;
submitter?: TaskPerson;
owner?: TaskPerson;
amount?: { value: string; currency?: string; symbol?: string };
actions?: string[];
updatedAt?: number;
raw: Record<string, unknown>;
}
export interface TaskPageResult {
view: TaskView;
total: number;
start: number;
count: number;
items: TaskItem[];
}