增加卡券销毁冻结状态校验
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import {
|
||||
isKuaishouIndustryEticketDestroyableStatus,
|
||||
resolveKuaishouIndustryEticketDisplayStatus,
|
||||
} from './check-available-service.js'
|
||||
|
||||
test('resolveKuaishouIndustryEticketDisplayStatus 按券码读取展示状态', () => {
|
||||
const response = {
|
||||
etickets: [
|
||||
{ code: 'OTHER-CODE', displayStatus: 'available' },
|
||||
{ code: 'TARGET-CODE', displayStatus: ' Frozen ' },
|
||||
],
|
||||
}
|
||||
|
||||
assert.equal(resolveKuaishouIndustryEticketDisplayStatus(response, 'target-code'), 'frozen')
|
||||
assert.equal(isKuaishouIndustryEticketDestroyableStatus('frozen'), true)
|
||||
})
|
||||
|
||||
test('resolveKuaishouIndustryEticketDisplayStatus 兼容 data 内的 status 字段', () => {
|
||||
const response = {
|
||||
data: {
|
||||
eTicketList: [{ id: 'TARGET-CODE', status: 'FROZEN' }],
|
||||
},
|
||||
}
|
||||
|
||||
assert.equal(resolveKuaishouIndustryEticketDisplayStatus(response, 'TARGET-CODE'), 'frozen')
|
||||
})
|
||||
|
||||
test('isKuaishouIndustryEticketDestroyableStatus 拒绝非冻结和缺失状态', () => {
|
||||
assert.equal(isKuaishouIndustryEticketDestroyableStatus('available'), false)
|
||||
assert.equal(isKuaishouIndustryEticketDestroyableStatus(''), false)
|
||||
})
|
||||
|
||||
test('resolveKuaishouIndustryEticketDisplayStatus 拒绝其他券码的冻结状态', () => {
|
||||
assert.equal(
|
||||
resolveKuaishouIndustryEticketDisplayStatus(
|
||||
{ etickets: [{ code: 'OTHER-CODE', displayStatus: 'frozen' }] },
|
||||
'TARGET-CODE',
|
||||
),
|
||||
'',
|
||||
)
|
||||
})
|
||||
@@ -6,6 +6,8 @@ import {
|
||||
type JsonObject,
|
||||
} from './openapi-values.js'
|
||||
|
||||
export const KUAISHOU_INDUSTRY_DESTROYABLE_DISPLAY_STATUS = 'frozen'
|
||||
|
||||
export type KuaishouIndustryAvailableEticketInput = {
|
||||
id?: unknown
|
||||
code?: unknown
|
||||
@@ -44,6 +46,41 @@ export function checkKuaishouIndustryEticketAvailable(
|
||||
})
|
||||
}
|
||||
|
||||
export function resolveKuaishouIndustryEticketDisplayStatus(
|
||||
response: unknown,
|
||||
voucherCode: unknown,
|
||||
): string {
|
||||
const etickets = extractAvailableEtickets(response)
|
||||
const normalizedVoucherCode = String(voucherCode || '')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
const matched = normalizedVoucherCode
|
||||
? etickets.find((eticket) =>
|
||||
[eticket.code, eticket.id].some(
|
||||
(value) =>
|
||||
String(value || '')
|
||||
.trim()
|
||||
.toLowerCase() === normalizedVoucherCode,
|
||||
),
|
||||
)
|
||||
: undefined
|
||||
const soleEticket = etickets.length === 1 ? etickets[0] : undefined
|
||||
const soleEticketHasIdentity = Boolean(String(soleEticket?.code || soleEticket?.id || '').trim())
|
||||
const target = matched || (!soleEticketHasIdentity ? soleEticket : undefined)
|
||||
|
||||
return String(target?.displayStatus || target?.status || '')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
export function isKuaishouIndustryEticketDestroyableStatus(displayStatus: unknown): boolean {
|
||||
return (
|
||||
String(displayStatus || '')
|
||||
.trim()
|
||||
.toLowerCase() === KUAISHOU_INDUSTRY_DESTROYABLE_DISPLAY_STATUS
|
||||
)
|
||||
}
|
||||
|
||||
function normalizeAvailableEtickets(value: unknown): JsonObject[] {
|
||||
if (!Array.isArray(value)) {
|
||||
return []
|
||||
@@ -70,3 +107,35 @@ function normalizeAvailableEticket(value: unknown): JsonObject | null {
|
||||
|
||||
return pickDefinedBizParams({ id, code, num })
|
||||
}
|
||||
|
||||
function extractAvailableEtickets(value: unknown): JsonObject[] {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
return []
|
||||
}
|
||||
|
||||
const current = value as JsonObject
|
||||
const data =
|
||||
current.data && typeof current.data === 'object' && !Array.isArray(current.data)
|
||||
? (current.data as JsonObject)
|
||||
: null
|
||||
const candidates = [
|
||||
current.etickets,
|
||||
current.eTicketList,
|
||||
current.eticketList,
|
||||
current.list,
|
||||
data?.etickets,
|
||||
data?.eTicketList,
|
||||
data?.eticketList,
|
||||
data?.list,
|
||||
]
|
||||
|
||||
for (const candidate of candidates) {
|
||||
if (Array.isArray(candidate)) {
|
||||
return candidate.filter((item): item is JsonObject =>
|
||||
Boolean(item && typeof item === 'object' && !Array.isArray(item)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user