Skip to content

Commit

Permalink
#984: added a function to retrieve and deploy that is used for fixKey…
Browse files Browse the repository at this point in the history
…s and updateNotifications
  • Loading branch information
phjulia committed Aug 30, 2023
1 parent 0dc51ae commit 23d0cea
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 89 deletions.
45 changes: 41 additions & 4 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ Provides default functionality that can be overwritten by child metadata type cl
<dt><a href="#Mcdev.">Mcdev.(cred, bu, type, [keyArr])</a> ⇒ <code>Promise.&lt;Array.&lt;string&gt;&gt;</code></dt>
<dd><p>Updates the key to match the name field</p>
</dd>
<dt><a href="#Mcdev.">Mcdev.(type, [keyArr], buObject)</a> ⇒ <code>Promise.&lt;Object.&lt;string, Array.&lt;string&gt;&gt;&gt;</code></dt>
<dt><a href="#Mcdev.">Mcdev.(cred, bu, type, [keyArr], methodName)</a> ⇒ <code>Promise.&lt;Array.&lt;string&gt;&gt;</code></dt>
<dd><p>A function to retrieve, update and deploy items</p>
</dd>
<dt><a href="#Mcdev.">Mcdev.(type, metadataMap, methodName)</a> ⇒ <code>Array.&lt;string&gt;</code></dt>
<dd><p>helper function to get keys of items to update</p>
</dd>
<dt><a href="#Mcdev.">Mcdev.(cred, bu, type, [keyArr])</a> ⇒ <code>Promise.&lt;Array.&lt;string&gt;&gt;</code></dt>
<dd><p>Updates notification email address field</p>
</dd>
<dt><a href="#Automation.">Automation.(metadata)</a> ⇒ <code>boolean</code></dt>
Expand Down Expand Up @@ -8650,17 +8656,48 @@ Updates the key to match the name field

<a name="Mcdev."></a>

## Mcdev.(type, [keyArr], buObject) ⇒ <code>Promise.&lt;Object.&lt;string, Array.&lt;string&gt;&gt;&gt;</code>
## Mcdev.(cred, bu, type, [keyArr], methodName) ⇒ <code>Promise.&lt;Array.&lt;string&gt;&gt;</code>
A function to retrieve, update and deploy items

**Kind**: global function
**Returns**: <code>Promise.&lt;Array.&lt;string&gt;&gt;</code> - list of keys that were affected

| Param | Type | Description |
| --- | --- | --- |
| cred | <code>string</code> | name of Credential |
| bu | <code>string</code> | name of BU |
| type | <code>TYPE.SupportedMetadataTypes</code> | limit execution to given metadata type |
| [keyArr] | <code>Array.&lt;string&gt;</code> | customerkey of the metadata |
| methodName | <code>string</code> | name of the function to execute |

<a name="Mcdev."></a>

## Mcdev.(type, metadataMap, methodName) ⇒ <code>Array.&lt;string&gt;</code>
helper function to get keys of items to update

**Kind**: global function
**Returns**: <code>Array.&lt;string&gt;</code> - list of keys

| Param | Type | Description |
| --- | --- | --- |
| type | <code>TYPE.SupportedMetadataTypes</code> | limit execution to given metadata type |
| metadataMap | <code>TYPE.MetadataTypeMap</code> | metadata mapped by their keyField |
| methodName | <code>string</code> | name of the method to execute |

<a name="Mcdev."></a>

## Mcdev.(cred, bu, type, [keyArr]) ⇒ <code>Promise.&lt;Array.&lt;string&gt;&gt;</code>
Updates notification email address field

**Kind**: global function
**Returns**: <code>Promise.&lt;Object.&lt;string, Array.&lt;string&gt;&gt;&gt;</code> - key: business unit name, value: list of affected item keys
**Returns**: <code>Promise.&lt;Array.&lt;string&gt;&gt;</code> - list of keys that were affected

| Param | Type | Description |
| --- | --- | --- |
| cred | <code>string</code> | name of Credential |
| bu | <code>string</code> | name of BU |
| type | <code>TYPE.SupportedMetadataTypes</code> | limit execution to given metadata type |
| [keyArr] | <code>Array.&lt;string&gt;</code> | customerkey of the metadata |
| buObject | <code>TYPE.BuObject</code> | properties for auth |

<a name="Automation."></a>

