优化
This commit is contained in:
@@ -97,6 +97,34 @@ export async function findPricedOrderRoute({ receiving, destinations, channels,
|
||||
return { priced: null, response: lastResponse, attempts, failures }
|
||||
}
|
||||
|
||||
export async function findPricedOrderRouteAcrossOrigins({ receivings, destinations, loadChannels, estimate,
|
||||
concurrency = PRICING_PROBE_CONCURRENCY }) {
|
||||
const originFailures = []
|
||||
let attempts = 0
|
||||
let response = null
|
||||
for (const receiving of receivings) {
|
||||
if (!receiving?.id) continue
|
||||
const channelResponse = await loadChannels(receiving)
|
||||
response = channelResponse || response
|
||||
const channels = success(channelResponse) && Array.isArray(channelResponse.body?.data)
|
||||
? channelResponse.body.data : []
|
||||
const priceableChannels = channels.filter((item) => Array.isArray(item.pricingTaxModes) && item.pricingTaxModes.length > 0)
|
||||
if (priceableChannels.length === 0) {
|
||||
originFailures.push({ originWarehouseId: id(receiving.id), stage: 'channels', http: channelResponse?.http ?? 'missing',
|
||||
code: channelResponse?.body?.code ?? 'missing', requestId: channelResponse?.requestId ?? 'missing',
|
||||
traceId: channelResponse?.traceId ?? 'missing', count: channels.length })
|
||||
continue
|
||||
}
|
||||
const result = await findPricedOrderRoute({ receiving, destinations, channels: priceableChannels, estimate, concurrency })
|
||||
attempts += result.attempts
|
||||
response = result.response || response
|
||||
if (result.priced) return { receiving, ...result, originFailures }
|
||||
originFailures.push({ originWarehouseId: id(receiving.id), stage: 'estimate', attempts: result.attempts,
|
||||
evidence: pricingFailureEvidence(result) })
|
||||
}
|
||||
return { receiving: null, priced: null, response, attempts, originFailures }
|
||||
}
|
||||
|
||||
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}` : ''}]`
|
||||
@@ -104,6 +132,14 @@ function pricingFailureEvidence(result) {
|
||||
return `没有可计价组合;attempts=${result.attempts}, first=${describe(result.failures[0])}, last=${describe(result.failures.at(-1))}`
|
||||
}
|
||||
|
||||
function originPricingFailureEvidence(result) {
|
||||
const describe = (failure) => !failure ? 'missing'
|
||||
: failure.stage === 'channels'
|
||||
? `origin=${failure.originWarehouseId},channels[HTTP=${failure.http},code=${failure.code},count=${failure.count},requestId=${failure.requestId},traceId=${failure.traceId}]`
|
||||
: `origin=${failure.originWarehouseId},estimate[attempts=${failure.attempts},${failure.evidence}]`
|
||||
return `没有 ACTIVE 国内仓可计价组合;origins=${result.originFailures?.length ?? 0}, attempts=${result.attempts}, first=${describe(result.originFailures?.[0])}, last=${describe(result.originFailures?.at(-1))}`
|
||||
}
|
||||
|
||||
async function request(context, method, path, body, token, extraHeaders = {}) {
|
||||
const headers = { Accept: 'application/json', Authorization: `Bearer ${token}`, ...extraHeaders }
|
||||
let payload = body
|
||||
@@ -171,32 +207,25 @@ 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 receivings = records(domesticPage).filter((warehouse) => warehouse?.status === 'ACTIVE')
|
||||
const destinations = records(fbaPage)
|
||||
if (!receiving || destinations.length === 0) throw new Error('缺少创建测试订单所需的收货仓或目的仓')
|
||||
if (receivings.length === 0 || 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 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 (priceableChannels.length === 0) throw new Error('缺少创建测试订单所需的可用渠道')
|
||||
|
||||
const pricingResult = await findPricedOrderRoute({
|
||||
receiving, destinations, channels: priceableChannels,
|
||||
const pricingResult = await findPricedOrderRouteAcrossOrigins({
|
||||
receivings, destinations,
|
||||
loadChannels: (receiving) => request(context, 'GET',
|
||||
`/api/merchant/v1/first-leg/pricing/channels?originWarehouseId=${encodeURIComponent(receiving.id)}`,
|
||||
undefined, merchantToken),
|
||||
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),
|
||||
const receiving = pricingResult.receiving
|
||||
context.addStep(MODULE, 'Client 从 ACTIVE 国内仓选择具有真实价格覆盖的渠道和目的仓', 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),
|
||||
: originPricingFailureEvidence(pricingResult),
|
||||
CONTRACT_OPTIONS)
|
||||
if (!priced) throw new Error('当前起运仓、渠道、报价税制和FBA目的仓没有可计价组合')
|
||||
if (!priced) throw new Error('当前 ACTIVE 国内收货仓、渠道、报价税制和FBA目的仓没有可计价组合')
|
||||
|
||||
const partnerResponse = await request(context, 'GET',
|
||||
'/api/merchant/v1/partners/candidates?type=SHIPPER', undefined, merchantToken)
|
||||
|
||||
Reference in New Issue
Block a user