Last active
February 24, 2020 20:25
-
-
Save ryandhubbard/a7e5c554842de29089367228b3ef3f9b to your computer and use it in GitHub Desktop.
A revision to Bhubbards gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ConnectWiseRest = require('connectwise-rest'); | |
const cw = new ConnectWiseRest({ | |
companyId: process.env.CONNECTWISE_COMPANY_ID, | |
companyUrl: process.env.CONNETWISE_COMPANY_URL, | |
publicKey: process.env.CONNECTWISE_PUBLIC_KEY, | |
privateKey: process.env.CONNECTWISE_PRIVATE_KEY, | |
clientId: process.env.CONNETWISE_CLIENT_ID, | |
entryPoint: 'v4_6_release', // optional, defaults to 'v4_6_release' | |
timeout: 20000, // optional, request connection timeout in ms, defaults to 20000 | |
retry: false, // optional, defaults to false | |
retryOptions: { // optional, override retry behavior, defaults as shown | |
retries: 4, // maximum number of retries | |
minTimeout: 50, // number of ms to wait between retries | |
maxTimeout: 20000, // maximum number of ms between retries | |
randomize: true, // randomize timeouts | |
}, | |
debug: false, // optional, enable debug logging | |
logger: (level, text, meta) => {} // optional, pass in logging function | |
}); | |
exports.auditContact = (req, res) => { | |
var id; | |
// Array of updates made to the contact | |
var revisions = [] | |
if (req.query && req.query.id) { | |
id = req.query.id; | |
} else { | |
return res.status(420).send('Missing required \'id\' parameter.'); | |
} | |
cw.CompanyAPI.Contacts.getContactById(req.query.id) | |
.then((contact) => { | |
/* - Update PSL to 1 if found empty (Fix if Needed) - replaces `update_cw_portal_security_level` */ | |
if (contact.portalSecurityLevel === "" || contact.portalSecurityLevel == undefined) { | |
contact.portalSecurityLevel = 1 | |
revisions.push({ | |
'op': 'update', | |
'path': "portalSecurityLevel", | |
'value': { | |
'id': '1', | |
'name': "contact.portalSecurityLevel" | |
} | |
}) | |
} | |
/* - Empty Email Check (Fix if Needed) - replaces `maybe_fix_missing_email` */ | |
var defaultEmail = { | |
value: contact.id + '@accessa.xyz', | |
defaultFlag: true, | |
domain: '@accessa.xyz', | |
communicationType: 'Email' | |
} | |
// If the communications items array does not include an email create a default email for the customer | |
if (contact.communicationItems.map(a => a.communicationType).includes('Email') == false) { | |
// Add the default email to the customers communicationItems | |
contact.communicationItems.push(defaultEmail) | |
revisions.push({ | |
'op': 'add', | |
'path':"communicationItems", | |
'value': { | |
'id': defaultEmail, | |
'name': "contact.communicationItems" | |
} | |
}) | |
} else { | |
/* - Check Emails Formatting (Fix if Needed) - replaces `update_cw_formatted_email` */ | |
for (var i = 0; i < contact.communicationItems.length; i++) { | |
// Email is invalid so overwrite with default customer email | |
if (is_email(contact.communicationItems[i].value) == false && contact.communicationItems[i].communicationType == "Email") { | |
contact.communicationItems[i] = defaultEmail | |
revisions.push({ | |
'op': 'update', | |
'path': "communicationItems", | |
'value': { | |
'id': defaultEmail, | |
'name': "contact.communicationItems" | |
} | |
}) | |
} | |
} | |
} | |
/* - Check Status (Fix if Needed) - replaces `maybe_update_user_status` */ | |
// If not a bot group | |
if (contact.portalSecurityLevel !== 2) { | |
// Force Portal Login Flag. | |
if (contact.disablePortalLoginFlag != false) { | |
contact.disablePortalLoginFlag = false | |
revisions.push({ | |
'op': 'update', | |
'path': "disablePortalLoginFlag", | |
'value': { | |
'id': false, | |
'name': "contact.disablePortalLoginFlag" | |
} | |
}) | |
} | |
if (contact.inactiveFlag == true) { | |
if (contact.relationship.name != 'Inactive') { | |
contact.relationship.name = 'Inactive' | |
revisions.push({ | |
'op': 'update', | |
'path': "relationship", | |
'value': { | |
'id': 'Inactive', | |
'name': "contact.relationship.name" | |
} | |
}) | |
} | |
} else { | |
if (contact.relationship.name != 'Active') { | |
contact.relationship.name = 'Active' | |
revisions.push({ | |
'op': 'update', | |
'path': "relationship", | |
'value': { | |
'id': 'Active', | |
'name': "contact.relationship.name" | |
} | |
}) | |
} | |
} | |
} else { | |
// Force correct status for bots | |
if (contact.disablePortalLoginFlag != true) { | |
contact.disablePortalLoginFlag = true | |
revisions.push({ | |
'op': 'update', | |
'path': "disablePortalLoginFlag", | |
'value': { | |
'id': true, | |
'name': "contact.disablePortalLoginFlag" | |
} | |
}) | |
} | |
if (contact.inactiveFlag == true) { | |
if (contact.relationship.name != 'Inactive - Bot/Group') { | |
contact.relationship.name = 'Inactive - Bot/Group' | |
revisions.push({ | |
'op': 'update', | |
'path':"relationship", | |
'value': { | |
'id': 'Inactive - Bot/Group', | |
'name': "contact.relationship.name" | |
} | |
}) | |
} | |
} else { | |
if (contact.relationship.name != 'Active - Bot/Group') { | |
contact.relationship.name = 'Active - Bot/Group' | |
revisions.push({ | |
'op': 'update', | |
'path':"relationship", | |
'value': { | |
'id': 'Active - Bot/Group', | |
'name': "contact.relationship.name" | |
} | |
}) | |
} | |
} | |
} | |
/* - Set Contact Type based on the PSL to make sure they match */ | |
switch (contact.portalSecurityLevel) { | |
case 1: | |
if (contact.type.id != 15){ | |
contact.type.id = 15 | |
revisions.push({ | |
'op': 'update', | |
'path':"type", | |
'value': { | |
'id': '15', | |
'name': "contact.type.id" | |
} | |
}) | |
} | |
if (contact.type.name != "Tech"){ | |
contact.type.name = "Tech"; | |
revisions.push({ | |
'op': 'update', | |
'path': "type", | |
'value': { | |
'id': 'Tech', | |
'name': "contact.type.name" | |
} | |
}) | |
} | |
break; | |
case 2: | |
if (contact.type.id != 17){ | |
contact.type.id = 17 | |
revisions.push({ | |
'op': 'update', | |
'path':"type", | |
'value': { | |
'id': '17', | |
'name': "contact.type.id" | |
} | |
}) | |
} | |
if (contact.type.name != "Bot/Group"){ | |
contact.type.name = "Bot/Group"; | |
revisions.push({ | |
'op': 'update', | |
'path': "type", | |
'value': { | |
'id': 'Bot/Group', | |
'name': "contact.type.name" | |
} | |
}) | |
} | |
break; | |
case 3: | |
if (contact.type.id != 23){ | |
contact.type.id = 23 | |
revisions.push({ | |
'op': 'update', | |
'path':"type", | |
'value': { | |
'id': '23', | |
'name': "contact.type.id" | |
} | |
}) | |
} | |
if (contact.type.name != "AN - Logistics"){ | |
contact.type.name = "AN - Logistics"; | |
revisions.push({ | |
'op': 'update', | |
'path': "type", | |
'value': { | |
'id': 'AN - Logistics', | |
'name': "contact.type.name" | |
} | |
}) | |
} | |
break; | |
case 4: | |
if (contact.type.id != 19){ | |
contact.type.id = 19 | |
revisions.push({ | |
'op': 'update', | |
'path':"type", | |
'value': { | |
'id': '19', | |
'name': "contact.type.id" | |
} | |
}) | |
} | |
if (contact.type.name != "Admin"){ | |
contact.type.name = "Admin"; | |
revisions.push({ | |
'op': 'update', | |
'path': "type", | |
'value': { | |
'id': 'Admin', | |
'name': "contact.type.name" | |
} | |
}) | |
} | |
break; | |
case 5: | |
if (contact.type.id != 24){ | |
contact.type.id = 24 | |
revisions.push({ | |
'op': 'update', | |
'path':"type", | |
'value': { | |
'id': '24', | |
'name': "contact.type.id" | |
} | |
}) | |
} | |
if (contact.type.name != "AN - Engineering"){ | |
contact.type.name = "AN - Engineering"; | |
revisions.push({ | |
'op': 'update', | |
'path': "type", | |
'value': { | |
'id': 'AN - Engineering', | |
'name': "contact.type.name" | |
} | |
}) | |
} | |
break; | |
case 6: | |
if (contact.type.id != 10){ | |
contact.type.id = 10 | |
revisions.push({ | |
'op': 'update', | |
'path':"type", | |
'value': { | |
'id': '10', | |
'name': "contact.type.id" | |
} | |
}) | |
} | |
if (contact.type.name != "Owner"){ | |
contact.type.name = "Owner"; | |
revisions.push({ | |
'op': 'update', | |
'path': "type", | |
'value': { | |
'id': 'Owner', | |
'name': "contact.type.name" | |
} | |
}) | |
} | |
break; | |
default: | |
// Do nothing | |
} | |
// Print out changes made | |
console.log(revisions) | |
// cw.CompanyAPI.Contacts.updateContact = function (8813, operations); | |
// Return Result of Update Company or no Change Needed Response. | |
res.status(200).send(revisions); | |
}) | |
.catch((error) => { | |
// Failed to get contact. Send error message as return | |
res.status(400).send(error); | |
}); | |
function is_email($email) { | |
// Test for the minimum length the email can be | |
if ($email.length < 6) { | |
return false; | |
} | |
// Test for an @ character after the first position | |
if ($email.indexOf('@') === -1 || $email.indexOf('@') !== $email.lastIndexOf('@')) { | |
/** This filter is documented in wp-includes/formatting.php */ | |
return false; | |
} | |
// Split out the local and domain parts | |
var parts = $email.split('@', 2); | |
var $local = parts[0], | |
$domain = parts[1]; | |
// LOCAL PART | |
// Test for invalid characters | |
if (!/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/.test($local)) { | |
/** This filter is documented in wp-includes/formatting.php */ | |
return false; | |
} | |
// DOMAIN PART | |
// Test for sequences of periods | |
if (/\.{2,}/.test($domain)) { | |
/** This filter is documented in wp-includes/formatting.php */ | |
return false; | |
} | |
// Test for leading and trailing periods and whitespace | |
if (str_trim($domain, " \t\n\r\0\x0B.") !== $domain) { | |
/** This filter is documented in wp-includes/formatting.php */ | |
return false; | |
} | |
// Split the domain into subs | |
var $subs = $domain.split('.'); | |
// Assume the domain will have at least two subs | |
if (2 > $subs.length) { | |
/** This filter is documented in wp-includes/formatting.php */ | |
return false; | |
} | |
// Loop through each sub | |
for (i in $subs) { | |
var $sub = $subs[i]; | |
// Test for leading and trailing hyphens and whitespace | |
if (str_trim($sub, " \t\n\r\0\x0B-") !== $sub) { | |
/** This filter is documented in wp-includes/formatting.php */ | |
return false; | |
} | |
// Test for invalid characters | |
if (!/^[a-z0-9-]+$/i.test($sub)) { | |
/** This filter is documented in wp-includes/formatting.php */ | |
return false; | |
} | |
} | |
// Congratulations your email made it! | |
/** This filter is documented in wp-includes/formatting.php */ | |
return true; | |
function str_trim(str, regex) { | |
var chr = regex.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|\:\!\,\=]/g, "\\$&"); | |
return str.replace(new RegExp('/^[' + chr + ']*/'), '').replace(new RegExp('/[' + chr + ']*$/'), ''); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment