Files
jiapuapp/services/api/business-dictionary-contract.js

73 lines
2.6 KiB
JavaScript

import { createRequestError } from './request-client.js'
import {
normalizeOptionalNonnegativeInteger,
normalizeOptionalNumericId,
normalizeResponseText
} from './response-normalizers.js'
const supportedDictionaryTypes = new Set([
'gen_parent_relationship_variant',
'gen_education_type',
'gen_death_expression',
'gen_spouse_relationship_variant',
'gen_ceremony_type',
'gen_person_document_type',
'gen_growth_record_type',
'gen_merit_type',
'gen_feedback_type',
'gen_zodiac'
])
const optionStates = new Set(['ACTIVE', 'DISABLED', 'UNKNOWN'])
export const normalizeBusinessOptionProjection = (value, label, state, subject, errorCode) => {
const optionValue = normalizeResponseText(value, `${subject}编码`)
const optionLabel = normalizeResponseText(label, `${subject}名称`)
const optionState = normalizeResponseText(state, `${subject}状态`)
if (optionValue && !optionStates.has(optionState)) {
throw createRequestError(`${subject}历史值状态无效`, errorCode)
}
return {
value: optionValue,
label: optionLabel || optionValue,
state: optionValue ? optionState : ''
}
}
export const normalizeBusinessDictionaryType = (dictType) => {
if (!supportedDictionaryTypes.has(dictType)) {
throw new TypeError('当前页面不支持该业务字典')
}
return dictType
}
export const normalizeBusinessDictionaryOptions = (dictType, value) => {
normalizeBusinessDictionaryType(dictType)
if (!Array.isArray(value)) {
throw createRequestError('业务字典响应不是列表', 'BUSINESS_DICTIONARY_RESPONSE_INVALID')
}
const values = new Set()
return value.map((item) => {
if (!item || typeof item !== 'object' || Array.isArray(item)) {
throw createRequestError('业务字典响应包含无效条目', 'BUSINESS_DICTIONARY_RESPONSE_INVALID')
}
if (item.enabled !== true) {
throw createRequestError('业务字典选择接口返回了不可选项', 'BUSINESS_DICTIONARY_RESPONSE_INVALID')
}
const optionValue = normalizeResponseText(item.value, 'value')
const label = normalizeResponseText(item.label, 'label')
if (!optionValue || !label || values.has(optionValue)) {
throw createRequestError('业务字典响应缺少稳定选项', 'BUSINESS_DICTIONARY_RESPONSE_INVALID')
}
values.add(optionValue)
return {
code: normalizeOptionalNumericId(item.code, '业务字典编码', 'BUSINESS_DICTIONARY_RESPONSE_INVALID'),
value: optionValue,
label,
sort: normalizeOptionalNonnegativeInteger(item.sort, '业务字典排序值', 'BUSINESS_DICTIONARY_RESPONSE_INVALID'),
default: item.default === true,
enabled: true
}
})
}