From 538882c84baf292b93c81d0e91b8bb2acfeadffb Mon Sep 17 00:00:00 2001 From: Noah Overcash Date: Thu, 26 Sep 2024 14:20:11 -0400 Subject: [PATCH] [UIPQB-125] Handle _version in queries (#155) * [UIPQB-125] Include _version in queries * Changelog, add explicit fqm-query dep * no ts :( * initial values coverage * rearrange --- CHANGELOG.md | 1 + package.json | 4 +++- .../QueryBuilderModal/QueryBuilderModal.js | 7 +++++-- .../helpers/upgradeInitialValues.js | 19 ++++++++++++++++--- .../helpers/upgradeInitialValues.test.js | 5 +++-- src/constants/query.js | 2 ++ src/hooks/useFqmVersion.js | 15 +++++++++++++++ 7 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 src/hooks/useFqmVersion.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 806e65e7..e3a3021b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * [UIPQB-131](https://folio-org.atlassian.net/browse/UIPQB-131) Columns and empty area display in the list details page, when we refresh the page 1st time or duplicate the list * [UIPQB-132](https://folio-org.atlassian.net/browse/UIPQB-132) Save not empty previews results and show it in test query * [UIPQB-126](https://folio-org.atlassian.net/browse/UIPQB-126) Update date format in requests to UTC +* [UIPQB-125](https://folio-org.atlassian.net/browse/UIPQB-125) Add support for FQM _version ## [1.1.4](https://github.com/folio-org/ui-plugin-query-builder/tree/v1.1.4) (2024-04-02) diff --git a/package.json b/package.json index 42333ba9..eb878a79 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,9 @@ ], "pluginType": "query-builder", "displayName": "ui-plugin-query-builder.meta.title", - "okapiInterfaces": {}, + "okapiInterfaces": { + "fqm-query": "2.0" + }, "stripesDeps": [ "@folio/stripes-acq-components" ] diff --git a/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js b/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js index a5802412..c09ce8c2 100644 --- a/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js +++ b/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js @@ -18,8 +18,9 @@ import { useRunQuery } from '../../../hooks/useRunQuery'; import { getSourceValue, useQuerySource } from '../../../hooks/useQuerySource'; import { queryBuilderModalPropTypes } from '../../propTypes'; import { QUERY_DETAILS_STATUSES, QUERY_KEYS } from '../../../constants/query'; -import { useEntityType } from '../../../hooks/useEntityType'; import { useCancelQuery } from '../../../hooks/useCancelQuery'; +import { useEntityType } from '../../../hooks/useEntityType'; +import { useFqmVersion } from '../../../hooks/useFqmVersion'; import { useTestQuery } from '../../../hooks/useTestQuery'; import { getFieldOptions } from '../helpers/selectOptions'; import upgradeInitialValues from '../helpers/upgradeInitialValues'; @@ -62,6 +63,8 @@ export const QueryBuilderModal = ({ }, }); + const fqmVersion = useFqmVersion(); + const { cancelQuery } = useCancelQuery({ cancelQueryDataSource }); const initialValues = useMemo( @@ -160,7 +163,7 @@ export const QueryBuilderModal = ({ const handleRun = async () => { await runQuery({ queryId, - fqlQuery, + fqlQuery: { ...fqlQuery, _version: fqmVersion }, userFriendlyQuery: queryStr, }); diff --git a/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js index 2fd261df..2481a05b 100644 --- a/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js +++ b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js @@ -2,12 +2,25 @@ * Upgrades initial values to indirectly reference id columns (e.g. vendor_code instead of vendor_id). * FQM used to previously require vendor_id, but this was changed in MODFQMMGR-151 to allow for better expression * and to allow for more flexibility in the future. + * + * As part of UIPQB-125, we're stripping out the _version key from the initial values, too. We will assume that any + * queries edited/created here are the latest version, as we only have the latest version of entity types available. + * In the future, it might be neat to send a request to /fqm/migrate if we see initialValues are out of date, but that's + * outside the scope of UIPQB-125 as we already upgrade queries in the background in mod-lists. */ export default function upgradeInitialValues(initialValues, entityType) { - if (!initialValues || !entityType) { + if (!initialValues) { return initialValues; } + const withoutVersion = { ...initialValues }; + + delete withoutVersion._version; + + if (!entityType) { + return withoutVersion; + } + const idColumnMapping = {}; entityType.columns.forEach((column) => { @@ -18,10 +31,10 @@ export default function upgradeInitialValues(initialValues, entityType) { const upgradedInitialValues = {}; - Object.keys(initialValues).forEach((key) => { + Object.keys(withoutVersion).forEach((key) => { const newKey = idColumnMapping[key] || key; - upgradedInitialValues[newKey] = initialValues[key]; + upgradedInitialValues[newKey] = withoutVersion[key]; }); return upgradedInitialValues; diff --git a/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.test.js b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.test.js index ed9e27d1..301c45e3 100644 --- a/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.test.js +++ b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.test.js @@ -13,7 +13,7 @@ describe('initial values legacy conversion', () => { [{}, null], [{}, undefined], ])('ignores initialValues=%s and entityType=%s', (initialValues, entityType) => { - expect(upgradeInitialValues(initialValues, entityType)).toBe(initialValues); + expect(upgradeInitialValues(initialValues, entityType)).toStrictEqual(initialValues); }); it.each([{}, { foo: '' }, { bar: '' }, { foo: '', bar: '' }])( @@ -26,7 +26,8 @@ describe('initial values legacy conversion', () => { ); it.each([ - [{ idColumn: '' }, { foo: '' }], + [{ _version: '1', foo: '' }, { foo: '' }], + [{ _version: '1', idColumn: '' }, { foo: '' }], [ { idColumn: '', bar: '' }, { foo: '', bar: '' }, diff --git a/src/constants/query.js b/src/constants/query.js index 6a69ec62..0de10655 100644 --- a/src/constants/query.js +++ b/src/constants/query.js @@ -5,6 +5,8 @@ export const QUERY_DETAILS_STATUSES = { }; export const QUERY_KEYS = { + FQM_VERSION: 'FQM_VERSION', + QUERY_PLUGIN_CONTENT_DATA: 'QUERY_PLUGIN_CONTENT_DATA', QUERY_PLUGIN_ENTITY_TYPE: 'QUERY_PLUGIN_ENTITY_TYPE', QUERY_PLUGIN_PREVIEW_ENTITY_TYPE: 'QUERY_PLUGIN_PREVIEW_ENTITY_TYPE', diff --git a/src/hooks/useFqmVersion.js b/src/hooks/useFqmVersion.js new file mode 100644 index 00000000..42127f2f --- /dev/null +++ b/src/hooks/useFqmVersion.js @@ -0,0 +1,15 @@ +import { useNamespace, useOkapiKy } from '@folio/stripes/core'; +import { useQuery } from 'react-query'; +import { QUERY_KEYS } from '../constants/query'; + +export function useFqmVersion() { + const ky = useOkapiKy(); + const [queryKey] = useNamespace({ key: QUERY_KEYS.FQM_VERSION }); + + return ( + useQuery({ + queryKey: [queryKey], + queryFn: () => ky.get('fqm/version').text(), + }).data ?? '0' + ); +}