闭合拆分预览令牌单次消费、样本 checksum 绑定与确认审计;正式提取改为 bindings-only。 同步统一标注工作台交互,并补齐进度投影/任务中心与迁移文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
224 lines
6.9 KiB
TypeScript
224 lines
6.9 KiB
TypeScript
/**
|
||
* features/admin/api/shipping-audit.api.ts
|
||
* 用途:货代统一审计查询/详情/关联链/哈希校验/导出 HTTP 客户端。
|
||
* 依赖:shared/api/client(requestJson / requestBlob)、getAuthHeaders。
|
||
* 逻辑:由 ShippingAuditCenterPage / ShippingAuditEventDetailDrawer 调用;
|
||
* 导出走 requestBlob;失败为 BusinessError;敏感详情由后端按 audit_sensitive_read 裁剪。
|
||
*/
|
||
import { requestBlob, requestJson } from '@/shared/api/client';
|
||
import { getAuthHeaders } from '@/shared/utils/auth';
|
||
|
||
export type ShippingAuditEventSummary = {
|
||
id: number;
|
||
event_type: string;
|
||
aggregate_type: string;
|
||
aggregate_id: string;
|
||
run_no?: string | null;
|
||
task_no?: string | null;
|
||
document_id?: string | null;
|
||
conversation_id?: string | null;
|
||
workflow_id?: string | null;
|
||
workflow_item_id?: string | null;
|
||
review_case_no?: string | null;
|
||
owner_user_id?: string | null;
|
||
actor_type: string;
|
||
actor_id?: string | null;
|
||
request_id?: string | null;
|
||
operation_id?: string | null;
|
||
result?: string | null;
|
||
action?: string | null;
|
||
reason_code?: string | null;
|
||
error_code?: string | null;
|
||
sensitivity_level?: string | null;
|
||
previous_event_hash?: string | null;
|
||
event_hash?: string | null;
|
||
has_snapshots: boolean;
|
||
sensitive_redacted?: boolean;
|
||
created_at?: string | null;
|
||
};
|
||
|
||
export type ShippingAuditDiffRow = {
|
||
key: string;
|
||
change: 'added' | 'removed' | 'changed' | string;
|
||
before: unknown;
|
||
after: unknown;
|
||
};
|
||
|
||
export type ShippingAuditXfaSnapshotRef = {
|
||
purpose: string;
|
||
snapshot_ref: string;
|
||
plaintext_sha256?: string | null;
|
||
};
|
||
|
||
export type ShippingAuditEventDetail = ShippingAuditEventSummary & {
|
||
before_snapshot?: Record<string, unknown> | null;
|
||
after_snapshot?: Record<string, unknown> | null;
|
||
before_hash?: string | null;
|
||
after_hash?: string | null;
|
||
diff?: ShippingAuditDiffRow[];
|
||
diff_json?: Record<string, unknown> | null;
|
||
metadata?: Record<string, unknown>;
|
||
evidence_refs?: Array<Record<string, unknown>>;
|
||
artifact_refs?: Array<Record<string, unknown>>;
|
||
context_refs?: Record<string, unknown>;
|
||
has_xfa_full_snapshots?: boolean;
|
||
xfa_snapshot_refs?: ShippingAuditXfaSnapshotRef[];
|
||
};
|
||
|
||
export type ShippingAuditFilters = {
|
||
event_type?: string;
|
||
/**
|
||
* 事件分组:review_workbench / chat_workbench / process_gate /
|
||
* candidate_qualification / workflow_node。
|
||
*/
|
||
event_type_group?: string;
|
||
actor_type?: string;
|
||
/** 操作者用户 ID;与后端 audit_query_service.actor_id 对齐 */
|
||
actor_id?: string;
|
||
run_no?: string;
|
||
task_no?: string;
|
||
/** Candidate 编号;匹配 aggregate_id */
|
||
candidate_no?: string;
|
||
/** Process Gate / 节点码;匹配 aggregate_id 后缀 */
|
||
gate_code?: string;
|
||
document_id?: string;
|
||
operation_id?: string;
|
||
request_id?: string;
|
||
conversation_id?: string;
|
||
workflow_id?: string;
|
||
workflow_item_id?: string;
|
||
review_case_no?: string;
|
||
owner_user_id?: string;
|
||
/** 稳定业务原因码;与后端 audit_query_service.reason_code 对齐 */
|
||
reason_code?: string;
|
||
/** 订单模板 Profile ID;匹配 aggregate_id */
|
||
profile_id?: string;
|
||
/** 统一 binding_id;匹配 metadata/context_refs */
|
||
binding_id?: string;
|
||
/** Draft Revision 序号 */
|
||
revision_no?: string;
|
||
/** 拆分 segment_id */
|
||
segment_id?: string;
|
||
result?: string;
|
||
created_from?: string;
|
||
created_to?: string;
|
||
q?: string;
|
||
};
|
||
|
||
export type ShippingAuditAssociationChain = {
|
||
items: ShippingAuditEventSummary[];
|
||
total: number;
|
||
association_keys: Record<string, string[]>;
|
||
truncated: boolean;
|
||
limit: number;
|
||
};
|
||
|
||
export type ShippingAuditHashVerifyResult = {
|
||
ok: boolean;
|
||
checked_chains: number;
|
||
failed_chains: number;
|
||
failures_sample: Array<Record<string, unknown>>;
|
||
mode?: string;
|
||
skipped?: boolean;
|
||
};
|
||
|
||
const auth = () => ({ headers: getAuthHeaders() });
|
||
|
||
/**
|
||
* 分页查询审计事件列表(无快照全文)。
|
||
*/
|
||
export function listShippingAuditEvents(page = 1, pageSize = 25, filters: ShippingAuditFilters = {}) {
|
||
const params = new URLSearchParams({ page: String(page), page_size: String(pageSize) });
|
||
Object.entries(filters).forEach(([key, value]) => {
|
||
if (value) params.set(key, String(value));
|
||
});
|
||
return requestJson<{ items: ShippingAuditEventSummary[]; total: number; page: number; page_size: number }>(
|
||
`/api/admin/shipping-notice/audit-events?${params}`,
|
||
auth(),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 读取单条审计详情(含白名单快照与 diff;敏感项可能脱敏)。
|
||
*/
|
||
export function getShippingAuditEvent(eventId: number) {
|
||
return requestJson<ShippingAuditEventDetail>(
|
||
`/api/admin/shipping-notice/audit-events/${encodeURIComponent(String(eventId))}`,
|
||
auth(),
|
||
);
|
||
}
|
||
|
||
export type ShippingAuditXfaSnapshotDetail = {
|
||
summary: Record<string, unknown>;
|
||
payload: Record<string, unknown> | null;
|
||
sensitive_redacted: boolean;
|
||
};
|
||
|
||
/**
|
||
* 按需读取 XFA 加密完整快照;后端按 audit_sensitive_read 裁剪并再写访问审计。
|
||
*/
|
||
export function getShippingXfaAuditSnapshot(snapshotRef: string) {
|
||
return requestJson<ShippingAuditXfaSnapshotDetail>(
|
||
`/api/admin/shipping-notice/audit-snapshots/${encodeURIComponent(snapshotRef)}`,
|
||
auth(),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 按 Workflow / Item / Run / PDF / Case / Task 关联键追溯全链。
|
||
*/
|
||
export function listShippingAuditAssociationChain(filters: ShippingAuditFilters = {}, limit = 500) {
|
||
const params = new URLSearchParams({ limit: String(limit) });
|
||
Object.entries(filters).forEach(([key, value]) => {
|
||
if (value) params.set(key, String(value));
|
||
});
|
||
return requestJson<ShippingAuditAssociationChain>(
|
||
`/api/admin/shipping-notice/audit-events/association-chain?${params}`,
|
||
auth(),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 校验审计哈希链完整性。
|
||
*/
|
||
export function verifyShippingAuditHashChain(body: {
|
||
aggregate_type?: string;
|
||
aggregate_id?: string;
|
||
workflow_id?: string;
|
||
workflow_item_id?: string;
|
||
sample_limit?: number;
|
||
} = {}) {
|
||
return requestJson<ShippingAuditHashVerifyResult>(
|
||
'/api/admin/shipping-notice/audit-events/verify-hash-chain',
|
||
{
|
||
method: 'POST',
|
||
headers: { ...getAuthHeaders(), 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(body),
|
||
},
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 受控导出审计 CSV/JSON,并触发浏览器下载。
|
||
* 核心逻辑:经 requestBlob 统一鉴权与 BusinessError;成功后触发本地下载。
|
||
*/
|
||
export async function exportShippingAuditEvents(
|
||
filters: ShippingAuditFilters = {},
|
||
format: 'csv' | 'json' = 'csv',
|
||
): Promise<void> {
|
||
const { blob, filename } = await requestBlob('/api/admin/shipping-notice/audit-events/export', {
|
||
method: 'POST',
|
||
headers: {
|
||
...getAuthHeaders(),
|
||
'Content-Type': 'application/json',
|
||
},
|
||
body: JSON.stringify({ ...filters, format, limit: 2000 }),
|
||
});
|
||
const objectUrl = URL.createObjectURL(blob);
|
||
const anchor = document.createElement('a');
|
||
anchor.href = objectUrl;
|
||
anchor.download = filename || `shipping-audit.${format}`;
|
||
anchor.click();
|
||
URL.revokeObjectURL(objectUrl);
|
||
}
|