闭合拆分预览令牌单次消费、样本 checksum 绑定与确认审计;正式提取改为 bindings-only。 同步统一标注工作台交互,并补齐进度投影/任务中心与迁移文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
174 lines
7.3 KiB
TypeScript
174 lines
7.3 KiB
TypeScript
/**
|
|
* features/chat/shipping/normalizeShippingProcessing.ts
|
|
* 用途:将后端 snake_case ShippingProcessingProjection 规范为前端 camelCase DTO。
|
|
* 依赖:shipping.api.types ShippingProcessingProjection / ProcessGateSummary。
|
|
* 逻辑:API 边界一次性转换;透传 Gate attemptNo 与 Intake 失败恢复摘要字段。
|
|
*/
|
|
|
|
import type {
|
|
OrderIntakeSourceType,
|
|
ProcessGateSummary,
|
|
ShippingOrderTaskSummary,
|
|
ShippingProcessingProjection,
|
|
ShippingProcessingStage,
|
|
ShippingProcessingStageNode,
|
|
ShippingProcessingStageNodeStatus,
|
|
ShippingProcessingStatus,
|
|
ShippingProcessingWorkItemType,
|
|
} from '../types/shipping.api.types';
|
|
|
|
/**
|
|
* 读取原始对象上的字符串字段(兼容 snake / camel)。
|
|
*/
|
|
function readString(raw: Record<string, unknown>, snake: string, camel: string): string {
|
|
const value = raw[snake] ?? raw[camel];
|
|
return value == null ? '' : String(value);
|
|
}
|
|
|
|
/**
|
|
* 读取可空字符串。
|
|
*/
|
|
function readNullableString(
|
|
raw: Record<string, unknown>,
|
|
snake: string,
|
|
camel: string,
|
|
): string | null {
|
|
const value = raw[snake] ?? raw[camel];
|
|
if (value == null || value === '') return null;
|
|
return String(value);
|
|
}
|
|
|
|
/**
|
|
* 规范单个 OT 摘要。
|
|
*/
|
|
function normalizeChildTask(raw: unknown): ShippingOrderTaskSummary | null {
|
|
if (!raw || typeof raw !== 'object') return null;
|
|
const item = raw as Record<string, unknown>;
|
|
const orderTaskNo = readString(item, 'order_task_no', 'orderTaskNo');
|
|
if (!orderTaskNo) return null;
|
|
return {
|
|
orderTaskNo,
|
|
status: readString(item, 'status', 'status') as ShippingProcessingStatus,
|
|
stage: readString(item, 'stage', 'stage') as ShippingProcessingStage,
|
|
customerName: readNullableString(item, 'customer_name', 'customerName'),
|
|
poNumber: readNullableString(item, 'po_number', 'poNumber'),
|
|
lastActivityAt: readNullableString(item, 'last_activity_at', 'lastActivityAt'),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 规范 Gate 摘要。
|
|
*/
|
|
function normalizeGate(raw: unknown): ProcessGateSummary | null {
|
|
if (!raw || typeof raw !== 'object') return null;
|
|
const item = raw as Record<string, unknown>;
|
|
const gateCode = readString(item, 'gate_code', 'gateCode');
|
|
if (!gateCode) return null;
|
|
return {
|
|
gateCode,
|
|
status: readString(item, 'status', 'status') || 'unknown',
|
|
nextActionCode: readNullableString(item, 'next_action_code', 'nextActionCode'),
|
|
blockingReasonCode: readNullableString(item, 'blocking_reason_code', 'blockingReasonCode'),
|
|
attemptNo: (() => {
|
|
const rawAttempt = item.attempt_no ?? item.attemptNo;
|
|
if (rawAttempt == null || rawAttempt === '') return null;
|
|
const n = Number(rawAttempt);
|
|
return Number.isFinite(n) ? n : null;
|
|
})(),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 规范单个 stage_node(兼容 snake / camel)。
|
|
*/
|
|
function normalizeStageNode(raw: unknown): ShippingProcessingStageNode | null {
|
|
if (!raw || typeof raw !== 'object') return null;
|
|
const item = raw as Record<string, unknown>;
|
|
const stage = readString(item, 'stage', 'stage') as ShippingProcessingStage;
|
|
if (!stage) return null;
|
|
const absoluteIndexRaw = item.absolute_index ?? item.absoluteIndex;
|
|
const absoluteIndex = Number.isFinite(Number(absoluteIndexRaw))
|
|
? Number(absoluteIndexRaw)
|
|
: 0;
|
|
const status = readString(item, 'status', 'status') as ShippingProcessingStageNodeStatus;
|
|
return {
|
|
stage,
|
|
absoluteIndex,
|
|
status: status || 'pending',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 将后端 / 混合字段投影规范为 ShippingProcessingProjection。
|
|
* 核心逻辑:缺字段给安全默认值,保证四页面可渲染;非法输入返回 null。
|
|
*
|
|
* @param raw - API 原始对象
|
|
* @returns 规范化投影;无法识别时 null
|
|
*/
|
|
export function normalizeShippingProcessingProjection(
|
|
raw: unknown,
|
|
): ShippingProcessingProjection | null {
|
|
if (!raw || typeof raw !== 'object') return null;
|
|
const item = raw as Record<string, unknown>;
|
|
const workItemNo = readString(item, 'work_item_no', 'workItemNo');
|
|
if (!workItemNo) return null;
|
|
|
|
const summaryRaw = (item.source_summary ?? item.sourceSummary ?? {}) as Record<string, unknown>;
|
|
const childrenRaw = item.child_order_tasks ?? item.childOrderTasks;
|
|
const children = Array.isArray(childrenRaw)
|
|
? childrenRaw.map(normalizeChildTask).filter((row): row is ShippingOrderTaskSummary => Boolean(row))
|
|
: [];
|
|
const actionsRaw = item.available_actions ?? item.availableActions;
|
|
const availableActions = Array.isArray(actionsRaw)
|
|
? actionsRaw.map((action) => String(action || '').trim()).filter(Boolean)
|
|
: [];
|
|
const stageNodesRaw = item.stage_nodes ?? item.stageNodes;
|
|
const stageNodes = Array.isArray(stageNodesRaw)
|
|
? stageNodesRaw.map(normalizeStageNode).filter((row): row is ShippingProcessingStageNode => Boolean(row))
|
|
: undefined;
|
|
|
|
return {
|
|
workItemType: (readString(item, 'work_item_type', 'workItemType') || 'order_task') as ShippingProcessingWorkItemType,
|
|
workItemNo,
|
|
sourceType: (readNullableString(item, 'source_type', 'sourceType') || 'eml') as OrderIntakeSourceType,
|
|
rootOrderTaskNo: readNullableString(item, 'root_order_task_no', 'rootOrderTaskNo'),
|
|
shippingNoticeNo: readNullableString(item, 'shipping_notice_no', 'shippingNoticeNo'),
|
|
runNo: readNullableString(item, 'run_no', 'runNo'),
|
|
caseNo: readNullableString(item, 'case_no', 'caseNo'),
|
|
conversationId: readNullableString(item, 'conversation_id', 'conversationId'),
|
|
submissionNo: readString(item, 'submission_no', 'submissionNo'),
|
|
intakeNo: readString(item, 'intake_no', 'intakeNo'),
|
|
status: (readString(item, 'status', 'status') || 'processing') as ShippingProcessingStatus,
|
|
stage: (readString(item, 'stage', 'stage') || 'upload_received') as ShippingProcessingStage,
|
|
progressVersion: Number(item.progress_version ?? item.progressVersion ?? 0) || 0,
|
|
lastActivityAt: readString(item, 'last_activity_at', 'lastActivityAt'),
|
|
stageNodes: stageNodes?.length ? stageNodes : undefined,
|
|
currentGate: normalizeGate(item.current_gate ?? item.currentGate),
|
|
availableActions,
|
|
sourceSummary: {
|
|
emailSubject: readNullableString(summaryRaw, 'email_subject', 'emailSubject'),
|
|
filename: readNullableString(summaryRaw, 'filename', 'filename'),
|
|
documentId: Number(summaryRaw.document_id ?? summaryRaw.documentId ?? 0) || null,
|
|
documentCount: Number(summaryRaw.document_count ?? summaryRaw.documentCount ?? 0) || 0,
|
|
parsedDocumentCount: Number(summaryRaw.parsed_document_count ?? summaryRaw.parsedDocumentCount ?? 0) || 0,
|
|
candidateCount: Number(summaryRaw.candidate_count ?? summaryRaw.candidateCount ?? 0) || 0,
|
|
customerName: readNullableString(summaryRaw, 'customer_name', 'customerName'),
|
|
poNumber: readNullableString(summaryRaw, 'po_number', 'poNumber'),
|
|
failureReasonCode: readNullableString(summaryRaw, 'failure_reason_code', 'failureReasonCode'),
|
|
parseAttemptCount: Number(summaryRaw.parse_attempt_count ?? summaryRaw.parseAttemptCount ?? 0) || 0,
|
|
assetStatus: readNullableString(summaryRaw, 'asset_status', 'assetStatus'),
|
|
},
|
|
childOrderTasks: children,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 将会话级统一投影响应中的 work_items 规范为数组。
|
|
*/
|
|
export function normalizeShippingProcessingWorkItems(raw: unknown): ShippingProcessingProjection[] {
|
|
if (!Array.isArray(raw)) return [];
|
|
return raw
|
|
.map(normalizeShippingProcessingProjection)
|
|
.filter((item): item is ShippingProcessingProjection => Boolean(item));
|
|
}
|