a8b8150ebe
新增 shop-data-download.ts(打包请求体 result_ids + 下载 job 状态), shop-data-api.ts 增加 requestShopDataZipDownload(POST /download-zip, blob)。 TDD: task-113.test.ts 8 用例先 RED 后 GREEN。
49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
/** 店铺数据结果批量下载任务(任务 113):打包请求体与下载 job 状态;纯逻辑。 */
|
|
|
|
export type ShopDataDownloadStatus = 'idle' | 'running' | 'done' | 'failed'
|
|
|
|
export interface ShopDataDownloadJob {
|
|
status: ShopDataDownloadStatus
|
|
message?: string
|
|
fileCount?: number
|
|
errorCount?: number
|
|
}
|
|
|
|
function finitePositive(value: unknown): number | null {
|
|
const number = typeof value === 'number' && Number.isFinite(value)
|
|
? value
|
|
: typeof value === 'string' && /^\d+$/.test(value.trim())
|
|
? Number(value.trim())
|
|
: null
|
|
return number === null || number < 1 ? null : Math.floor(number)
|
|
}
|
|
|
|
/** result_id → POST /download-zip 请求体(过滤非法 id,按序去重)。 */
|
|
export function toShopDataZipRequest(resultIds: readonly (number | string)[]): { result_ids: number[] } {
|
|
const ids: number[] = []
|
|
const seen = new Set<number>()
|
|
for (const id of resultIds) {
|
|
const parsed = finitePositive(id)
|
|
if (parsed === null || seen.has(parsed)) continue
|
|
seen.add(parsed)
|
|
ids.push(parsed)
|
|
}
|
|
return { result_ids: ids }
|
|
}
|
|
|
|
export function createShopDataDownloadJob(): ShopDataDownloadJob {
|
|
return { status: 'idle' }
|
|
}
|
|
|
|
export function startShopDataDownload(): ShopDataDownloadJob {
|
|
return { status: 'running' }
|
|
}
|
|
|
|
export function succeedShopDataDownload(_job: ShopDataDownloadJob, fileCount: number, errorCount: number): ShopDataDownloadJob {
|
|
return { status: 'done', fileCount, errorCount }
|
|
}
|
|
|
|
export function failShopDataDownload(_job: ShopDataDownloadJob, message: string): ShopDataDownloadJob {
|
|
return { status: 'failed', message }
|
|
}
|