Skip to content

Instantly share code, notes, and snippets.

@ckoppelman
Created September 5, 2017 18:08
Show Gist options
  • Save ckoppelman/e7ae89977d7b5a58e7985823872287b0 to your computer and use it in GitHub Desktop.
Save ckoppelman/e7ae89977d7b5a58e7985823872287b0 to your computer and use it in GitHub Desktop.
var lastIdList = [];
var getAllCallData = function (idList, knownData, callback) {
var deferreds = idList.map(function (id) {
var matchedEl = null;
if (knownData) {
matchedEl = knownData.find(function (el) { return el.id === id; });
if (matchedEl) {
return matchedEl;
}
}
var dfd = $.Deferred();
var data;
sforce.console.cti.getCallAttachedData(id, function (r) {
if (r && r.data) {
data = JSON.parse(r.data);
var newCallData = {
id: id,
ani: data.ani,
dnis: data.dnis,
campaignName: data.campaignName,
callType: r.type,
startedAt: new Date()
};
knownData.push(newCallData);
}
dfd.resolve();
}, {getCallType: true});
return dfd;
});
$.when.apply($, deferreds).done(function () {
callback(idList, knownData);
});
};
var checkCallObjectId = function () {
if (getCurrentFormValues().Should_Override_CTI_Fields__c) {
$(document).trigger('pp.removeSpinner');
// no reason to query for Call Objects if the user turns off CTI.
return;
}
var shouldRemoveSpinner = false;
if (isNewReload) {
$(document).trigger('pp.addSpinner');
shouldRemoveSpinner = true;
}
sforce.console.cti.getCallObjectIds(function (res) {
var currentCallObject = getSelectedLog().callObject;
// we get a "fail" message if there are no calls.
var idList = res.success && $.isArray(res.ids) ? res.ids : [];
if (!currentCallObject) {
currentCallObject = [];
}
// i need this object to stay around as is.
currentCallObject = currentCallObject.slice();
var isNew = false,
isSamePolarisSession = false;
isNew = lastIdList.length !== idList.length ||
lastIdList.some(function (el) { return idList.indexOf(el) === -1; }) ||
idList.some(function (el) { return lastIdList.indexOf(el) === -1; });
isSamePolarisSession = idList.some(function (el) {
return lastIdList.indexOf(el) !== -1;
});
if (isNew) {
if (isSamePolarisSession) {
lastIdList.forEach(function (el) {
if (idList.indexOf(el) === -1) {
idList.unshift(el);
}
});
}
lastIdList = idList.slice();
}
var eventObject = {
oldCall: currentCallObject.slice() || [],
hasData: doesCurrentLogHaveNotes(),
isSamePolarisSession: isSamePolarisSession
};
if (idList && idList[0] && (!currentCallObject.length || isNew)) {
// we're not tracking a call, and now there's a call to track!
var processNewLog = function () {
var dealWithAttachedData = function (allCallData, allCallIds) {
var data = allCallIds.map(function (id) {
return allCallData.find(function (el) {
return el.id === id;
});
});
eventObject.newCall = data;
if (!getSelectedLog().callObject || isSamePolarisSession) {
setSelectedLogValue({
CallObjectIds__c: JSON.stringify(data),
callObject: data
});
}
$(document).trigger('pp.changeCallObject', eventObject);
};
getAllCallData(idList, currentCallObject, function (allCallIds, knownData) {
if (wasLogSetManually) {
var callback = function () {
addNewRecentLog();
setLogById(NEW_ID, function () {
dealWithAttachedData(knownData, allCallIds);
}, true);
};
updateData(callback, true);
} else {
dealWithAttachedData(knownData, allCallIds);
}
}, {getCallType: true});
};
if (isNewReload) {
findOldLog(idList, processNewLog);
} else {
processNewLog();
}
} else {
$(document).trigger('pp.removeSpinner');
if (currentCallObject.length && !wasLogSetManually &&
(!res || !res.success || !$.isArray(res.ids))) {
updateData();
eventObject.newCall = [];
$(document).trigger('pp.changeCallObject', eventObject);
}
}
isNewReload = false;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment