Skip to content

Commit

Permalink
#139: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Feb 22, 2023
1 parent d83c06f commit 6eddfd7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
20 changes: 3 additions & 17 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ Provides default functionality that can be overwritten by child metadata type cl
<dd></dd>
<dt><a href="#DataExtensionMap">DataExtensionMap</a> : <code>object</code></dt>
<dd></dd>
<dt><a href="#AccountUserDocument">AccountUserDocument</a> : <code>object</code></dt>
<dt><a href="#beforeAfterAccountUserDocument">beforeAfterAccountUserDocument</a> : <code>object</code></dt>
<dd></dd>
<dt><a href="#AutomationActivity">AutomationActivity</a> : <code>object</code></dt>
<dd></dd>
Expand Down Expand Up @@ -744,7 +744,6 @@ MessageSendActivity MetadataType
* [.preDeployTasks(metadata)](#AccountUser.preDeployTasks) ⇒ <code>TYPE.MetadataTypeItem</code>
* [.createOrUpdate(metadata, metadataKey, hasError, metadataToUpdate, metadataToCreate)](#AccountUser.createOrUpdate) ⇒ <code>void</code>
* [.retrieveChangelog()](#AccountUser.retrieveChangelog) ⇒ <code>Promise.&lt;TYPE.MetadataTypeMapObj&gt;</code>
* [.generatePassword([length])](#AccountUser.generatePassword) ⇒ <code>string</code>
* [.document([metadata])](#AccountUser.document) ⇒ <code>Promise.&lt;void&gt;</code>
* [.postRetrieveTasks(metadata)](#AccountUser.postRetrieveTasks) ⇒ <code>TYPE.MetadataTypeItem</code>

Expand Down Expand Up @@ -828,19 +827,6 @@ Retrieves SOAP based metadata of metadata type into local filesystem. executes c

**Kind**: static method of [<code>AccountUser</code>](#AccountUser)
**Returns**: <code>Promise.&lt;TYPE.MetadataTypeMapObj&gt;</code> - Promise of metadata
<a name="AccountUser.generatePassword"></a>

### AccountUser.generatePassword([length]) ⇒ <code>string</code>
helper for [createOrUpdate](createOrUpdate) to generate a random initial password for new users
note: possible minimum length values in SFMC are 6, 8, 10, 15 chars. Therefore we should default here to 15 chars.

**Kind**: static method of [<code>AccountUser</code>](#AccountUser)
**Returns**: <code>string</code> - random password

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [length] | <code>number</code> | <code>15</code> | length of password; defaults to 15 |

<a name="AccountUser.document"></a>

### AccountUser.document([metadata]) ⇒ <code>Promise.&lt;void&gt;</code>
Expand Down Expand Up @@ -7607,9 +7593,9 @@ key=customer key
| [Template] | <code>object</code> | - |
| [Template.CustomerKey] | <code>string</code> | key of optionally associated DE teplate |

<a name="AccountUserDocument"></a>
<a name="beforeAfterAccountUserDocument"></a>

## AccountUserDocument : <code>object</code>
## beforeAfterAccountUserDocument : <code>object</code>
**Kind**: global typedef
**Properties**

Expand Down
23 changes: 18 additions & 5 deletions lib/metadataTypes/AccountUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,36 @@ class AccountUser extends MetadataType {
*/
static createOrUpdate(metadata, metadataKey, hasError, metadataToUpdate, metadataToCreate) {
super.createOrUpdate(metadata, metadataKey, hasError, metadataToUpdate, metadataToCreate);
AccountUser._setPasswordForNewUsers(metadataToCreate);
AccountUser._unassignRemovedRoles(metadataToUpdate);
}

/**
* helper for {@link AccountUser.createOrUpdate}
*
* @private
* @param {TYPE.AccountUserDocument[]} metadataToCreate list of created users
* @returns {void}
*/
static _setPasswordForNewUsers(metadataToCreate) {
for (const item of metadataToCreate) {
// if Password is not set during CREATE, generate one
// avoids error "Name, Email, UserID, and Password are required fields when creating a new user. (Code 11003)"
if (!item.Password) {
item.Password = this.generatePassword();
item.Password = this._generatePassword();
Util.logger.info(
` - Password for ${item.UserID} was not set. Generated password: ${item.Password}`
);
}
}
AccountUser._unassignRemovedRoles(metadataToUpdate);
}

/**
* helper for {@link AccountUser.createOrUpdate}
* it searches for roles that were removed from the user and unassigns them
*
* @private
* @param {{before:TYPE.AccountUserDocument,after:TYPE.AccountUserDocument}[]} metadataToUpdate list of updated users
* @param {TYPE.AccountUserDocumentDiff[]} metadataToUpdate list of updated users
* @returns {void}
*/
static _unassignRemovedRoles(metadataToUpdate) {
Expand Down Expand Up @@ -421,10 +433,11 @@ class AccountUser extends MetadataType {
* helper for {@link createOrUpdate} to generate a random initial password for new users
* note: possible minimum length values in SFMC are 6, 8, 10, 15 chars. Therefore we should default here to 15 chars.
*
* @private
* @param {number} [length] length of password; defaults to 15
* @returns {string} random password
*/
static generatePassword(length = 15) {
static _generatePassword(length = 15) {
const alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const special = '!@#$%&';
const numeric = '0123456789';
Expand Down Expand Up @@ -457,7 +470,7 @@ class AccountUser extends MetadataType {
);
return;
}
if (Object.keys(metadata).length === 0) {
if (Object.keys(metadata).length === 1) {
Util.logger.debug(
'Only 1 user found. Skipping documentation, assuming we ran retrieve-by-key.'
);
Expand Down
1 change: 1 addition & 0 deletions types/mcdev.d.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const SDK = require('sfmc-sdk');
* @property {string} LastSuccessfulLogin this.timeSinceDate(user.LastSuccessfulLogin)
* @property {string} CreatedDate user.CreatedDate
* @property {string} ModifiedDate user.ModifiedDate
* @typedef {{before:AccountUserDocument,after:AccountUserDocument}} beforeAfterAccountUserDocument
*/

/**
Expand Down

0 comments on commit 6eddfd7

Please sign in to comment.