diff --git a/JY-Salon-Front/src/utils/artist.test.ts b/JY-Salon-Front/src/utils/artist.test.ts new file mode 100644 index 00000000..44f18246 --- /dev/null +++ b/JY-Salon-Front/src/utils/artist.test.ts @@ -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() + }) +}) diff --git a/JY-Salon-Front/src/utils/artist.ts b/JY-Salon-Front/src/utils/artist.ts new file mode 100644 index 00000000..fe8df5df --- /dev/null +++ b/JY-Salon-Front/src/utils/artist.ts @@ -0,0 +1,26 @@ +import type { IdType } from '@/types/common' +import type { Artist } from '@/types/manager' + +type ArtistIdentity = Pick + +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 = (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 +} diff --git a/JY-Salon-Front/src/views/AppointmentFormView.vue b/JY-Salon-Front/src/views/AppointmentFormView.vue index d01e5a62..13e10fac 100644 --- a/JY-Salon-Front/src/views/AppointmentFormView.vue +++ b/JY-Salon-Front/src/views/AppointmentFormView.vue @@ -45,7 +45,7 @@
-
+
@@ -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 => { 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