Fix export filename date tag and add time range to approval-runs count
This commit is contained in:
1 parent
45a576a18e
commit
c7433db265
10 files changed
+302
-154
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import type { CorpConfig } from "../../core/config/types.js";
|
||||
import type { createSealClient } from "../../core/http/factory.js";
|
||||
import * as api from "../../domains/seal/api.js";
|
||||
import { exportApprovalRunsToExcel } from "../../domains/seal/approval-runs-export.js";
|
||||
import { exportApprovalRunsToExcel, normalizeTimestamp } from "../../domains/seal/approval-runs-export.js";
|
||||
import {
|
||||
downloadApprovalRunAttachments,
|
||||
extractApprovalRunAttachments
|
||||
@@ -71,6 +71,14 @@ export async function handleApprovalRunsCommand(
|
||||
}
|
||||
|
||||
if (action === "count") {
|
||||
const rawStart =
|
||||
stringOption(args.options.start) ??
|
||||
stringOption(args.options.startDate) ??
|
||||
stringOption(args.options.since);
|
||||
const rawEnd =
|
||||
stringOption(args.options.end) ??
|
||||
stringOption(args.options.endDate);
|
||||
|
||||
await runTool(
|
||||
"seal_approval_runs_count",
|
||||
client,
|
||||
@@ -79,7 +87,9 @@ export async function handleApprovalRunsCommand(
|
||||
status: stringOption(args.options.status),
|
||||
sourceSystem: stringOption(args.options.sourceSystem) ?? stringOption(args.options["source-system"]),
|
||||
taskMode: stringOption(args.options.taskMode) ?? stringOption(args.options["task-mode"]),
|
||||
search: stringOption(args.options.search) ?? stringOption(args.options.query)
|
||||
search: stringOption(args.options.search) ?? stringOption(args.options.query),
|
||||
startDate: rawStart ? normalizeTimestamp(rawStart, false) : undefined,
|
||||
endDate: rawEnd ? normalizeTimestamp(rawEnd, true) : undefined
|
||||
},
|
||||
outputOptions(args.options)
|
||||
);
|
||||
@@ -94,8 +104,13 @@ export async function handleApprovalRunsCommand(
|
||||
{
|
||||
limit: numberOption(args.options.limit),
|
||||
offset: numberOption(args.options.offset),
|
||||
startDate: stringOption(args.options.startDate),
|
||||
endDate: stringOption(args.options.endDate),
|
||||
startDate:
|
||||
stringOption(args.options.startDate) ??
|
||||
stringOption(args.options.start) ??
|
||||
stringOption(args.options.since),
|
||||
endDate:
|
||||
stringOption(args.options.endDate) ??
|
||||
stringOption(args.options.end),
|
||||
status: stringOption(args.options.status),
|
||||
taskMode: stringOption(args.options.taskMode),
|
||||
manualApprovalStatus: stringOption(args.options.manualApprovalStatus),
|
||||
|
||||
@@ -220,4 +220,12 @@ describe("CLI help execution for lifecycle commands", () => {
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.stdout).toContain("seal-use stop");
|
||||
});
|
||||
|
||||
test("approval-runs --help prints updated count and export parameter usage", async () => {
|
||||
const result = await runCli(["approval-runs", "--help"]);
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.stdout).toContain("approval-runs count [--status APPROVED] [--start <YYYY-MM-DD|ms>] [--end <YYYY-MM-DD|ms>]");
|
||||
expect(result.stdout).toContain("approval-runs export [--start <YYYY-MM-DD|ms>] [--end <YYYY-MM-DD|ms>] [--status <status>] [--manual-status <status>] [--completed-only]");
|
||||
});
|
||||
});
|
||||
+3
-3
@@ -322,13 +322,13 @@ export const HELP_REGISTRY: AreaHelp[] = [
|
||||
},
|
||||
{
|
||||
name: "count",
|
||||
summary: "Count approval runs filtered by status",
|
||||
usage: ["seal-use approval-runs count [--status APPROVED] [--corp <corpId>]"]
|
||||
summary: "Count approval runs filtered by status or time range",
|
||||
usage: ["seal-use approval-runs count [--status APPROVED] [--start <YYYY-MM-DD|ms>] [--end <YYYY-MM-DD|ms>] [--corp <corpId>]"]
|
||||
},
|
||||
{
|
||||
name: "export",
|
||||
summary: "Export approval run audit records to Excel workbook",
|
||||
usage: ["seal-use approval-runs export [--start <YYYY-MM-DD|ms>] [--end <YYYY-MM-DD|ms>] [--limit <n>] [--downloads] [--out <file>] [--corp <corpId>]"]
|
||||
usage: ["seal-use approval-runs export [--start <YYYY-MM-DD|ms>] [--end <YYYY-MM-DD|ms>] [--status <status>] [--manual-status <status>] [--completed-only] [--limit <n>] [--downloads] [--out <file>] [--corp <corpId>]"]
|
||||
},
|
||||
{
|
||||
name: "list",
|
||||
|
||||
@@ -4,6 +4,8 @@ import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import ExcelJS from "exceljs";
|
||||
import {
|
||||
formatDate,
|
||||
formatExportDateTag,
|
||||
getRuleBriefTitle,
|
||||
normalizeManualApprovalStatus,
|
||||
normalizeTimestamp,
|
||||
@@ -66,6 +68,43 @@ describe("approval-runs-export helpers", () => {
|
||||
recordCount: 50
|
||||
});
|
||||
expect(dl).toContain("Downloads/大黄蜂_审核记录_2026-08-07至今_50条.xlsx");
|
||||
|
||||
const rangeDl = resolveOutputFilePath({
|
||||
toDownloads: true,
|
||||
corpName: "华铁大黄蜂",
|
||||
dateTag: "2026-08-07至2026-09-13",
|
||||
recordCount: 9780
|
||||
});
|
||||
expect(rangeDl).toContain("Downloads/华铁大黄蜂_审核记录_2026-08-07至2026-09-13_9780条.xlsx");
|
||||
});
|
||||
|
||||
test("formatDate formats dates, epoch timestamps, and strings cleanly in Asia/Shanghai", () => {
|
||||
expect(formatDate("2026-08-07")).toBe("2026-08-07");
|
||||
expect(formatDate("2026-08-07T12:00:00Z")).toBe("2026-08-07");
|
||||
expect(formatDate("1786032000000")).toBe("2026-08-07");
|
||||
expect(formatDate("1786032000")).toBe("2026-08-07");
|
||||
expect(formatDate(1786032000000)).toBe("2026-08-07");
|
||||
expect(formatDate(new Date("2026-08-07T00:00:00+08:00"))).toBe("2026-08-07");
|
||||
expect(formatDate(undefined)).toBeUndefined();
|
||||
expect(formatDate("")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("formatExportDateTag handles startDate and endDate combinations", () => {
|
||||
// Both start and end
|
||||
expect(formatExportDateTag("2026-08-07", "2026-09-13")).toBe("2026-08-07至2026-09-13");
|
||||
expect(formatExportDateTag("1786032000000", "1789315199999")).toBe("2026-08-07至2026-09-13");
|
||||
|
||||
// Only start
|
||||
expect(formatExportDateTag("2026-08-07", undefined)).toBe("2026-08-07至今");
|
||||
expect(formatExportDateTag("2026-08-07", "")).toBe("2026-08-07至今");
|
||||
|
||||
// Only end
|
||||
expect(formatExportDateTag(undefined, "2026-09-13")).toBe("截至2026-09-13");
|
||||
expect(formatExportDateTag("", "2026-09-13")).toBe("截至2026-09-13");
|
||||
|
||||
// Neither
|
||||
const today = new Date().toLocaleDateString("en-CA", { timeZone: "Asia/Shanghai" });
|
||||
expect(formatExportDateTag(undefined, undefined)).toBe(today);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -50,6 +50,55 @@ export function normalizeTimestamp(val?: string | number, endOfDay = false): str
|
||||
return str;
|
||||
}
|
||||
|
||||
export function formatDate(val?: string | number | Date): string | undefined {
|
||||
if (val === undefined || val === null || val === "") return undefined;
|
||||
if (val instanceof Date) {
|
||||
return Number.isNaN(val.getTime())
|
||||
? undefined
|
||||
: val.toLocaleDateString("en-CA", { timeZone: "Asia/Shanghai" });
|
||||
}
|
||||
const raw = String(val).trim();
|
||||
if (!raw) return undefined;
|
||||
if (/^\d{4}-\d{2}-\d{2}/.test(raw)) {
|
||||
return raw.slice(0, 10);
|
||||
}
|
||||
if (/^\d{10}$/.test(raw)) {
|
||||
const d = new Date(Number(raw) * 1000);
|
||||
return Number.isNaN(d.getTime())
|
||||
? undefined
|
||||
: d.toLocaleDateString("en-CA", { timeZone: "Asia/Shanghai" });
|
||||
}
|
||||
if (/^\d{13}$/.test(raw) || /^\d+$/.test(raw)) {
|
||||
const d = new Date(Number(raw));
|
||||
return Number.isNaN(d.getTime())
|
||||
? undefined
|
||||
: d.toLocaleDateString("en-CA", { timeZone: "Asia/Shanghai" });
|
||||
}
|
||||
const d = new Date(raw);
|
||||
return Number.isNaN(d.getTime())
|
||||
? undefined
|
||||
: d.toLocaleDateString("en-CA", { timeZone: "Asia/Shanghai" });
|
||||
}
|
||||
|
||||
export function formatExportDateTag(
|
||||
startDate?: string | number | Date,
|
||||
endDate?: string | number | Date
|
||||
): string {
|
||||
const start = formatDate(startDate);
|
||||
const end = formatDate(endDate);
|
||||
|
||||
if (start && end) {
|
||||
return `${start}至${end}`;
|
||||
}
|
||||
if (start) {
|
||||
return `${start}至今`;
|
||||
}
|
||||
if (end) {
|
||||
return `截至${end}`;
|
||||
}
|
||||
return new Date().toLocaleDateString("en-CA", { timeZone: "Asia/Shanghai" });
|
||||
}
|
||||
|
||||
export function normalizeManualApprovalStatus(status?: string | string[]): string[] | undefined {
|
||||
if (!status) return undefined;
|
||||
const rawList = Array.isArray(status) ? status : [status];
|
||||
@@ -493,16 +542,7 @@ export async function exportApprovalRunsToExcel(
|
||||
}
|
||||
|
||||
// 4. 保存文件落地
|
||||
let dateTag: string | undefined;
|
||||
if (options.startDate) {
|
||||
const rawStart = String(options.startDate);
|
||||
if (/^\d{4}-\d{2}-\d{2}/.test(rawStart)) {
|
||||
dateTag = `${rawStart.slice(0, 10)}至今`;
|
||||
} else if (/^\d+$/.test(rawStart)) {
|
||||
const d = new Date(Number(rawStart));
|
||||
dateTag = `${d.getFullYear()}${String(d.getMonth() + 1).padStart(2, "0")}${String(d.getDate()).padStart(2, "0")}至今`;
|
||||
}
|
||||
}
|
||||
const dateTag = formatExportDateTag(options.startDate, options.endDate);
|
||||
|
||||
const targetPath = resolveOutputFilePath({
|
||||
outputFile: options.outputFile,
|
||||
|
||||
@@ -5,6 +5,7 @@ import ExcelJS from "exceljs";
|
||||
import type { KyInstance } from "ky";
|
||||
import type { CorpConfig } from "../../core/config/types.js";
|
||||
import * as api from "./api.js";
|
||||
import { formatExportDateTag } from "./approval-runs-export.js";
|
||||
import type { InsightPerformance } from "./types.js";
|
||||
|
||||
export interface InsightExportOptions {
|
||||
@@ -355,11 +356,7 @@ export async function exportInsightReportToExcel(
|
||||
}
|
||||
|
||||
// 4. 写入本地文件
|
||||
let dateTag: string | undefined;
|
||||
if (options.startDate) {
|
||||
const raw = String(options.startDate).slice(0, 10).replace(/[-:]/g, "");
|
||||
dateTag = `${raw}至今`;
|
||||
}
|
||||
const dateTag = formatExportDateTag(options.startDate, options.endDate);
|
||||
|
||||
const targetPath = resolveInsightOutputFilePath({
|
||||
outputFile: options.outputFile,
|
||||
|
||||
@@ -847,6 +847,39 @@ describe("sealTools", () => {
|
||||
outboundName: "DingTalk AI Audit"
|
||||
});
|
||||
});
|
||||
|
||||
test("seal_approval_runs_count supports startDate and endDate and normalizes them", async () => {
|
||||
const tool = sealTools.find((item) => item.name === "seal_approval_runs_count");
|
||||
expect(tool).toBeDefined();
|
||||
|
||||
const schema = z.toJSONSchema(tool!.parameters) as {
|
||||
properties?: Record<string, unknown>;
|
||||
};
|
||||
expect(schema.properties).toHaveProperty("startDate");
|
||||
expect(schema.properties).toHaveProperty("endDate");
|
||||
expect(schema.properties).toHaveProperty("status");
|
||||
|
||||
const requests: string[] = [];
|
||||
const client = fakeApprovalRunsClient(requests);
|
||||
const handler = tool!.handler as ToolHandler;
|
||||
|
||||
const res = await handler(
|
||||
client,
|
||||
{
|
||||
status: "APPROVED",
|
||||
startDate: "2026-08-07",
|
||||
endDate: "2026-09-13"
|
||||
},
|
||||
fakeContext()
|
||||
);
|
||||
|
||||
expect(res).toEqual({ total: 3 });
|
||||
expect(requests).toHaveLength(1);
|
||||
expect(requests[0]).toContain("api/v1/approvals?");
|
||||
expect(requests[0]).toContain("startDate=1786032000000");
|
||||
expect(requests[0]).toContain("endDate=1789315199999");
|
||||
expect(requests[0]).toContain("APPROVED");
|
||||
});
|
||||
});
|
||||
|
||||
function fakeApprovalRunsClient(requests: string[] = []): KyInstance {
|
||||
|
||||
@@ -28,7 +28,7 @@ import { MaterialPackFilePolicyPatchSchema, MaterialPackFilePolicySchema } from
|
||||
import {
|
||||
extractApprovalRunAttachments
|
||||
} from "./attachments.js";
|
||||
import { exportApprovalRunsToExcel } from "./approval-runs-export.js";
|
||||
import { exportApprovalRunsToExcel, normalizeTimestamp } from "./approval-runs-export.js";
|
||||
import { exportInsightReportToExcel, type InsightExportOptions } from "./insight-export.js";
|
||||
import {
|
||||
ThinkingPolicySchema,
|
||||
@@ -2496,15 +2496,29 @@ export const sealTools = [
|
||||
status: z.string().optional(),
|
||||
sourceSystem: z.string().optional(),
|
||||
taskMode: z.string().optional(),
|
||||
search: z.string().optional()
|
||||
search: z.string().optional(),
|
||||
startDate: z.string().optional().describe("起始时间戳(毫秒)或日期字符串"),
|
||||
endDate: z.string().optional().describe("结束时间戳(毫秒)或日期字符串")
|
||||
}),
|
||||
handler: async (client: KyInstance, params: { status?: string; sourceSystem?: string; taskMode?: string; search?: string }) => {
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
params: {
|
||||
status?: string;
|
||||
sourceSystem?: string;
|
||||
taskMode?: string;
|
||||
search?: string;
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
}
|
||||
) => {
|
||||
const runs = await api.listApprovalRuns(client, {
|
||||
limit: 1,
|
||||
status: params.status,
|
||||
sourceSystem: params.sourceSystem,
|
||||
taskMode: params.taskMode,
|
||||
search: params.search
|
||||
search: params.search,
|
||||
startDate: normalizeTimestamp(params.startDate, false),
|
||||
endDate: normalizeTimestamp(params.endDate, true)
|
||||
});
|
||||
return {
|
||||
total: runs.total ?? runs.records.length
|
||||
|
||||
Reference in new issue
Block a user