Expand Down
194 changes: 109 additions & 85 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ class Mcdev {
}
} else {
resultArr.push(
...(await this.#updateNotificationsGeneric(type, keyArr, buObject))
...(await this.#updateNotifications(cred, bu, type, keyArr))
);
}
break;
Expand Down Expand Up @@ -1110,16 +1110,71 @@ class Mcdev {
static async #fixKeys(cred, bu, type, keyArr) {
const properties = await config.getProperties();
let actuallyFixedKeys = [];
const resultArr = [];

if (
MetadataTypeDefinitions[type].keyIsFixed === true ||
MetadataTypeDefinitions[type].keyField === MetadataTypeDefinitions[type].idField
) {
Util.logger.error(`Key cannot be updated for this type`);
return resultArr;
return actuallyFixedKeys;
}
const buObject = await Cli.getCredentialObject(
properties,
cred === null ? null : cred + '/' + bu,
null,
true
);
this.setOptions({
changeKeyField: MetadataTypeDefinitions[type].nameField,
fromRetrieve: true,
});
actuallyFixedKeys = await this.#updateItems(cred, bu, type, keyArr, 'fixKeys');
const dependentTypes = await Util.getDependentMetadata(type);
if (actuallyFixedKeys && actuallyFixedKeys.length) {
Util.logger.info(
`Successfully updated ${actuallyFixedKeys.length} key${
actuallyFixedKeys.length === 1 ? '' : 's'
} of type ${type}`
);
if (dependentTypes.length) {
Util.logger.warn(
`Please re-retrieve the following types as your local copies might now be outdated: ${Util.getGrayMsg(
dependentTypes.join(', ')
)}`
);
const reRetrieve = await Cli.postFixKeysReretrieve(type, dependentTypes);
if (reRetrieve) {
Util.logger.info(
`Retrieving latest versions of ${dependentTypes.join(', ')} from server`
);
const retriever = new Retriever(properties, buObject);
await retriever.retrieve(dependentTypes, null, null, false);
}
} else {
Util.logger.info(
`No dependent types found that need to be re-retrieved after fixing keys of type ${type}.`
);
}
} else {
Util.logger.warn(`No keys of type ${type} updated.`);
}

return actuallyFixedKeys;
}
/**
* A function to retrieve, update and deploy items
*
* @param {string} cred name of Credential
* @param {string} bu name of BU
* @param {TYPE.SupportedMetadataTypes} type limit execution to given metadata type
* @param {string[]} [keyArr] customerkey of the metadata
* @param {string} methodName name of the function to execute
* @returns {Promise.<string[]>} list of keys that were affected
*/
static async #updateItems(cred, bu, type, keyArr, methodName) {
const properties = await config.getProperties();
let actuallyUpdatedItems = [];
const resultArr = [];

const buObject = await Cli.getCredentialObject(
properties,
cred === null ? null : cred + '/' + bu,
Expand All @@ -1132,55 +1187,43 @@ class Mcdev {
const retrieved = await retriever.retrieve([type], keyArr, null, false);

const metadataMap = Object.values(retrieved)[0][0];
const keysForDeploy = MetadataTypeInfo[type].getKeysForFixing(metadataMap);
const keysForDeploy = await this.#getKeys(type, metadataMap, methodName);
if (keysForDeploy.length < 1) {
Util.logger.warn(
`No items found with a key-name mismatch that match your criteria.\n`
);
return resultArr;
}
this.setOptions({
changeKeyField: MetadataTypeDefinitions[type].nameField,
fromRetrieve: true,
});
const deployed = await Deployer.deploy(cred + '/' + bu, [type], keysForDeploy);
actuallyFixedKeys = Object.keys(Object.values(Object.values(deployed)[0])[0]);
resultArr.push(...actuallyFixedKeys);
const dependentTypes = await Util.getDependentMetadata(type);
if (actuallyFixedKeys && actuallyFixedKeys.length) {
Util.logger.info(
`Successfully updated ${actuallyFixedKeys.length} key${
actuallyFixedKeys.length === 1 ? '' : 's'
} of type ${type}`
);
if (dependentTypes.length) {
Util.logger.warn(
`Please re-retrieve the following types as your local copies might now be outdated: ${Util.getGrayMsg(
dependentTypes.join(', ')
)}`
);
const reRetrieve = await Cli.postFixKeysReretrieve(type, dependentTypes);
if (reRetrieve) {
Util.logger.info(
`Retrieving latest versions of ${dependentTypes.join(', ')} from server`
);
const retriever = new Retriever(properties, buObject);
await retriever.retrieve(dependentTypes, null, null, false);
}
} else {
Util.logger.info(
`No dependent types found that need to be re-retrieved after fixing keys of type ${type}.`
);
}
} else {
Util.logger.warn(`No keys of type ${type} updated.`);
}
actuallyUpdatedItems = Object.keys(Object.values(Object.values(deployed)[0])[0]);
resultArr.push(...actuallyUpdatedItems);
} catch (ex) {
Util.logger.errorStack(ex, 'mcdev.fixKeys failed');
Util.logger.errorStack(ex, `mcdev.${methodName} failed`);
}
Util.logger.info(`:: Done\n`);
return resultArr;
}
/**
* helper function to get keys of items to update
*
* @param {TYPE.SupportedMetadataTypes} type limit execution to given metadata type
* @param {TYPE.MetadataTypeMap} metadataMap metadata mapped by their keyField
* @param {string} methodName name of the method to execute
* @returns {string[]} list of keys
*/
static async #getKeys(type, metadataMap, methodName) {
const keys = [];
switch (methodName) {
case 'fixKeys': {
keys.push(...(await MetadataTypeInfo[type].getKeysForFixing(metadataMap)));
break;
}
case 'updateNotifications': {
keys.push(...(await MetadataTypeInfo[type].getKeysToSetNotifications(metadataMap)));
break;
}
}
return keys;
}
/**
* Updates notification email address field
*
Expand All @@ -1207,63 +1250,44 @@ class Mcdev {
/**
* Updates notification email address field
*
* @param {string} cred name of Credential
* @param {string} bu name of BU
* @param {TYPE.SupportedMetadataTypes} type limit execution to given metadata type
* @param {string[]} [keyArr] customerkey of the metadata
* @param {TYPE.BuObject} buObject properties for auth
* @returns {Promise.<Object.<string, string[]>>} key: business unit name, value: list of affected item keys
* @returns {Promise.<string[]>} list of keys that were affected
*/
static async #updateNotificationsGeneric(type, keyArr, buObject) {
static async #updateNotifications(cred, bu, type, keyArr) {
let keysUpdatedNotifications = [];
const resultArr = [];
const properties = await config.getProperties();
if (Util.OPTIONS.errorEmail || Util.OPTIONS.errorNote || Util.OPTIONS.completionNote) {
Util.logger.error(
`--errorEmail, --errorNote and --completionNote options are not available for ${type}`
);
return resultArr;
return keysUpdatedNotifications;
}
if (!MetadataTypeDefinitions[type].fields.sendEmailNotification) {
Util.logger.error(`Update notifications is not supported for this type`);
return resultArr;
return keysUpdatedNotifications;
}
try {
Util.logger.info(`Retrieving latest versions of ${type} from server`);
const retriever = new Retriever(properties, buObject);
const retrieved = await retriever.retrieve([type], keyArr, null, false);

const metadataMap = Object.values(retrieved)[0][0];
const keysForDeploy = await MetadataTypeInfo[type].getKeysToSetNotifications(
metadataMap
);
if (keysForDeploy.length < 1) {
Util.logger.warn(
`No items found with a key-name mismatch that match your criteria.\n`
);
return resultArr;
}
this.setOptions({
fromRetrieve: true,
});
const deployed = await Deployer.deploy(
buObject.credential + '/' + buObject.businessUnit,
[type],
keysForDeploy
this.setOptions({
fromRetrieve: true,
});
keysUpdatedNotifications = await this.#updateItems(
cred,
bu,
type,
keyArr,
'updateNotifications'
);
if (keysUpdatedNotifications && keysUpdatedNotifications.length) {
Util.logger.info(
`Successfully updated notifications. Updated ${
keysUpdatedNotifications.length
} key${keysUpdatedNotifications.length === 1 ? '' : 's'} of type ${type}`
);
keysUpdatedNotifications = Object.keys(Object.values(Object.values(deployed)[0])[0]);
resultArr.push(...keysUpdatedNotifications);
if (keysUpdatedNotifications && keysUpdatedNotifications.length) {
Util.logger.info(
`Successfully updated ${keysUpdatedNotifications.length} key${
keysUpdatedNotifications.length === 1 ? '' : 's'
} of type ${type}`
);
} else {
Util.logger.warn(`No keys of type ${type} updated.`);
}
} catch (ex) {
Util.logger.errorStack(ex, 'mcdev.updateNotificationsGeneric failed');
} else {
Util.logger.warn(`No keys of type ${type} updated.`);
}
return resultArr;
return keysUpdatedNotifications;
}
}

Expand Down

0 comments on commit 23d0cea

Please sign in to comment.