增加履约高级配置
This commit is contained in:
@@ -23,6 +23,19 @@ import { syncKuaishouCloudRoleInfoBeforeDispatch } from "./dispatch-role-sync.js
|
||||
import { normalizeActor, parseTaskContext } from "./task-context.js";
|
||||
import type { TaskRow } from "../../../types/repository/rows.js";
|
||||
|
||||
type DispatchDeliveryItem = {
|
||||
cloudSkuId: number;
|
||||
cloudSkuName: string;
|
||||
quantity: number;
|
||||
};
|
||||
|
||||
type DispatchResultItem = DispatchDeliveryItem & {
|
||||
unitIndex: number;
|
||||
sendType: number;
|
||||
note: string;
|
||||
responseMessage: string;
|
||||
};
|
||||
|
||||
export async function dispatchKuaishouCloudFulfillmentTask(
|
||||
task: TaskRow,
|
||||
options: JsonObject = {}
|
||||
@@ -80,12 +93,51 @@ export async function dispatchKuaishouCloudFulfillmentTask(
|
||||
const syncedFlow = synced.flow;
|
||||
const syncedTaskContext = synced.taskContext;
|
||||
|
||||
const dispatchResult = await useCloudtentaclesSku({
|
||||
...cloudContext,
|
||||
id: syncedFlow.binding.skuId,
|
||||
virtualNumberId: syncedFlow.binding.vnId,
|
||||
phone: syncedFlow.binding.vnPhone,
|
||||
});
|
||||
const deliveryItems = resolveDispatchDeliveryItems(syncedFlow);
|
||||
if (deliveryItems.length === 0) {
|
||||
throw createHttpError("当前任务缺少 cloud 发货物品配置", {
|
||||
statusCode: 409,
|
||||
errorCode: "kuaishou_cloud_missing_delivery_items",
|
||||
});
|
||||
}
|
||||
|
||||
const dispatchResults: DispatchResultItem[] = [];
|
||||
for (const item of deliveryItems) {
|
||||
for (let index = 0; index < item.quantity; index += 1) {
|
||||
const dispatchResult = await useCloudtentaclesSku({
|
||||
...cloudContext,
|
||||
id: item.cloudSkuId,
|
||||
virtualNumberId: syncedFlow.binding.vnId,
|
||||
phone: syncedFlow.binding.vnPhone,
|
||||
});
|
||||
dispatchResults.push({
|
||||
cloudSkuId: item.cloudSkuId,
|
||||
cloudSkuName: item.cloudSkuName,
|
||||
unitIndex: index + 1,
|
||||
quantity: item.quantity,
|
||||
sendType: Number(dispatchResult.sendType || 0) || 0,
|
||||
note: String(dispatchResult.note || "").trim(),
|
||||
responseMessage: String(dispatchResult.responseMessage || "").trim(),
|
||||
});
|
||||
}
|
||||
}
|
||||
const firstDispatchResult: DispatchResultItem = dispatchResults[0] || {
|
||||
cloudSkuId: syncedFlow.binding.skuId,
|
||||
cloudSkuName: syncedFlow.binding.skuName,
|
||||
quantity: 1,
|
||||
unitIndex: 1,
|
||||
sendType: 0,
|
||||
note: "",
|
||||
responseMessage: "",
|
||||
};
|
||||
const dispatchSummary =
|
||||
dispatchResults.length > 1
|
||||
? `cloudtentacles 发货成功,共 ${dispatchResults.length} 次`
|
||||
: String(
|
||||
firstDispatchResult.responseMessage ||
|
||||
firstDispatchResult.note ||
|
||||
"cloudtentacles 发货成功"
|
||||
).trim();
|
||||
|
||||
const nextContext = {
|
||||
...syncedTaskContext,
|
||||
@@ -102,12 +154,9 @@ export async function dispatchKuaishouCloudFulfillmentTask(
|
||||
status: "success",
|
||||
dispatchAt: now,
|
||||
dispatchBy: actor,
|
||||
sendType: Number(dispatchResult.sendType || 0) || 0,
|
||||
note: String(
|
||||
dispatchResult.note ||
|
||||
dispatchResult.responseMessage ||
|
||||
"cloudtentacles 发货成功"
|
||||
).trim(),
|
||||
sendType: Number(firstDispatchResult.sendType || 0) || 0,
|
||||
note: dispatchSummary,
|
||||
items: dispatchResults,
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -116,11 +165,7 @@ export async function dispatchKuaishouCloudFulfillmentTask(
|
||||
task_status: "dispatched_pending_return",
|
||||
delivery_status: "delivered",
|
||||
result_code: "kuaishou_cloud_dispatched",
|
||||
result_message: String(
|
||||
dispatchResult.responseMessage ||
|
||||
dispatchResult.note ||
|
||||
"cloudtentacles 发货成功"
|
||||
).trim(),
|
||||
result_message: dispatchSummary,
|
||||
user_action_status: "not_required",
|
||||
last_error: "",
|
||||
context_json: JSON.stringify(nextContext),
|
||||
@@ -142,8 +187,10 @@ export async function dispatchKuaishouCloudFulfillmentTask(
|
||||
skuId: syncedFlow.binding.skuId,
|
||||
vnId: syncedFlow.binding.vnId,
|
||||
vnPhoneMasked: maskPhone(syncedFlow.binding.vnPhone),
|
||||
sendType: dispatchResult.sendType,
|
||||
note: dispatchResult.note,
|
||||
sendType: firstDispatchResult.sendType,
|
||||
note: dispatchSummary,
|
||||
deliveryItems,
|
||||
dispatchResults,
|
||||
actor,
|
||||
},
|
||||
now
|
||||
@@ -173,6 +220,43 @@ export async function dispatchKuaishouCloudFulfillmentTask(
|
||||
};
|
||||
}
|
||||
|
||||
function resolveDispatchDeliveryItems(flow: JsonObject): DispatchDeliveryItem[] {
|
||||
const rawItems = Array.isArray(flow.deliveryItems) ? flow.deliveryItems : [];
|
||||
const items = rawItems
|
||||
.map((item: unknown) => {
|
||||
const source = item && typeof item === "object" ? item as JsonObject : {};
|
||||
const cloudSkuId = Number(source.cloudSkuId || source.skuId || 0) || 0;
|
||||
const quantity = Number(source.quantity || 1) || 1;
|
||||
if (!Number.isInteger(cloudSkuId) || cloudSkuId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
cloudSkuId,
|
||||
cloudSkuName: String(source.cloudSkuName || source.skuName || "").trim(),
|
||||
quantity: Number.isInteger(quantity) && quantity > 0 ? quantity : 1,
|
||||
};
|
||||
})
|
||||
.filter((item): item is DispatchDeliveryItem => Boolean(item));
|
||||
|
||||
if (items.length > 0) {
|
||||
return items;
|
||||
}
|
||||
|
||||
const fallbackSkuId = Number(flow?.binding?.skuId || 0) || 0;
|
||||
if (!fallbackSkuId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
cloudSkuId: fallbackSkuId,
|
||||
cloudSkuName: String(flow?.binding?.skuName || "").trim(),
|
||||
quantity: 1,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
export async function returnKuaishouCloudFulfillmentTask(
|
||||
task: TaskRow,
|
||||
options: JsonObject = {}
|
||||
|
||||
Reference in New Issue
Block a user