task-110(任务与重复分析中心): 实现店铺数据任务列表
新增 shop-data-model.ts(按店铺分组任务列表 + 结果文件行解析) 与 shop-data-api.ts(GET /api/admin/shop-data-crawl-tasks 加载适配)。 TDD: task-110.test.ts 8 用例先 RED 后 GREEN。
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
/** 店铺数据任务列表加载适配(任务 110):GET /api/admin/shop-data-crawl-tasks + 筛选归一与解析。 */
|
||||||
|
import { http } from '@/api/http'
|
||||||
|
import { parseShopDataTaskPage, type ShopDataTaskPageResult } from './shop-data-model.ts'
|
||||||
|
import { toShopDataQuery, type ShopDataFilter } from './shop-data-filter.ts'
|
||||||
|
|
||||||
|
export const SHOP_DATA_CRAWL_TASKS_ENDPOINT = '/api/admin/shop-data-crawl-tasks'
|
||||||
|
|
||||||
|
export async function fetchShopDataTaskList(
|
||||||
|
filter: ShopDataFilter,
|
||||||
|
page: number,
|
||||||
|
pageSize: number,
|
||||||
|
): Promise<ShopDataTaskPageResult> {
|
||||||
|
const { data } = await http.get<unknown>(SHOP_DATA_CRAWL_TASKS_ENDPOINT, {
|
||||||
|
params: toShopDataQuery(filter, page, pageSize),
|
||||||
|
})
|
||||||
|
return parseShopDataTaskPage(data)
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
/** 店铺数据任务列表模型(任务 110):解析按店铺分组的任务列表与结果文件行,纯逻辑。 */
|
||||||
|
import { unwrap } from '../../api/envelope.ts'
|
||||||
|
import { SHOP_DATA_DEFAULT_PAGE_SIZE } from './shop-data-filter.ts'
|
||||||
|
import { normalizeTaskStatus, type ResultId, type TaskId, type TaskStatus } from './task-model.ts'
|
||||||
|
|
||||||
|
export interface ShopDataResultRow {
|
||||||
|
taskId?: TaskId
|
||||||
|
taskNo: string
|
||||||
|
resultId: ResultId
|
||||||
|
username: string
|
||||||
|
shopName: string
|
||||||
|
shopId: string
|
||||||
|
groupName: string
|
||||||
|
status: TaskStatus
|
||||||
|
success?: boolean | null
|
||||||
|
error: string
|
||||||
|
countryCodes: string[]
|
||||||
|
filename: string
|
||||||
|
fileUrl: string
|
||||||
|
fileReady: boolean
|
||||||
|
fileStatus: string
|
||||||
|
fileError: string
|
||||||
|
fileSize?: number
|
||||||
|
rowCount?: number
|
||||||
|
createdAt?: string
|
||||||
|
updatedAt?: string
|
||||||
|
finishedAt?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ShopDataTaskGroup {
|
||||||
|
shopName: string
|
||||||
|
shopId: string
|
||||||
|
groupName: string
|
||||||
|
latestCreatedAt?: string
|
||||||
|
results: ShopDataResultRow[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ShopDataTaskPageResult {
|
||||||
|
items: ShopDataTaskGroup[]
|
||||||
|
total: number
|
||||||
|
page: number
|
||||||
|
pageSize: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export function emptyShopDataTaskPage(): ShopDataTaskPageResult {
|
||||||
|
return { items: [], total: 0, page: 1, pageSize: SHOP_DATA_DEFAULT_PAGE_SIZE }
|
||||||
|
}
|
||||||
|
|
||||||
|
function text(value: unknown): string {
|
||||||
|
return typeof value === 'string' ? value.trim() : ''
|
||||||
|
}
|
||||||
|
|
||||||
|
function positiveNumber(value: unknown): number | undefined {
|
||||||
|
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? Math.floor(value) : undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 解析单条结果文件行;缺有效 result_id 视为无效。 */
|
||||||
|
export function toShopDataResultRow(raw: unknown): ShopDataResultRow | null {
|
||||||
|
if (!raw || typeof raw !== 'object') return null
|
||||||
|
const r = raw as Record<string, unknown>
|
||||||
|
const rawResultId = r.result_id
|
||||||
|
const resultId = (
|
||||||
|
typeof rawResultId === 'number' && Number.isFinite(rawResultId) && rawResultId >= 1
|
||||||
|
? String(Math.floor(rawResultId))
|
||||||
|
: text(rawResultId)
|
||||||
|
) as ResultId
|
||||||
|
if (!resultId) return null
|
||||||
|
const codesRaw = r.country_codes ?? r.countryCodes
|
||||||
|
const countryCodes = Array.isArray(codesRaw)
|
||||||
|
? (codesRaw as unknown[]).map((code) => text(code)).filter(Boolean)
|
||||||
|
: []
|
||||||
|
const row: ShopDataResultRow = {
|
||||||
|
resultId,
|
||||||
|
taskNo: text(r.task_no ?? r.taskNo),
|
||||||
|
username: text(r.username),
|
||||||
|
shopName: text(r.shop_name ?? r.shopName),
|
||||||
|
shopId: text(r.shop_id ?? r.shopId),
|
||||||
|
groupName: text(r.group_name ?? r.groupName),
|
||||||
|
status: normalizeTaskStatus(r.status),
|
||||||
|
error: text(r.error),
|
||||||
|
countryCodes,
|
||||||
|
filename: text(r.output_filename ?? r.result_filename ?? r.filename),
|
||||||
|
fileUrl: text(r.result_file_url ?? r.file_url ?? r.fileUrl),
|
||||||
|
fileReady: r.file_ready === true || r.fileReady === true,
|
||||||
|
fileStatus: text(r.file_status ?? r.fileStatus),
|
||||||
|
fileError: text(r.file_error ?? r.fileError),
|
||||||
|
}
|
||||||
|
const taskId = text(r.task_id)
|
||||||
|
if (taskId) row.taskId = taskId as TaskId
|
||||||
|
if (typeof r.success === 'boolean' || r.success === null) row.success = r.success as boolean | null
|
||||||
|
const fileSize = positiveNumber(r.file_size ?? r.fileSize)
|
||||||
|
if (fileSize !== undefined) row.fileSize = fileSize
|
||||||
|
const rowCount = positiveNumber(r.row_count ?? r.rowCount)
|
||||||
|
if (rowCount !== undefined) row.rowCount = rowCount
|
||||||
|
const createdAt = text(r.created_at ?? r.createdAt)
|
||||||
|
if (createdAt) row.createdAt = createdAt
|
||||||
|
const updatedAt = text(r.updated_at ?? r.updatedAt)
|
||||||
|
if (updatedAt) row.updatedAt = updatedAt
|
||||||
|
const finishedAt = text(r.finished_at ?? r.finishedAt)
|
||||||
|
if (finishedAt) row.finishedAt = finishedAt
|
||||||
|
return row
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 解析单店铺任务组;results 默认空数组。 */
|
||||||
|
export function toShopDataTaskGroup(raw: unknown): ShopDataTaskGroup | null {
|
||||||
|
if (!raw || typeof raw !== 'object') return null
|
||||||
|
const r = raw as Record<string, unknown>
|
||||||
|
const group: ShopDataTaskGroup = {
|
||||||
|
shopName: text(r.shop_name ?? r.shopName) || '未命名',
|
||||||
|
shopId: text(r.shop_id ?? r.shopId),
|
||||||
|
groupName: text(r.group_name ?? r.groupName),
|
||||||
|
results: Array.isArray(r.results)
|
||||||
|
? r.results.map((rawRow) => toShopDataResultRow(rawRow)).filter((row): row is ShopDataResultRow => row !== null)
|
||||||
|
: [],
|
||||||
|
}
|
||||||
|
const latest = text(r.latest_created_at ?? r.latestCreatedAt)
|
||||||
|
if (latest) group.latestCreatedAt = latest
|
||||||
|
return group
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 归一化店铺数据任务分页负载为前端结果;缺省字段回默认。 */
|
||||||
|
export function parseShopDataTaskPage(payload: unknown): ShopDataTaskPageResult {
|
||||||
|
const out = emptyShopDataTaskPage()
|
||||||
|
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) => toShopDataTaskGroup(raw))
|
||||||
|
.filter((group): group is ShopDataTaskGroup => group !== 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.page_size ?? record.pageSize
|
||||||
|
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
|
||||||
|
return out
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import test from 'node:test'
|
||||||
|
import assert from 'node:assert/strict'
|
||||||
|
import { readSource } from './helpers.ts'
|
||||||
|
import { parseShopDataTaskPage, toShopDataResultRow } from '../src/pages/tasks/shop-data-model.ts'
|
||||||
|
|
||||||
|
test('test_task_110_shop_data_list_load_normal_primary_path', () => {
|
||||||
|
// 正常主路径:按店铺分组的 snake 负载解析为任务组+结果行。
|
||||||
|
const page = parseShopDataTaskPage({
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
shop_name: 'BlueWave',
|
||||||
|
shop_id: 's1',
|
||||||
|
group_name: '华东组',
|
||||||
|
latest_created_at: '2026-01-02 10:00:00',
|
||||||
|
results: [
|
||||||
|
{
|
||||||
|
task_id: 11,
|
||||||
|
task_no: 'T-1',
|
||||||
|
result_id: 501,
|
||||||
|
username: '张伟',
|
||||||
|
shop_name: 'BlueWave',
|
||||||
|
group_name: '华东组',
|
||||||
|
status: 'SUCCESS',
|
||||||
|
success: true,
|
||||||
|
country_codes: ['DE'],
|
||||||
|
output_filename: 'shop_data_DE.xlsx',
|
||||||
|
result_file_url: 'https://oss/a.xlsx',
|
||||||
|
file_ready: true,
|
||||||
|
file_status: 'SUCCESS',
|
||||||
|
file_size: 2048,
|
||||||
|
row_count: 120,
|
||||||
|
finished_at: '2026-01-02 10:00:00',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
total: 1,
|
||||||
|
page: 1,
|
||||||
|
page_size: 20,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.equal(page.items.length, 1)
|
||||||
|
const group = page.items[0]
|
||||||
|
assert.equal(group.shopName, 'BlueWave')
|
||||||
|
assert.equal(group.groupName, '华东组')
|
||||||
|
assert.equal(group.results.length, 1)
|
||||||
|
const row = group.results[0]
|
||||||
|
assert.equal(row.resultId, '501')
|
||||||
|
assert.equal(row.status, 'SUCCESS')
|
||||||
|
assert.equal(row.success, true)
|
||||||
|
assert.deepEqual(row.countryCodes, ['DE'])
|
||||||
|
assert.equal(row.filename, 'shop_data_DE.xlsx')
|
||||||
|
assert.equal(row.fileUrl, 'https://oss/a.xlsx')
|
||||||
|
assert.equal(row.fileSize, 2048)
|
||||||
|
assert.equal(row.rowCount, 120)
|
||||||
|
assert.equal(page.total, 1)
|
||||||
|
assert.equal(page.pageSize, 20)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('test_task_110_shop_data_list_load_normal_variant_input', () => {
|
||||||
|
// 正常变体:组内无结果/单结果排序,缺 file_ready 默认 false。
|
||||||
|
const row = toShopDataResultRow({ task_id: 1, result_id: 9, shop_name: 'S', status: 'FAILED', error: 'x' })
|
||||||
|
assert.ok(row)
|
||||||
|
assert.equal(row.resultId, '9')
|
||||||
|
assert.equal(row.fileReady, false)
|
||||||
|
assert.equal(row.fileStatus, '')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('test_task_110_shop_data_list_load_repeated_is_idempotent', () => {
|
||||||
|
// 正常重复:解析稳定、不改输入。
|
||||||
|
const payload = { data: { items: [{ shop_name: 'A', results: [{ task_id: 1, result_id: 2, status: 'SUCCESS' }] }], total: 1, page: 1, page_size: 20 } }
|
||||||
|
assert.deepEqual(parseShopDataTaskPage(payload), parseShopDataTaskPage(payload))
|
||||||
|
})
|
||||||
|
|
||||||
|
test('test_task_110_shop_data_list_load_boundary_empty_input', () => {
|
||||||
|
// 边界空值:空负载回默认分页空结果。
|
||||||
|
const page = parseShopDataTaskPage({})
|
||||||
|
assert.deepEqual(page.items, [])
|
||||||
|
assert.equal(page.total, 0)
|
||||||
|
assert.equal(page.pageSize, 20)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('test_task_110_shop_data_list_load_boundary_single_item', () => {
|
||||||
|
// 边界单元素:单组单行;缺 result_id 行被过滤。
|
||||||
|
const row = toShopDataResultRow({ task_id: 1, status: 'SUCCESS' })
|
||||||
|
assert.equal(row, null)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('test_task_110_shop_data_list_load_boundary_limit_or_missing_field', () => {
|
||||||
|
// 边界上限/缺字段:可选字段缺省回默认;success 为 null 保留 null。
|
||||||
|
const row = toShopDataResultRow({ task_id: 1, result_id: 4, success: null })
|
||||||
|
assert.ok(row)
|
||||||
|
assert.equal(row.success, null)
|
||||||
|
assert.equal(row.finishedAt, undefined)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('test_task_110_shop_data_list_load_invalid_input_rejected', () => {
|
||||||
|
// 异常输入:success=false 抛后端 message;缺 result_id/非对象行过滤。
|
||||||
|
assert.throws(() => parseShopDataTaskPage({ success: false, message: '无权访问店铺数据任务' }), /无权访问/)
|
||||||
|
assert.equal(toShopDataResultRow('garbage'), null)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('test_task_110_shop_data_list_load_dependency_failure_returns_actionable_message', () => {
|
||||||
|
// 依赖失败/加载走 adapter:GET /api/admin/shop-data-crawl-tasks + http.get + 解析。
|
||||||
|
const api = readSource('src/pages/tasks/shop-data-api.ts')
|
||||||
|
assert.match(api, /\/api\/admin\/shop-data-crawl-tasks/)
|
||||||
|
assert.match(api, /fetchShopDataTaskList/)
|
||||||
|
assert.match(api, /http\.get/)
|
||||||
|
const model = readSource('src/pages/tasks/shop-data-model.ts')
|
||||||
|
assert.equal(/axios|http\./.test(model), false, '店铺数据列表解析保持纯逻辑')
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user