You are on page 1of 4

angular

.module('ff4e89d6-e846-4cbf-90ac-1bb715447a26', ['core.services.WidgetAPI'])
.directive('journeyElements', widgetComponent);

function widgetComponent(WidgetAPI, $http) {


function widgetContainer(scope, element, params) {
// widget API constructor
var api = new WidgetAPI(params);

var INTERACTION_TYPES = {
VOICE: 'voice',
SMS: 'sms',
EMAIL: 'email'
};

var UPDATE_MESSAGE = 'JOURNEY_UPDATED';


var RESOLVED_MESSAGE = 'JOURNEY_RESOLVED';
var REJECTED_MESSAGE = 'JOURNEY_REJECTED';
var MAX_KEYS = 3;

var API_PROTOCOLS = {
HTTP: 'http://',
HTTPS: 'https://'
};

var baseCustomerManagementUrl = '';


var isJourneyEmpty = false;
var workRequestId = '';
scope.saveBtnText = 'Save';
scope.additionalInfo = [];
scope.otherInformationError = '';

setCustomerManagementFQDN();

/* Updates an existing Journey Element via CustomerManagement API


* Notifies CJV widget of Journey update */
scope.saveJourneyElement = function (customerNote) {
//1. Update save button text
scope.saveBtnText = 'Saving....';

//2. Create journey element object from key + value


var journeyElementObject = {
elementId: workRequestId,
agentNote: customerNote,
type: INTERACTION_TYPES.VOICE
};

//2.1 Check if touchpoint needs to be a create or update


var httpMethodType = isJourneyEmpty ? 'POST' : 'PUT';
var apiRoute = isJourneyEmpty ? '/journeys/element' : '/journeys/element/' +
workRequestId;

//3.1 Send the data via Customer Management API


//to Customer Journey
var requestConfig = {
method: httpMethodType,
url: baseCustomerManagementUrl + apiRoute,
data: journeyElementObject,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};

$http(requestConfig)
.then(function successCallback(response) {
isJourneyEmpty = false
scope.saveBtnText = 'Save';
api.sendMessage(UPDATE_MESSAGE);
api.sendNotification('info', 'Widget: ' + ' Customer Journey details
saved');
}, function errorCallback(response) {
scope.saveBtnText = 'Save';
api.sendNotification('warning', 'Widget: ' + ' Customer Journey details
failed to update');
console.error('updateJourneyElement error: ' + JSON.stringify(response,
null, 2));
});
};

/**
* Adds additional key/value pairs to additionalInfo collection
* Prevents adding duplicate keys to additionalInfo collection
*/
scope.addOtherInformation = function (additionalKey, additionalValue) {

if (additionalKey && additionalValue) {


if (scope.additionalInfo.length <= MAX_KEYS) {
if (_.some(scope.additionalInfo, function (item) {
return _.has(item, additionalKey);
})) {
scope.otherInformationError = 'Key already exists in table..';
console.log('addOtherInformation function: key already exists in
collection');
} else {
scope.otherInformationError = '';
//make a copy of additionalKey / additionalValue, so
#otherInformationInputGroup
//values can be cleared without deleting values from the table
scope.additionalKeyCopy = angular.copy(additionalKey);
scope.additionalValueCopy = angular.copy(additionalValue);
var additionalInfoObj = {};
additionalInfoObj[scope.additionalKeyCopy] = scope.additionalValueCopy;
scope.additionalInfo.push(additionalInfoObj);
//clear input values
scope.additionalKey = null;
scope.additionalValue = null;
}
}
}
};

/**
* Remove selected table row
*/
scope.removeTableRow = function (idx) {
scope.additionalInfo.splice(idx, 1);
};
/**
* Checks Configuration settings for a valid
* customerManagementFQDN domain.
*/
function setCustomerManagementFQDN() {
var message = 'Invalid Context Store settings: ';
var configObject = api.getConfiguration();

if (!_.isUndefined(configObject.settings.customerManagementFQDN)) {
var customerManagementFQDN = configObject.settings.customerManagementFQDN;
baseCustomerManagementUrl = API_PROTOCOLS.HTTPS + customerManagementFQDN +
'/services/CustomerManagement/rest/customers';
} else {
console.error(message + ' customerManagementFQDN is not valid. ' + '
customerManagementFQDN: ' + configObject.settings.customerManagementFQDN);
}
}

/**
* This event is triggered when customer and context data is available in
* Chat, SMS, Email and Social work cards. This event can be used to obtain
* customer details and customer history data from Chat, SMS, Email and
* Social interactions.
*/
api.onDataEvent('onContextDataEvent', function (data) {
if (data.customerDetails[0]) {
scope.customerEmail = data.customerDetails[0].data.email;
}
});

/**
* This event is triggered when a new work card (interaction) appears in
* Workspaces. This event is triggered in the background when the work
* card is in the ALERTING state and it is called at least once per incoming
* interaction.
* */
api.onDataEvent('onInteractionEvent', function (data) {
if (data.workRequestId) {
workRequestId = data.workRequestId;
}
});

/**
* This event is triggered when other widgets broadcast a data message by using
the sendMessage() API method.
* This event is used primarily for inter-widget communication.
*/
api.onDataEvent('onMessageEvent',  function (data) {
var message = data;
if (message === REJECTED_MESSAGE) {
isJourneyEmpty = true;
} else if (message === RESOLVED_MESSAGE) {
isJourneyEmpty = false;
}
});

// called automatically when widget is destroyed


element.on('$destroy', function () {
api.unregister();
});
}

return {
scope: {},
replace: true,
template: template,
link: widgetContainer
};
}

You might also like