fix(firstleg): persist ocean container allocation
This commit is contained in:
@@ -5,26 +5,6 @@ const TRUCK = '/api/platform/v1/truck-appointments'
|
||||
|
||||
function id(value) { return value == null ? '' : String(value) }
|
||||
function codeOf(response) { return response?.body?.code == null ? 'missing' : String(response.body.code) }
|
||||
async function request(context, method, path, body, headers = {}) {
|
||||
try {
|
||||
const response = await fetch(`${context.options.gateway}${path}`, {
|
||||
method,
|
||||
headers: {
|
||||
Accept: 'application/json', Authorization: `Bearer ${context.token}`,
|
||||
...(body === undefined ? {} : { 'Content-Type': 'application/json; charset=utf-8' }), ...headers,
|
||||
},
|
||||
body: body === undefined ? undefined : JSON.stringify(body), signal: AbortSignal.timeout(60_000),
|
||||
})
|
||||
const raw = await response.text(); let payload = null
|
||||
try { payload = raw ? JSON.parse(raw) : null } catch { /* bounded raw evidence */ }
|
||||
return { method, path, http: response.status, body: payload,
|
||||
raw: payload === null ? raw.slice(0, 500) : null,
|
||||
requestId: payload?.requestId || response.headers.get('x-request-id'),
|
||||
traceId: payload?.traceId || response.headers.get('x-trace-id') }
|
||||
} catch (error) {
|
||||
return { method, path, http: 0, body: null, raw: String(error), requestId: null, traceId: null }
|
||||
}
|
||||
}
|
||||
|
||||
export async function run(context) {
|
||||
const ocean = context.createdResources?.oceanBooking
|
||||
@@ -46,26 +26,15 @@ export async function run(context) {
|
||||
&& id(containerFlow?.tenantId) === customerId
|
||||
&& Boolean(containerFlow?.containerBookingId)
|
||||
&& Boolean(containerFlow?.loadingPlanId)
|
||||
let oceanVersion = Number(oceanDetail?.oceanBooking?.version)
|
||||
if (containerFlow && !linkContainer) {
|
||||
context.addStep(MODULE, '本轮柜次与订舱客户归属可串联', oceanResponse, false,
|
||||
`oceanCustomerId=${customerId}, containerTenantId=${id(containerFlow?.tenantId) || 'missing'}, containerStatus=${containerFlow?.finalStatus || 'missing'}`, OPTIONS)
|
||||
return
|
||||
}
|
||||
if (linkContainer) {
|
||||
const requirement = oceanDetail?.containerRequirements?.find?.((item) => item?.containerType === '40HQ')
|
||||
context.assert(MODULE, '订舱存在匹配本轮柜型的箱型需求', oceanResponse,
|
||||
Boolean(requirement?.id), `requirementId=${id(requirement?.id) || 'missing'}`, OPTIONS)
|
||||
if (!requirement?.id || !Number.isInteger(oceanVersion)) return
|
||||
const allocationResponse = await request(context, 'POST', `${OCEAN}/${ocean.id}/allocations`, {
|
||||
requirementId: id(requirement.id), containerBookingId: id(containerFlow.containerBookingId), expectedVersion: oceanVersion,
|
||||
}, { 'Idempotency-Key': `${context.marker}-flow-container-allocation`.slice(0, 128) })
|
||||
const allocated = context.apiSuccess(MODULE, '将本轮柜次分配至已放舱订舱', allocationResponse, '', OPTIONS)
|
||||
oceanVersion = Number(allocated?.oceanBooking?.version)
|
||||
context.assert(MODULE, '订舱分配保留柜次关联并递增版本', allocationResponse,
|
||||
allocated?.containerAllocations?.some?.((item) => id(item?.containerBookingId) === id(containerFlow.containerBookingId))
|
||||
&& Number.isInteger(oceanVersion),
|
||||
`containerBookingId=${id(containerFlow.containerBookingId)}, version=${oceanVersion}`, OPTIONS)
|
||||
context.assert(MODULE, '已放舱订舱保留本轮柜次分配', oceanResponse,
|
||||
oceanDetail?.containerAllocations?.some?.((item) => id(item?.containerBookingId) === id(containerFlow.containerBookingId)),
|
||||
`containerBookingId=${id(containerFlow.containerBookingId)}, allocations=${oceanDetail?.containerAllocations?.length ?? 'n/a'}`, OPTIONS)
|
||||
}
|
||||
const payload = {
|
||||
outboundOrderId: linkContainer ? id(containerFixture?.orderId) || null : null,
|
||||
|
||||
Reference in New Issue
Block a user