优化快手电子凭证发码和手动处理
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { findLatestOrderByPlatformOrderId } from '../../../repositories/order-repo.js'
|
||||
import { listTasksByOrderId } from '../../../repositories/task-repo.js'
|
||||
import {
|
||||
findKuaishouIndustryVoucherByCode,
|
||||
updateKuaishouIndustryVoucherByCode,
|
||||
upsertKuaishouIndustryVoucher,
|
||||
} from '../../../repositories/kuaishou-industry-voucher-repo.js'
|
||||
@@ -31,6 +32,15 @@ import type { KuaishouIndustryVoucherRow, OrderRow } from '../../../types/reposi
|
||||
|
||||
type JsonObject = Record<string, any>
|
||||
|
||||
type SendCodeCallbackParams = {
|
||||
oid: string
|
||||
sendType: string
|
||||
sellerId: string
|
||||
token: string
|
||||
eticketType?: string
|
||||
ext?: string
|
||||
}
|
||||
|
||||
export async function handleSendCode(rawBody: JsonObject = {}) {
|
||||
const config = getKuaishouIndustryConfig()
|
||||
|
||||
@@ -125,6 +135,47 @@ export async function handleSendCode(rawBody: JsonObject = {}) {
|
||||
)
|
||||
}
|
||||
|
||||
export async function resendKuaishouIndustryVoucherSendCallback(input: {
|
||||
voucherCode: string
|
||||
oid?: string
|
||||
preferredTotalGoodsValue?: number
|
||||
}) {
|
||||
const voucher = await findKuaishouIndustryVoucherByCode(input.voucherCode, input.oid || '')
|
||||
if (!voucher) {
|
||||
return {
|
||||
success: false,
|
||||
error: '电子凭证不存在,无法重发发码回调',
|
||||
voucher: null,
|
||||
}
|
||||
}
|
||||
|
||||
const rawPayload = parseJsonObject(voucher.raw_payload_json)
|
||||
const body = parseJsonObject(rawPayload.body)
|
||||
const params = resolveSendCodeCallbackParams(voucher, body)
|
||||
const eticketType = String(params.eticketType || '').trim()
|
||||
const eticket = buildKuaishouIndustryEticketFromVoucher(voucher, eticketType)
|
||||
const result = await sendAndRecordCallback({
|
||||
oid: params.oid,
|
||||
sendType: params.sendType,
|
||||
etickets: [eticket],
|
||||
vouchers: [voucher],
|
||||
sellerId: params.sellerId,
|
||||
token: params.token,
|
||||
eticketType,
|
||||
source: 'admin_resend',
|
||||
...(input.preferredTotalGoodsValue !== undefined
|
||||
? { preferredTotalGoodsValue: input.preferredTotalGoodsValue }
|
||||
: {}),
|
||||
...(params.ext ? { ext: params.ext } : {}),
|
||||
})
|
||||
const updatedVoucher = await findKuaishouIndustryVoucherByCode(voucher.voucher_code, voucher.oid)
|
||||
|
||||
return {
|
||||
...result,
|
||||
voucher: updatedVoucher || voucher,
|
||||
}
|
||||
}
|
||||
|
||||
async function sendAndRecordCallback(input: {
|
||||
oid: string
|
||||
sendType: string
|
||||
@@ -135,6 +186,7 @@ async function sendAndRecordCallback(input: {
|
||||
eticketType?: string
|
||||
ext?: string
|
||||
preferredTotalGoodsValue?: number
|
||||
source?: string
|
||||
}) {
|
||||
const eticketItems = input.etickets.map((e) => ({
|
||||
id: String(e.id || ''),
|
||||
@@ -156,6 +208,7 @@ async function sendAndRecordCallback(input: {
|
||||
const totalGoodsValue = goodsValuePlan.totalGoodsValue
|
||||
|
||||
logIntegration('[kuaishou-industry/send-code]', '准备发起电子凭证发货回调', {
|
||||
source: input.source || 'send_code',
|
||||
oid: input.oid,
|
||||
sellerId: input.sellerId,
|
||||
sendType: input.sendType,
|
||||
@@ -202,6 +255,7 @@ async function sendAndRecordCallback(input: {
|
||||
|
||||
if (!result.success) {
|
||||
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调执行失败,发码请求已拒绝继续', {
|
||||
source: input.source || 'send_code',
|
||||
oid: input.oid,
|
||||
error: result.error || '',
|
||||
response: result.response || null,
|
||||
@@ -210,6 +264,7 @@ async function sendAndRecordCallback(input: {
|
||||
}
|
||||
|
||||
logIntegration('[kuaishou-industry/send-code]', '电子凭证发货回调执行完成', {
|
||||
source: input.source || 'send_code',
|
||||
oid: input.oid,
|
||||
response: result.response || null,
|
||||
})
|
||||
@@ -217,6 +272,20 @@ async function sendAndRecordCallback(input: {
|
||||
return result
|
||||
}
|
||||
|
||||
function resolveSendCodeCallbackParams(
|
||||
voucher: KuaishouIndustryVoucherRow,
|
||||
body: JsonObject,
|
||||
): SendCodeCallbackParams {
|
||||
return {
|
||||
oid: String(body.oid || voucher.oid || '').trim(),
|
||||
sendType: String(body.sendType || 'VIRTUAL').trim() || 'VIRTUAL',
|
||||
sellerId: String(body.sellerId || voucher.seller_id || '').trim(),
|
||||
token: String(body.token || voucher.token || '').trim(),
|
||||
eticketType: String(body.eticketType || '').trim(),
|
||||
ext: String(body.ext || '').trim(),
|
||||
}
|
||||
}
|
||||
|
||||
function resolveSendCallbackFailureMessage(result: Awaited<ReturnType<typeof sendCallback>>) {
|
||||
const error = String(result.error || '').trim()
|
||||
if (error) {
|
||||
|
||||
Reference in New Issue
Block a user