task-68(ASIN 数据中心): 实现品牌数据库列表查询
品牌数据库面板即 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 绿。
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
/** 品牌数据库(不符合ASIN)列表加载适配(任务 68):GET /api/admin/invalid-asin-data。 */
|
||||
import { http } from '@/api/http'
|
||||
import { parseInvalidAsinPage, type InvalidAsinPageResult } from './invalid-asin-model'
|
||||
|
||||
export const INVALID_ASIN_ENDPOINT = '/api/admin/invalid-asin-data'
|
||||
|
||||
export interface InvalidAsinQuery {
|
||||
page: number
|
||||
pageSize: number
|
||||
dataValue?: string
|
||||
brand?: string
|
||||
groupId?: number | null
|
||||
}
|
||||
|
||||
function trimOrUndefined(value: unknown): string | undefined {
|
||||
return typeof value === 'string' && value.trim() ? value.trim() : undefined
|
||||
}
|
||||
|
||||
function toQuery(query: InvalidAsinQuery): Record<string, string | number> {
|
||||
const params: Record<string, string | number> = { page: query.page, page_size: query.pageSize }
|
||||
const dataValue = trimOrUndefined(query.dataValue)
|
||||
const brand = trimOrUndefined(query.brand)
|
||||
if (dataValue) params.data_value = dataValue
|
||||
if (brand) params.brand = brand
|
||||
if (typeof query.groupId === 'number' && query.groupId >= 1) params.group_id = query.groupId
|
||||
return params
|
||||
}
|
||||
|
||||
export async function fetchInvalidAsinPage(query: InvalidAsinQuery): Promise<InvalidAsinPageResult> {
|
||||
const { data } = await http.get<unknown>(INVALID_ASIN_ENDPOINT, { params: toQuery(query) })
|
||||
return parseInvalidAsinPage(data)
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/** 品牌数据库(不符合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
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readSource } from './helpers.ts'
|
||||
import { parseInvalidAsinPage, recordSourceLabel, toInvalidAsinItem } from '../src/pages/invalidasin/invalid-asin-model.ts'
|
||||
|
||||
test('test_task_068_brand_db_list_normal_primary_path', () => {
|
||||
// 正常主路径:解析 InvalidAsinDataPageVo(camel) 列表项。
|
||||
const page = parseInvalidAsinPage({
|
||||
success: true,
|
||||
data: {
|
||||
items: [
|
||||
{
|
||||
id: 1,
|
||||
dataValue: 'B0ABC',
|
||||
brand: '苹果',
|
||||
groupId: 2,
|
||||
groupName: '华东组',
|
||||
recordSource: 'MANUAL',
|
||||
createdAt: '2026-01-01T10:00:00',
|
||||
},
|
||||
],
|
||||
total: 1,
|
||||
page: 1,
|
||||
pageSize: 15,
|
||||
},
|
||||
})
|
||||
assert.equal(page.items.length, 1)
|
||||
const item = page.items[0]
|
||||
assert.equal(item.dataValue, 'B0ABC')
|
||||
assert.equal(item.brand, '苹果')
|
||||
assert.equal(item.groupName, '华东组')
|
||||
assert.equal(item.recordSource, 'MANUAL')
|
||||
assert.equal(page.total, 1)
|
||||
})
|
||||
|
||||
test('test_task_068_brand_db_list_normal_variant_input', () => {
|
||||
// 正常变体:缺分组/来源给默认;来源标签映射。
|
||||
const item = toInvalidAsinItem({ id: 2, dataValue: 'X', brand: 'b', recordSource: 'AUTO' })
|
||||
assert.equal(item?.groupId, null)
|
||||
assert.equal(item?.groupName, '')
|
||||
assert.equal(item?.createdAt, undefined)
|
||||
assert.equal(recordSourceLabel('MANUAL'), '手动新增')
|
||||
assert.equal(recordSourceLabel('AUTO'), '自动导入')
|
||||
assert.equal(recordSourceLabel(''), '自动导入')
|
||||
})
|
||||
|
||||
test('test_task_068_brand_db_list_normal_repeated_operation_is_idempotent', () => {
|
||||
// 正常重复:解析不改输入、结果稳定。
|
||||
const payload = { data: { items: [{ id: 3, dataValue: 'A', brand: 'b' }], total: 1, page: 1, pageSize: 15 } }
|
||||
assert.deepEqual(parseInvalidAsinPage(payload), parseInvalidAsinPage(payload))
|
||||
})
|
||||
|
||||
test('test_task_068_brand_db_list_boundary_empty_input', () => {
|
||||
// 边界空值:空负载回默认分页结果。
|
||||
const page = parseInvalidAsinPage({})
|
||||
assert.deepEqual(page.items, [])
|
||||
assert.equal(page.total, 0)
|
||||
assert.equal(page.page, 1)
|
||||
assert.equal(page.pageSize, 15)
|
||||
})
|
||||
|
||||
test('test_task_068_brand_db_list_boundary_single_item', () => {
|
||||
// 边界单元素:单条记录;缺 id 行被过滤。
|
||||
const page = parseInvalidAsinPage({
|
||||
data: { items: [{ id: 7, dataValue: 'Z' }, { dataValue: 'no-id' }], total: 2, page: 1, pageSize: 15 },
|
||||
})
|
||||
assert.equal(page.items.length, 1)
|
||||
assert.equal(page.items[0].id, 7)
|
||||
})
|
||||
|
||||
test('test_task_068_brand_db_list_boundary_limit_or_missing_field', () => {
|
||||
// 边界上限/缺字段:缺来源按 AUTO 展示;dataValue 空保留空串。
|
||||
const item = toInvalidAsinItem({ id: 9, dataValue: '' })
|
||||
assert.equal(item?.dataValue, '')
|
||||
assert.equal(item?.recordSource, '')
|
||||
})
|
||||
|
||||
test('test_task_068_brand_db_list_invalid_input_rejected', () => {
|
||||
// 异常输入:success=false 抛后端 message。
|
||||
assert.throws(() => parseInvalidAsinPage({ success: false, message: '无权访问' }), /无权访问/)
|
||||
assert.equal(toInvalidAsinItem('garbage'), null)
|
||||
})
|
||||
|
||||
test('test_task_068_brand_db_list_dependency_failure_returns_actionable_message', () => {
|
||||
// 依赖失败/加载走 adapter:GET /api/admin/invalid-asin-data 解析分页。
|
||||
const api = readSource('src/pages/invalidasin/invalid-asin-api.ts')
|
||||
assert.match(api, /invalid-asin-data/)
|
||||
assert.match(api, /http\.get/)
|
||||
assert.match(api, /parseInvalidAsinPage/)
|
||||
const mod = readSource('src/pages/invalidasin/invalid-asin-model.ts')
|
||||
assert.equal(/axios|http\./.test(mod), false, '品牌数据库模型保持纯逻辑')
|
||||
})
|
||||
Reference in New Issue
Block a user