修复订单详情错误
This commit is contained in:
@@ -5,6 +5,7 @@ import { createWebhookEvent, updateWebhookEvent } from '../../repositories/webho
|
||||
import { enrichAgisoXianyuTradeOrder } from '../platforms/agiso/xianyu/order-detail-service.js'
|
||||
import { upsertOrderFromWebhook } from './order-service.js'
|
||||
import { createHttpError } from '../../utils/http.js'
|
||||
import { parseJsonObject } from '../../utils/json.js'
|
||||
import { logWebhook } from '../../utils/logger.js'
|
||||
import { parseAmountToFen } from '../../utils/money.js'
|
||||
import { nowIso } from '../../utils/time.js'
|
||||
@@ -172,8 +173,8 @@ async function enrichTradeBeforeUpsert(parsed, { requestId = '', webhookEventId
|
||||
return { parsed, enriched: false, reason: 'not_supported' }
|
||||
}
|
||||
|
||||
if (Number(parsed.totalAmount || 0) > 0) {
|
||||
return { parsed, enriched: false, reason: 'already_has_amount' }
|
||||
if (!shouldEnrichAgisoXianyuTrade(parsed)) {
|
||||
return { parsed, enriched: false, reason: 'enough_fields_present' }
|
||||
}
|
||||
|
||||
const detailResult = await enrichAgisoXianyuTradeOrder(parsed, { requestId })
|
||||
@@ -201,6 +202,54 @@ async function enrichTradeBeforeUpsert(parsed, { requestId = '', webhookEventId
|
||||
}
|
||||
}
|
||||
|
||||
function shouldEnrichAgisoXianyuTrade(parsed) {
|
||||
if (!parsed || parsed.provider !== 'agiso' || parsed.platform !== 'xianyu') {
|
||||
return false
|
||||
}
|
||||
|
||||
if (Number(parsed.totalAmount || 0) <= 0) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (!String(parsed.buyerName || '').trim()) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (!String(parsed.shopName || '').trim()) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (!hasDetailedOrderItems(parsed.items)) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
function hasDetailedOrderItems(items) {
|
||||
const normalizedItems = Array.isArray(items) ? items : []
|
||||
|
||||
if (normalizedItems.length === 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
return normalizedItems.some((item) => {
|
||||
const spec = isPlainObject(item?.spec) ? item.spec : {}
|
||||
const title = pickFirstNonEmpty([
|
||||
spec.title,
|
||||
spec.Title,
|
||||
spec.itemTitle,
|
||||
spec.item_title,
|
||||
spec.goods_name,
|
||||
spec.goodsName,
|
||||
item?.skuName,
|
||||
])
|
||||
const skuCode = String(item?.skuCode || '').trim()
|
||||
|
||||
return Boolean(title && title !== skuCode)
|
||||
})
|
||||
}
|
||||
|
||||
function parseAgisoTradeRequest(requestLike) {
|
||||
const query = normalizeRecord(requestLike.query)
|
||||
const body = normalizeRecord(requestLike.body)
|
||||
@@ -473,15 +522,10 @@ function parsePayloadJson(rawJson) {
|
||||
return {}
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(rawJson)
|
||||
return isPlainObject(parsed) ? parsed : {}
|
||||
} catch {
|
||||
throw createHttpError('json 参数不是合法 JSON', {
|
||||
statusCode: 400,
|
||||
errorCode: 'invalid_json_payload',
|
||||
})
|
||||
}
|
||||
return parseJsonObject(rawJson, {
|
||||
preserveLargeIntegers: true,
|
||||
throwOnError: true,
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeOrderItems(payload) {
|
||||
|
||||
Reference in New Issue
Block a user