This commit is contained in:
2026-08-13 18:34:44 +08:00
parent f779ab56dd
commit 8cf6709bde
6 changed files with 127 additions and 14 deletions
+32 -13
View File
@@ -16,6 +16,14 @@ function pageOk(value) { return Array.isArray(value?.records) && Number.isFinite
function success(response) { return response.http === 200 && String(response.body?.code) === '200' }
function sleep(milliseconds) { return new Promise((resolve) => setTimeout(resolve, milliseconds)) }
function key(context, operation) { return `${context.marker}-${operation}`.slice(0, 128) }
export function approvedInboundReady(orderDetail, taskResponse, tenantId, orderId) {
const summary = orderDetail?.summary
const task = success(taskResponse) ? taskResponse.body?.data : null
return Boolean(summary?.inboundId)
&& MEASURABLE_STATUSES.has(summary?.mainStatus)
&& id(task?.orderId) === id(orderId)
&& id(task?.tenantId) === id(tenantId)
}
function tomorrow(days = 30) {
const date = new Date(); date.setUTCDate(date.getUTCDate() + days)
return date.toISOString().slice(0, 10)
@@ -202,18 +210,27 @@ async function assertDimensionConfig(context, tenantId) {
async function waitForInbound(context, tenantId, orderId) {
const path = `/api/platform/v1/orders/${encodeURIComponent(orderId)}?tenantId=${encodeURIComponent(tenantId)}`
let response = null; let detail = null
for (let attempt = 1; attempt <= 20; attempt += 1) {
const taskPath = `/api/platform/v1/first-leg/tasks/by-order/${encodeURIComponent(orderId)}?tenantId=${encodeURIComponent(tenantId)}`
let response = null; let detail = null; let taskResponse = null; let task = null
for (let attempt = 1; attempt <= 30; attempt += 1) {
response = await context.request('GET', path)
if (success(response) && response.body?.data?.summary?.inboundId) {
detail = response.body.data
const candidate = success(response) ? response.body?.data : null
if (candidate?.summary?.inboundId) {
taskResponse = await context.request('GET', taskPath)
}
if (approvedInboundReady(candidate, taskResponse, tenantId, orderId)) {
detail = candidate
task = taskResponse.body.data
break
}
if (attempt < 20) await sleep(1_000)
if (attempt < 30) await sleep(1_000)
}
context.addStep(MODULE, '审核后生成隔离入库单', response, Boolean(detail),
detail ? `inboundId=${detail.summary.inboundId}` : '20秒内未返回 inboundId', CONTRACT_OPTIONS)
return { response, detail }
context.addStep(MODULE, '审核后入库单与履约任务异步收敛', response, Boolean(detail && task),
detail
? `inboundId=${detail.summary.inboundId}, status=${detail.summary.mainStatus}, taskId=${id(task?.id)}`
: `30秒内未收敛;status=${response?.body?.data?.summary?.mainStatus ?? 'missing'}, taskCode=${taskResponse?.body?.code ?? 'not-requested'}`,
CONTRACT_OPTIONS)
return { response, detail, taskResponse, task }
}
export async function run(context) {
@@ -320,7 +337,9 @@ export async function run(context) {
const inboundResult = await waitForInbound(context, tenantId, orderId)
const inboundId = id(inboundResult.detail?.summary?.inboundId)
if (!inboundId) throw new Error('订单审核后的入库单契约未部署,无法向 OCR/墨家模块交接')
if (!inboundId || !inboundResult.task) {
throw new Error('订单审核后的入库单与履约任务未在30秒内收敛,无法向 OCR/墨家模块交接')
}
const inboundPath = `/api/platform/inbounds/${encodeURIComponent(inboundId)}?tenantId=${encodeURIComponent(tenantId)}`
const inboundResponse = await context.request('GET', inboundPath)
@@ -358,10 +377,10 @@ export async function run(context) {
context.context.testInbound = context.context.inboundWeightOcr
context.context.createdInbound = context.context.inboundWeightOcr
const taskResponse = await context.request('GET',
`/api/platform/v1/first-leg/tasks/by-order/${encodeURIComponent(orderId)}?tenantId=${encodeURIComponent(tenantId)}`)
const task = context.apiSuccess(MODULE, 'Admin 查询本轮订单履约任务', taskResponse,
'新订单应生成可查询的履约任务', CONTRACT_OPTIONS)
const taskResponse = inboundResult.taskResponse
const task = inboundResult.task
context.apiSuccess(MODULE, 'Admin 查询本轮订单履约任务', taskResponse,
'审批 Outbox 收敛后应生成可查询的履约任务', CONTRACT_OPTIONS)
context.assert(MODULE, '履约任务与本轮订单和客户一致', taskResponse,
id(task?.orderId) === orderId && id(task?.tenantId) === tenantId,
`taskId=${id(task?.id) || 'missing'}, status=${task?.status ?? 'missing'}`, CONTRACT_OPTIONS)