Skip to content

Commit

Permalink
#984: added email validator
Browse files Browse the repository at this point in the history
  • Loading branch information
phjulia committed Aug 14, 2023
1 parent 83f679d commit ae4cc5d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 26 deletions.
26 changes: 26 additions & 0 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6124,6 +6124,7 @@ CLI entry for SFMC DevTools
* [.isFalse(attrValue)](#Util.isFalse) ⇒ <code>boolean</code>
* [._isValidType(selectedType, [handleOutside])](#Util._isValidType) ⇒ <code>boolean</code>
* [.getTypeAndSubType(selectedType)](#Util.getTypeAndSubType) ⇒ <code>Array.&lt;string&gt;</code>
* [.emailValidator(email)](#Util.emailValidator) ⇒ <code>Boolean</code>
* [.getRetrieveTypeChoices()](#Util.getRetrieveTypeChoices) ⇒ <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code>
* [._createNewLoggerTransport([noLogFile])](#Util._createNewLoggerTransport) ⇒ <code>object</code>
* [.startLogger([restart], [noLogFile])](#Util.startLogger) ⇒ <code>void</code>
Expand Down Expand Up @@ -6277,6 +6278,18 @@ helper that deals with extracting type and subtype
| --- | --- | --- |
| selectedType | <code>string</code> | "type" or "type-subtype" |

<a name="Util.emailValidator"></a>

### Util.emailValidator(email) ⇒ <code>Boolean</code>
helper that validates email address

**Kind**: static method of [<code>Util</code>](#Util)
**Returns**: <code>Boolean</code> - first elem is type, second elem is subType

| Param | Type | Description |
| --- | --- | --- |
| email | <code>string</code> | email to validate |

<a name="Util.getRetrieveTypeChoices"></a>

### Util.getRetrieveTypeChoices() ⇒ <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code>
Expand Down Expand Up @@ -8043,6 +8056,7 @@ Util that contains logger and simple util methods
* [.isFalse(attrValue)](#Util.isFalse) ⇒ <code>boolean</code>
* [._isValidType(selectedType, [handleOutside])](#Util._isValidType) ⇒ <code>boolean</code>
* [.getTypeAndSubType(selectedType)](#Util.getTypeAndSubType) ⇒ <code>Array.&lt;string&gt;</code>
* [.emailValidator(email)](#Util.emailValidator) ⇒ <code>Boolean</code>
* [.getRetrieveTypeChoices()](#Util.getRetrieveTypeChoices) ⇒ <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code>
* [._createNewLoggerTransport([noLogFile])](#Util._createNewLoggerTransport) ⇒ <code>object</code>
* [.startLogger([restart], [noLogFile])](#Util.startLogger) ⇒ <code>void</code>
Expand Down Expand Up @@ -8196,6 +8210,18 @@ helper that deals with extracting type and subtype
| --- | --- | --- |
| selectedType | <code>string</code> | "type" or "type-subtype" |

<a name="Util.emailValidator"></a>

### Util.emailValidator(email) ⇒ <code>Boolean</code>
helper that validates email address

**Kind**: static method of [<code>Util</code>](#Util)
**Returns**: <code>Boolean</code> - first elem is type, second elem is subType

| Param | Type | Description |
| --- | --- | --- |
| email | <code>string</code> | email to validate |

<a name="Util.getRetrieveTypeChoices"></a>

### Util.getRetrieveTypeChoices() ⇒ <code>Array.&lt;TYPE.SupportedMetadataTypes&gt;</code>
Expand Down
2 changes: 0 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,6 @@ class Mcdev {
if (Util.OPTIONS.clear) {
// if no options were provided there is nothing to update
result = await this.#runMethod('clearNotifications', businessUnit, selectedType, keys);
return result;
}
if (
!Util.OPTIONS.completionEmail &&
Expand All @@ -1216,7 +1215,6 @@ class Mcdev {
) {
// if no options were provided there is nothing to update
Util.logger.error(`No email addresses or run notes were provided`);
return null;
} else {
// if both clear and notes/emails options were provided
// clear will execute first, and then updateNotifications overwriting result
Expand Down
64 changes: 40 additions & 24 deletions lib/metadataTypes/Automation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1663,9 +1663,9 @@ class Automation extends MetadataType {
(notification) => notification.notificationType == 'Complete'
))
) {
const email = await Cli.updateNotificationNotes('completionEmail');
if (email) {
completionEmail.push(email);
const emails = await Cli.updateNotificationNotes('completionEmail');
if (emails) {
completionEmail = completionEmail.concat(emails.split(','));
} else {
completionNote = undefined;
shouldUpdateCompletion = false;
Expand All @@ -1683,9 +1683,9 @@ class Automation extends MetadataType {
(notification) => notification.notificationType == 'Error'
))
) {
const email = await Cli.updateNotificationNotes('errorEmail');
if (email) {
errorEmail.push(email);
const emails = await Cli.updateNotificationNotes('errorEmail');
if (emails) {
errorEmail = errorEmail.concat(emails.split(','));
} else {
errorNote = undefined;
shouldUpdateError = false;
Expand All @@ -1710,7 +1710,7 @@ class Automation extends MetadataType {
for (const email of completionEmail) {
if (oldCompletionEmails.includes(email)) {
Util.logger.info(
` ☇ skipping ${completionEmail}- this email address is already in the notifications`
` ☇ skipping ${email}- this email address is already in the notifications`
);
completionEmail.splice(completionEmail.indexOf(email), 1);
}
Expand All @@ -1721,7 +1721,7 @@ class Automation extends MetadataType {
for (const email of errorEmail) {
if (oldErrorEmails.includes(email)) {
Util.logger.info(
` ☇ skipping ${errorEmail}- this email address is already in the notifications`
` ☇ skipping ${email}- this email address is already in the notifications`
);
errorEmail.splice(errorEmail.indexOf(email), 1);
}
Expand All @@ -1742,32 +1742,48 @@ class Automation extends MetadataType {
programId: automationLegacyMapObj.metadata[key].id,
workers: [],
};
// copy existing notifications into payload
if (notificationsResult.workers) {
notificationBody.workers = notificationsResult.workers;
}
if (completionEmail.length > 0 || (completionNote && completionNote.length > 0)) {
// completionEmail is an array even if there is only one email
for (const email of completionEmail) {
// TODO validate email addresses (simple validation)
// if(email.match('\\.+@\\.+.\\'))
notificationBody.workers.push({
programId: automationLegacyMapObj.metadata[key].id,
notificationType: 'Complete',
definition: email,
body: completionNote,
channelType: 'Account',
});
if (Util.emailValidator(email)) {
notificationBody.workers.push({
programId: automationLegacyMapObj.metadata[key].id,
notificationType: 'Complete',
definition: email,
body: completionNote,
channelType: 'Account',
});
} else {
Util.logger.warn(
` ☇ skipping ${email}- this email address is not a valid email address`
);
}
}
}
if (errorEmail.length > 0 || (errorNote && errorNote.length > 0)) {
for (const email of errorEmail) {
notificationBody.workers.push({
programId: automationLegacyMapObj.metadata[key].id,
notificationType: 'Error',
definition: email,
body: errorNote,
channelType: 'Account',
});
if (Util.emailValidator(email)) {
notificationBody.workers.push({
programId: automationLegacyMapObj.metadata[key].id,
notificationType: 'Error',
definition: email,
body: errorNote,
channelType: 'Account',
});
} else {
Util.logger.warn(
` ☇ skipping ${email}- this email address is not a valid email address`
);
}
}
}
if (
notificationBody.workers &&
notificationBody.workers.length > 0 &&
(await this.#updateNotificationInfoREST(
key,
automationLegacyMapObj.metadata[key].id,
Expand Down
10 changes: 10 additions & 0 deletions lib/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ const Util = {
return [];
}
},
/**
* helper that validates email address
*
* @param {string} email email to validate
* @returns {boolean} first elem is type, second elem is subType
*/
emailValidator(email) {
const regex = new RegExp('[a-z0-9]+@[a-z]+.[a-z]{2,3}');
return regex.test(email);
},

/**
* helper for getDefaultProperties()
Expand Down

0 comments on commit ae4cc5d

Please sign in to comment.