43463e12ca
品牌数据库面板即 invalid-asin-data;新增 invalid-asin-model.ts 解析 InvalidAsinDataPageVo(camel dataValue/brand/groupId/groupName/recordSource/createdAt) + recordSourceLabel(MANUAL 手动新增), invalid-asin-api.ts fetchInvalidAsinPage GET /api/admin/invalid-asin-data(支持 ASIN/品牌/分组过滤)。 8 用例全过,499 单测 + build 绿。
77 lines
2.8 KiB
TypeScript
77 lines
2.8 KiB
TypeScript
/** 品牌数据库(不符合ASIN)列表模型(任务 68):解析 InvalidAsinDataPageVo(camel),纯逻辑。 */
|
|
import { unwrap } from '../../api/envelope.ts'
|
|
import { ASIN_PAGE_DEFAULT_SIZE } from '../asin/asin-filter.ts'
|
|
|
|
export type InvalidAsinSource = 'MANUAL' | 'AUTO'
|
|
|
|
export interface InvalidAsinItem {
|
|
id: number
|
|
dataValue: string
|
|
brand: string
|
|
groupId: number | null
|
|
groupName: string
|
|
recordSource: string
|
|
createdAt?: string
|
|
}
|
|
|
|
export interface InvalidAsinPageResult {
|
|
items: InvalidAsinItem[]
|
|
total: number
|
|
page: number
|
|
pageSize: number
|
|
}
|
|
|
|
export function emptyInvalidAsinPage(): InvalidAsinPageResult {
|
|
return { items: [], total: 0, page: 1, pageSize: ASIN_PAGE_DEFAULT_SIZE }
|
|
}
|
|
|
|
function text(value: unknown): string {
|
|
return typeof value === 'string' ? value.trim() : ''
|
|
}
|
|
|
|
function numberOrNull(value: unknown): number | null {
|
|
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
|
|
}
|
|
|
|
/** 来源展示标签:MANUAL 手动新增,其余(含空)自动导入。 */
|
|
export function recordSourceLabel(source: string | null | undefined): string {
|
|
return (source || '').trim().toUpperCase() === 'MANUAL' ? '手动新增' : '自动导入'
|
|
}
|
|
|
|
/** 解析单条品牌数据库项;缺 id 视为无效。 */
|
|
export function toInvalidAsinItem(raw: unknown): InvalidAsinItem | null {
|
|
if (!raw || typeof raw !== 'object') return null
|
|
const record = raw as Record<string, unknown>
|
|
const id = numberOrNull(record.id)
|
|
if (id === null) return null
|
|
const item: InvalidAsinItem = {
|
|
id,
|
|
dataValue: text(record.dataValue ?? record.data_value),
|
|
brand: text(record.brand),
|
|
groupId: numberOrNull(record.groupId ?? record.group_id),
|
|
groupName: text(record.groupName ?? record.group_name),
|
|
recordSource: (text(record.recordSource ?? record.record_source)).toUpperCase(),
|
|
}
|
|
const createdAt = text(record.createdAt ?? record.created_at)
|
|
if (createdAt) item.createdAt = createdAt
|
|
return item
|
|
}
|
|
|
|
/** 归一化分页负载(信封或已解包 VO)为前端结果。 */
|
|
export function parseInvalidAsinPage(payload: unknown): InvalidAsinPageResult {
|
|
const out = emptyInvalidAsinPage()
|
|
const core = unwrap<unknown>(payload)
|
|
if (!core || typeof core !== 'object') return out
|
|
const record = core as Record<string, unknown>
|
|
if (Array.isArray(record.items)) {
|
|
out.items = record.items
|
|
.map((raw) => toInvalidAsinItem(raw))
|
|
.filter((item): item is InvalidAsinItem => item !== null)
|
|
}
|
|
if (typeof record.total === 'number') out.total = Math.floor(record.total)
|
|
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
|
|
const rawSize = record.pageSize ?? record.page_size
|
|
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
|
|
return out
|
|
}
|