优化后台订单列表展示并支持商品标题与SKU规格
This commit is contained in:
@@ -8,7 +8,12 @@ import {
|
||||
import { canRegenerateClaimLinkStatus, isTaskFinalStatus } from '../../domain/task-status.js'
|
||||
|
||||
import type { AdminViewerSessionInput } from '../../types/admin/read-inputs.js'
|
||||
import type { OrderItemRow, TaskRow } from '../../types/repository/rows.js'
|
||||
import type {
|
||||
KuaishouIndustryVoucherRow,
|
||||
OrderItemRow,
|
||||
OrderRow,
|
||||
TaskRow,
|
||||
} from '../../types/repository/rows.js'
|
||||
|
||||
type CloudSourceLabelMap = Map<string, string> | Record<string, string>
|
||||
|
||||
@@ -422,23 +427,154 @@ export function parseTaskState(task: TaskLike | null | undefined): JsonRecord {
|
||||
return parseTaskStateValue(task)
|
||||
}
|
||||
|
||||
export function resolveOrderItemTitle(item: Partial<OrderItemRow> | null | undefined): string {
|
||||
if (!item) {
|
||||
return ''
|
||||
export function parseExtRecord(value: unknown): JsonRecord {
|
||||
if (!value) return {}
|
||||
if (typeof value === 'object' && !Array.isArray(value)) {
|
||||
return value as JsonRecord
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return safeParseJson(value)
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
export function resolveOrderItemTitle(
|
||||
item: Partial<OrderItemRow> | null | undefined,
|
||||
fallbackOrder?: Partial<OrderRow> | null | undefined,
|
||||
vouchers?: KuaishouIndustryVoucherRow[] | null | undefined,
|
||||
): string {
|
||||
if (item) {
|
||||
const spec = safeParseJson(item.spec_json)
|
||||
const snapshot = safeParseJson(item.item_snapshot_json)
|
||||
const ksSendCode = asJsonObject(snapshot.kuaishouSendCode)
|
||||
const ksRawPayload = asJsonObject(snapshot.kuaishouSendCodeRawPayload)
|
||||
const ksParams = asJsonObject(snapshot.params)
|
||||
|
||||
const titleCandidate = pickFirstNonEmpty([
|
||||
spec.itemTitle,
|
||||
spec.title,
|
||||
spec.Title,
|
||||
spec.item_title,
|
||||
ksSendCode.itemTitle,
|
||||
ksRawPayload.itemTitle,
|
||||
ksParams.itemTitle,
|
||||
snapshot.itemTitle,
|
||||
spec.goods_name,
|
||||
spec.goodsName,
|
||||
spec.productName,
|
||||
snapshot.productName,
|
||||
])
|
||||
|
||||
if (titleCandidate) {
|
||||
return titleCandidate
|
||||
}
|
||||
}
|
||||
|
||||
const spec = safeParseJson(item.spec_json)
|
||||
if (fallbackOrder) {
|
||||
const orderPayload = safeParseJson(fallbackOrder.raw_payload_json)
|
||||
const body = asJsonObject(
|
||||
orderPayload.body && typeof orderPayload.body === 'object' ? orderPayload.body : orderPayload,
|
||||
)
|
||||
const fromOrder = pickFirstNonEmpty([
|
||||
body.itemTitle,
|
||||
orderPayload.itemTitle,
|
||||
body.productName,
|
||||
orderPayload.productName,
|
||||
body.title,
|
||||
orderPayload.title,
|
||||
])
|
||||
if (fromOrder) {
|
||||
return fromOrder
|
||||
}
|
||||
}
|
||||
|
||||
return pickFirstNonEmpty([
|
||||
spec.title,
|
||||
spec.Title,
|
||||
spec.itemTitle,
|
||||
spec.item_title,
|
||||
spec.goods_name,
|
||||
spec.goodsName,
|
||||
item.sku_name,
|
||||
item.sku_code,
|
||||
])
|
||||
if (Array.isArray(vouchers) && vouchers.length > 0) {
|
||||
for (const voucher of vouchers) {
|
||||
const voucherPayload = safeParseJson(voucher.raw_payload_json)
|
||||
const body = asJsonObject(
|
||||
voucherPayload.body && typeof voucherPayload.body === 'object'
|
||||
? voucherPayload.body
|
||||
: voucherPayload,
|
||||
)
|
||||
const fromVoucher = pickFirstNonEmpty([body.itemTitle, voucherPayload.itemTitle, body.title])
|
||||
if (fromVoucher) {
|
||||
return fromVoucher
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (item) {
|
||||
return pickFirstNonEmpty([item.sku_name, item.sku_code])
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
export function resolveOrderItemSkuNick(
|
||||
item: Partial<OrderItemRow> | null | undefined,
|
||||
fallbackOrder?: Partial<OrderRow> | null | undefined,
|
||||
vouchers?: KuaishouIndustryVoucherRow[] | null | undefined,
|
||||
): string {
|
||||
if (item) {
|
||||
const spec = safeParseJson(item.spec_json)
|
||||
const snapshot = safeParseJson(item.item_snapshot_json)
|
||||
const ksSendCode = asJsonObject(snapshot.kuaishouSendCode)
|
||||
const ksRawPayload = asJsonObject(snapshot.kuaishouSendCodeRawPayload)
|
||||
const ksParams = asJsonObject(snapshot.params)
|
||||
const extFromSnapshot = parseExtRecord(ksRawPayload.ext || ksParams.ext || snapshot.ext)
|
||||
const extFromSpec = parseExtRecord(spec.ext)
|
||||
|
||||
const candidateSkuNick = pickFirstNonEmpty([
|
||||
spec.skuNick,
|
||||
spec.sku_nick,
|
||||
ksSendCode.skuNick,
|
||||
extFromSnapshot.skuNick,
|
||||
extFromSpec.skuNick,
|
||||
snapshot.externalSkuName,
|
||||
snapshot.externalSkuNameNormalized,
|
||||
snapshot.skuNick,
|
||||
spec.skuName,
|
||||
])
|
||||
|
||||
if (candidateSkuNick) {
|
||||
return candidateSkuNick
|
||||
}
|
||||
|
||||
const resolvedTitle = resolveOrderItemTitle(item, fallbackOrder, vouchers)
|
||||
if (item.sku_name && item.sku_name.trim() !== resolvedTitle) {
|
||||
return item.sku_name.trim()
|
||||
}
|
||||
}
|
||||
|
||||
if (fallbackOrder) {
|
||||
const orderPayload = safeParseJson(fallbackOrder.raw_payload_json)
|
||||
const body = asJsonObject(
|
||||
orderPayload.body && typeof orderPayload.body === 'object' ? orderPayload.body : orderPayload,
|
||||
)
|
||||
const ext = parseExtRecord(body.ext || orderPayload.ext)
|
||||
const fromOrder = pickFirstNonEmpty([ext.skuNick, body.skuNick, orderPayload.skuNick])
|
||||
if (fromOrder) {
|
||||
return fromOrder
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(vouchers) && vouchers.length > 0) {
|
||||
for (const voucher of vouchers) {
|
||||
const voucherPayload = safeParseJson(voucher.raw_payload_json)
|
||||
const body = asJsonObject(
|
||||
voucherPayload.body && typeof voucherPayload.body === 'object'
|
||||
? voucherPayload.body
|
||||
: voucherPayload,
|
||||
)
|
||||
const ext = parseExtRecord(body.ext || voucherPayload.ext)
|
||||
const fromVoucher = pickFirstNonEmpty([ext.skuNick, body.skuNick, voucherPayload.skuNick])
|
||||
if (fromVoucher) {
|
||||
return fromVoucher
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
export function resolveOrderItemDeliveryMode(
|
||||
|
||||
Reference in New Issue
Block a user