From 6eddfd7a41d75f7377b0b554ce3e926b4c6385e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Wed, 22 Feb 2023 17:06:31 +0100 Subject: [PATCH] #139: refactoring --- docs/dist/documentation.md | 20 +++----------------- lib/metadataTypes/AccountUser.js | 23 ++++++++++++++++++----- types/mcdev.d.js | 1 + 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/docs/dist/documentation.md b/docs/dist/documentation.md index a6d8df066..fe3ff3136 100644 --- a/docs/dist/documentation.md +++ b/docs/dist/documentation.md @@ -219,7 +219,7 @@ Provides default functionality that can be overwritten by child metadata type cl
DataExtensionMap : object
-
AccountUserDocument : object
+
beforeAfterAccountUserDocument : object
AutomationActivity : object
@@ -744,7 +744,6 @@ MessageSendActivity MetadataType * [.preDeployTasks(metadata)](#AccountUser.preDeployTasks) ⇒ TYPE.MetadataTypeItem * [.createOrUpdate(metadata, metadataKey, hasError, metadataToUpdate, metadataToCreate)](#AccountUser.createOrUpdate) ⇒ void * [.retrieveChangelog()](#AccountUser.retrieveChangelog) ⇒ Promise.<TYPE.MetadataTypeMapObj> - * [.generatePassword([length])](#AccountUser.generatePassword) ⇒ string * [.document([metadata])](#AccountUser.document) ⇒ Promise.<void> * [.postRetrieveTasks(metadata)](#AccountUser.postRetrieveTasks) ⇒ TYPE.MetadataTypeItem @@ -828,19 +827,6 @@ Retrieves SOAP based metadata of metadata type into local filesystem. executes c **Kind**: static method of [AccountUser](#AccountUser) **Returns**: Promise.<TYPE.MetadataTypeMapObj> - Promise of metadata - - -### AccountUser.generatePassword([length]) ⇒ string -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 [AccountUser](#AccountUser) -**Returns**: string - random password - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| [length] | number | 15 | length of password; defaults to 15 | - ### AccountUser.document([metadata]) ⇒ Promise.<void> @@ -7607,9 +7593,9 @@ key=customer key | [Template] | object | - | | [Template.CustomerKey] | string | key of optionally associated DE teplate | - + -## AccountUserDocument : object +## beforeAfterAccountUserDocument : object **Kind**: global typedef **Properties** diff --git a/lib/metadataTypes/AccountUser.js b/lib/metadataTypes/AccountUser.js index c347707ee..317c7f1f7 100644 --- a/lib/metadataTypes/AccountUser.js +++ b/lib/metadataTypes/AccountUser.js @@ -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) { @@ -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'; @@ -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.' ); diff --git a/types/mcdev.d.js b/types/mcdev.d.js index 2bda803a2..7015c770e 100644 --- a/types/mcdev.d.js +++ b/types/mcdev.d.js @@ -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 */ /**