Skip to content

Commit

Permalink
HCK-6859, HCK-6860, HCK-6861: fix dependency and selection for primar…
Browse files Browse the repository at this point in the history
…y key structure segments (#36)

* HCK-6860: fix dependency for primary key structure

* HCK-6859: fix dependency and selection for pk structure segments

* HCK-6859: fix selection for nested PK structure field segment
  • Loading branch information
serhii-filonenko authored Jun 20, 2024
1 parent ac87bac commit e3e80c2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
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

0 comments on commit e3e80c2

Please sign in to comment.