feat(shipping-notice): 完善发货通知自动推进与多库存源复核闭环

This commit is contained in:
hugo committed 2026-08-09 11:43:53 +08:00
1 parent cfa19a0b7b
commit 8e0bbf7a81
180 files changed
+31210 -1864

No files matched your search

@@ -8,7 +8,12 @@
import type {
OrderIntakeSourceType,
ProcessGateSummary,
ShippingInventoryPerSourceAttempt,
ShippingInventoryPreparationSummary,
ShippingOrderTaskSummary,
ShippingProcessingBlockingReason,
ShippingProcessingFacts,
ShippingProcessingPrimaryAction,
ShippingProcessingProjection,
ShippingProcessingStage,
ShippingProcessingStageNode,
@@ -98,6 +103,186 @@ function normalizeStageNode(raw: unknown): ShippingProcessingStageNode | null {
};
}
/**
* [T9 用户态 Projection] 规范 blocking_reason(后端 snake_case {code, i18n_key, details})。
*/
function normalizeBlockingReason(raw: unknown): ShippingProcessingBlockingReason | null {
if (!raw || typeof raw !== 'object') return null;
const item = raw as Record<string, unknown>;
const code = String(item.code ?? '').trim();
if (!code) return null;
return {
code,
i18nKey: item.i18n_key != null ? String(item.i18n_key) : (item.i18nKey != null ? String(item.i18nKey) : null),
details:
typeof item.details === 'object' && item.details != null
? (item.details as Record<string, unknown>)
: {},
};
}
/**
* [T9 用户态 Projection] 规范 primary_action(后端 snake_case {code, i18n_key, parameters})。
*/
function normalizePrimaryAction(raw: unknown): ShippingProcessingPrimaryAction | null {
if (!raw || typeof raw !== 'object') return null;
const item = raw as Record<string, unknown>;
const code = String(item.code ?? '').trim();
if (!code) return null;
return {
code,
i18nKey: item.i18n_key != null ? String(item.i18n_key) : (item.i18nKey != null ? String(item.i18nKey) : null),
parameters:
typeof item.parameters === 'object' && item.parameters != null
? (item.parameters as Record<string, unknown>)
: {},
};
}
/**
* [T9 用户态 Projection] 规范 facts(后端 snake_case 子状态字典;camelCase key)。
*/
function normalizeFacts(raw: unknown): ShippingProcessingFacts | undefined {
if (!raw || typeof raw !== 'object') return undefined;
const item = raw as Record<string, unknown>;
const toStrArr = (value: unknown): string[] => {
if (!Array.isArray(value)) return [];
return value.map((c) => String(c ?? '').trim()).filter(Boolean);
};
const toLineArr = (value: unknown): Record<string, unknown>[] => {
if (!Array.isArray(value)) return [];
return value.filter((row): row is Record<string, unknown> => Boolean(row) && typeof row === 'object');
};
const out: ShippingProcessingFacts = {};
if ('inventory_source_status' in item || 'inventorySourceStatus' in item) {
out.inventorySourceStatus =
item.inventory_source_status != null
? String(item.inventory_source_status)
: item.inventorySourceStatus != null
? String(item.inventorySourceStatus)
: null;
}
if ('candidate_set_id' in item || 'candidateSetId' in item) {
const v = item.candidate_set_id ?? item.candidateSetId;
out.candidateSetId = v == null || v === '' ? null : Number(v);
}
if ('candidate_set_version' in item || 'candidateSetVersion' in item) {
const v = item.candidate_set_version ?? item.candidateSetVersion;
out.candidateSetVersion = v == null || v === '' ? null : Number(v);
}
if ('candidate_set_status' in item || 'candidateSetStatus' in item) {
const v = item.candidate_set_status ?? item.candidateSetStatus;
out.candidateSetStatus = v == null || v === '' ? null : String(v) as ShippingProcessingFacts['candidateSetStatus'];
}
if ('candidate_source_codes' in item || 'candidateSourceCodes' in item) {
out.candidateSourceCodes = toStrArr(item.candidate_source_codes ?? item.candidateSourceCodes);
}
if ('prepared_source_codes' in item || 'preparedSourceCodes' in item) {
out.preparedSourceCodes = toStrArr(item.prepared_source_codes ?? item.preparedSourceCodes);
}
if ('pending_source_codes' in item || 'pendingSourceCodes' in item) {
out.pendingSourceCodes = toStrArr(item.pending_source_codes ?? item.pendingSourceCodes);
}
if ('failed_source_codes' in item || 'failedSourceCodes' in item) {
out.failedSourceCodes = toStrArr(item.failed_source_codes ?? item.failedSourceCodes);
}
if ('source_attempts' in item || 'sourceAttempts' in item) {
const v = item.source_attempts ?? item.sourceAttempts;
out.sourceAttempts = typeof v === 'object' && v != null ? v as Record<string, unknown> : {};
}
if ('line_resolutions' in item || 'lineResolutions' in item) {
out.lineResolutions = toLineArr(item.line_resolutions ?? item.lineResolutions);
}
if ('export_state' in item || 'exportState' in item) {
const v = item.export_state ?? item.exportState;
out.exportState = v == null ? 'not_created' : String(v);
}
if ('upload_state' in item || 'uploadState' in item) {
const v = item.upload_state ?? item.uploadState;
out.uploadState = v == null ? 'not_created' : String(v);
}
if ('switch_state' in item || 'switchState' in item) {
const v = item.switch_state ?? item.switchState;
out.switchState = v == null ? 'not_triggered' : String(v);
}
if ('forced_match_source_code' in item || 'forcedMatchSourceCode' in item) {
const v = item.forced_match_source_code ?? item.forcedMatchSourceCode;
out.forcedMatchSourceCode = v == null || v === '' ? null : String(v).toUpperCase();
}
for (const [k, v] of Object.entries(item)) {
if (!(k in out)) {
out[k] = v;
}
}
return out;
}
/**
* 题4A:规范 inventory_preparation 结构化字段(snake_case → camelCase)。
* 注意:failed_sources 会进一步透传到 exhausted_sources 分开(二者互斥;仅 max_attempts 耗尽的源进 exhausted)。
*/
function normalizeInventoryPreparation(raw: unknown): ShippingInventoryPreparationSummary | null {
if (!raw || typeof raw !== 'object') return null;
const item = raw as Record<string, unknown>;
const toStrArr = (value: unknown): string[] => {
if (!Array.isArray(value)) return [];
return value.map((c) => String(c ?? '').trim()).filter(Boolean);
};
const requiredSources = toStrArr(item.required_sources ?? item.requiredSources);
if (!requiredSources.length) {
// required_sources 空代表 inventory_preparation 实际尚未进入 inventory 阶段,返回 null 避免前端误判。
return null;
}
const preparedSources = toStrArr(item.prepared_sources ?? item.preparedSources);
const pendingSources = toStrArr(item.pending_sources ?? item.pendingSources);
const failedSources = toStrArr(item.failed_sources ?? item.failedSources);
const exhaustedSources = toStrArr(item.exhausted_sources ?? item.exhaustedSources);
const activeAttemptCount = Number(item.active_attempt_count ?? item.activeAttemptCount ?? 0) || 0;
const gateAttemptActive = Boolean(item.gate_attempt_active ?? item.gateAttemptActive ?? false);
const latestRaw = (item.latest_attempts ?? item.latestAttempts) as Record<string, unknown> | undefined;
const latestAttempts: Record<string, ShippingInventoryPerSourceAttempt> = {};
if (latestRaw && typeof latestRaw === 'object') {
for (const [key, val] of Object.entries(latestRaw)) {
if (!val || typeof val !== 'object') continue;
const row = val as Record<string, unknown>;
const sourceCode = String(row.source_code ?? row.sourceCode ?? key ?? '').trim().toUpperCase();
if (!sourceCode) continue;
const attemptNo = Number(row.attempt_no ?? row.attemptNo ?? 0) || 0;
const status = String(row.status || '').trim().toLowerCase() || 'unknown';
const entry: ShippingInventoryPerSourceAttempt = {
sourceCode,
attemptNo,
status,
manualRetryable: (() => {
if (row.manual_retryable != null) return Boolean(row.manual_retryable);
if (row.manualRetryable != null) return Boolean(row.manualRetryable);
return ['failed', 'exhausted'].includes(status);
})(),
message:
(row.error_message as string | null | undefined) ??
(row.errorMessage as string | null | undefined) ??
(row.message as string | null | undefined) ??
null,
updatedAt:
readNullableString(row, 'updated_at', 'updatedAt') ??
readNullableString(row, 'finished_at', 'finishedAt') ??
readNullableString(row, 'started_at', 'startedAt'),
};
latestAttempts[sourceCode] = entry;
}
}
return {
requiredSources,
preparedSources,
pendingSources,
failedSources,
exhaustedSources,
activeAttemptCount,
gateAttemptActive,
latestAttempts: Object.keys(latestAttempts).length ? latestAttempts : undefined,
};
}
/**
* 将后端 / 混合字段投影规范为 ShippingProcessingProjection。
* 核心逻辑:缺字段给安全默认值,保证四页面可渲染;非法输入返回 null。
@@ -160,6 +345,23 @@ export function normalizeShippingProcessingProjection(
assetStatus: readNullableString(summaryRaw, 'asset_status', 'assetStatus'),
},
childOrderTasks: children,
// [T9 用户态 Projection 7 字段] 优先读后端 user_* / blocking_reason / retryable / primary_action / facts;
// 缺省为 null(旧后端不会下发,旧前端不读取,完全兼容)。
userStatus: readNullableString(item, 'user_status', 'userStatus') as ShippingProcessingProjection['userStatus'],
userStage: readNullableString(item, 'user_stage', 'userStage') as ShippingProcessingProjection['userStage'],
userCurrentGateCode: readNullableString(item, 'user_current_gate_code', 'userCurrentGateCode'),
blockingReason: normalizeBlockingReason(item.blocking_reason ?? item.blockingReason),
retryable:
(item.retryable as boolean | undefined) == null ? false : Boolean(item.retryable),
primaryAction: normalizePrimaryAction(item.primary_action ?? item.primaryAction),
facts: normalizeFacts(item.facts),
legacyStatus: readNullableString(item, 'legacy_status', 'legacyStatus'),
legacyStage: readNullableString(item, 'legacy_stage', 'legacyStage'),
hasFormalOt:
(item.has_formal_ot as boolean | undefined) == null ? false : Boolean(item.has_formal_ot),
inventoryPreparation: normalizeInventoryPreparation(
item.inventory_preparation ?? item.inventoryPreparation,
),
};
}