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

[UIPQB-85] Use non-id columns for queries #86

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -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,
Expand All @@ -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,
Expand All @@ -48,6 +49,11 @@ export const QueryBuilderModal = ({

const { cancelQuery } = useCancelQuery({ cancelQueryDataSource });

const initialValues = useMemo(
() => upgradeInitialValues(originalInitialValues, entityType),
[originalInitialValues, entityType],
);

const {
source,
setSource,
Expand Down
2 changes: 1 addition & 1 deletion src/QueryBuilder/QueryBuilder/helpers/selectOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
28 changes: 28 additions & 0 deletions src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js
Original file line number Diff line number Diff line change
@@ -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;
}
37 changes: 37 additions & 0 deletions src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading