优化多账号选择
This commit is contained in:
@@ -53,13 +53,12 @@ type DeliveryTaskDeps = {
|
||||
randomId?: (prefix?: string) => string
|
||||
}
|
||||
|
||||
type RuntimeDeliveryTaskDeps = Required<Pick<
|
||||
DeliveryTaskDeps,
|
||||
'updateTask'
|
||||
| 'createTaskClaimToken'
|
||||
| 'notifyTaskAutoManualReview'
|
||||
| 'nowIso'
|
||||
>>
|
||||
type RuntimeDeliveryTaskDeps = Required<
|
||||
Pick<
|
||||
DeliveryTaskDeps,
|
||||
'updateTask' | 'createTaskClaimToken' | 'notifyTaskAutoManualReview' | 'nowIso'
|
||||
>
|
||||
>
|
||||
|
||||
type TaskContext = {
|
||||
[key: string]: unknown
|
||||
@@ -103,11 +102,18 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
}
|
||||
|
||||
const itemMap = new Map(orderItems.map((item) => [item.id, item]))
|
||||
const preparedTasks = await Promise.all(existingTasks.map((task) => preparePaidTask({
|
||||
...task,
|
||||
skuCode: itemMap.get(task.order_item_id)?.sku_code || '',
|
||||
skuName: itemMap.get(task.order_item_id)?.sku_name || '',
|
||||
}, runtimeDeps)))
|
||||
const preparedTasks = await Promise.all(
|
||||
existingTasks.map((task) =>
|
||||
preparePaidTask(
|
||||
{
|
||||
...task,
|
||||
skuCode: itemMap.get(task.order_item_id)?.sku_code || '',
|
||||
skuName: itemMap.get(task.order_item_id)?.sku_name || '',
|
||||
},
|
||||
runtimeDeps,
|
||||
),
|
||||
),
|
||||
)
|
||||
return preparedTasks.filter(isTaskRow)
|
||||
}
|
||||
|
||||
@@ -125,6 +131,11 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
const cloudtentaclesConfig = parseJsonObject(fulfillmentConfig.cloudtentacles)
|
||||
const kuaishouConsumeConfig = parseJsonObject(fulfillmentConfig.kuaishouConsume)
|
||||
const kuaishouShopConfig = parseJsonObject(fulfillmentConfig.kuaishouShop)
|
||||
const cloudSourceKeys = normalizeStringArray(cloudtentaclesConfig.cloudSourceKeys)
|
||||
const resolvedCloudSourceKey =
|
||||
cloudSourceKeys.length === 1
|
||||
? String(cloudtentaclesConfig.resolvedSourceKey || cloudSourceKeys[0] || '').trim()
|
||||
: ''
|
||||
const deliveryItems = normalizeCloudDeliveryItems(cloudtentaclesConfig)
|
||||
const primaryDeliveryItem = deliveryItems[0] || {
|
||||
cloudSkuId: Number(cloudtentaclesConfig.skuId || 0) || 0,
|
||||
@@ -134,9 +145,8 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
|
||||
for (let index = 0; index < quantity; index += 1) {
|
||||
const createdAt = getNowIso()
|
||||
const initialStatus = order.pay_status === 'paid'
|
||||
? resolvePaidTaskStatus(profile)
|
||||
: TASK_STATUS.PENDING_PAYMENT
|
||||
const initialStatus =
|
||||
order.pay_status === 'paid' ? resolvePaidTaskStatus(profile) : TASK_STATUS.PENDING_PAYMENT
|
||||
|
||||
const task = await createDeliveryTask({
|
||||
orderId: order.id,
|
||||
@@ -168,78 +178,90 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
skuName: item.sku_name,
|
||||
kuaishouCloudFulfillment: isKuaishouCloudExecutor(profile.executor_key)
|
||||
? {
|
||||
flowType: 'kuaishou_cloud_fulfillment',
|
||||
configId: String(fulfillmentConfig.configId || '').trim(),
|
||||
internalSkuCode: item.sku_code,
|
||||
internalSkuName: item.sku_name,
|
||||
deliveryItems,
|
||||
ticket: {
|
||||
code: '',
|
||||
status: 'pending',
|
||||
capturedAt: null,
|
||||
capturedBy: null,
|
||||
verifiedAt: null,
|
||||
oid: '',
|
||||
formToken: '',
|
||||
leftCount: 0,
|
||||
goodsTitle: '',
|
||||
},
|
||||
binding: {
|
||||
prepareStatus: 'pending',
|
||||
cloudSourceKeys: normalizeStringArray(cloudtentaclesConfig.cloudSourceKeys),
|
||||
resolvedSourceKey: String(cloudtentaclesConfig.resolvedSourceKey || '').trim(),
|
||||
skuId: primaryDeliveryItem.cloudSkuId,
|
||||
skuName: primaryDeliveryItem.cloudSkuName,
|
||||
vnKey: '1',
|
||||
vnId: 0,
|
||||
vnPhone: '',
|
||||
bindUrl: '',
|
||||
bindPreparedAt: null,
|
||||
bindExpiresAt: null,
|
||||
bindProbeAt: null,
|
||||
bindProbeStatus: '',
|
||||
bindProbeMessage: '',
|
||||
},
|
||||
role: {
|
||||
status: 'pending',
|
||||
name: '',
|
||||
rid: '',
|
||||
refreshedAt: null,
|
||||
errorMessage: '',
|
||||
rawInfo: null,
|
||||
},
|
||||
purchase: {
|
||||
autoBuyEnabled: cloudtentaclesConfig.autoBuyEnabled !== false,
|
||||
minAssetReserve: Number(cloudtentaclesConfig.minAssetReserve || 0) || 0,
|
||||
usedKnapsack: false,
|
||||
purchaseTriggered: false,
|
||||
assetBefore: 0,
|
||||
assetAfter: 0,
|
||||
purchaseAt: null,
|
||||
},
|
||||
dispatch: {
|
||||
status: 'pending',
|
||||
dispatchAt: null,
|
||||
dispatchBy: null,
|
||||
sendType: 0,
|
||||
note: '',
|
||||
},
|
||||
returnNumber: {
|
||||
status: 'pending',
|
||||
returnedAt: null,
|
||||
returnedBy: null,
|
||||
autoReturnEnabled: cloudtentaclesConfig.autoReturnNumberAfterDispatch === true,
|
||||
},
|
||||
consume: {
|
||||
status: 'pending',
|
||||
shopId: String(kuaishouConsumeConfig.shopId || kuaishouShopConfig.shopId || itemSnapshot.shopId || order.shop_id || '').trim(),
|
||||
shopName: String(kuaishouConsumeConfig.shopName || kuaishouShopConfig.kshopName || itemSnapshot.shopName || order.shop_name || '').trim(),
|
||||
autoConsumeEnabled: kuaishouConsumeConfig.autoConsumeAfterDispatch === true,
|
||||
consumedAt: null,
|
||||
errorMessage: '',
|
||||
},
|
||||
notes: String(fulfillmentConfig.notes || '').trim(),
|
||||
}
|
||||
flowType: 'kuaishou_cloud_fulfillment',
|
||||
configId: String(fulfillmentConfig.configId || '').trim(),
|
||||
internalSkuCode: item.sku_code,
|
||||
internalSkuName: item.sku_name,
|
||||
deliveryItems,
|
||||
ticket: {
|
||||
code: '',
|
||||
status: 'pending',
|
||||
capturedAt: null,
|
||||
capturedBy: null,
|
||||
verifiedAt: null,
|
||||
oid: '',
|
||||
formToken: '',
|
||||
leftCount: 0,
|
||||
goodsTitle: '',
|
||||
},
|
||||
binding: {
|
||||
prepareStatus: 'pending',
|
||||
cloudSourceKeys,
|
||||
resolvedSourceKey: resolvedCloudSourceKey,
|
||||
skuId: primaryDeliveryItem.cloudSkuId,
|
||||
skuName: primaryDeliveryItem.cloudSkuName,
|
||||
vnKey: '1',
|
||||
vnId: 0,
|
||||
vnPhone: '',
|
||||
bindUrl: '',
|
||||
bindPreparedAt: null,
|
||||
bindExpiresAt: null,
|
||||
bindProbeAt: null,
|
||||
bindProbeStatus: '',
|
||||
bindProbeMessage: '',
|
||||
},
|
||||
role: {
|
||||
status: 'pending',
|
||||
name: '',
|
||||
rid: '',
|
||||
refreshedAt: null,
|
||||
errorMessage: '',
|
||||
rawInfo: null,
|
||||
},
|
||||
purchase: {
|
||||
autoBuyEnabled: cloudtentaclesConfig.autoBuyEnabled !== false,
|
||||
minAssetReserve: Number(cloudtentaclesConfig.minAssetReserve || 0) || 0,
|
||||
usedKnapsack: false,
|
||||
purchaseTriggered: false,
|
||||
assetBefore: 0,
|
||||
assetAfter: 0,
|
||||
purchaseAt: null,
|
||||
},
|
||||
dispatch: {
|
||||
status: 'pending',
|
||||
dispatchAt: null,
|
||||
dispatchBy: null,
|
||||
sendType: 0,
|
||||
note: '',
|
||||
},
|
||||
returnNumber: {
|
||||
status: 'pending',
|
||||
returnedAt: null,
|
||||
returnedBy: null,
|
||||
autoReturnEnabled: cloudtentaclesConfig.autoReturnNumberAfterDispatch === true,
|
||||
},
|
||||
consume: {
|
||||
status: 'pending',
|
||||
shopId: String(
|
||||
kuaishouConsumeConfig.shopId ||
|
||||
kuaishouShopConfig.shopId ||
|
||||
itemSnapshot.shopId ||
|
||||
order.shop_id ||
|
||||
'',
|
||||
).trim(),
|
||||
shopName: String(
|
||||
kuaishouConsumeConfig.shopName ||
|
||||
kuaishouShopConfig.kshopName ||
|
||||
itemSnapshot.shopName ||
|
||||
order.shop_name ||
|
||||
'',
|
||||
).trim(),
|
||||
autoConsumeEnabled: kuaishouConsumeConfig.autoConsumeAfterDispatch === true,
|
||||
consumedAt: null,
|
||||
errorMessage: '',
|
||||
},
|
||||
notes: String(fulfillmentConfig.notes || '').trim(),
|
||||
}
|
||||
: null,
|
||||
}),
|
||||
createdAt,
|
||||
@@ -344,7 +366,9 @@ function parseTaskContext(task: { context_json?: unknown } | null | undefined):
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(String(value || '{}'))
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed as TaskContext : {}
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
||||
? (parsed as TaskContext)
|
||||
: {}
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
@@ -361,7 +385,9 @@ function parseJsonObject(value: unknown): JsonObject {
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(String(value || '{}'))
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed as JsonObject : {}
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
||||
? (parsed as JsonObject)
|
||||
: {}
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
@@ -385,7 +411,11 @@ async function resolveDynamicCloudtentaclesProfile(
|
||||
quantity: 1,
|
||||
}
|
||||
|
||||
if (!primaryDeliveryItem.cloudSkuId || !primaryDeliveryItem.cloudSkuName || cloudSourceKeys.length === 0) {
|
||||
if (
|
||||
!primaryDeliveryItem.cloudSkuId ||
|
||||
!primaryDeliveryItem.cloudSkuName ||
|
||||
cloudSourceKeys.length === 0
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -409,7 +439,10 @@ async function resolveDynamicCloudtentaclesProfile(
|
||||
cloudSourceKeys,
|
||||
skuId: primaryDeliveryItem.cloudSkuId,
|
||||
skuName: primaryDeliveryItem.cloudSkuName,
|
||||
resolvedSourceKey: String(cloudtentacles.resolvedSourceKey || '').trim(),
|
||||
resolvedSourceKey:
|
||||
cloudSourceKeys.length === 1
|
||||
? String(cloudtentacles.resolvedSourceKey || cloudSourceKeys[0] || '').trim()
|
||||
: '',
|
||||
deliveryItems,
|
||||
vnKey: '1',
|
||||
autoBuyEnabled: true,
|
||||
@@ -421,9 +454,10 @@ async function resolveDynamicCloudtentaclesProfile(
|
||||
shopName: String(snapshot.shopName || '').trim(),
|
||||
autoConsumeAfterDispatch: false,
|
||||
},
|
||||
notes: cloudtentacles.matchMode === 'cloudtentacles_override'
|
||||
? '91卡券商品名命中 cloudtentacles 覆盖规则'
|
||||
: '91卡券商品名自动匹配 cloudtentacles 商品',
|
||||
notes:
|
||||
cloudtentacles.matchMode === 'cloudtentacles_override'
|
||||
? '91卡券商品名命中 cloudtentacles 覆盖规则'
|
||||
: '91卡券商品名自动匹配 cloudtentacles 商品',
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -433,9 +467,7 @@ function normalizeStringArray(value: unknown): string[] {
|
||||
return []
|
||||
}
|
||||
|
||||
return Array.from(
|
||||
new Set(value.map((item) => String(item || '').trim()).filter(Boolean)),
|
||||
)
|
||||
return Array.from(new Set(value.map((item) => String(item || '').trim()).filter(Boolean)))
|
||||
}
|
||||
|
||||
function normalizeCloudDeliveryItems(value: JsonObject): Array<{
|
||||
@@ -446,7 +478,9 @@ function normalizeCloudDeliveryItems(value: JsonObject): Array<{
|
||||
const rawItems = Array.isArray(value.deliveryItems) ? value.deliveryItems : []
|
||||
const items = rawItems
|
||||
.map((item) => normalizeCloudDeliveryItem(item))
|
||||
.filter((item): item is { cloudSkuId: number; cloudSkuName: string; quantity: number } => Boolean(item))
|
||||
.filter((item): item is { cloudSkuId: number; cloudSkuName: string; quantity: number } =>
|
||||
Boolean(item),
|
||||
)
|
||||
|
||||
if (items.length > 0) {
|
||||
return mergeCloudDeliveryItems(items)
|
||||
@@ -457,17 +491,18 @@ function normalizeCloudDeliveryItems(value: JsonObject): Array<{
|
||||
return []
|
||||
}
|
||||
|
||||
return [{
|
||||
cloudSkuId,
|
||||
cloudSkuName: String(value.skuName || '').trim(),
|
||||
quantity: 1,
|
||||
}]
|
||||
return [
|
||||
{
|
||||
cloudSkuId,
|
||||
cloudSkuName: String(value.skuName || '').trim(),
|
||||
quantity: 1,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
function normalizeCloudDeliveryItem(value: unknown) {
|
||||
const source = value && typeof value === 'object' && !Array.isArray(value)
|
||||
? value as JsonObject
|
||||
: {}
|
||||
const source =
|
||||
value && typeof value === 'object' && !Array.isArray(value) ? (value as JsonObject) : {}
|
||||
const cloudSkuId = Number(source.cloudSkuId || source.skuId || 0) || 0
|
||||
if (!Number.isInteger(cloudSkuId) || cloudSkuId <= 0) {
|
||||
return null
|
||||
|
||||
Reference in New Issue
Block a user