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

HCK-6859, HCK-6860, HCK-6861: fix dependency and selection for primary key structure segments #36

Merged
merged 3 commits into from
Jun 20, 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
Expand Up @@ -3,14 +3,17 @@
* @typedef {import('../../../shared/types').UUID} UUID
*/
const RandExp = require('randexp');
const { get, isEmpty, isPlainObject } = require('lodash');
const { PK_SEGMENT_TYPE } = require('../../../shared/constants');

/**
* @param {{ collection: object, jsonData: object }}
* @returns {string}
*/
const getPrimaryKeySampleByStructure = ({ collection, jsonData }) => {
const keyField = Object.values(collection.properties || {}).find(field => field.primaryKeyStructure);
const keyField = Object.values(collection.properties || {}).find(
field => field.primaryKey && !isEmpty(field.primaryKeyStructure),
);
const primaryKeyStructure = keyField?.primaryKeyStructure;

if (!Array.isArray(primaryKeyStructure)) {
Expand Down Expand Up @@ -42,14 +45,11 @@ const getPrimaryKeySampleByStructure = ({ collection, jsonData }) => {
* @returns {string}
*/
const getPrimaryKeyStructureFieldValue = ({ segment, collection, jsonData }) => {
const fieldNames = (segment.segmentKey || []).map(({ keyId }) => {
return findFieldNameById({ collection, id: keyId });
const fieldNamePaths = (segment.segmentKey || []).map(({ keyId }) => {
return getFieldNamePath({ collection, keyId });
});

return fieldNames
.filter(Boolean)
.map(fieldName => jsonData[fieldName])
.join('');
return fieldNamePaths.map(fieldNamePath => get(jsonData, fieldNamePath)).join('');
};

/**
Expand All @@ -67,21 +67,41 @@ const getPrimaryKeyStructurePatternValue = ({ segment }) => {
};

/**
* @param {{ collection: object, id: UUID }}
* @returns {string}
* @param {{ collection: object, keyId: UUID }}
* @returns {string[]}
*/
const findFieldNameById = ({ collection, id }) => {
return Object.entries(collection.properties || {}).reduce((result, [fieldName, field]) => {
if (result) {
return result;
}
const getFieldNamePath = ({ collection, keyId }) => {
const properties = getCollectionProperties({ collection });

if (field.GUID === id) {
return fieldName;
return Object.entries(properties).reduce((result, [fieldName, field]) => {
if (field.GUID === keyId) {
return [...result, fieldName];
}

return findFieldNameById({ collection: field, id });
}, '');
const namePath = getFieldNamePath({ collection: field, keyId });

return namePath.length ? [...result, fieldName, ...namePath] : result;
}, []);
};

/**
* @param {{ collection: object }}
* @returns {object}
*/
const getCollectionProperties = ({ collection }) => {
if (collection.properties) {
return collection.properties;
}

if (Array.isArray(collection.items)) {
return collection.items.reduce((result, item, index) => ({ ...result, [index]: item }), {});
}

if (isPlainObject(collection.items)) {
return collection.items;
}

return {};
};

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const getInsertScriptForCollection = ({ jsonData, collection }) => {
}

const keyPropertyName = Object.keys(collection?.properties ?? {}).find(propertyName =>
Boolean(collection?.properties?.[propertyName]['<key>']),
Boolean(collection?.properties?.[propertyName]?.primaryKey),
);

const insertionPath = getKeySpaceReference(collection);
Expand Down
10 changes: 9 additions & 1 deletion properties_pane/field_level/fieldLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ making sure that you maintain a proper JSON format.
"propertyKeyword": "primaryKeyStructure",
"propertyTooltip": "For user-defined key structure, describe the different segments for the PK",
"propertyType": "group",
"groupControl": true,
"templateOptions": {
"hasToggler": true
},
"structure": [
{
"propertyName": "Segment type",
Expand Down Expand Up @@ -175,6 +179,10 @@ making sure that you maintain a proper JSON format.
"propertyType": "fieldList",
"template": "orderedList",
"parentType": "primaryKeyStructure",
"disabledItemStrategy": {
"type": "allowTypes",
"parameters": ["string", "number"]
},
"dependency": {
"key": "segmentType",
"value": "field"
Expand Down Expand Up @@ -224,7 +232,7 @@ making sure that you maintain a proper JSON format.
}
],
"dependency": {
"key": "containerLevelKey",
"key": "primaryKey",
"value": true
}
},
Expand Down