The minimalistic document archiving platform.
fix(demo): custom property name normalization for search (#974)
Corentin Thomasset1c1e904ba186
diff --git a/apps/papra-client/src/modules/demo/demo-api-mock.ts b/apps/papra-client/src/modules/demo/demo-api-mock.tsindex 94cb64f9..e9f720e6 100644--- a/apps/papra-client/src/modules/demo/demo-api-mock.ts+++ b/apps/papra-client/src/modules/demo/demo-api-mock.ts@@ -24,7 +24,7 @@ import {webhooksStorage,} from './demo.storage';import { findMany, getValues } from './demo.storage.models';-import { searchDemoDocuments } from './search/demo.search.services';+import { generatePropertyKey, searchDemoDocuments } from './search/demo.search.services';import { demoUser } from './seed/users.fixtures';function assert(condition: unknown, { message = 'Error', status }: { message?: string; status?: number } = {}): asserts condition {@@ -971,14 +971,14 @@ const inMemoryApiMock: Record<string, { handler: any }> = {id: createId({ prefix: 'cpd' }),organizationId,name: get(body, ['name']) as string,- key: (get(body, ['name']) as string).toLowerCase().replace(/\s+/g, '_'),+ key: generatePropertyKey({ name: get(body, ['name']) as string }),description: (get(body, ['description']) ?? null) as string | null,type: get(body, ['type']) as string,displayOrder: existingDefinitions.length,options: (get(body, ['options']) as { name: string }[] ?? []).map((option, index) => ({id: createId({ prefix: 'opt' }),name: option.name,- key: option.name.toLowerCase().replace(/\s+/g, '_'),+ key: generatePropertyKey({ name: option.name }),displayOrder: index,})),createdAt: new Date(),diff --git a/apps/papra-client/src/modules/demo/search/demo.search.services.test.ts b/apps/papra-client/src/modules/demo/search/demo.search.services.test.tsindex 78c8e6bb..45130692 100644--- a/apps/papra-client/src/modules/demo/search/demo.search.services.test.ts+++ b/apps/papra-client/src/modules/demo/search/demo.search.services.test.ts@@ -18,6 +18,7 @@ describe('demo search services', () => {{ id: 'doc_4', name: 'Grocery List', content: 'Eggs, milk, bread, and butter.', tags: [tags.cooking, tags.personal], createdAt: new Date('2023-04-05') },{ id: 'doc_5', name: 'Project Plan', content: 'Outline project goals and milestones.', tags: [tags.work], createdAt: new Date('2023-05-20') },{ id: 'doc_6', name: 'Vacation Ideas', content: 'Consider visiting the beach or mountains.', tags: [], createdAt: new Date('2023-06-15') },+ { id: 'doc_7', name: 'Invoice 001', content: 'Invoice for services.', tags: [tags.work], createdAt: new Date('2023-07-01'), customProperties: [{ key: 'invoicenumber', type: 'text', value: 'INV-001' }] },] as unknown as Document[];const queries = [@@ -26,15 +27,18 @@ describe('demo search services', () => {{ query: 'pancakes flour', expectedIds: ['doc_1'] },{ query: 'tag:cooking', expectedIds: ['doc_1', 'doc_4'] },{ query: 'tag:cooking butter', expectedIds: ['doc_4'] },- { query: 'tag:work created:>2023-03-01', expectedIds: ['doc_5'] },+ { query: 'tag:work created:>2023-03-01', expectedIds: ['doc_5', 'doc_7'] },{ query: '-tag:work', expectedIds: ['doc_1', 'doc_3', 'doc_4', 'doc_6'] },- { query: 'has:tags', expectedIds: ['doc_1', 'doc_2', 'doc_3', 'doc_4', 'doc_5'] },+ { query: 'has:tags', expectedIds: ['doc_1', 'doc_2', 'doc_3', 'doc_4', 'doc_5', 'doc_7'] },{ query: '-has:tags', expectedIds: ['doc_6'] },{ query: 'NOT has:tags', expectedIds: ['doc_6'] },{ query: '-has:tags OR tag:personal', expectedIds: ['doc_3', 'doc_4', 'doc_6'] },{ query: 'ncakes', expectedIds: [] },{ query: 'name:ncakes', expectedIds: [] },{ query: 'content:ncakes', expectedIds: [] },+ { query: 'InvoiceNumber:INV', expectedIds: ['doc_7'] },+ { query: 'invoicenumber:INV', expectedIds: ['doc_7'] },+ { query: 'INVOICENUMBER:INV', expectedIds: ['doc_7'] },];for (const { query, expectedIds } of queries) {diff --git a/apps/papra-client/src/modules/demo/search/demo.search.services.ts b/apps/papra-client/src/modules/demo/search/demo.search.services.tsindex 1510ac1d..eb071f45 100644--- a/apps/papra-client/src/modules/demo/search/demo.search.services.ts+++ b/apps/papra-client/src/modules/demo/search/demo.search.services.ts@@ -2,6 +2,12 @@ import type { AndExpression, Expression, FilterExpression, NotExpression, OrExprimport type { Document } from '../../documents/documents.types';import { parseSearchQuery } from '@papra/search-parser';+export function generatePropertyKey({ name }: { name: string }): string {+ return name+ .replace(/[^\p{L}\p{N}]/gu, '')+ .toLowerCase();+}+type DocumentCondition = (params: { document: Document }) => boolean;const falseCondition: DocumentCondition = () => false;@@ -151,8 +157,10 @@ function buildHasDateFilter({ expression }: { expression: FilterExpression }): D}function buildHasCustomPropertyFilter({ propertyKey }: { propertyKey: string }): DocumentCondition {+ const normalizedKey = generatePropertyKey({ name: propertyKey });+return ({ document }) => {- const prop = document.customProperties?.find(p => p.key === propertyKey);+ const prop = document.customProperties?.find(p => p.key === normalizedKey);return prop !== undefined && prop.value != null;};@@ -160,9 +168,10 @@ function buildHasCustomPropertyFilter({ propertyKey }: { propertyKey: string }):function buildCustomPropertyFilterCondition({ field, expression }: { field: string; expression: FilterExpression }): DocumentCondition {const { value, operator } = expression;+ const normalizedField = generatePropertyKey({ name: field });return ({ document }) => {- const prop = document.customProperties?.find(p => p.key === field);+ const prop = document.customProperties?.find(p => p.key === normalizedField);if (!prop || prop.value == null) {return false;