orderRequiredSettings.helpers.js
2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const FIELD_FLAG_MAP = {
carBrand: 'carTypeRequiredFlag',
carNo: 'standNoRequiredFlag',
plateNo: 'plateNumRequiredFlag',
customerPhone: 'customerPhoneRequiredFlag',
phone: 'customerPhoneRequiredFlag',
mobile: 'customerPhoneRequiredFlag'
}
const MANAGED_REQUIRED_FIELDS = [
'carBrand',
'standNo',
'plateNumber',
'customerPhone'
]
const REQUIRED_FIELD_MAP = {
carTypeRequiredFlag: 'carBrand',
standNoRequiredFlag: 'standNo',
plateNumRequiredFlag: 'plateNumber'
}
function normalizeRequiredFlag(value) {
if (value === true || value === 1 || value === '1') return 'Y'
if (value === false || value === 0 || value === '0') return 'N'
if (typeof value === 'string') {
const flag = value.trim().toUpperCase()
if (flag === 'Y' || flag === 'N') return flag
}
}
function parsePhoneConfig(value) {
if (Array.isArray(value)) return value
if (!value) return []
try {
const result = typeof value === 'string' ? JSON.parse(value) : value
return Array.isArray(result) ? result : []
} catch (error) {
return []
}
}
export function normalizeMerchantOrderSettings(
data = {},
{ shopMerchantId, yqMerchantId } = {}
) {
const result = {}
Object.keys(data).forEach(key => {
if (key !== 'orderFieldSettingList' && key !== 'yqPhoneConfig') {
result[key] = data[key]
}
})
Object.keys(result).forEach(key => {
const flag = normalizeRequiredFlag(result[key])
if (key.endsWith('RequiredFlag') && flag) result[key] = flag
})
const fieldSettingList = Array.isArray(data.orderFieldSettingList)
? data.orderFieldSettingList
: []
fieldSettingList.forEach(item => {
const targetKey = FIELD_FLAG_MAP[item?.fieldKey] || item?.fieldKey
const flag = normalizeRequiredFlag(item?.isRequired)
if (targetKey && flag) result[targetKey] = flag
})
const selectedPhoneConfig = parsePhoneConfig(data.yqPhoneConfig).find(
item => String(item?.yqMerchantId) === String(yqMerchantId)
)
result.shopMerchantId = shopMerchantId
result.yqMerchantId = yqMerchantId
result.yqPhoneConfig = selectedPhoneConfig?.shopMerchantPhoneList || false
return result
}
export function buildCustomerRequiredList(
settings = {},
currentRequiredList = []
) {
const requiredList = currentRequiredList.filter(
field => !MANAGED_REQUIRED_FIELDS.includes(field)
)
Object.entries(REQUIRED_FIELD_MAP).forEach(([flagKey, field]) => {
if (normalizeRequiredFlag(settings[flagKey]) === 'Y') {
requiredList.push(field)
}
})
if (normalizeRequiredFlag(settings.customerPhoneRequiredFlag) !== 'N') {
requiredList.push('customerPhone')
}
return [...new Set(requiredList)]
}
export function shouldValidateCustomerPhone(requiredList = [], phone = '') {
return (
requiredList.includes('customerPhone') || String(phone ?? '').trim() !== ''
)
}