task-93(店铺中心): 实现店铺凭证字段分离
新增 shop-credential-model.ts:凭证明文仅经凭证端点解码且落 SensitiveString,
与列表行(仅掩码)字段分离;shop-manage-api.ts 增加 fetchShopCredential
(GET /api/admin/shop-manages/{id}/credential?shop_name=…)。
TDD: task-93.test.ts 8 用例先 RED 后 GREEN。
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/** 店铺明文凭据解析模型(任务 93):凭证明文仅来自凭证端点且落 SensitiveString,
|
||||
* 与列表行(仅掩码)字段分离;纯逻辑。 */
|
||||
import { unwrap } from '../../api/envelope.ts'
|
||||
import { toSensitiveString, type ShopCredential } 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 toShopCredential(raw: unknown): ShopCredential | 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 password = text(record.password)
|
||||
if (!password) return null
|
||||
const credential: ShopCredential = {
|
||||
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),
|
||||
password: toSensitiveString(password),
|
||||
}
|
||||
return credential
|
||||
}
|
||||
|
||||
/** 解包凭证端点负载(data 为 ShopManageCredentialVo)为明文凭据;无效时抛可操作错误。 */
|
||||
export function parseShopCredential(payload: unknown): ShopCredential {
|
||||
const credential = toShopCredential(unwrap<unknown>(payload))
|
||||
if (!credential) {
|
||||
throw new Error('读取店铺凭证响应异常:未返回有效的明文凭据')
|
||||
}
|
||||
return credential
|
||||
}
|
||||
Reference in New Issue
Block a user