42722d0cc6
新增 shop-manage-model.ts(ShopManagePageVo camel 解析,仅保留密码掩码) 与 shop-manage-api.ts(GET /api/admin/shop-manages 加载适配)。 TDD: task-88.test.ts 8 用例先 RED 后 GREEN。
54 lines
2.4 KiB
TypeScript
54 lines
2.4 KiB
TypeScript
/** 店铺列表加载模型(任务 88):解析 ShopManagePageVo(camel) 店铺行,纯逻辑。 */
|
|
import { unwrap } from '../../api/envelope.ts'
|
|
import { emptyShopPageResult, type ShopPageResult, type ShopSummary } from './shop-dto.ts'
|
|
|
|
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
|
|
}
|
|
|
|
/** 解析单条店铺行;缺 id 视为无效;密码只保留掩码、不落明文字段。 */
|
|
export function toShopSummary(raw: unknown): ShopSummary | 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: ShopSummary = {
|
|
id,
|
|
groupId: numberOrNull(record.groupId ?? record.group_id),
|
|
groupName: text(record.groupName ?? record.group_name),
|
|
shopName: text(record.shopName ?? record.shop_name),
|
|
mallName: text(record.mallName ?? record.mall_name),
|
|
znUsername: text(record.znUsername ?? record.zn_username),
|
|
account: text(record.account),
|
|
}
|
|
const masked = text(record.passwordMasked ?? record.password_masked) || text(record.password)
|
|
if (masked) item.passwordMasked = masked
|
|
const createdAt = text(record.createdAt ?? record.created_at)
|
|
if (createdAt) item.createdAt = createdAt
|
|
const updatedAt = text(record.updatedAt ?? record.updated_at)
|
|
if (updatedAt) item.updatedAt = updatedAt
|
|
return item
|
|
}
|
|
|
|
/** 归一化店铺分页负载(信封或已解包 VO)为前端结果;缺省字段回默认。 */
|
|
export function parseShopManagePage(payload: unknown): ShopPageResult {
|
|
const out = emptyShopPageResult()
|
|
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) => toShopSummary(raw))
|
|
.filter((item): item is ShopSummary => 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
|
|
}
|