import _ from "lodash"; import qs from "@garrettcharliehill/query-string-object"; import { AndFilter, FieldValue, GQLFiltersObject, OmniFilter, OmniFilterOperator, OmniFilterSet, OmniFilterValue, StickyObject } from "../common/types"; import { isAndFilter } from "../common/utils"; import { allOperatorDefinitions } from "../common/constants"; import { flatten } from 'flat'; import { v4 as uuid } from 'uuid' export const convertOmniFiltersToStickyObject = (filters: OmniFilter[]): { omni: StickyObject } => { // create an object to store the filters object inside const filtersMap: StickyObject = {} // iterate through all of the filters and add them to the map for (const filter of filters) { const { field, operator, value } = filter // if field, operator, or value are null then the filter isn't valid if (field !== null && operator !== null && value !== null) { // get the key path for the object // example: given this filtersMap: { myFilter: { eq: 'this' } }, // the key path for the value in the filter would be "myFilter.eq" const filterKeyPath = `${field.id}.${operator}` const filtersMapValue = _.get(filtersMap, filterKeyPath) as OmniFilterValue | undefined if (filtersMapValue) { // if a value for this keypath already exists, we need to merge this value into it if (isAndFilter(filtersMapValue)) { // if the value is already an AndFilter, just add this value to the end of the array const newValue: AndFilter = { and: [ ...filtersMapValue.and, value ] } _.set(filtersMap, filterKeyPath, newValue) } else { // otherwise, create a new AndFilter combining the old value and new value into the array const newValue: AndFilter = { and: [filtersMapValue, value] } _.set(filtersMap, filterKeyPath, newValue) } } else { // if a value for this keypath doesn't already exist, just set it to this value _.set(filtersMap, filterKeyPath, value) } } } // set the filtersMap to a single omni property const queryParamsObject = { omni: filtersMap } return queryParamsObject } export const convertStickyObjectToFilters = (queryStringObject: StickyObject, filterSet: OmniFilterSet): OmniFilter[] => { // iterate through all of the properties of the object and create filters const filters: OmniFilter[] = [] for (const filterId of Object.keys(queryStringObject)) { // retrieve the filterOption from the filterSet that corresponds to this filter const filterOption = filterSet[filterId] // if the filter option doesn't exist, skip that filter if (filterOption) { // iterate through all of the operators for this filterId for (const [operator, value] of Object.entries(queryStringObject[filterId])) { if (isAndFilter(value)) { // if the value is an AndFilter, extract the individual array items into their own separate filters for (const subValue of value.and) { const newFilter: OmniFilter = { field: { id: filterId, option: filterOption, }, operator: operator as OmniFilterOperator, value: subValue, } filters.push(newFilter) } } else { // otherwise, create a filter from the value const newFilter: OmniFilter = { field: { id: filterId, option: filterOption, }, operator: operator as OmniFilterOperator, value, } filters.push(newFilter) } } } } // return the new filters array return filters } export const extractOmniFiltersFromQueryString = (queryString: string, filterSet: OmniFilterSet): OmniFilter[] => { if (queryString.startsWith('?')) { queryString = queryString.slice(1) } // parse the entire query string into an object const queryParamsObject = qs.parse(queryString) as { omni?: StickyObject } // extract only the omni filter property const filtersMap = queryParamsObject.omni // if the omni filter property doesn't exist, return an empty filters array if (!filtersMap) return [] const filters = convertStickyObjectToFilters(filtersMap, filterSet) // return the new filters array return filters } export const convertOmniFiltersToGQLObject = (filters: OmniFilter[]): GQLFiltersObject => { // create an object to store the filters object inside const gqlObject: GQLFiltersObject = {} const optionOperators = allOperatorDefinitions .filter(definition => definition.supportsOptions) .map(definition => definition.operator) // iterate through all of the filters and add them to the map for (const filter of filters) { const { field, operator, value } = filter // if field, operator, or value are null then the filter isn't valid if (field !== null && operator !== null && value !== null) { // get the key path for the object // example: given this gqlObject: { businessUnitSettings: { _filter: { value: { eq: 'this' } } } }, // the key path for the value in the filter would be "businessUnitSettings._filter.value.eq" const useOptionApi = field.option.resolveOptions && optionOperators.includes(operator) && field.option.optionApi const fieldApi = useOptionApi ? field.option.optionApi : field.option.api const filterKeyPath = `${fieldApi}.${operator}` const filtersMapValue = _.get(gqlObject, filterKeyPath) as unknown as OmniFilterValue | undefined if (filtersMapValue !== undefined) { // if a value for this keypath already exists, we need to merge this value into it if (isAndFilter(filtersMapValue)) { // if the value is already an AndFilter, just add this value to the end of the array const newValue: AndFilter = { and: [...filtersMapValue.and, value] } _.set(gqlObject, filterKeyPath, newValue) } else { // otherwise, create a new AndFilter combining the old value and new value into the array const newValue: AndFilter = { and: [filtersMapValue, value] } _.set(gqlObject, filterKeyPath, newValue) } } else { // if a value for this keypath doesn't already exist, just set it to this value _.set(gqlObject, filterKeyPath, value) } } } return gqlObject } export const convertGQLObjectToStickyObject = (gqlObject: GQLFiltersObject, filterSet: OmniFilterSet): StickyObject => { const flattenedObject = flatten(gqlObject) as Object; const filtersMap = {} as StickyObject; for (const [keyPath, value] of Object.entries(flattenedObject)) { const [fieldId, searchOption] = Object.entries(filterSet) .find(([,option]) => keyPath.startsWith(option.api) || (option.optionApi && keyPath.startsWith(option.optionApi))) || [undefined, undefined]; if (fieldId && searchOption) { let valueKeyPath = keyPath; valueKeyPath = valueKeyPath.replace(`${searchOption.api}.`, ""); valueKeyPath = valueKeyPath.replace(`${searchOption.optionApi}.`, ""); const filterKeyPath = `${fieldId}.${valueKeyPath}`; _.set(filtersMap, filterKeyPath, value); } } return filtersMap; } export const generateFiltersMapFromFilters = (filters: OmniFilter[]): { [key: string]: OmniFilter } => { const newFilters: { [key: string]: OmniFilter } = {} for (const filter of filters) { newFilters[uuid()] = filter } return newFilters }