diff --git a/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js b/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js index 12c083b5..e1c71426 100644 --- a/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js +++ b/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import { FormattedMessage, useIntl } from 'react-intl'; import { Modal, @@ -21,12 +21,13 @@ import { useEntityType } from '../../../hooks/useEntityType'; import { useCancelQuery } from '../../../hooks/useCancelQuery'; import { useTestQuery } from '../../../hooks/useTestQuery'; import { getFieldOptions } from '../helpers/selectOptions'; +import upgradeInitialValues from '../helpers/upgradeInitialValues'; export const QueryBuilderModal = ({ isOpen, setIsModalShown, saveBtnLabel, - initialValues, + initialValues: originalInitialValues, entityTypeDataSource, runQueryDataSource, testQueryDataSource, @@ -48,6 +49,11 @@ export const QueryBuilderModal = ({ const { cancelQuery } = useCancelQuery({ cancelQueryDataSource }); + const initialValues = useMemo( + () => upgradeInitialValues(originalInitialValues, entityType), + [originalInitialValues, entityType], + ); + const { source, setSource, diff --git a/src/QueryBuilder/QueryBuilder/helpers/selectOptions.js b/src/QueryBuilder/QueryBuilder/helpers/selectOptions.js index 67a27f02..a4438284 100644 --- a/src/QueryBuilder/QueryBuilder/helpers/selectOptions.js +++ b/src/QueryBuilder/QueryBuilder/helpers/selectOptions.js @@ -117,7 +117,7 @@ export const getFieldOptions = (options) => { return options?.filter(o => !ids.includes(o.name)).map(o => ({ label: o.labelAlias, - value: o.idColumnName || o.name, + value: o.name, dataType: o.dataType.dataType, source: o.source, values: getFilledValues(o.values), diff --git a/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js new file mode 100644 index 00000000..2fd261df --- /dev/null +++ b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js @@ -0,0 +1,28 @@ +/** + * 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. + */ +export default function upgradeInitialValues(initialValues, entityType) { + if (!initialValues || !entityType) { + return initialValues; + } + + const idColumnMapping = {}; + + entityType.columns.forEach((column) => { + if (column.idColumnName) { + idColumnMapping[column.idColumnName] = column.name; + } + }); + + const upgradedInitialValues = {}; + + Object.keys(initialValues).forEach((key) => { + const newKey = idColumnMapping[key] || key; + + upgradedInitialValues[newKey] = initialValues[key]; + }); + + return upgradedInitialValues; +} diff --git a/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.test.js b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.test.js new file mode 100644 index 00000000..ed9e27d1 --- /dev/null +++ b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.test.js @@ -0,0 +1,37 @@ +import upgradeInitialValues from './upgradeInitialValues'; + +describe('initial values legacy conversion', () => { + const ENTITY_TYPE = { columns: [{ name: 'foo', idColumnName: 'idColumn' }] }; + + it.each([ + [null, null], + [null, undefined], + [undefined, null], + [undefined, undefined], + [null, {}], + [undefined, {}], + [{}, null], + [{}, undefined], + ])('ignores initialValues=%s and entityType=%s', (initialValues, entityType) => { + expect(upgradeInitialValues(initialValues, entityType)).toBe(initialValues); + }); + + it.each([{}, { foo: '' }, { bar: '' }, { foo: '', bar: '' }])( + 'processes but does not convert non-id columns in %s', + (values) => { + expect(upgradeInitialValues(values, ENTITY_TYPE)).toStrictEqual(values); + // indicates that processing was actually done + expect(upgradeInitialValues(values, ENTITY_TYPE)).not.toBe(values); + }, + ); + + it.each([ + [{ idColumn: '' }, { foo: '' }], + [ + { idColumn: '', bar: '' }, + { foo: '', bar: '' }, + ], + ])('converts %s to %s', (input, expected) => { + expect(upgradeInitialValues(input, ENTITY_TYPE)).toStrictEqual(expected); + }); +});