# Conflicts:
#	scripts/admin-test-env-modules/phase4-carrier-nodes.mjs
This commit is contained in:
2026-09-02 14:46:15 +08:00
9 changed files with 73 additions and 14 deletions
@@ -17,8 +17,8 @@ async function post(context, path, body, operation) {
return context.request('POST', path, body, context.token, { 'Idempotency-Key': key(context, operation) })
}
async function uploadBillOfLading(context, appointmentId, index) {
const path = `${TRUCK_BASE}/${appointmentId}/attachments/upload?attachmentType=BILL_OF_LADING`
async function uploadDispatchOrder(context, appointmentId, index) {
const path = `${TRUCK_BASE}/${appointmentId}/attachments/upload?attachmentType=DISPATCH_ORDER`
const form = new FormData()
const png = Uint8Array.from([137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,1,0,0,0,1,8,6,0,0,0,31,21,196,137,0,0,0,13,73,68,65,84,8,215,99,248,207,192,240,31,0,5,0,1,255,137,153,61,29,0,0,0,0,73,69,78,68,174,66,96,130])
form.append('file', new Blob([png], { type: 'image/png' }), `${context.marker}-truck-${index}.png`)
@@ -77,10 +77,30 @@ async function oceanAction(context, value, endpoint, body = {}) {
return next
}
async function uploadBookingConfirmation(context, value, index) {
const bookingId = id(value?.oceanBooking?.id)
const form = new FormData()
form.append('expectedVersion', String(Number(value?.oceanBooking?.version)))
form.append('documentType', 'BOOKING_CONFIRMATION')
form.append('file', new Blob([`${context.marker}-BOOKING_CONFIRMATION-${index}`],
{ type: 'text/plain' }), `booking-confirmation-${suffix(context, index)}.txt`)
const response = await context.requestMultipart('POST',
`${OCEAN_BASE}/${bookingId}/documents/upload`, form, context.token,
{ 'Idempotency-Key': key(context, `ocean-booking-document-${index}`) })
const document = context.apiSuccess(MODULE, `订舱样本 ${index} 上传订舱确认文件`, response, '', OPTIONS)
if (!Number.isInteger(Number(document?.bookingVersion))) {
throw new Error(`订舱样本 ${index} 未返回有效文件版本`)
}
return { ...value, oceanBooking: { ...value.oceanBooking, version: Number(document.bookingVersion) } }
}
async function buildOcean(context, template, status, index) {
let value = await createOcean(context, template, index)
if (status === 'DRAFT') return value
if (status === 'RELEASED') return oceanAction(context, value, 'release')
if (status === 'RELEASED') {
value = await uploadBookingConfirmation(context, value, index)
return oceanAction(context, value, 'release')
}
throw new Error(`unsupported ocean status ${status}`)
}
@@ -107,7 +127,7 @@ async function createTruck(context, oceanBooking, index) {
const response = await context.request('POST', TRUCK_BASE, truckPayload(context, oceanBooking, carrier, index))
const value = context.apiSuccess(MODULE, `创建拖车状态样本 ${index}`, response, '', OPTIONS)
if (!value?.id) throw new Error(`创建拖车样本 ${index} 失败`)
const billUpload = await uploadBillOfLading(context, id(value.id), index)
const billUpload = await uploadDispatchOrder(context, id(value.id), index)
const billAttachment = context.apiSuccess(MODULE, `拖车状态样本 ${index} 上传提单附件`, billUpload, '', OPTIONS)
if (!billAttachment?.id) throw new Error(`拖车样本 ${index} 未上传有效提单附件`)
return value
@@ -133,9 +133,18 @@ function routes(context, merchantToken) {
}
export async function prepare(context) {
const outerMarker = context.marker
const restore = await context.preparePhase3LoadingFixture('-CUSTOMS-MIXED', false, {
primary: 'BUY_DECLARATION', incremental: 'TAX_REFUND',
})
const executionMarker = `${outerMarker}-CUSTOMS-MIXED`
context.marker = executionMarker
if (context.createdResources?.orchestrationFixtures?.phase3Loading) {
context.createdResources.orchestrationFixtures.phase3Loading.marker = executionMarker
}
if (context.createdResources?.orchestrationFixtures?.phase3LoadingIncremental) {
context.createdResources.orchestrationFixtures.phase3LoadingIncremental.marker = executionMarker
}
const loading = await import('./phase3-loading.mjs')
await context.executeModule(`${MODULE}-loading`, () => loading.run(context))
const finalize = await import('./phase3-loading-finalize.mjs')
@@ -143,6 +152,7 @@ export async function prepare(context) {
const source = context.createdResources?.phase3Loading
if (!source?.loadingPlanId || !source?.containerBookingId || source?.finalStatus !== 'LOADED') {
context.marker = outerMarker
restore()
throw new Error(`混合柜未进入LOADED: planId=${id(source?.loadingPlanId) || 'missing'}, status=${source?.finalStatus || 'missing'}`)
}
@@ -157,6 +167,7 @@ export async function prepare(context) {
buy.length > 0 && refund.length > 0 && inbounds.every((item) => /^\d{16,}$/.test(id(item.inboundId))),
`buy=${buy.length}, tax=${refund.length}, inbounds=${inbounds.length}`, OPTIONS)
if (!buy.length || !refund.length) {
context.marker = outerMarker
restore()
throw new Error(`正式装柜API未形成混合柜: buy=${buy.length}, tax=${refund.length}`)
}
@@ -178,10 +189,11 @@ export async function prepare(context) {
const buyInboundIds = new Set(buy.map((item) => id(item.inboundId)))
const planBuyRows = planItems.filter((item) => buyInboundIds.has(id(item.inboundId))).length
const expectedBuyRows = planBuyRows > 0 ? planBuyRows : buy.length
context.marker = outerMarker
context.createdResources.phase3ContainerCustomsFixtures = {
marker: context.marker, requestedKinds: ['MIXED'],
marker: outerMarker, requestedKinds: ['MIXED'],
containers: [{
kind: 'MIXED', marker: context.marker, status: 'LOADED', autoDispatchMode: 'IMMEDIATE',
kind: 'MIXED', marker: outerMarker, status: 'LOADED', autoDispatchMode: 'IMMEDIATE',
loadingPlanId: id(source.loadingPlanId), containerBookingId: id(source.containerBookingId),
oceanBookingId: id(source.oceanBookingId), containerNo: source.containerNo,
inbounds: inbounds.map((item) => ({
@@ -194,6 +206,7 @@ export async function prepare(context) {
context.context.phase3ContainerCustomsRoutes = routes(context, merchantToken)
return () => {
delete context.context.phase3ContainerCustomsRoutes
context.marker = outerMarker
restore()
}
}
@@ -251,7 +251,7 @@ export async function run(context) {
const warehouseRemarkPath = `${smartLoadingBase}/plans/${loadingPlanId}/items/${id(assignedItem.id)}/warehouse-remark`
const warehouseRemarkBody = Object.freeze({ warehouseRemark,
expectedPlanVersion: Number(assignedLoadingPlan.version), expectedItemVersion: Number(assignedItem.version),
operationId: `${context.marker}-warehouse-remark`.slice(0, 128) })
operationId: `${context.marker}-${loadingPlanId}-warehouse-remark`.slice(0, 128) })
const remarkResponse = await request(context, 'PATCH', warehouseRemarkPath, warehouseRemarkBody)
const remarkedPlan = context.apiSuccess(MODULE, 'EDITING 装柜明细保存仓库备注', remarkResponse, '', OPTIONS)
const remarkedItem = remarkedPlan?.items?.find?.((item) => id(item.id) === id(assignedItem.id))
@@ -274,7 +274,7 @@ export async function run(context) {
const staleRemarkResponse = await request(context, 'PATCH',
`${smartLoadingBase}/plans/${loadingPlanId}/items/${id(assignedItem.id)}/warehouse-remark`, {
warehouseRemark: `${warehouseRemark}-STALE`, expectedPlanVersion: Number(assignedLoadingPlan.version),
expectedItemVersion: Number(assignedItem.version), operationId: `${context.marker}-warehouse-remark-stale`.slice(0, 128),
expectedItemVersion: Number(assignedItem.version), operationId: `${context.marker}-${loadingPlanId}-warehouse-remark-stale`.slice(0, 128),
})
context.addStep(MODULE, '仓库备注旧版本 A0409 且零写入', staleRemarkResponse,
staleRemarkResponse.http === 200 && codeOf(staleRemarkResponse) === 'A0409',
@@ -290,7 +290,7 @@ export async function run(context) {
const clearRemarkResponse = await request(context, 'PATCH',
`${smartLoadingBase}/plans/${loadingPlanId}/items/${id(remarkedItem.id)}/warehouse-remark`, {
warehouseRemark: ' ', expectedPlanVersion: Number(remarkedPlan.version),
expectedItemVersion: Number(remarkedItem.version), operationId: `${context.marker}-warehouse-remark-clear`.slice(0, 128),
expectedItemVersion: Number(remarkedItem.version), operationId: `${context.marker}-${loadingPlanId}-warehouse-remark-clear`.slice(0, 128),
})
const clearedPlan = context.apiSuccess(MODULE, 'EDITING 装柜明细空白仓库备注归一化为 null', clearRemarkResponse, '', OPTIONS)
const clearedItem = clearedPlan?.items?.find?.((item) => id(item.id) === id(remarkedItem.id))
@@ -302,7 +302,7 @@ export async function run(context) {
const restoreRemarkResponse = await request(context, 'PATCH',
`${smartLoadingBase}/plans/${loadingPlanId}/items/${id(clearedItem.id)}/warehouse-remark`, {
warehouseRemark, expectedPlanVersion: Number(clearedPlan.version), expectedItemVersion: Number(clearedItem.version),
operationId: `${context.marker}-warehouse-remark-restore`.slice(0, 128),
operationId: `${context.marker}-${loadingPlanId}-warehouse-remark-restore`.slice(0, 128),
})
const persistedRemarkPlan = context.apiSuccess(MODULE, '恢复真实仓库备注供装柜完成事件下发', restoreRemarkResponse, '', OPTIONS)
if (!Number.isInteger(Number(persistedRemarkPlan?.version))) return
@@ -79,6 +79,22 @@ export async function run(context) {
&& gateItems.every((item) => !['TRUCK_DEPARTED', 'SAILING_SCHEDULE'].includes(item.code)),
`truck=${aggregate?.truckAppointmentStatus || 'none'}, ocean=${aggregate?.oceanBookingStatus || 'none'}`, OPTIONS)
if (aggregate?.dispatchStatus === 'WAITING_FOR_LOADING') {
context.assert(MODULE, '整柜报关完成后系统已自动确认国内离仓', detailResponse,
aggregate?.inbounds?.every?.((item) => item?.inboundStatus === 'LOADED')
&& detail?.timeline?.some?.((item) => item.module === 'OUTBOUND_DISPATCH'
&& item.action === 'CONFIRM_DISPATCH'),
`dispatch=${aggregate.dispatchStatus}, inbounds=${aggregate?.inbounds?.length ?? 'missing'}`, OPTIONS)
context.createdResources.phase3OutboundDispatch = {
marker: context.marker, inboundId, containerBookingId,
orderId: id(inboundBinding?.orderId), tenantId: id(inboundBinding?.tenantId),
confirmedStatus: aggregate.dispatchStatus, confirmedInboundStatus: 'LOADED',
confirmedVersion: Number(aggregate.version), finalStatus: aggregate.dispatchStatus,
finalInboundStatus: 'LOADED', finalVersion: Number(aggregate.version),
}
return
}
const expectedVersion = Number(aggregate.version)
const confirmKey = `${context.marker}-outbound-confirm`.slice(0, 128)
if (!gate?.passed) {
@@ -257,7 +257,7 @@ async function awaitProducerConsumption(context, producers) {
if (!expected.length) return true
const consumed = new Set()
const lastResponses = new Map()
for (let attempt = 1; attempt <= 30; attempt += 1) {
for (let attempt = 1; attempt <= 90; attempt += 1) {
const missing = expected.filter((containerNo) => !consumed.has(containerNo))
const results = await Promise.all(missing.map(async (containerNo) => {
const response = await context.requestOverseas('GET',