优化券码操作类型, 增加手动销毁

This commit is contained in:
yml2213
2026-07-12 18:46:30 +08:00
parent 1b8cdf16d3
commit 3d760f987c
16 changed files with 481 additions and 29 deletions
@@ -19,13 +19,18 @@ import {
type KuaishouIndustrySourceConfig,
} from '../platforms/kuaishou-industry/source-config-service.js'
import { resendKuaishouIndustryVoucherSendCallback } from '../platforms/kuaishou-industry/send-code-service.js'
import { consumeKuaishouIndustryVoucher } from '../platforms/kuaishou-industry/voucher-service.js'
import {
consumeKuaishouIndustryVoucher,
destroyKuaishouIndustryVoucher,
resolveKuaishouIndustryEticketType,
} from '../platforms/kuaishou-industry/voucher-service.js'
import {
approveKuaishouIndustryRefund,
disagreeKuaishouIndustryRefund,
listKuaishouIndustryRefunds,
} from '../platforms/kuaishou-industry/refund-service.js'
import { reverseKuaishouIndustryCallback } from '../platforms/kuaishou-industry/reverse-callback-service.js'
import { attachKuaishouIndustryVoucherToTask } from '../platforms/kuaishou-industry/voucher-binding-service.js'
import type { KuaishouIndustryOpenApiCallResult } from '../platforms/kuaishou-industry/openapi-client.js'
import type { KuaishouIndustryVoucherRow, TaskRow } from '../../types/repository/rows.js'
@@ -103,10 +108,15 @@ export async function checkAdminKuaishouIndustryVoucherAvailable(input: JsonObje
const payload = buildVoucherOpenApiPayload(input, voucher)
assertRequired(payload.sellerId, '卖家编号未填写')
assertRequired(payload.eticketType || payload.bizTypeCode, '电子凭证类型未填写')
assertRequired(payload.eticketType || payload.bizTypeCode, '电子凭证类型未填写(请选择券码或手动填写类型)')
assertEtickets(payload.etickets, '电子凭证列表未填写')
return mapOpenApiResult(await checkKuaishouIndustryEticketAvailable(payload))
const result = mapOpenApiResult(await checkKuaishouIndustryEticketAvailable(payload))
return {
...result,
eticketType: String(payload.eticketType || payload.bizTypeCode || ''),
voucher: voucher ? mapAdminKuaishouIndustryVoucher(voucher) : null,
}
}
export async function reverseAdminKuaishouIndustryVoucher(input: JsonObject = {}) {
@@ -188,12 +198,55 @@ export async function resendAdminKuaishouIndustryVoucherCode(input: JsonObject =
}
}
export async function destroyAdminKuaishouIndustryVoucherByCode(input: JsonObject = {}) {
const voucher = await resolveRequiredVoucher(input)
const task = voucher.task_id ? await getTaskById(voucher.task_id) : null
const destroyInput: Parameters<typeof destroyKuaishouIndustryVoucher>[1] = {
source: 'admin_tool_manual_destroy',
reason: String(input.reason || '').trim(),
token: String(input.token || voucher.token || '').trim(),
eticketType: resolveKuaishouIndustryEticketType(voucher, input.eticketType),
}
if (task) {
destroyInput.task = task
}
const result = await destroyKuaishouIndustryVoucher(voucher, destroyInput)
if (!result.ok || !result.voucher) {
throw createHttpError(result.errorMessage || '电子凭证销毁失败', {
statusCode: 409,
errorCode: 'admin_kuaishou_industry_destroy_failed',
})
}
const nextTask = result.task || task
if (nextTask && result.voucher && !result.alreadyDestroyed) {
await attachKuaishouIndustryVoucherToTask(nextTask, result.voucher, {
source: 'admin_tool_manual_destroy',
now: new Date().toISOString(),
})
}
return {
success: true,
alreadyDestroyed: Boolean(result.alreadyDestroyed),
reason: result.reason || '',
voucher: mapAdminKuaishouIndustryVoucher(result.voucher),
task: nextTask ? mapTaskReference(nextTask) : null,
}
}
function buildVoucherOpenApiPayload(
input: JsonObject,
voucher: KuaishouIndustryVoucherRow | null,
): JsonObject {
const etickets = normalizeAdminEtickets(input.etickets)
const voucherCode = String(voucher?.voucher_code || input.voucherCode || '').trim()
const eticketType = resolveKuaishouIndustryEticketType(
voucher,
input.eticketType || input.bizTypeCode,
)
return {
...input,
@@ -202,6 +255,8 @@ function buildVoucherOpenApiPayload(
orderId: String(input.orderId || input.oid || voucher?.oid || '').trim(),
token: String(input.token || voucher?.token || '').trim(),
serialNum: String(input.serialNum || voucher?.consume_serial_num || '').trim(),
eticketType,
...(eticketType ? { bizTypeCode: eticketType } : {}),
etickets:
etickets.length > 0
? etickets
@@ -414,6 +469,7 @@ function mapAdminKuaishouIndustryVoucher(
skuCode: String(listRow.sku_code || '').trim(),
skuName: String(listRow.sku_name || '').trim(),
tokenMasked: maskSecret(voucher.token),
eticketType: resolveKuaishouIndustryEticketType(voucher),
status: voucher.status,
validStartTime: Number(voucher.valid_start_time || 0) || 0,
validEndTime: Number(voucher.valid_end_time || 0) || 0,