Skip to content

Instantly share code, notes, and snippets.

@induprasad
Created May 3, 2018 13:23
Show Gist options
  • Save induprasad/dbca05ae2cbe519711e545f35135c774 to your computer and use it in GitHub Desktop.
Save induprasad/dbca05ae2cbe519711e545f35135c774 to your computer and use it in GitHub Desktop.
Helper of the LCMP_DisplayChart component
({
//Generic Method to perform server calls
callToServer: function(component, method, callback, params) {
component.set("v.showSpinner", true);
var action = component.get(method);
action.setStorable();
if(params) {
action.setParams(params);
}
action.setCallback(this, function(response) {
component.set("v.showSpinner", false);
var state = response.getState();
if (state === "SUCCESS") {
callback.call(this, response.getReturnValue());
} else if(state === "ERROR") {
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
component.set("v.alertMessage", "Error message: " + errors[0].message);
component.set("v.isAlert", true);
}
}
} else {
component.set("v.alertMessage", "ERROR: Unknown Error");
component.set("v.isAlert", true);
}
});
$A.enqueueAction(action);
},
//Method which retrieves the cases and account details
getCases: function(component) {
var self = this;
self.callToServer(
component,
"c.getAllCases",
function(response) {
if(response.length > 0) {
var result = response[0];
if(result.cases !== undefined && result.accounts !== undefined) {
var accounts = result.accounts;
self.countCases(component, result.cases, accounts);
} else {
component.set("v.alertMessage", "No Access");
component.set("v.isAlert", true);
}
} else {
component.set("v.caseList", []);
}
},
{
});
},
countCases: function(component, cases, accounts) {
var self = this;
var openCasesMap = {};
var closedCasesMap = {};
var result = [];
//Map of account and open cases
function openCasesToMap(key, value) {
openCasesMap[key] = openCasesMap[key] || [];
var temp = openCasesMap[key];
temp.push(value)
openCasesMap[key] = temp;
}
//Map of account and closed cases
function closedCasesToMap(key, value) {
closedCasesMap[key] = closedCasesMap[key] || [];
var temp = closedCasesMap[key];
temp.push(value)
closedCasesMap[key] = temp;
}
//Adding values to map based on case status and accountId
for(var i=0;i<cases.length;i=i+1) {
var record = cases[i];
if(record.Status === "New" || record.Status === "Working" || record.Status === "Escalated") {
openCasesToMap(record.AccountId, record);
} else if(record.Status === "Closed") {
closedCasesToMap(record.AccountId, record);
}
}
for(var k=0;k<accounts.length;k=k+1) {
var openCases = 0;
var closedCases = 0;
var rec = {};
var account = accounts[k];
rec.AccountId = account.Id;
rec.AccountName = account.Name;
if(openCasesMap[account.Id] !== undefined) {
openCases = openCasesMap[account.Id].length;
}
if(closedCasesMap[account.Id] !== undefined) {
closedCases = closedCasesMap[account.Id].length;
}
rec.totalOpenCases = openCases;
rec.totalClosedCases = closedCases;
rec.totalCases = openCases + closedCases;
result.push(rec);
}
component.set("v.lstResults", result);
if(result.length > 0) {
if(result[0].totalCases > 0) {
component.set("v.isLoaded", true);
}
}
component.set("v.barData",result);
self.setChartData(component);
},
setChartData : function(component, event) {
var data = component.get("v.barData");
if(data.length > 0) {
component.set("v.isLoaded", "true");
var barData = [];
for(var i=0; i<data.length; i=i+1) {
var barItem = {};
barItem.label = data[i].AccountName;
barItem.OpenCases = data[i].totalOpenCases;
barItem.ClosedCases = data[i].totalClosedCases;
barItem.TotalCases = data[i].totalCases;
barData.push(barItem);
}
/*
* If you don't have any cases in the system, you can hardcode the values as below and see a preview
*
var barItem1 = {};
barItem1.label = 'Test Account';
barItem1.OpenCases = 80;
barItem1.ClosedCases = 20;
barItem1.TotalCases = 100;
barData.push(barItem1);
*/
component.set("v.barData", barData);
} else {
component.set("v.nocasesMsg","true");
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment