72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
import { parseJsonObject } from '../../utils/json.js'
|
|
|
|
export function extractAgisoTradePayload(body) {
|
|
const normalizedBody = normalizeRecord(body)
|
|
const rawJson = String(normalizedBody.json || normalizedBody.JSON || '').trim()
|
|
|
|
if (rawJson) {
|
|
return normalizeRecord(parseJsonObject(rawJson, { preserveLargeIntegers: true }))
|
|
}
|
|
|
|
return normalizedBody
|
|
}
|
|
|
|
export function resolveAgisoTradePlatformOrderId(payload) {
|
|
const normalizedPayload = normalizeRecord(payload)
|
|
const firstItem = extractAgisoTradeOrderItemSources(normalizedPayload)[0] || {}
|
|
|
|
return pickFirstNonEmpty([
|
|
normalizedPayload.biz_order_id,
|
|
normalizedPayload.Tid,
|
|
normalizedPayload.tid,
|
|
normalizedPayload.Oid,
|
|
normalizedPayload.oid,
|
|
normalizedPayload.order_id,
|
|
normalizedPayload.orderId,
|
|
firstItem.Oid,
|
|
firstItem.oid,
|
|
])
|
|
}
|
|
|
|
export function extractAgisoTradeOrderItemSources(payload) {
|
|
const normalizedPayload = normalizeRecord(payload)
|
|
const candidates = [
|
|
normalizedPayload.items,
|
|
normalizedPayload.Items,
|
|
normalizedPayload.orders,
|
|
normalizedPayload.Orders,
|
|
normalizedPayload.order_list,
|
|
normalizedPayload.OrderList,
|
|
]
|
|
|
|
for (const current of candidates) {
|
|
if (Array.isArray(current) && current.length > 0) {
|
|
return current.map((item) => normalizeRecord(item)).filter((item) => Object.keys(item).length > 0)
|
|
}
|
|
}
|
|
|
|
if (Object.keys(normalizedPayload).length > 0) {
|
|
return [normalizedPayload]
|
|
}
|
|
|
|
return []
|
|
}
|
|
|
|
function normalizeRecord(value) {
|
|
return value && typeof value === 'object' && !Array.isArray(value) ? value : {}
|
|
}
|
|
|
|
function pickFirstNonEmpty(values) {
|
|
for (const value of values) {
|
|
if (typeof value === 'string' && value.trim()) {
|
|
return value.trim()
|
|
}
|
|
|
|
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
return String(value)
|
|
}
|
|
}
|
|
|
|
return ''
|
|
}
|