From 9ea3aca5e4776599aa5924917b7d73a2ebbeba4b Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 11 Sep 2024 12:21:19 +0100 Subject: [PATCH] Typing improvements and fixing issue detected by test cases (old lucene syntax). --- .../server/src/api/controllers/row/views.ts | 17 ++++++++++------- packages/shared-core/src/filters.ts | 10 ++++++---- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/controllers/row/views.ts b/packages/server/src/api/controllers/row/views.ts index 5957531857..de01386f6e 100644 --- a/packages/server/src/api/controllers/row/views.ts +++ b/packages/server/src/api/controllers/row/views.ts @@ -39,9 +39,9 @@ export async function searchView( // Enrich saved query with ephemeral query params. // We prevent searching on any fields that are saved as part of the query, as // that could let users find rows they should not be allowed to access. - let query: any = supportsLogicalOperators + let query = supportsLogicalOperators ? dataFilters.buildQuery(view.query) - : dataFilters.buildQueryLegacy(view.query as SearchFilter[]) + : dataFilters.buildQueryLegacy(view.query) delete query?.onEmptyFilter @@ -51,8 +51,10 @@ export async function searchView( if (!supportsLogicalOperators) { // In the unlikely event that a Grouped Filter is in a non-SQS environment - // It needs to be ignored. Entirely - let queryFilters: SearchFilter[] = Array.isArray(query) ? query : [] + // It needs to be ignored entirely + let queryFilters: SearchFilter[] = Array.isArray(view.query) + ? view.query + : [] // Extract existing fields const existingFields = @@ -64,15 +66,16 @@ export async function searchView( Object.keys(body.query).forEach(key => { const operator = key as Exclude Object.keys(body.query[operator] || {}).forEach(field => { - if (!existingFields.includes(db.removeKeyNumbering(field))) { + if (query && !existingFields.includes(db.removeKeyNumbering(field))) { query[operator]![field] = body.query[operator]![field] } }) }) } else { + const conditions = query ? [query] : [] query = { $and: { - conditions: [query, body.query], + conditions: [...conditions, body.query], }, } } @@ -80,7 +83,7 @@ export async function searchView( await context.ensureSnippetContext(true) - const enrichedQuery = await enrichSearchContext(query, { + const enrichedQuery = await enrichSearchContext(query || {}, { user: sdk.users.getUserContextBindings(ctx.user), }) diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 63d3044da9..941c1846ca 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -428,8 +428,9 @@ const builderFilter = (expression: SearchFilter) => { return query } -export const buildQueryLegacy = (filter: SearchFilter[]) => { - // +export const buildQueryLegacy = ( + filter?: SearchFilterGroup | SearchFilter[] +): SearchFilters | undefined => { let query: SearchFilters = { string: {}, fuzzy: {}, @@ -523,7 +524,6 @@ export const buildQueryLegacy = (filter: SearchFilter[]) => { } } } else if (isLogicalSearchOperator(queryOperator)) { - // TODO } else if (query[queryOperator] && operator !== "onEmptyFilter") { if (type === "boolean") { // Transform boolean filters to cope with null. @@ -548,7 +548,9 @@ export const buildQueryLegacy = (filter: SearchFilter[]) => { return query } -export const buildQuery = (filter?: SearchFilterGroup | SearchFilter[]) => { +export const buildQuery = ( + filter?: SearchFilterGroup | SearchFilter[] +): SearchFilters | undefined => { if (!filter) { return }