fix: search brand knowledge by type

This commit is contained in:
江林铖 2026-09-21 14:24:51 +08:00
parent b931e9b08e
commit 3bc36d141e
4 changed files with 57 additions and 1 deletions

View File

@ -215,6 +215,25 @@ describe('brand knowledge upload route', () => {
expect(isSuperAdmin).toHaveBeenCalledWith('user-1')
})
test('searches knowledge type as well as title and file metadata', async () => {
await GET(new Request('http://localhost/api/department-dashboard/brand-knowledge?q=%E4%B8%BB%E5%9B%BE%E6%A8%A1%E6%9D%BF'))
expect(prisma.brandKnowledge.findMany).toHaveBeenCalledWith(expect.objectContaining({
where: expect.objectContaining({
OR: expect.arrayContaining([
{ knowledgeType: { contains: '主图模板', mode: 'insensitive' } },
]),
}),
}))
expect(prisma.brandKnowledge.count).toHaveBeenCalledWith(expect.objectContaining({
where: expect.objectContaining({
OR: expect.arrayContaining([
{ knowledgeType: { contains: '主图模板', mode: 'insensitive' } },
]),
}),
}))
})
test('stores ordered WebP slices and keeps the first slice as the card preview', async () => {
const response = await POST(uploadRequest())
const call = (prisma.brandKnowledge.create as jest.Mock).mock.calls[0][0]

View File

@ -174,6 +174,7 @@ export async function GET(request: Request) {
...(query ? {
OR: [
{ title: { contains: query, mode: 'insensitive' } },
{ knowledgeType: { contains: query, mode: 'insensitive' } },
{ fileName: { contains: query, mode: 'insensitive' } },
{ brand: { contains: query, mode: 'insensitive' } },
{ productCategoryName: { contains: query, mode: 'insensitive' } },
@ -210,6 +211,7 @@ export async function GET(request: Request) {
...(query ? {
OR: [
{ title: { contains: query, mode: 'insensitive' } },
{ knowledgeType: { contains: query, mode: 'insensitive' } },
{ fileName: { contains: query, mode: 'insensitive' } },
{ brand: { contains: query, mode: 'insensitive' } },
{ productCategoryName: { contains: query, mode: 'insensitive' } },

View File

@ -93,6 +93,41 @@ describe('BrandKnowledgeDashboard', () => {
expect(screen.getByText('没有找到与“索菲亚”匹配的知识内容')).toBeInTheDocument()
})
it('searches by knowledge type when the title uses a different template spelling', async () => {
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: async () => ({
success: true,
total: 1,
data: [{
id: 'knowledge-template-1',
title: '升降桌主图模版-索菲亚',
knowledgeType: '主图模板',
brand: '索菲亚',
productCategoryName: '书桌',
fileName: '升降桌主图模板.psd',
mimeType: 'image/vnd.adobe.photoshop',
fileSize: 1024,
downloadCount: 0,
createdAt: '2026-09-21T00:00:00.000Z',
uploadedBy: '无邪',
downloadUrl: '/api/department-dashboard/brand-knowledge/knowledge-template-1/file',
previewUrl: null,
previewImageName: null,
likeCount: 0,
isLiked: false,
isFavorited: false,
}],
}),
} as Response)
render(<BrandKnowledgeDashboard />)
expect(await screen.findByRole('heading', { level: 3, name: '升降桌主图模版-索菲亚' })).toBeInTheDocument()
fireEvent.change(screen.getByRole('searchbox', { name: '搜索品牌知识' }), { target: { value: '主图模板' } })
expect(screen.getByRole('heading', { level: 3, name: '升降桌主图模版-索菲亚' })).toBeInTheDocument()
})
it('shows uploaded knowledge with the material-library card hierarchy', async () => {
global.fetch = jest.fn().mockResolvedValue({
ok: true,

View File

@ -440,7 +440,7 @@ export default function BrandKnowledgeDashboard() {
showToast('品牌知识已删除,原文件已保留', { variant: 'success' })
}, [showToast])
const visibleItems = useMemo(() => { const keyword = searchKeyword.trim().toLowerCase(); return items.filter((item) => (selectedType === '全部' || item.knowledgeType === selectedType) && (!keyword || [item.id, item.title, item.fileName, item.brand, item.productCategoryName, item.uploadedBy].some((value) => value.toLowerCase().includes(keyword)))) }, [items, searchKeyword, selectedType])
const visibleItems = useMemo(() => { const keyword = searchKeyword.trim().toLowerCase(); return items.filter((item) => (selectedType === '全部' || item.knowledgeType === selectedType) && (!keyword || [item.id, item.title, item.knowledgeType, item.fileName, item.brand, item.productCategoryName, item.uploadedBy].some((value) => value.toLowerCase().includes(keyword)))) }, [items, searchKeyword, selectedType])
const emptyMessage = useMemo(() => {
if (loadError) return loadError