This commit is contained in:
2026-08-13 19:00:33 +08:00
parent 8ba04fc87d
commit cca1bc002e
11 changed files with 253 additions and 80 deletions
+98 -9
View File
@@ -9,6 +9,10 @@ const BOXES = [
{ boxType: 'EXTRA_LARGE', lengthCm: 60, widthCm: 50, heightCm: 50 },
]
const MEASURABLE_STATUSES = new Set(['WAIT_MEASURE', 'MEASURING', 'MEASURE_REJECTED'])
const PRICING_PROBE_CONCURRENCY = 6
const ORDER_ESTIMATE_WEIGHT_KG = BOXES.reduce((total, _box, index) => total + 10 + index, 0)
const ORDER_ESTIMATE_VOLUME_CBM = BOXES.reduce((total, box) =>
total + box.lengthCm * box.widthCm * box.heightCm / 1_000_000, 0)
function id(value) { return value == null ? '' : String(value) }
function records(value) { return Array.isArray(value?.records) ? value.records : [] }
@@ -29,6 +33,77 @@ function tomorrow(days = 30) {
return date.toISOString().slice(0, 10)
}
function pricingEstimateRequest(receiving, channel, pricingTaxMode, destination) {
return {
originWarehouseId: receiving.id,
channelCode: channel.channelCode,
destinationCode: destination.warehouseCode,
destinationType: 'FBA',
destinationWarehouseId: destination.id,
destinationName: destination.warehouseName,
pricingTaxMode,
actualWeightKg: ORDER_ESTIMATE_WEIGHT_KG,
actualVolumeCbm: ORDER_ESTIMATE_VOLUME_CBM,
}
}
function estimateMatches(response, candidate) {
const estimate = success(response) ? response.body?.data : null
return Boolean(estimate)
&& id(estimate.priceBookVersionId) === id(candidate.channel.priceBookVersionId)
&& id(estimate.channelCode) === id(candidate.channel.channelCode)
&& id(estimate.pricingTaxMode) === id(candidate.pricingTaxMode)
}
export async function findPricedOrderRoute({ receiving, destinations, channels, estimate,
concurrency = PRICING_PROBE_CONCURRENCY }) {
const candidates = []
for (const channel of channels) {
if (!channel?.priceBookVersionId || !channel?.channelCode || !Array.isArray(channel.pricingTaxModes)) continue
for (const pricingTaxMode of channel.pricingTaxModes) {
if (!pricingTaxMode) continue
for (const destination of destinations) {
if (!destination?.id || !destination?.warehouseCode) continue
candidates.push({ channel, pricingTaxMode, destination,
request: pricingEstimateRequest(receiving, channel, pricingTaxMode, destination) })
}
}
}
const failures = []
let lastResponse = null
const batchSize = Math.max(1, Math.min(Number(concurrency) || 1, PRICING_PROBE_CONCURRENCY))
let attempts = 0
for (let offset = 0; offset < candidates.length; offset += batchSize) {
const batch = candidates.slice(offset, offset + batchSize)
const results = await Promise.all(batch.map(async (candidate) => ({
candidate, response: await estimate(candidate.request),
})))
attempts += results.length
lastResponse = results.at(-1)?.response || lastResponse
const match = results.find(({ candidate, response }) => estimateMatches(response, candidate))
if (match) return { priced: match.candidate, response: match.response, attempts, failures }
for (const { candidate, response } of results) {
const returned = success(response) ? response.body?.data : null
failures.push({
tuple: `${candidate.channel.channelCode}/${candidate.pricingTaxMode}/${candidate.destination.warehouseCode}`,
http: response?.http ?? 'missing', code: response?.body?.code ?? 'missing',
requestId: response?.requestId ?? 'missing', traceId: response?.traceId ?? 'missing',
mismatch: returned
? `version=${id(returned.priceBookVersionId) || 'missing'},channel=${id(returned.channelCode) || 'missing'},tax=${id(returned.pricingTaxMode) || 'missing'}`
: null,
})
}
}
return { priced: null, response: lastResponse, attempts, failures }
}
function pricingFailureEvidence(result) {
const describe = (failure) => failure
? `${failure.tuple}[HTTP=${failure.http},code=${failure.code},requestId=${failure.requestId},traceId=${failure.traceId}${failure.mismatch ? `,response=${failure.mismatch}` : ''}]`
: 'missing'
return `没有可计价组合;attempts=${result.attempts}, first=${describe(result.failures[0])}, last=${describe(result.failures.at(-1))}`
}
async function request(context, method, path, body, token, extraHeaders = {}) {
const headers = { Accept: 'application/json', Authorization: `Bearer ${token}`, ...extraHeaders }
let payload = body
@@ -96,19 +171,32 @@ async function loadMasterData(context, merchantToken) {
const fbaPage = context.apiSuccess(MODULE, 'Client 查询 FBA 目的仓', fbaResponse)
context.assert(MODULE, '存在可用 FBA 目的仓', fbaResponse, records(fbaPage).length > 0,
`count=${records(fbaPage).length}`)
const receiving = records(domesticPage)[0]; const destination = records(fbaPage)[0]
if (!receiving || !destination) throw new Error('缺少创建测试订单所需的收货仓或目的仓')
const receiving = records(domesticPage)[0]
const destinations = records(fbaPage)
if (!receiving || destinations.length === 0) throw new Error('缺少创建测试订单所需的收货仓或目的仓')
const channelResponse = await request(context, 'GET',
`/api/merchant/v1/first-leg/pricing/channels?originWarehouseId=${encodeURIComponent(receiving.id)}`,
undefined, merchantToken)
const channels = context.apiSuccess(MODULE, 'Client 查询可用计费渠道', channelResponse)
const channel = Array.isArray(channels)
? channels.find((item) => Array.isArray(item.pricingTaxModes) && item.pricingTaxModes.length > 0)
: null
context.assert(MODULE, '存在带计价税制的可用渠道', channelResponse, Boolean(channel),
const priceableChannels = Array.isArray(channels)
? channels.filter((item) => Array.isArray(item.pricingTaxModes) && item.pricingTaxModes.length > 0)
: []
context.assert(MODULE, '存在带计价税制的可用渠道', channelResponse, priceableChannels.length > 0,
`count=${Array.isArray(channels) ? channels.length : 'n/a'}`)
if (!channel) throw new Error('缺少创建测试订单所需的可用渠道')
if (priceableChannels.length === 0) throw new Error('缺少创建测试订单所需的可用渠道')
const pricingResult = await findPricedOrderRoute({
receiving, destinations, channels: priceableChannels,
estimate: (body) => request(context, 'POST', '/api/merchant/v1/first-leg/pricing/estimate', body, merchantToken),
})
const priced = pricingResult.priced
context.addStep(MODULE, 'Client 选择具有真实价格覆盖的渠道和目的仓', pricingResult.response, Boolean(priced),
priced
? `origin=${receiving.id}, priceBookVersionId=${priced.channel.priceBookVersionId}, channel=${priced.channel.channelCode}, pricingTaxMode=${priced.pricingTaxMode}, destination=${priced.destination.warehouseCode}, attempts=${pricingResult.attempts}`
: pricingFailureEvidence(pricingResult),
CONTRACT_OPTIONS)
if (!priced) throw new Error('当前起运仓、渠道、报价税制和FBA目的仓没有可计价组合')
const partnerResponse = await request(context, 'GET',
'/api/merchant/v1/partners/candidates?type=SHIPPER', undefined, merchantToken)
@@ -116,7 +204,8 @@ async function loadMasterData(context, merchantToken) {
const partner = Array.isArray(partners)
? partners.find((item) => item.status === 'ACTIVE' && item.types?.includes?.('SHIPPER'))
: null
return { receiving, destination, channel, partner }
return { receiving, destination: priced.destination, channel: priced.channel,
pricingTaxMode: priced.pricingTaxMode, partner }
}
function buildPayload(context, profile, master, productImageFileId) {
@@ -135,7 +224,7 @@ function buildPayload(context, profile, master, productImageFileId) {
receivingWarehouseName: master.receiving.warehouseName,
channelCode: master.channel.channelCode, channelName: master.channel.channelName,
carrierId: null, carrierName: null, taxMode: 'BUY_DECLARATION',
pricingTaxMode: master.channel.pricingTaxModes[0], containerMode: 'LCL',
pricingTaxMode: master.pricingTaxMode, containerMode: 'LCL',
customsInspection: false, destinationType: 'FBA',
amazonWarehouseCode: master.destination.warehouseCode,
shipmentId, referenceId,