feat(fulfillment): 自动核销任务展示并搜索核销码
This commit is contained in:
@@ -6,6 +6,7 @@ import { listKuaishouIndustryVouchersByOid } from '../../repositories/kuaishou-i
|
||||
import {
|
||||
findRedeemCertificateByOrder,
|
||||
listRedeemCertificatesByLocalOrder,
|
||||
resolveRedeemVerifyCertNo,
|
||||
type RedeemCertificateRow,
|
||||
} from '../../repositories/redeem-certificate-repo.js'
|
||||
import type { WorkOrderRow } from '../../repositories/worker-platform/index.js'
|
||||
@@ -70,6 +71,7 @@ export type VoucherConsumeJobRow = {
|
||||
max_attempts: number
|
||||
locked_until: string | null
|
||||
last_error: string
|
||||
voucher_codes: string[]
|
||||
result_json: string | Record<string, unknown>
|
||||
created_at: string
|
||||
updated_at: string
|
||||
@@ -331,13 +333,14 @@ async function scheduleVoucherConsumeTarget(
|
||||
order_item_id,
|
||||
task_id,
|
||||
work_order_id,
|
||||
voucher_codes,
|
||||
due_at,
|
||||
status,
|
||||
attempt_count,
|
||||
max_attempts,
|
||||
updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10::text[], $11,
|
||||
'pending', 0, 8, NOW()
|
||||
)
|
||||
ON CONFLICT (provider, target_key)
|
||||
@@ -355,6 +358,7 @@ async function scheduleVoucherConsumeTarget(
|
||||
target.orderItemId,
|
||||
target.taskId,
|
||||
target.workOrderId,
|
||||
candidates.voucherCodes,
|
||||
dueAt,
|
||||
],
|
||||
)
|
||||
@@ -488,7 +492,8 @@ export async function listVoucherConsumeJobs(
|
||||
`(CAST(id AS TEXT) ILIKE $${params.length}
|
||||
OR platform_order_id ILIKE $${params.length}
|
||||
OR target_key ILIKE $${params.length}
|
||||
OR CAST(order_id AS TEXT) ILIKE $${params.length})`,
|
||||
OR CAST(order_id AS TEXT) ILIKE $${params.length}
|
||||
OR fulfillment_voucher_codes_search_text(voucher_codes) ILIKE $${params.length})`,
|
||||
)
|
||||
}
|
||||
if (provider) {
|
||||
@@ -580,6 +585,10 @@ async function executeKuaishouConsumeJob(job: VoucherConsumeJobRow) {
|
||||
if (vouchers.length === 0) {
|
||||
throw new Error('未找到待核销的快手电子凭证')
|
||||
}
|
||||
await updateVoucherConsumeJobCodes(
|
||||
job.id,
|
||||
vouchers.map((voucher) => String(voucher.voucher_code || '').trim()).filter(Boolean),
|
||||
)
|
||||
|
||||
const task = job.task_id ? await getTaskById(job.task_id) : null
|
||||
let consumedCount = 0
|
||||
@@ -631,6 +640,18 @@ async function executeDouyinConsumeJob(job: VoucherConsumeJobRow) {
|
||||
if (certificates.length === 0) {
|
||||
throw new Error('未找到待核销的抖音券')
|
||||
}
|
||||
await updateVoucherConsumeJobCodes(
|
||||
job.id,
|
||||
Array.from(
|
||||
new Set(
|
||||
certificates.flatMap((certificate) => [
|
||||
String(certificate.cert_no || '').trim(),
|
||||
String(certificate.platform_cert_no || '').trim(),
|
||||
resolveRedeemVerifyCertNo(certificate),
|
||||
]),
|
||||
),
|
||||
).filter(Boolean),
|
||||
)
|
||||
|
||||
let consumedCount = 0
|
||||
let alreadyConsumedCount = 0
|
||||
@@ -779,6 +800,7 @@ function buildSyntheticJob(
|
||||
max_attempts: 8,
|
||||
locked_until: null,
|
||||
last_error: '',
|
||||
voucher_codes: [],
|
||||
result_json: {},
|
||||
created_at: '',
|
||||
updated_at: '',
|
||||
@@ -837,6 +859,23 @@ async function finishVoucherConsumeJob(
|
||||
)
|
||||
}
|
||||
|
||||
async function updateVoucherConsumeJobCodes(jobId: number, voucherCodes: string[]) {
|
||||
const codes = Array.from(new Set(voucherCodes.map((code) => String(code || '').trim()))).filter(
|
||||
Boolean,
|
||||
)
|
||||
if (codes.length === 0) {
|
||||
return
|
||||
}
|
||||
await query(
|
||||
`
|
||||
UPDATE voucher_consume_jobs
|
||||
SET voucher_codes = $1::text[], updated_at = NOW()
|
||||
WHERE id = $2
|
||||
`,
|
||||
[codes, jobId],
|
||||
)
|
||||
}
|
||||
|
||||
async function failVoucherConsumeJob(job: VoucherConsumeJobRow, error: unknown, retry: boolean) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
if (!retry) {
|
||||
|
||||
Reference in New Issue
Block a user