修复店务预约员工ID传参

This commit is contained in:
2026-07-01 23:27:24 +08:00
parent 2e41aaef70
commit b6386c97fb
3 changed files with 57 additions and 2 deletions
+27
View File
@@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest'
import { findArtistByUserId, normalizeArtistSelectionId, resolveArtistUserId } from '@/utils/artist'
import type { Artist } from '@/types/manager'
const artists: Artist[] = [
{ id: '11', userId: '101', name: '美甲师1' },
{ id: '12', userId: '102', name: '美甲师2' },
{ id: '103', name: '美甲师3' }
]
describe('artist identity utils', () => {
it('uses userId as the backend artist id when it exists', () => {
expect(resolveArtistUserId(artists[0]!)).toBe('101')
expect(resolveArtistUserId(artists[2]!)).toBe('103')
})
it('normalizes legacy artist info ids from drafts to user ids', () => {
expect(normalizeArtistSelectionId('11', artists)).toBe('101')
expect(normalizeArtistSelectionId('101', artists)).toBe('101')
expect(normalizeArtistSelectionId('', artists)).toBe('')
})
it('matches bookable artist ids by user id', () => {
expect(findArtistByUserId(artists, '101')?.name).toBe('美甲师1')
expect(findArtistByUserId(artists, '11')).toBeUndefined()
})
})
+26
View File
@@ -0,0 +1,26 @@
import type { IdType } from '@/types/common'
import type { Artist } from '@/types/manager'
type ArtistIdentity = Pick<Artist, 'id' | 'userId'>
export const resolveArtistUserId = (artist: ArtistIdentity): IdType => artist.userId ?? artist.id
export const sameArtistUserId = (artist: ArtistIdentity, artistId: IdType | string | null | undefined): boolean => {
if (artistId === null || artistId === undefined || artistId === '') {
return false
}
return String(resolveArtistUserId(artist)) === String(artistId)
}
export const findArtistByUserId = <T extends ArtistIdentity>(artists: T[], artistId: IdType | string | null | undefined): T | undefined => {
return artists.find(artist => sameArtistUserId(artist, artistId))
}
export const normalizeArtistSelectionId = (artistId: IdType | string | null | undefined, artists: ArtistIdentity[]): string => {
const target = String(artistId ?? '').trim()
if (!target) {
return ''
}
const matched = artists.find(artist => String(artist.id) === target || String(artist.userId ?? '') === target)
return matched ? String(resolveArtistUserId(matched)) : target
}
@@ -45,7 +45,7 @@
<div class="field"><label class="required">预约日期</label><input v-model="form.appointmentDate" type="date" @change="onDateChange" /></div>
<div class="field"><label class="required">预约作品</label><button class="field-value picker-value" type="button" @click="productModalVisible = true">{{ selectedProductTitle }}</button></div>
<div class="field"><label class="required">预计时长</label><input v-model.trim="form.durationMinutes" inputmode="numeric" placeholder="分钟" @input="refreshAvailableTimes" /></div>
<div class="field"><label class="required">指定员工</label><select v-model="form.artistId" @change="refreshAvailableTimes"><option value="">请选择员工</option><option v-for="artist in artists" :key="String(artist.id)" :value="String(artist.id)">{{ artist.name || artist.nickname }}</option></select></div>
<div class="field"><label class="required">指定员工</label><select v-model="form.artistId" @change="refreshAvailableTimes"><option value="">请选择员工</option><option v-for="artist in artists" :key="String(resolveArtistUserId(artist))" :value="String(resolveArtistUserId(artist))">{{ artist.name || artist.nickname }}</option></select></div>
<div class="field"><label class="required">预约时间</label><button class="field-value picker-value" type="button" @click="openTimeSheet">{{ form.appointmentTime || timeSelectTip }}</button></div>
<div class="field field--stack"><label>备注</label><textarea v-model.trim="form.remark" placeholder="请输入预约备注"></textarea></div>
</section>
@@ -146,6 +146,7 @@ import { useToastStore } from '@/stores/toast'
import { formatDate, minutesOfDay, timeFromMinutes } from '@/utils/date'
import { buildAvailableTimeGroups, type AvailableTimeGroup } from '@/utils/available-times'
import { buildClientRequestId, normalizeDuration } from '@/utils/planning'
import { findArtistByUserId, normalizeArtistSelectionId, resolveArtistUserId } from '@/utils/artist'
import { readDraftForm, removeDraftForm, writeDraftForm } from '@/utils/appointment-draft'
import { readFormModalDraft, removeFormModalDraft, writeFormModalDraft } from '@/utils/form-modal-draft'
import { markPlanningRefresh } from '@/utils/storage'
@@ -311,7 +312,7 @@ const resolveProductArtistText = (product: Product, artistOptions: Artist[]): st
return '全部美甲师'
}
const artistNames = artistIds
.map(id => artistOptions.find(artist => String(artist.id) === id))
.map(id => findArtistByUserId(artistOptions, id))
.filter((artist): artist is Artist => Boolean(artist))
.map(artist => artist.name || artist.nickname || '')
.filter(Boolean)
@@ -348,6 +349,7 @@ const loadOptions = async (): Promise<void> => {
managerEmployeesApi.list()
])
artists.value = employeeData
form.artistId = normalizeArtistSelectionId(form.artistId, artists.value)
const productList = Array.isArray(productData) ? productData : productData.list
products.value = productList.map(product => normalizeProduct(product, artists.value))
filteredProducts.value = products.value