Files
calendar/packages/features/data-table/lib/server.ts
T
a7e7561efa feat: add text & numeric filters to insights routing responses (#18016)
* feat: improve text filters (WIP)

* move function to bottom

* apply some styles

* fix selection of TextFilterOptions

* rename value to operand

* remove unused file

* merge filters/filters into filters/utils

* fix regression of not putting url params correctly

* move makeWhereClause to filters/utils

* fix negative, empty, and not empty operators

* fix initial filtering from search state (url)

* fix type errors

* do not send an empty array to query

* update yarn.lock

* i18n for text filter operators

* extract logic as useColumnFilters()

* add missing import

* fix type error

* revert yarn.lock

* use i18n

* insensitive text match

* move data-table to @calcom/features

* fix type errors

* fix type errors

* fix type errors

* feat: support DataTable filters for Insights Routing WIP

* remove unused filters

* remove additionalFilters and fix types

* clean up filter components

* support icons for ActiveFilters

* support filters on json

* fix filter ui

* fix type error and clean up

* revert changes

* revert change

* clean up

* revert change

* fix compatibility with insights booking page

* remove unused params

* fix type errors

* update yarn.lock

* fix field filter and adjust ui

* chore: update yarn.lock

* fix text filter

* add Clear Filters button

* add more test data

* feat: support numeric filter WIP

* feat: support numeric filter

* feat: move People to an external filter

* fixing query states

* rename state to activeFilters

* add useDataTable

* fix ResponseCellValue bug and enable resizing for RoutingFormResponsesTable

* fix response values

* truncate attribute text

* seed booking attendees

* fix accessor (no actual change though)

* add a gap

* rename

* use pathname as default identifier for DataTable

* fix type error

* support text filter for assignment reason

* Apply suggestions from code review

Co-authored-by: Alex van Andel <me@alexvanandel.com>

* rename fields to headers

* use safeParse

* fix type error

* push yarn.lock with faker

* fix MemberList

---------

Co-authored-by: Udit Takkar <udit222001@gmail.com>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
Co-authored-by: sean-brydon <sean@cal.com>
2024-12-12 16:06:30 +00:00

140 lines
3.6 KiB
TypeScript

import type { FilterValue } from "./types";
import { isSelectFilterValue, isTextFilterValue, isNumberFilterValue } from "./utils";
type makeWhereClauseProps = {
columnName: string;
filterValue: FilterValue;
json?: true | { path: string[] };
};
export function makeWhereClause(props: makeWhereClauseProps) {
const { columnName, filterValue } = props;
const isJson = props.json === true || (typeof props.json === "object" && props.json.path?.length > 0);
const jsonPath = isJson && typeof props.json === "object" ? props.json.path : undefined;
const jsonPathObj = isJson && jsonPath ? { path: jsonPath } : {};
if (isSelectFilterValue(filterValue)) {
return {
[columnName]: {
...jsonPathObj,
...(isJson ? { array_contains: filterValue } : { in: filterValue }),
},
};
} else if (isTextFilterValue(filterValue)) {
const { operator, operand } = filterValue.data;
switch (operator) {
case "equals":
return {
[columnName]: {
...jsonPathObj,
equals: operand,
},
};
case "notEquals":
return {
[columnName]: {
...jsonPathObj,
not: operand,
},
};
case "contains":
return {
[columnName]: {
...jsonPathObj,
...(isJson ? { string_contains: operand } : { contains: operand, mode: "insensitive" }),
},
};
case "notContains":
return {
NOT: {
[columnName]: {
...jsonPathObj,
...(isJson ? { string_contains: operand } : { contains: operand, mode: "insensitive" }),
},
},
};
case "startsWith":
return {
[columnName]: {
...jsonPathObj,
...(isJson ? { string_starts_with: operand } : { startsWith: operand, mode: "insensitive" }),
},
};
case "endsWith":
return {
[columnName]: {
...jsonPathObj,
...(isJson ? { string_ends_with: operand } : { endsWith: operand, mode: "insensitive" }),
},
};
case "isEmpty":
return {
[columnName]: {
...jsonPathObj,
equals: "",
},
};
case "isNotEmpty":
return {
NOT: {
[columnName]: {
...jsonPathObj,
equals: "",
},
},
};
default:
throw new Error(`Invalid operator for text filter: ${operator}`);
}
} else if (isNumberFilterValue(filterValue)) {
const { operator, operand } = filterValue.data;
switch (operator) {
case "eq":
return {
[columnName]: {
...jsonPathObj,
equals: operand,
},
};
case "neq":
return {
[columnName]: {
...jsonPathObj,
not: operand,
},
};
case "gt":
return {
[columnName]: {
...jsonPathObj,
gt: operand,
},
};
case "gte":
return {
[columnName]: {
...jsonPathObj,
gte: operand,
},
};
case "lt":
return {
[columnName]: {
...jsonPathObj,
lt: operand,
},
};
case "lte":
return {
[columnName]: {
...jsonPathObj,
lte: operand,
},
};
default:
throw new Error(`Invalid operator for number filter: ${operator}`);
}
}
return {};
}