diff --git a/apps/backend/src/services/admin/admin-read-service.ts b/apps/backend/src/services/admin/admin-read-service.ts index 7f6a968c..dea8c153 100644 --- a/apps/backend/src/services/admin/admin-read-service.ts +++ b/apps/backend/src/services/admin/admin-read-service.ts @@ -63,6 +63,10 @@ import type { AdminTaskListQueryInput, AdminViewerSessionInput, } from '../../types/admin/read-inputs.js' +import type { + KuaishouIndustryVoucherRow, + TaskRow, +} from '../../types/repository/rows.js' type JsonRecord = Record @@ -220,11 +224,14 @@ export async function getAdminTaskDetail( } const primaryClaimTokenId = getTaskPrimaryClaimTokenId(task) - const [order, claimToken, taskEvents, taskKuaishouIndustryVouchers] = await Promise.all([ + const [order, claimToken, taskEvents, taskKuaishouIndustryVouchers, oidKuaishouIndustryVouchers] = await Promise.all([ getOrderById(task.order_id), primaryClaimTokenId ? getClaimTokenById(primaryClaimTokenId) : Promise.resolve(null), listTaskEventsByTaskId(task.id), listKuaishouIndustryVouchersByTaskId(task.id), + task.platform_order_id + ? listKuaishouIndustryVouchersByOid(task.platform_order_id) + : Promise.resolve([]), ]) const orderItems = order ? await listOrderItemsByOrderId(order.id) : [] const orderItem = orderItems.find((item) => item.id === task.order_item_id) || null @@ -241,7 +248,12 @@ export async function getAdminTaskDetail( mapKuaishouCloudFulfillmentContext(taskContext.kuaishouCloudFulfillment, { cloudSourceLabelMap, }) || mapKuaishouCloudTaskStateProjection(task, { cloudSourceLabelMap }) - const firstKuaishouIndustryVoucher = taskKuaishouIndustryVouchers[0] || null + const firstKuaishouIndustryVoucher = resolveKuaishouIndustryVoucherForTask( + task, + taskContext, + taskKuaishouIndustryVouchers, + oidKuaishouIndustryVouchers, + ) const kuaishouIndustryVoucher = firstKuaishouIndustryVoucher ? mapAdminKuaishouIndustryVoucher(firstKuaishouIndustryVoucher) @@ -375,6 +387,46 @@ function mapKuaishouIndustryVoucherContext(value: unknown): JsonRecord | null { } } +function resolveKuaishouIndustryVoucherForTask( + task: TaskRow, + taskContext: JsonRecord, + taskVouchers: KuaishouIndustryVoucherRow[], + oidVouchers: KuaishouIndustryVoucherRow[], +): KuaishouIndustryVoucherRow | null { + const linkedVoucher = taskVouchers[0] || null + if (linkedVoucher) { + return linkedVoucher + } + + const voucherContext = + taskContext.kuaishouIndustryVoucher && + typeof taskContext.kuaishouIndustryVoucher === 'object' && + !Array.isArray(taskContext.kuaishouIndustryVoucher) + ? taskContext.kuaishouIndustryVoucher as JsonRecord + : {} + const voucherCode = String(voucherContext.voucherCode || voucherContext.eticketId || '').trim() + if (voucherCode) { + const matchedByCode = oidVouchers.find( + (voucher) => String(voucher.voucher_code || '').trim() === voucherCode, + ) + if (matchedByCode) { + return matchedByCode + } + } + + const unitIndex = Number(task.unit_index || 0) || 0 + if (unitIndex > 0) { + const matchedByUnit = oidVouchers.find( + (voucher) => Number(voucher.unit_index || 0) === unitIndex, + ) + if (matchedByUnit) { + return matchedByUnit + } + } + + return oidVouchers.length === 1 ? (oidVouchers[0] || null) : null +} + function buildCloudSourceLabelMap() { const labelMap = new Map() diff --git a/apps/backend/src/services/admin/write/kuaishou-cloud-actions.ts b/apps/backend/src/services/admin/write/kuaishou-cloud-actions.ts index e55c16ae..b6dc1dcf 100644 --- a/apps/backend/src/services/admin/write/kuaishou-cloud-actions.ts +++ b/apps/backend/src/services/admin/write/kuaishou-cloud-actions.ts @@ -969,6 +969,30 @@ async function getRequiredIndustryVoucherForTask(task: TaskRow): Promise String(voucher.voucher_code || '').trim() === voucherCode, + ) + if (matchedByCode) { + return matchedByCode + } + } + + const unitIndex = Number(task.unit_index || 0) || 0 + if (unitIndex > 0) { + const matchedByUnit = oidVouchers.find( + (voucher) => Number(voucher.unit_index || 0) === unitIndex, + ) + if (matchedByUnit) { + return matchedByUnit + } + } + + if (oidVouchers.length === 1 && oidVouchers[0]) { + return oidVouchers[0] + } + throw createHttpError('当前任务没有关联电子凭证,无法执行该操作', { statusCode: 409, errorCode: 'admin_task_kuaishou_industry_voucher_missing',