修复店务预约员工ID传参
This commit is contained in:
@@ -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()
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user