调整预约客户选择和快捷新增
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { UserInfo } from '@/types/common'
|
||||
import {
|
||||
buildCustomerCreateDraft,
|
||||
resolveCustomerDisplay,
|
||||
validateAppointmentCustomerSelection
|
||||
} from '@/utils/customer-selection'
|
||||
|
||||
describe('customer selection helpers', () => {
|
||||
it('requires a selected customer id before appointment submit', () => {
|
||||
expect(validateAppointmentCustomerSelection('', '王女士', '13800138000')).toBe('请先选择客户或新增客户')
|
||||
expect(validateAppointmentCustomerSelection('42', '', '13800138000')).toBe('请补全客户姓名和手机号')
|
||||
expect(validateAppointmentCustomerSelection('42', '王女士', '13800138000')).toBeNull()
|
||||
})
|
||||
|
||||
it('resolves selected customer display data from existing user', () => {
|
||||
const customer: UserInfo = {
|
||||
id: 42,
|
||||
nickname: '王女士',
|
||||
phone: '13800138000',
|
||||
avatar: 'https://img.test/avatar.jpg'
|
||||
}
|
||||
|
||||
expect(resolveCustomerDisplay(customer)).toEqual({
|
||||
id: '42',
|
||||
name: '王女士',
|
||||
phone: '13800138000',
|
||||
avatar: 'https://img.test/avatar.jpg',
|
||||
initial: '王',
|
||||
searchText: '王女士 13800138000'
|
||||
})
|
||||
})
|
||||
|
||||
it('prefills quick create form from search keyword', () => {
|
||||
expect(buildCustomerCreateDraft('王女士 13800138000')).toEqual({
|
||||
nickname: '王女士',
|
||||
phone: '13800138000'
|
||||
})
|
||||
expect(buildCustomerCreateDraft('13800138000')).toEqual({
|
||||
nickname: '',
|
||||
phone: '13800138000'
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,52 @@
|
||||
import type { UserInfo } from '@/types/common'
|
||||
|
||||
export interface CustomerCreateDraft {
|
||||
nickname: string
|
||||
phone: string
|
||||
}
|
||||
|
||||
export interface CustomerDisplay {
|
||||
id: string
|
||||
name: string
|
||||
phone: string
|
||||
avatar: string
|
||||
initial: string
|
||||
searchText: string
|
||||
}
|
||||
|
||||
const PHONE_PATTERN = /1[3-9]\d{9}/
|
||||
|
||||
export const buildCustomerSearchText = (name: string, phone: string): string => {
|
||||
return [name, phone].map(value => value.trim()).filter(Boolean).join(' ')
|
||||
}
|
||||
|
||||
export const buildCustomerCreateDraft = (keyword: string): CustomerCreateDraft => {
|
||||
const normalizedKeyword = keyword.trim()
|
||||
const phone = normalizedKeyword.match(PHONE_PATTERN)?.[0] || ''
|
||||
const nickname = phone ? normalizedKeyword.replace(phone, '').trim() : normalizedKeyword
|
||||
return { nickname, phone }
|
||||
}
|
||||
|
||||
export const resolveCustomerDisplay = (customer: UserInfo): CustomerDisplay => {
|
||||
const name = String(customer.nickname || customer.account || customer.phone || customer.contactPhone || '未命名客户')
|
||||
const phone = String(customer.phone || customer.contactPhone || '')
|
||||
const avatar = String(customer.avatar || '')
|
||||
return {
|
||||
id: String(customer.id),
|
||||
name,
|
||||
phone,
|
||||
avatar,
|
||||
initial: name.slice(0, 1) || '客',
|
||||
searchText: buildCustomerSearchText(name, phone)
|
||||
}
|
||||
}
|
||||
|
||||
export const validateAppointmentCustomerSelection = (customerUserId: string, customerName: string, phone: string): string | null => {
|
||||
if (!customerUserId.trim()) {
|
||||
return '请先选择客户或新增客户'
|
||||
}
|
||||
if (!customerName.trim() || !phone.trim()) {
|
||||
return '请补全客户姓名和手机号'
|
||||
}
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user