修复错误
This commit is contained in:
@@ -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