修复错误
This commit is contained in:
@@ -86,6 +86,16 @@ export async function createOpen91Order(payload = {}, { requestId = '' } = {}) {
|
||||
})
|
||||
}
|
||||
|
||||
if (isWaitingKuaishouIndustrySendCallback(result.ignoreReason) && result.order) {
|
||||
return buildOpen91SuccessResponse({
|
||||
orderNo: normalized.orderNo,
|
||||
outTradeNo: buildOpen91OutTradeNo(result.order),
|
||||
orderStatus: 10,
|
||||
orderCost: buildOpen91OrderCost(0),
|
||||
cards: '',
|
||||
})
|
||||
}
|
||||
|
||||
if (!result.order || !Array.isArray(result.tasks) || result.tasks.length === 0) {
|
||||
return buildOpen91SuccessResponse({
|
||||
orderNo: normalized.orderNo,
|
||||
@@ -106,9 +116,13 @@ export async function createOpen91Order(payload = {}, { requestId = '' } = {}) {
|
||||
}
|
||||
|
||||
function resolveOpen91CreateFailReason(ignoreReason: unknown) {
|
||||
if (String(ignoreReason || '').trim() === 'kuaishou_industry_send_callback_unconfirmed') {
|
||||
if (isWaitingKuaishouIndustrySendCallback(ignoreReason)) {
|
||||
return '电子凭证发码回调未成功,订单暂不能履约'
|
||||
}
|
||||
|
||||
return '商品未配置或订单无法履约'
|
||||
}
|
||||
|
||||
function isWaitingKuaishouIndustrySendCallback(ignoreReason: unknown) {
|
||||
return String(ignoreReason || '').trim() === 'kuaishou_industry_send_callback_unconfirmed'
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { findLatestOrderByPlatformOrderId } from '../../repositories/order-repo.js'
|
||||
import { listKuaishouIndustryVouchersByOid } from '../../repositories/kuaishou-industry-voucher-repo.js'
|
||||
import { listTasksByOrderId } from '../../repositories/task-repo.js'
|
||||
import { buildClaimUrl } from '../claim/claim-service.js'
|
||||
import { ensureTaskClaimLink } from '../fulfillment/kuaishou-cloud/index.js'
|
||||
@@ -11,6 +12,10 @@ import {
|
||||
OPEN_91_MANUAL_FAILED_STATUS,
|
||||
OPEN_91_PENDING_CONFIG_STATUS,
|
||||
} from '../platforms/ninetyone/order-service.js'
|
||||
import {
|
||||
isKuaishouIndustryVoucherSendCallbackSuccess,
|
||||
resolveKuaishouIndustryVoucherSendCallbackMessage,
|
||||
} from '../platforms/kuaishou-industry/voucher-service.js'
|
||||
import {
|
||||
assertOpen91Config,
|
||||
OPEN_91_DEFAULT_FAIL_CODE,
|
||||
@@ -30,7 +35,7 @@ import {
|
||||
buildOpen91SuccessResponse,
|
||||
resolveOpen91QueryState,
|
||||
} from './response.js'
|
||||
import type { OrderRow } from '../../types/repository/rows.js'
|
||||
import type { KuaishouIndustryVoucherRow, OrderRow, TaskRow } from '../../types/repository/rows.js'
|
||||
|
||||
type JsonObject = Record<string, any>
|
||||
|
||||
@@ -59,6 +64,7 @@ export async function queryOpen91Order(payload: JsonObject = {}, { requestId = '
|
||||
}
|
||||
|
||||
const tasks = await listTasksByOrderId(order.id)
|
||||
const industryVoucherState = await resolveOpen91IndustryVoucherState(order, tasks)
|
||||
|
||||
if (tasks.length === 0) {
|
||||
if (String(order.order_status || '').trim() === OPEN_91_PENDING_CONFIG_STATUS) {
|
||||
@@ -82,10 +88,23 @@ export async function queryOpen91Order(payload: JsonObject = {}, { requestId = '
|
||||
failReason: resolveOpen91OrderFailReason(order) || '商家手动标记无法履约',
|
||||
})
|
||||
}
|
||||
|
||||
if (industryVoucherState.voucherCount > 0) {
|
||||
return buildOpen91SuccessResponse({
|
||||
orderNo: normalized.orderNo,
|
||||
outTradeNo: buildOpen91OutTradeNo(order),
|
||||
orderStatus: 10,
|
||||
failCode: 0,
|
||||
failReason: '',
|
||||
orderCost: buildOpen91OrderCost(0),
|
||||
cards: '',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const cardItems = []
|
||||
const readyTaskIds = []
|
||||
const industryReadyTaskIds = new Set(industryVoucherState.readyTaskIds)
|
||||
|
||||
for (const task of tasks) {
|
||||
let claimUrl = ''
|
||||
@@ -115,6 +134,10 @@ export async function queryOpen91Order(payload: JsonObject = {}, { requestId = '
|
||||
continue
|
||||
}
|
||||
|
||||
if (!industryReadyTaskIds.has(Number(task.id))) {
|
||||
continue
|
||||
}
|
||||
|
||||
readyTaskIds.push(Number(task.id))
|
||||
cardItems.push(buildOpen91CardItem({
|
||||
claimUrl,
|
||||
@@ -133,6 +156,9 @@ export async function queryOpen91Order(payload: JsonObject = {}, { requestId = '
|
||||
orderId: order.id,
|
||||
taskCount: tasks.length,
|
||||
readyTaskCount: readyTaskIds.length,
|
||||
voucherCount: industryVoucherState.voucherCount,
|
||||
voucherReadyTaskCount: industryVoucherState.readyTaskIds.length,
|
||||
voucherPendingReason: industryVoucherState.pendingReason,
|
||||
orderStatus: queryState.orderStatus,
|
||||
})
|
||||
|
||||
@@ -189,6 +215,50 @@ function resolveOpen91OrderFailReason(order: OrderRow) {
|
||||
return String(payload.manualFailedReason || '').trim()
|
||||
}
|
||||
|
||||
async function resolveOpen91IndustryVoucherState(order: OrderRow, tasks: TaskRow[]) {
|
||||
const vouchers = await listKuaishouIndustryVouchersByOid(order.platform_order_id)
|
||||
const voucherByTaskId = new Map<number, KuaishouIndustryVoucherRow>()
|
||||
const voucherByUnitIndex = new Map<number, KuaishouIndustryVoucherRow>()
|
||||
|
||||
for (const voucher of vouchers) {
|
||||
const taskId = Number(voucher.task_id || 0)
|
||||
const unitIndex = Number(voucher.unit_index || 0)
|
||||
if (taskId > 0 && !voucherByTaskId.has(taskId)) {
|
||||
voucherByTaskId.set(taskId, voucher)
|
||||
}
|
||||
if (unitIndex > 0 && !voucherByUnitIndex.has(unitIndex)) {
|
||||
voucherByUnitIndex.set(unitIndex, voucher)
|
||||
}
|
||||
}
|
||||
|
||||
const readyTaskIds: number[] = []
|
||||
let pendingReason = ''
|
||||
|
||||
for (const task of tasks) {
|
||||
const taskId = Number(task.id || 0)
|
||||
const unitIndex = Number(task.unit_index || 0)
|
||||
const voucher = voucherByTaskId.get(taskId) || voucherByUnitIndex.get(unitIndex) || null
|
||||
|
||||
if (!voucher) {
|
||||
pendingReason = pendingReason || '电子凭证发码通知尚未完成'
|
||||
continue
|
||||
}
|
||||
|
||||
if (!isKuaishouIndustryVoucherSendCallbackSuccess(voucher)) {
|
||||
pendingReason = pendingReason || resolveKuaishouIndustryVoucherSendCallbackMessage(voucher)
|
||||
continue
|
||||
}
|
||||
|
||||
readyTaskIds.push(taskId)
|
||||
}
|
||||
|
||||
return {
|
||||
voucherCount: vouchers.length,
|
||||
readyTaskIds,
|
||||
pendingReason,
|
||||
}
|
||||
}
|
||||
|
||||
function parseJsonObject(value: unknown): JsonObject {
|
||||
if (!value) {
|
||||
return {}
|
||||
|
||||
Reference in New Issue
Block a user