87 lines
5.0 KiB
JavaScript
87 lines
5.0 KiB
JavaScript
const MODULE = 'phase4-carrier-nodes'
|
|
const OPTIONS = { scenario: 'phase4-carrier-nodes-lifecycle', deploymentBlocker: true }
|
|
const BASE = '/api/platform/v1/carrier-schedule-queries'
|
|
|
|
export const QUERY_STATUSES = Object.freeze(['READY', 'MANUAL_REQUIRED', 'ARRIVED', 'PICKUP_READY', 'STOPPED'])
|
|
export const STANDARD_NODES = Object.freeze([
|
|
'DEPARTED_WAREHOUSE', 'AT_ORIGIN_PORT', 'DEPARTED_ORIGIN_PORT',
|
|
'IN_TRANSIT', 'ARRIVED', 'PICKUP_READY',
|
|
])
|
|
|
|
function id(value) { return value == null ? '' : String(value) }
|
|
function number(value) { return Number.isFinite(Number(value)) }
|
|
function nullableText(value) { return value == null || typeof value === 'string' }
|
|
function nullableId(value) { return value == null || Boolean(id(value)) }
|
|
function nullableIso(value) { return value == null || (typeof value === 'string' && !Number.isNaN(Date.parse(value))) }
|
|
|
|
function validTask(task) {
|
|
return Boolean(id(task?.id) && task?.queryNo && id(task?.customerId) && id(task?.oceanBookingId))
|
|
&& QUERY_STATUSES.includes(task?.status)
|
|
&& nullableId(task?.containerBookingId)
|
|
&& nullableText(task?.latestStandardNode)
|
|
&& (task?.latestStandardNode == null || STANDARD_NODES.includes(task.latestStandardNode))
|
|
&& ['latestNodeObservedAt', 'lastSuccessAt', 'sourceUpdatedAt', 'expiresAt', 'stoppedAt']
|
|
.every((field) => nullableIso(task?.[field]))
|
|
&& nullableText(task?.stopReason)
|
|
}
|
|
|
|
function validNode(node) {
|
|
return Boolean(id(node?.id) && node?.providerCode && node?.providerNodeCode)
|
|
&& STANDARD_NODES.includes(node?.standardNode)
|
|
&& number(node?.nodeSequence) && Number(node.nodeSequence) > 0
|
|
&& nullableIso(node?.observedAt) && node?.observedAt != null
|
|
&& nullableIso(node?.estimatedAt) && nullableIso(node?.actualAt)
|
|
&& nullableText(node?.locationCode) && nullableText(node?.locationName)
|
|
&& nullableText(node?.sourceReference) && nullableIso(node?.createdAt)
|
|
}
|
|
|
|
function validHistory(item) {
|
|
return Boolean(id(item?.id) && item?.action)
|
|
&& (item?.fromVersion == null || number(item.fromVersion))
|
|
&& (item?.toVersion == null || number(item.toVersion))
|
|
&& nullableText(item?.reason) && nullableId(item?.operatorId)
|
|
&& nullableText(item?.operatorName) && nullableIso(item?.createdAt)
|
|
}
|
|
|
|
export async function run(context) {
|
|
const carriersResponse = await context.request('GET', `${BASE}/carriers`)
|
|
const carriers = context.apiSuccess(MODULE, '查询船司只读辅助信息', carriersResponse, '', OPTIONS)
|
|
context.assert(MODULE, '船司辅助信息保持公开只读描述结构', carriersResponse,
|
|
Array.isArray(carriers) && carriers.every((item) => item?.carrierCode && item?.carrierName
|
|
&& nullableText(item?.officialUrl) && nullableText(item?.accessMode)
|
|
&& typeof item?.automated === 'boolean' && typeof item?.coverageGap === 'boolean'),
|
|
`count=${Array.isArray(carriers) ? carriers.length : 'missing'}`, OPTIONS)
|
|
|
|
const pageResponse = await context.request('GET', `${BASE}?page=1&size=20`)
|
|
const page = context.apiSuccess(MODULE, '查询四期船节点任务分页', pageResponse, '', OPTIONS)
|
|
const records = Array.isArray(page?.records) ? page.records : []
|
|
context.assert(MODULE, '船节点分页结构稳定', pageResponse,
|
|
Array.isArray(page?.records) && ['page', 'size', 'total', 'pages'].every((field) => number(page?.[field]))
|
|
&& records.every((item) => typeof item?.expired === 'boolean' && validTask(item?.query)),
|
|
`records=${records.length}, total=${page?.total ?? 'missing'}`, OPTIONS)
|
|
|
|
if (records.length === 0) {
|
|
context.assert(MODULE, '无真实任务时返回稳定分页空态', pageResponse,
|
|
Number(page?.total) === 0 && Number(page?.pages) === 0,
|
|
`records=0, total=${page?.total ?? 'missing'}, pages=${page?.pages ?? 'missing'}`, OPTIONS)
|
|
return
|
|
}
|
|
|
|
const queryId = id(records[0]?.query?.id)
|
|
const detailResponse = await context.request('GET', `${BASE}/${encodeURIComponent(queryId)}`)
|
|
const detail = context.apiSuccess(MODULE, '查询真实船节点任务详情', detailResponse, `queryId=${queryId}`, OPTIONS)
|
|
const nodes = Array.isArray(detail?.nodes) ? detail.nodes : []
|
|
const histories = Array.isArray(detail?.histories) ? detail.histories : []
|
|
context.assert(MODULE, '详情返回Task、标准节点与业务历史', detailResponse,
|
|
validTask(detail?.query?.query) && typeof detail?.query?.expired === 'boolean'
|
|
&& Array.isArray(detail?.nodes) && nodes.every(validNode)
|
|
&& Array.isArray(detail?.histories) && histories.every(validHistory),
|
|
`status=${detail?.query?.query?.status || 'missing'}, nodes=${nodes.length}, histories=${histories.length}`, OPTIONS)
|
|
|
|
const historyResponse = await context.request('GET', `${BASE}/${encodeURIComponent(queryId)}/history`)
|
|
const history = context.apiSuccess(MODULE, '查询真实船节点任务历史', historyResponse, `queryId=${queryId}`, OPTIONS)
|
|
context.assert(MODULE, '独立历史端点返回可审计操作者与nullable字段', historyResponse,
|
|
Array.isArray(history) && history.every(validHistory),
|
|
`history=${Array.isArray(history) ? history.length : 'missing'}`, OPTIONS)
|
|
}
|