接入多发货平台与电子凭证
This commit is contained in:
@@ -18,6 +18,7 @@ import { getCloudtentaclesKnapsack } from "../../platforms/cloudtentacles/knapsa
|
||||
import { backCloudtentaclesVirtualNumber } from "../../platforms/cloudtentacles/virtual-number-service.js";
|
||||
import { consumeKuaishouEticket } from "../../platforms/kuaishou-eticket/consume-service.js";
|
||||
import { isKuaishouEticketMockTicketCode } from "../../platforms/kuaishou-eticket/mock-ticket-service.js";
|
||||
import { consumeKuaishouIndustryVouchersForTask } from "../../platforms/kuaishou-industry/voucher-service.js";
|
||||
import {
|
||||
getKuaishouEticketSourceConfig,
|
||||
resolveKuaishouEticketShopConfig,
|
||||
@@ -631,6 +632,15 @@ function isPlainObject(value: unknown): value is JsonObject {
|
||||
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function hasKuaishouIndustryVoucherContext(value: JsonObject): boolean {
|
||||
const voucher = isPlainObject(value.kuaishouIndustryVoucher)
|
||||
? value.kuaishouIndustryVoucher
|
||||
: {};
|
||||
const voucherCode = String(voucher.voucherCode || voucher.eticketId || "").trim();
|
||||
const status = String(voucher.status || "UNUSED").trim().toUpperCase();
|
||||
return Boolean(voucherCode && status !== "DESTROYED");
|
||||
}
|
||||
|
||||
export function buildDispatchStockItems(
|
||||
deliveryItems: DispatchDeliveryItem[],
|
||||
{ skuItems = [], knapsackItems = [] }: { skuItems?: JsonObject[]; knapsackItems?: JsonObject[] } = {}
|
||||
@@ -794,15 +804,46 @@ export async function returnKuaishouCloudFulfillmentTask(
|
||||
let nextResultCode = "kuaishou_cloud_completed";
|
||||
let nextResultMessage = "cloudtentacles 发货、退号并完成快手核销";
|
||||
const consumeAlreadyCompleted = flow.consume.status === "success";
|
||||
const isIndustryTask = isIndustryEVoucherTask(task)
|
||||
const hasIndustryVoucher = hasKuaishouIndustryVoucherContext(taskContext);
|
||||
const isIndustryTask = isIndustryEVoucherTask(task);
|
||||
let industryVoucherContextPatch: JsonObject | null = null;
|
||||
|
||||
if (consumeAlreadyCompleted) {
|
||||
consumeStatus = "success";
|
||||
consumedAt = flow.consume.consumedAt || now;
|
||||
} else if (isIndustryTask) {
|
||||
consumeStatus = "success";
|
||||
consumedAt = flow.consume.consumedAt || now;
|
||||
consumeErrorMessage = "行业电子凭证核销由平台回调处理,已跳过主动核销";
|
||||
} else if (hasIndustryVoucher || isIndustryTask) {
|
||||
const industryResult = hasIndustryVoucher
|
||||
? await consumeKuaishouIndustryVouchersForTask(task, {
|
||||
source: String(options.source || "system_auto_finalize").trim() || "system_auto_finalize",
|
||||
token: String(taskContext.kuaishouIndustryVoucher?.token || "").trim(),
|
||||
consumeType: "delivery",
|
||||
consumeTime: Date.now(),
|
||||
})
|
||||
: { ok: true, consumed: [], failed: [] };
|
||||
|
||||
if (industryResult.ok) {
|
||||
consumeStatus = "success";
|
||||
consumedAt = now;
|
||||
const consumedVoucher = industryResult.consumed[0] || null;
|
||||
if (consumedVoucher) {
|
||||
industryVoucherContextPatch = {
|
||||
oid: consumedVoucher.oid,
|
||||
token: consumedVoucher.token,
|
||||
eticketId: consumedVoucher.voucher_code,
|
||||
voucherCode: consumedVoucher.voucher_code,
|
||||
unitIndex: Number(consumedVoucher.unit_index || 0) || 0,
|
||||
status: "CONSUMED",
|
||||
validStartTime: Number(consumedVoucher.valid_start_time || 0) || 0,
|
||||
validEndTime: Number(consumedVoucher.valid_end_time || 0) || 0,
|
||||
consumedAt: now,
|
||||
consumeSerialNum: consumedVoucher.consume_serial_num || `CONSUME-${consumedVoucher.voucher_code}`,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
consumeStatus = "failed";
|
||||
consumeErrorMessage =
|
||||
industryResult.failed[0]?.errorMessage || "电子凭证核销回调失败,请人工处理";
|
||||
}
|
||||
} else if (!order) {
|
||||
consumeStatus = "failed";
|
||||
consumeErrorMessage = "任务关联订单不存在,无法执行快手核销";
|
||||
@@ -860,6 +901,16 @@ export async function returnKuaishouCloudFulfillmentTask(
|
||||
|
||||
const nextContext = {
|
||||
...taskContext,
|
||||
...(industryVoucherContextPatch
|
||||
? {
|
||||
kuaishouIndustryVoucher: {
|
||||
...(isPlainObject(taskContext.kuaishouIndustryVoucher)
|
||||
? taskContext.kuaishouIndustryVoucher
|
||||
: {}),
|
||||
...industryVoucherContextPatch,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
kuaishouCloudFulfillment: {
|
||||
...flow,
|
||||
returnNumber: {
|
||||
|
||||
Reference in New Issue
Block a user