Adding dedicated function for removing empty filters.

This commit is contained in:
mike12345567 2023-09-12 14:06:20 +01:00
parent aeac7aaa0c
commit aed5694ac2
2 changed files with 36 additions and 17 deletions

View File

@ -18,6 +18,7 @@ import {
import sdk from "../../../sdk" import sdk from "../../../sdk"
import * as utils from "./utils" import * as utils from "./utils"
import { dataFilters } from "@budibase/shared-core" import { dataFilters } from "@budibase/shared-core"
import { removeEmptyFilters } from "./utils"
export async function handleRequest( export async function handleRequest(
operation: Operation, operation: Operation,
@ -26,20 +27,8 @@ export async function handleRequest(
) { ) {
// make sure the filters are cleaned up, no empty strings for equals, fuzzy or string // make sure the filters are cleaned up, no empty strings for equals, fuzzy or string
if (opts && opts.filters) { if (opts && opts.filters) {
for (let filterField of NoEmptyFilterStrings) { opts.filters = utils.removeEmptyFilters(opts.filters)
if (!opts.filters[filterField]) {
continue
} }
// @ts-ignore
for (let [key, value] of Object.entries(opts.filters[filterField])) {
if (utils.invalidFilter(value)) {
// @ts-ignore
delete opts.filters[filterField][key]
}
}
}
if ( if (
!dataFilters.hasFilters(opts?.filters) && !dataFilters.hasFilters(opts?.filters) &&
opts?.filters?.onEmptyFilter === EmptyFilterOption.RETURN_NONE opts?.filters?.onEmptyFilter === EmptyFilterOption.RETURN_NONE

View File

@ -1,8 +1,15 @@
import { InternalTables } from "../../../db/utils" import { InternalTables } from "../../../db/utils"
import * as userController from "../user" import * as userController from "../user"
import { context } from "@budibase/backend-core" import { context } from "@budibase/backend-core"
import { Ctx, FieldType, Row, Table, UserCtx } from "@budibase/types" import {
import { FieldTypes } from "../../../constants" Ctx,
FieldType,
Row,
SearchFilters,
Table,
UserCtx,
} from "@budibase/types"
import { FieldTypes, NoEmptyFilterStrings } from "../../../constants"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import validateJs from "validate.js" import validateJs from "validate.js"
@ -142,6 +149,29 @@ export async function validate({
// don't do a pure falsy check, as 0 is included // don't do a pure falsy check, as 0 is included
// https://github.com/Budibase/budibase/issues/10118 // https://github.com/Budibase/budibase/issues/10118
export function invalidFilter(value: any) { export function removeEmptyFilters(filters: SearchFilters) {
return value == null || value === "" for (let filterField of NoEmptyFilterStrings) {
if (!filters[filterField]) {
continue
}
for (let filterType of Object.keys(filters)) {
if (filterType !== filterField) {
continue
}
// don't know which one we're checking, type could be anything
const value = filters[filterType] as unknown
if (typeof value === "object") {
for (let [key, value] of Object.entries(
filters[filterType] as object
)) {
if (value == null || value === "") {
// @ts-ignore
delete filters[filterField][key]
}
}
}
}
}
return filters
} }