Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi select filtering (#5358) #8452

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('useColumnDefinitionsFromFieldMetadata', () => {
result.current;

expect(columnDefinitions.length).toBe(21);
expect(filterDefinitions.length).toBe(14);
expect(filterDefinitions.length).toBe(15);
expect(sortDefinitions.length).toBe(14);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const formatFieldMetadataItemsAsFilterDefinitions = ({
FieldMetadataType.Address,
FieldMetadataType.Relation,
FieldMetadataType.Select,
FieldMetadataType.MultiSelect,
FieldMetadataType.Currency,
FieldMetadataType.Rating,
FieldMetadataType.Actor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type StringFilter = {
regex?: string;
iregex?: string;
is?: IsFilter;
ad-elias marked this conversation as resolved.
Show resolved Hide resolved
containsIlike?: string;
containsAny?: string[];
ad-elias marked this conversation as resolved.
Show resolved Hide resolved
};

export type FloatFilter = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isDefined } from 'twenty-ui';

import { DATE_FILTER_TYPES } from '@/object-record/object-filter-dropdown/constants/DateFilterTypes';
import { NUMBER_FILTER_TYPES } from '@/object-record/object-filter-dropdown/constants/NumberFilterTypes';
import { SELECT_FILTER_TYPES } from '@/object-record/object-filter-dropdown/constants/SelectFilterTypes';
import { TEXT_FILTER_TYPES } from '@/object-record/object-filter-dropdown/constants/TextFilterTypes';

type ObjectFilterDropdownFilterInputProps = {
Expand Down Expand Up @@ -93,7 +94,9 @@ export const ObjectFilterDropdownFilterInput = ({
<ObjectFilterDropdownSourceSelect />
</>
)}
{filterDefinitionUsedInDropdown.type === 'SELECT' && (
{SELECT_FILTER_TYPES.includes(
filterDefinitionUsedInDropdown.type,
) && (
<>
<ObjectFilterDropdownSearchInput />
<ObjectFilterDropdownOptionSelect />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SELECT_FILTER_TYPES = ['SELECT', 'MULTI_SELECT'];
ad-elias marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const getOperandsForFilterDefinition = (
];
case 'RELATION':
return [...relationOperands, ...emptyOperands];
case 'MULTI_SELECT':
case 'SELECT':
return [...relationOperands];
case 'ACTOR': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,53 @@ const computeFilterRecordGqlOperationFilter = (
`Unknown operand ${filter.operand} for ${filter.definition.type} filter`,
);
}
case 'MULTI_SELECT': {
if (isEmptyOperand) {
return getEmptyRecordGqlOperationFilter(
filter.operand,
correspondingField,
filter.definition,
);
}
const stringifiedSelectValues = filter.value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we manipulated stringified values here? I feel they should be parsed as as regular values way earlier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved parsing the string to resolveFilterValue function, it's a bit cleaner now.

Could be good to first do a bigger refactoring of filtering, especially remove the Filter type and use ViewFilter everywhere instead, and after that move parsing earlier. What do you think?

let parsedOptionValues: string[] = [];

if (!isNonEmptyString(stringifiedSelectValues)) {
break;
}
ad-elias marked this conversation as resolved.
Show resolved Hide resolved

try {
parsedOptionValues = JSON.parse(stringifiedSelectValues);
} catch (e) {
throw new Error(
`Cannot parse filter value for MULTI_SELECT filter : "${stringifiedSelectValues}"`,
);
}

if (parsedOptionValues.length > 0) {
ad-elias marked this conversation as resolved.
Show resolved Hide resolved
ad-elias marked this conversation as resolved.
Show resolved Hide resolved
switch (filter.operand) {
case ViewFilterOperand.Is:
return {
[correspondingField.name]: {
containsAny: parsedOptionValues,
} as UUIDFilter,
};
case ViewFilterOperand.IsNot:
return {
not: {
[correspondingField.name]: {
containsAny: parsedOptionValues,
} as UUIDFilter,
},
};
default:
throw new Error(
`Unknown operand ${filter.operand} for ${filter.definition.type} filter`,
);
}
}
break;
}
case 'SELECT': {
if (isEmptyOperand) {
return getEmptyRecordGqlOperationFilter(
Expand Down Expand Up @@ -648,6 +695,35 @@ const computeFilterRecordGqlOperationFilter = (
}
break;
}
case 'ARRAY': {
switch (filter.operand) {
case ViewFilterOperand.Contains:
return {
[correspondingField.name]: {
containsIlike: filter.value, // Why doesn't `%${filter.value}%` here? %-signs only work when they are in compute-where-condition-parts.ts.
ad-elias marked this conversation as resolved.
Show resolved Hide resolved
} as StringFilter,
};
case ViewFilterOperand.DoesNotContain:
return {
not: {
[correspondingField.name]: {
containsIlike: `%${filter.value}%`,
} as StringFilter,
ad-elias marked this conversation as resolved.
Show resolved Hide resolved
},
};
case ViewFilterOperand.IsEmpty:
case ViewFilterOperand.IsNotEmpty:
return getEmptyRecordGqlOperationFilter(
filter.operand,
correspondingField,
filter.definition,
);
default:
throw new Error(
`Unknown operand ${filter.operand} for ${filter.definition.type} filter`,
);
}
}
// TODO: fix this with a new composite field in ViewFilter entity
case 'ACTOR': {
switch (filter.operand) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,70 @@ export const isMatchingStringFilter = ({
value,
}: {
stringFilter: StringFilter;
value: string;
value: string | null;
}) => {
const defaultedValue = value ?? '';

switch (true) {
case stringFilter.eq !== undefined: {
return value === stringFilter.eq;
return defaultedValue === stringFilter.eq;
}
case stringFilter.neq !== undefined: {
return value !== stringFilter.neq;
return defaultedValue !== stringFilter.neq;
}
case stringFilter.like !== undefined: {
const regexPattern = stringFilter.like.replace(/%/g, '.*');
const regexCaseSensitive = new RegExp(`^${regexPattern}$`);

return regexCaseSensitive.test(value);
return regexCaseSensitive.test(defaultedValue);
}
case stringFilter.ilike !== undefined: {
const regexPattern = stringFilter.ilike.replace(/%/g, '.*');
const regexCaseInsensitive = new RegExp(`^${regexPattern}$`, 'i');

return regexCaseInsensitive.test(value);
return regexCaseInsensitive.test(defaultedValue);
}
case stringFilter.in !== undefined: {
return stringFilter.in.includes(value);
return stringFilter.in.includes(defaultedValue);
}
case stringFilter.is !== undefined: {
if (stringFilter.is === 'NULL') {
return value === null;
return defaultedValue === null;
} else {
return value !== null;
return defaultedValue !== null;
}
}
case stringFilter.regex !== undefined: {
const regexPattern = stringFilter.regex;
const regexCaseSensitive = new RegExp(regexPattern);

return regexCaseSensitive.test(value);
return regexCaseSensitive.test(defaultedValue);
}
case stringFilter.iregex !== undefined: {
const regexPattern = stringFilter.iregex;
const regexCaseInsensitive = new RegExp(regexPattern, 'i');

return regexCaseInsensitive.test(value);
return regexCaseInsensitive.test(defaultedValue);
}
case stringFilter.gt !== undefined: {
return value > stringFilter.gt;
return defaultedValue > stringFilter.gt;
}
case stringFilter.gte !== undefined: {
return value >= stringFilter.gte;
return defaultedValue >= stringFilter.gte;
}
case stringFilter.lt !== undefined: {
return value < stringFilter.lt;
return defaultedValue < stringFilter.lt;
}
case stringFilter.lte !== undefined: {
return value <= stringFilter.lte;
return defaultedValue <= stringFilter.lte;
}
case stringFilter.startsWith !== undefined: {
return value.startsWith(stringFilter.startsWith);
return defaultedValue.startsWith(stringFilter.startsWith);
}
case stringFilter.containsAny !== undefined: {
return stringFilter.containsAny.some((item) =>
defaultedValue.includes(item),
);
}
default: {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Filter } from '@/object-record/object-filter-dropdown/types/Filter';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';

import { SELECT_FILTER_TYPES } from '@/object-record/object-filter-dropdown/constants/SelectFilterTypes';
import { useGetCurrentView } from '@/views/hooks/useGetCurrentView';
import { useUpsertCombinedViewFilters } from '@/views/hooks/useUpsertCombinedViewFilters';
import { availableFilterDefinitionsComponentState } from '@/views/states/availableFilterDefinitionsComponentState';
Expand Down Expand Up @@ -75,7 +76,10 @@ export const ViewBarFilterEffect = ({
? JSON.parse(viewFilterUsedInDropdown.value)
: [];
setObjectFilterDropdownSelectedRecordIds(viewFilterSelectedRecords);
} else if (filterDefinitionUsedInDropdown?.type === 'SELECT') {
} else if (
isDefined(filterDefinitionUsedInDropdown) &&
SELECT_FILTER_TYPES.includes(filterDefinitionUsedInDropdown.type)
) {
const viewFilterUsedInDropdown =
currentViewWithCombinedFiltersAndSorts?.viewFilters.find(
(filter) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,21 @@ export const computeWhereConditionParts = (
sql: `"${objectNameSingular}"."${key}" @> ARRAY[:...${key}${uuid}]`,
params: { [`${key}${uuid}`]: value },
};

case 'not_contains':
return {
sql: `NOT ("${objectNameSingular}"."${key}" && ARRAY[:...${key}${uuid}])`,
sql: `NOT ("${objectNameSingular}"."${key}"::text[] && ARRAY[:...${key}${uuid}]::text[])`,
params: { [`${key}${uuid}`]: value },
};
case 'containsAny':
return {
sql: `"${objectNameSingular}"."${key}"::text[] && ARRAY[:...${key}${uuid}]::text[]`,
ad-elias marked this conversation as resolved.
Show resolved Hide resolved
params: { [`${key}${uuid}`]: value },
};
case 'containsIlike':
return {
sql: `EXISTS (SELECT 1 FROM unnest("${objectNameSingular}"."${key}") AS elem WHERE elem ILIKE :${key}${uuid})`,
ad-elias marked this conversation as resolved.
Show resolved Hide resolved
params: { [`${key}${uuid}`]: `%${value}%` },
};

default:
throw new GraphqlQueryRunnerException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class InputTypeFactory {
eq: { type: enumType },
neq: { type: enumType },
in: { type: new GraphQLList(enumType) },
containsAny: { type: new GraphQLList(enumType) },
is: { type: FilterIs },
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FilterIs } from 'src/engine/api/graphql/workspace-schema-builder/graphq
export const ArrayFilterType = new GraphQLInputObjectType({
name: 'ArrayFilter',
fields: {
containsIlike: { type: new GraphQLList(GraphQLString) },
contains: { type: new GraphQLList(GraphQLString) },
not_contains: { type: new GraphQLList(GraphQLString) },
is: { type: FilterIs },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export const generateFields = <
? {
nullable: fieldMetadata.isNullable,
defaultValue: fieldMetadata.defaultValue,
isArray: fieldMetadata.type === FieldMetadataType.MULTI_SELECT,
isArray:
kind !== InputTypeDefinitionKind.Filter &&
FelixMalfait marked this conversation as resolved.
Show resolved Hide resolved
fieldMetadata.type === FieldMetadataType.MULTI_SELECT,
settings: fieldMetadata.settings,
isIdField: fieldMetadata.name === 'id',
}
Expand Down
Loading