完善接单平台上传与物品规则
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
resolveOrderFulfillmentReadiness,
|
||||
syncOrderFulfillmentAttachments,
|
||||
} from '../fulfillment/order-fulfillment-readiness-service.js'
|
||||
import { syncWorkerOrdersForSourceOrder } from '../worker-platform/worker-platform-service.js'
|
||||
import { nowIso } from '../../utils/time.js'
|
||||
import { logIntegration } from '../../utils/logger.js'
|
||||
import { createHttpError } from '../../utils/http.js'
|
||||
@@ -61,13 +62,15 @@ type UpsertOrderIgnoredResult = {
|
||||
tasks: []
|
||||
}
|
||||
|
||||
type UpsertOrderResult = {
|
||||
ignored?: false
|
||||
ignoreReason?: string
|
||||
order: OrderRow
|
||||
orderItems: OrderItemRow[]
|
||||
tasks: TaskRow[]
|
||||
} | UpsertOrderIgnoredResult
|
||||
type UpsertOrderResult =
|
||||
| {
|
||||
ignored?: false
|
||||
ignoreReason?: string
|
||||
order: OrderRow
|
||||
orderItems: OrderItemRow[]
|
||||
tasks: TaskRow[]
|
||||
}
|
||||
| UpsertOrderIgnoredResult
|
||||
|
||||
type OrderStateInput = {
|
||||
order_status?: string
|
||||
@@ -108,13 +111,17 @@ export async function upsertOrderFromSource(
|
||||
})
|
||||
|
||||
const resolvedItems = await Promise.all(
|
||||
event.items.map((item) => resolveOrderItemForFulfillment({
|
||||
provider: event.provider,
|
||||
platform: event.platform,
|
||||
item,
|
||||
})),
|
||||
event.items.map((item) =>
|
||||
resolveOrderItemForFulfillment({
|
||||
provider: event.provider,
|
||||
platform: event.platform,
|
||||
item,
|
||||
}),
|
||||
),
|
||||
)
|
||||
const configuredItems = (resolvedItems as FulfillmentOrderItem[]).filter(
|
||||
(item) => item.isConfigured,
|
||||
)
|
||||
const configuredItems = (resolvedItems as FulfillmentOrderItem[]).filter((item) => item.isConfigured)
|
||||
|
||||
if (configuredItems.length === 0) {
|
||||
logIntegration('[order-service]', `${sourceLabel} 订单已忽略:未命中任何已配置履约商品`, {
|
||||
@@ -156,16 +163,16 @@ export async function upsertOrderFromSource(
|
||||
|
||||
const order = existing
|
||||
? await updateOrder(existing.id, {
|
||||
...basePayload,
|
||||
...mergedPayload,
|
||||
updatedAt: now,
|
||||
})
|
||||
...basePayload,
|
||||
...mergedPayload,
|
||||
updatedAt: now,
|
||||
})
|
||||
: await createOrder({
|
||||
...basePayload,
|
||||
...mergedPayload,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
...basePayload,
|
||||
...mergedPayload,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
|
||||
if (!order) {
|
||||
throw createHttpError('订单写入失败', {
|
||||
@@ -189,14 +196,19 @@ export async function upsertOrderFromSource(
|
||||
|
||||
const readiness = await resolveOrderFulfillmentReadiness(order)
|
||||
if (!readiness.allowed) {
|
||||
logIntegration('[order-service]', `${sourceLabel} 订单暂停履约:电子凭证发码回调未确认`, {
|
||||
orderId: order.id,
|
||||
provider: order.provider,
|
||||
platform: order.platform,
|
||||
platformOrderId: order.platform_order_id,
|
||||
voucherCount: readiness.voucherCount,
|
||||
reason: readiness.reason,
|
||||
}, { level: 'warn' })
|
||||
logIntegration(
|
||||
'[order-service]',
|
||||
`${sourceLabel} 订单暂停履约:电子凭证发码回调未确认`,
|
||||
{
|
||||
orderId: order.id,
|
||||
provider: order.provider,
|
||||
platform: order.platform,
|
||||
platformOrderId: order.platform_order_id,
|
||||
voucherCount: readiness.voucherCount,
|
||||
reason: readiness.reason,
|
||||
},
|
||||
{ level: 'warn' },
|
||||
)
|
||||
|
||||
return {
|
||||
ignoreReason: readiness.blockReason || 'fulfillment_not_ready',
|
||||
@@ -211,6 +223,10 @@ export async function upsertOrderFromSource(
|
||||
source: `${sourceLabel}_order_upsert`,
|
||||
now,
|
||||
})
|
||||
await syncWorkerOrdersForSourceOrder(order, orderItems, {
|
||||
source: `${sourceLabel}_order_upsert`,
|
||||
autoOnly: true,
|
||||
})
|
||||
|
||||
logIntegration('[order-service]', `${sourceLabel} 订单 upsert 完成`, {
|
||||
orderId: order.id,
|
||||
@@ -249,17 +265,23 @@ export function mergeSourceOrderState(
|
||||
existing: OrderStateInput | null | undefined,
|
||||
incoming: OrderStateInput | null | undefined,
|
||||
): OrderStateMergeResult {
|
||||
const orderStatus = chooseHigherPriorityStatus(
|
||||
normalizeStateValue(existing?.order_status ?? existing?.orderStatus),
|
||||
normalizeStateValue(incoming?.orderStatus ?? incoming?.order_status),
|
||||
ORDER_STATUS_PRIORITY,
|
||||
) || normalizeStateValue(incoming?.orderStatus ?? incoming?.order_status) || 'created'
|
||||
const orderStatus =
|
||||
chooseHigherPriorityStatus(
|
||||
normalizeStateValue(existing?.order_status ?? existing?.orderStatus),
|
||||
normalizeStateValue(incoming?.orderStatus ?? incoming?.order_status),
|
||||
ORDER_STATUS_PRIORITY,
|
||||
) ||
|
||||
normalizeStateValue(incoming?.orderStatus ?? incoming?.order_status) ||
|
||||
'created'
|
||||
|
||||
const payStatus = chooseHigherPriorityStatus(
|
||||
normalizeStateValue(existing?.pay_status ?? existing?.payStatus),
|
||||
normalizeStateValue(incoming?.payStatus ?? incoming?.pay_status),
|
||||
PAY_STATUS_PRIORITY,
|
||||
) || normalizeStateValue(incoming?.payStatus ?? incoming?.pay_status) || 'unpaid'
|
||||
const payStatus =
|
||||
chooseHigherPriorityStatus(
|
||||
normalizeStateValue(existing?.pay_status ?? existing?.payStatus),
|
||||
normalizeStateValue(incoming?.payStatus ?? incoming?.pay_status),
|
||||
PAY_STATUS_PRIORITY,
|
||||
) ||
|
||||
normalizeStateValue(incoming?.payStatus ?? incoming?.pay_status) ||
|
||||
'unpaid'
|
||||
|
||||
return {
|
||||
orderStatus,
|
||||
@@ -296,7 +318,9 @@ function resolveStatusPriority(value: string, priorityMap: StatusPriorityMap): n
|
||||
}
|
||||
|
||||
function normalizeStateValue(value: unknown): string {
|
||||
return String(value || '').trim().toLowerCase()
|
||||
return String(value || '')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
function resolveMergedPaidAt({
|
||||
|
||||
Reference in New Issue
Block a user