Skip to content

Commit

Permalink
#1316: enable --metadata and multi-type for fixKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed May 14, 2024
1 parent 53996d6 commit fee3e04
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
15 changes: 13 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ yargs(hideBin(process.argv))
})
// @ts-expect-error
.command({
command: 'fixKeys <BU> <TYPE> [KEY]',
command: 'fixKeys <BU> [TYPE] [KEY]',
aliases: ['fx'],
desc: 'changes the key of the items to match the name',
builder: (yargs) => {
Expand All @@ -655,6 +655,12 @@ yargs(hideBin(process.argv))
type: 'string',
describe: 'key(s) of the metadata component(s)',
})
.option('metadata', {
type: 'string',
alias: 'm',
group: 'Options for fixKeys:',
describe: 'type or type:key or type:i:id or type:n:name to fix',
})
.option('like', {
type: 'string',
group: 'Options for fixKeys:',
Expand Down Expand Up @@ -686,7 +692,12 @@ yargs(hideBin(process.argv))
},
handler: (argv) => {
Mcdev.setOptions(argv);
Mcdev.fixKeys(argv.BU, argv.TYPE, csvToArray(argv.KEY));
const typeKeyCombo = Mcdev.metadataToTypeKey(argv.metadata);
if ('undefined' === typeof typeKeyCombo) {
Mcdev.fixKeys(argv.BU, csvToArray(argv.TYPE), csvToArray(argv.KEY));
} else {
Mcdev.fixKeys(argv.BU, typeKeyCombo);
}
},
})
// @ts-expect-error
Expand Down
51 changes: 47 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,12 +884,55 @@ class Mcdev {
* Updates the key to match the name field
*
* @param {string} businessUnit name of BU
* @param {string} selectedType limit to given metadata types
* @param {string[] | TypeKeyCombo} selectedTypesArr limit retrieval to given metadata type
* @param {string[]} [keys] customerkey of the metadata
* @returns {Promise.<Object.<string, string[]>>} key: business unit name, value: list of paused item keys
* @returns {Promise.<Object.<string, object>>} key1: business unit name, key2:type value: list of fixed item keys
*/
static async fixKeys(businessUnit, selectedType, keys) {
return this.#runMethod('fixKeys', businessUnit, selectedType, keys);
static async fixKeys(businessUnit, selectedTypesArr, keys) {
if (selectedTypesArr) {
// check if types are valid
for (const selectedType of Array.isArray(selectedTypesArr)
? selectedTypesArr
: Object.keys(selectedTypesArr)) {
if (!Util._isValidType(selectedType)) {
return;
}
}
} else {
// do it for all standard retrieve types
const properties = await config.getProperties();
selectedTypesArr = [];
selectedTypesArr.push(
...new Set(
properties.metaDataTypes.retrieve
.map((type) => type.split('-')[0])
.filter(
(type) =>
!MetadataTypeDefinitions[type].keyIsFixed &&
MetadataTypeDefinitions[type].keyField !==
MetadataTypeDefinitions[type].nameField &&
MetadataTypeDefinitions[type].keyField !==
MetadataTypeDefinitions[type].idField
)
)
);
Util.logger.info(`:: Fixing keys for ${selectedTypesArr.join(', ')}`);
}

const response = {};
for (const selectedType of Array.isArray(selectedTypesArr)
? selectedTypesArr
: Object.keys(selectedTypesArr)) {
const temp = await this.#runMethod(
'fixKeys',
businessUnit,
selectedType,
Array.isArray(selectedTypesArr) ? keys : selectedTypesArr[selectedType]
);
response[businessUnit] ||= {};
response[businessUnit][selectedType] = temp[businessUnit];
}
return response;
}
/**
* run a method across BUs
Expand Down

0 comments on commit fee3e04

Please sign in to comment.