Created
October 8, 2021 11:02
-
-
Save TeamAssetSonar/e7e61003a21c6515b475f69c91b134ca to your computer and use it in GitHub Desktop.
request_page.hbs code
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
<!-- Insert this at the end of request_page.hbs file --> | |
<input type="hidden" id="request-id-hidden-field" value="{{request.id}}"> | |
<script> | |
function ezoIntegrationInit(){ | |
var ezoFieldId = 'ASSET_FIELD_ID'; // enter id of field created for linked assets | |
var subdomain = 'SUBDOMAIN.assetsonar.com'; // enter SUBDOMAIN | |
var requestId = $('#request-id-hidden-field').val(); | |
var requestUrl = '/api/v2/requests/' + requestId; | |
$.getJSON(requestUrl).done(function(data){ | |
var ezoFieldData = data.request.custom_fields.find(function(e) { return e.id == ezoFieldId }); | |
if(!ezoFieldData || !ezoFieldData.value) { return true; } | |
$.getJSON("/api/v2/users/me").done(function(userData){ | |
var userId = userData.user.id; | |
if (userId) { | |
assetIds = getAssetIds(ezoFieldData.value); | |
if(!assetIds || assetIds.length == 0) { return true; } | |
linkAssets(assetIds); | |
fetchAndShowLinkedAssets(assetIds); | |
} | |
}); | |
}); | |
function processAssetId(str){ | |
if(!str) { return null; } | |
str = str.trim(); | |
var matchData = str.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i); | |
if(!matchData) { return null; } | |
return str; | |
} | |
function getAssetIds(fieldValue){ | |
return fieldValue.split(',').map(processAssetId).filter(function(id) { return id != null; }); | |
} | |
function addEZOContainer(){ | |
$('dl.request-details').last().after('<dl class="request-details" id="ezo-assets-container"><dt>AssetSonar Assets</dt><dd><ul></ul></dd></dl>'); | |
} | |
function linkAssets(assetIds){ | |
var url = "/api/sunshine/objects/records/zen:ticket:" + requestId + "/relationships/ez_web_as_ticket_has_many_assets"; | |
$.getJSON(url).done(function(response){ | |
if(response.data.length === assetIds.length) { return true; } | |
$.ajax({ | |
url: 'https://' + subdomain + '/webhooks/zendesk/sync_tickets_to_assets_relation.json', | |
type: 'POST', | |
data: {"ticket": { "ticket_id": requestId, "assets_field_id": ezoFieldId }} | |
}); | |
}); | |
} | |
function fetchAndShowLinkedAssets(assetIds){ | |
if(!assetIds || assetIds.length == 0) { return true; } | |
addEZOContainer(); | |
assetIds.forEach(function(id){ | |
var url = "/api/sunshine/objects/records/" + id; | |
$.getJSON(url).done(function(response){ | |
showLinkedAsset(response.data.attributes.name); | |
}); | |
}); | |
} | |
function showLinkedAsset(assetName){ | |
var assetUrl = getAssetUrl(assetName); | |
var ezoContainerBody = $('#ezo-assets-container dd ul'); | |
if (assetUrl) { | |
ezoContainerBody.append("<li><a target='_blank' href='" + assetUrl + "'>" + assetName + "</a></li>"); | |
} else { | |
ezoContainerBody.append("<li>" + assetName + "</li>"); | |
} | |
} | |
function getAssetUrl(assetName){ | |
if(!assetName) { return null; } | |
assetName = assetName.trim(); | |
var matchData = assetName.match(/^(Asset|Asset Stock) # (\d+) /); | |
if (matchData){ | |
var type = matchData[1]; | |
var id = matchData[2]; | |
return 'https://' + subdomain + getAssetPath(type, id); | |
} | |
return null; | |
} | |
function getAssetPath(type, id){ | |
if (type === 'Asset') { | |
return '/assets/' + id; | |
} else { | |
return '/stock_assets/' + id; | |
} | |
} | |
}; | |
function loadJqueryAndInitIntegration(){ | |
if (typeof jQuery == 'undefined') { | |
getScript('https://code.jquery.com/jquery-3.6.0.min.js', function() { | |
ezoIntegrationInit(); | |
}); | |
} else { // jQuery was already loaded | |
ezoIntegrationInit(); | |
}; | |
} | |
document.addEventListener('DOMContentLoaded', loadJqueryAndInitIntegration); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment