优化了订单展示
This commit is contained in:
@@ -2,6 +2,7 @@ import crypto from 'node:crypto'
|
||||
|
||||
import { runtimeConfig } from '../../config/runtime.js'
|
||||
import { createWebhookEvent, updateWebhookEvent } from '../../repositories/webhook-event-repo.js'
|
||||
import { enrichAgisoXianyuTradeOrder } from '../platforms/agiso/xianyu/order-detail-service.js'
|
||||
import { upsertOrderFromWebhook } from './order-service.js'
|
||||
import { createHttpError } from '../../utils/http.js'
|
||||
import { logWebhook } from '../../utils/logger.js'
|
||||
@@ -87,7 +88,11 @@ async function executeAgisoTradeWebhook(parsed, webhookEventId, { requestId = ''
|
||||
})
|
||||
}
|
||||
|
||||
const result = await upsertOrderFromWebhook(parsed)
|
||||
const enriched = await enrichTradeBeforeUpsert(parsed, {
|
||||
requestId,
|
||||
webhookEventId,
|
||||
})
|
||||
const result = await upsertOrderFromWebhook(enriched.parsed)
|
||||
updateWebhookEvent(webhookEventId, {
|
||||
processed: true,
|
||||
process_error: '',
|
||||
@@ -103,22 +108,28 @@ async function executeAgisoTradeWebhook(parsed, webhookEventId, { requestId = ''
|
||||
shopName: parsed.shopName,
|
||||
eventKey: parsed.eventKey,
|
||||
eventType: parsed.eventType,
|
||||
platformOrderId: parsed.platformOrderId,
|
||||
platformOrderId: enriched.parsed.platformOrderId,
|
||||
orderId: result.order.id,
|
||||
enriched: enriched.enriched,
|
||||
enrichReason: enriched.reason || '',
|
||||
totalAmountFen: Number(enriched.parsed.totalAmount || 0),
|
||||
taskCount: result.tasks.length,
|
||||
messageDeliveryCount: Array.isArray(result.messageDeliveries) ? result.messageDeliveries.length : 0,
|
||||
})
|
||||
|
||||
return {
|
||||
accepted: true,
|
||||
provider: parsed.provider,
|
||||
platform: parsed.platform,
|
||||
shopId: parsed.shopId,
|
||||
shopName: parsed.shopName,
|
||||
platformRaw: parsed.platformRaw,
|
||||
eventType: parsed.eventType,
|
||||
platformOrderId: parsed.platformOrderId,
|
||||
provider: enriched.parsed.provider,
|
||||
platform: enriched.parsed.platform,
|
||||
shopId: enriched.parsed.shopId,
|
||||
shopName: enriched.parsed.shopName,
|
||||
platformRaw: enriched.parsed.platformRaw,
|
||||
eventType: enriched.parsed.eventType,
|
||||
platformOrderId: enriched.parsed.platformOrderId,
|
||||
orderId: result.order.id,
|
||||
totalAmountFen: Number(enriched.parsed.totalAmount || 0),
|
||||
enriched: enriched.enriched,
|
||||
enrichReason: enriched.reason || '',
|
||||
taskCount: result.tasks.length,
|
||||
messageDeliveries: result.messageDeliveries || [],
|
||||
tasks: result.tasks.map((task) => ({
|
||||
@@ -156,6 +167,40 @@ async function executeAgisoTradeWebhook(parsed, webhookEventId, { requestId = ''
|
||||
}
|
||||
}
|
||||
|
||||
async function enrichTradeBeforeUpsert(parsed, { requestId = '', webhookEventId = null } = {}) {
|
||||
if (parsed.provider !== 'agiso' || parsed.platform !== 'xianyu') {
|
||||
return { parsed, enriched: false, reason: 'not_supported' }
|
||||
}
|
||||
|
||||
if (Number(parsed.totalAmount || 0) > 0) {
|
||||
return { parsed, enriched: false, reason: 'already_has_amount' }
|
||||
}
|
||||
|
||||
const detailResult = await enrichAgisoXianyuTradeOrder(parsed, { requestId })
|
||||
const nextParsed = detailResult?.parsed || parsed
|
||||
|
||||
logWebhook('[webhook-service/agiso]', 'Agiso webhook 订单补查结束', {
|
||||
requestId,
|
||||
webhookEventId,
|
||||
provider: nextParsed.provider,
|
||||
platform: nextParsed.platform,
|
||||
shopId: nextParsed.shopId,
|
||||
platformOrderId: nextParsed.platformOrderId,
|
||||
enriched: Boolean(detailResult?.enriched),
|
||||
reason: String(detailResult?.reason || ''),
|
||||
totalAmountFen: Number(nextParsed.totalAmount || 0),
|
||||
paidAt: nextParsed.paidAt || null,
|
||||
errorMessage: String(detailResult?.errorMessage || ''),
|
||||
})
|
||||
|
||||
return {
|
||||
parsed: nextParsed,
|
||||
enriched: Boolean(detailResult?.enriched),
|
||||
reason: String(detailResult?.reason || ''),
|
||||
errorMessage: String(detailResult?.errorMessage || ''),
|
||||
}
|
||||
}
|
||||
|
||||
function parseAgisoTradeRequest(requestLike) {
|
||||
const query = normalizeRecord(requestLike.query)
|
||||
const body = normalizeRecord(requestLike.body)
|
||||
|
||||
Reference in New Issue
Block a user