gongxiangp2/lib/task-detail-privacy-fields.ts

61 lines
1.8 KiB
TypeScript

export const TASK_DETAIL_DEMAND_TYPE_OPTIONS = ['新品', '优化'] as const
export type TaskDetailDemandType = (typeof TASK_DETAIL_DEMAND_TYPE_OPTIONS)[number]
export interface TaskDetailPrivacyFieldInput {
accessScope?: unknown
taskDetailDemandType?: unknown
taskDetailPrivate?: unknown
}
export interface NormalizedTaskDetailPrivacyFields {
taskDetailDemandType: TaskDetailDemandType
taskDetailPrivate: boolean
}
export function isTaskDetailDemandType(value: unknown): value is TaskDetailDemandType {
return TASK_DETAIL_DEMAND_TYPE_OPTIONS.includes(value as TaskDetailDemandType)
}
export function normalizeTaskDetailPrivacyFields(
input: TaskDetailPrivacyFieldInput,
): NormalizedTaskDetailPrivacyFields {
const rawDemandType = input?.taskDetailDemandType
const rawPrivate = input?.taskDetailPrivate
if (input.accessScope === 'operations_g' && rawPrivate === true) {
throw new Error('运营中心 G 专属素材在中心内共享,不能设为个人私密')
}
let taskDetailDemandType: TaskDetailDemandType = '新品'
if (rawDemandType != null) {
if (typeof rawDemandType !== 'string') {
throw new Error('需求类型只能是“新品”或“优化”')
}
const normalizedDemandType = rawDemandType.trim()
if (!normalizedDemandType || !isTaskDetailDemandType(normalizedDemandType)) {
throw new Error('需求类型只能是“新品”或“优化”')
}
taskDetailDemandType = normalizedDemandType
}
let taskDetailPrivate = false
if (rawPrivate != null) {
if (typeof rawPrivate !== 'boolean') {
throw new Error('私密标记只能传 true 或 false')
}
taskDetailPrivate = rawPrivate
}
if (taskDetailPrivate && taskDetailDemandType !== '优化') {
throw new Error('仅优化需求支持私密上传')
}
return {
taskDetailDemandType,
taskDetailPrivate,
}
}