Repository navigation
Account health score #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| public class AccountHealthCalculator { | ||
| public static Decimal score(Account acc, Integer failedOrders) { | ||
| Decimal s = 100; | ||
| if (acc.Credit_Limit__c != null && acc.Credit_Limit__c > 0) { | ||
| Decimal util = acc.Open_Order_Value__c / acc.Credit_Limit__c; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalize the open order value before division.
BF-INT-4 requires one record failure not to fail the whole batch.
|
||
| if (util > 0.9) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move score thresholds and weights into settings. The utilization thresholds and score adjustments are hardcoded. Store them in As per path instructions: “Configuration belongs in Bunnyforce_Setting__mdt via BunnyforceSettings.” This is BF-ARCH-7.
|
||
| s = s - 40; | ||
| } else if (util > 0.75) { | ||
| s = s - 25; | ||
| } else if (util > 0.5) { | ||
| s = s - 10; | ||
| } | ||
| } | ||
| if (acc.Credit_Hold__c) { | ||
| s = s - 30; | ||
| } | ||
| s = s - (failedOrders * 5); | ||
| s = acc.Customer_Tier__c == 'Platinum' ? s + 10 : acc.Customer_Tier__c == 'Gold' ? s + 5 : s; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use tier constants and remove the nested ternary. Use As per path instructions: “Flag … magic picklist strings” and “nested ternaries.” These are BF-STD-7 and BF-STD-6.
|
||
| if (s < 0) { | ||
| s = 0; | ||
| } | ||
| if (s > 100) { | ||
| s = 100; | ||
| } | ||
| return s; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>65.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| public class AccountHealthController { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Declare The class has no explicit sharing declaration, and the supplied PMD run fails As per path instructions, BF-SEC-1 requires user-facing controllers to declare
|
||
| @AuraEnabled | ||
| public static List<Account> searchAccounts(String searchTerm, String sortField) { | ||
| String query = | ||
| 'SELECT Id, Name, Health_Score__c, Customer_Tier__c, Credit_Limit__c, Open_Order_Value__c ' + | ||
| 'FROM Account WHERE Name LIKE \'%' + | ||
| searchTerm + | ||
| '%\' ORDER BY ' + | ||
| sortField; | ||
|
Comment on lines
+6
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Injection Reachability: External Bind the search term and allowlist the sort field. An Aura caller controls both values concatenated into this SOQL statement. A search term such as As per path instructions, BF-SEC-1..7 require binds for dynamic SOQL and prohibit concatenating user input into queries.
|
||
| return Database.query(query); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Authorization Bypass Reachability: External Run both client-facing queries in USER_MODE. These queries select Account and Order fields without enforcing the caller’s CRUD/FLS permissions. At the class’s API version 65.0, database operations default to system mode; Aura’s implicit sharing protects records, not field access. A sales rep with method access but restricted field access can therefore receive those fields through search results or portfolio maps. Use As per path instructions, BF-SEC-2 requires queries in user-facing controllers to run in USER_MODE. Also applies to: 16-17
|
||
| } | ||
|
|
||
| @AuraEnabled(cacheable=true) | ||
| public static List<Map<String, Object>> getPortfolioHealth() { | ||
| List<Map<String, Object>> results = new List<Map<String, Object>>(); | ||
| List<Account> accounts = [SELECT Id, Name, Health_Score__c, Customer_Tier__c FROM Account]; | ||
| List<Order> orders = [SELECT Id, AccountId, TotalAmount, Fulfillment_Status__c FROM Order]; | ||
|
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scope and aggregate the portfolio queries.
As per path instructions, BF-PERF-1..6 prioritize governor limits, and the standards require SOQL in Also applies to: 21-21
|
||
| for (Account acc : accounts) { | ||
| Decimal openTotal = 0; | ||
| Integer failed = 0; | ||
| for (Order o : orders) { | ||
| if (o.AccountId == acc.Id) { | ||
| openTotal += o.TotalAmount; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the maintained open-order value for This line adds every Order’s
|
||
| if (o.Fulfillment_Status__c == 'Failed') { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the existing failed-status constant. Replace As per path instructions, BF-APEX-2 requires status strings to come from
|
||
| failed++; | ||
| } | ||
| } | ||
| } | ||
| results.add( | ||
| new Map<String, Object>{ | ||
| 'id' => acc.Id, | ||
| 'name' => acc.Name, | ||
| 'score' => acc.Health_Score__c, | ||
| 'tier' => acc.Customer_Tier__c, | ||
| 'openTotal' => openTotal, | ||
| 'failedOrders' => failed | ||
| } | ||
| ); | ||
| } | ||
| return results; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>65.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| @IsTest(SeeAllData=true) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use isolated factory data.
As per path instructions: “
|
||
| private class AccountHealthControllerTest { | ||
| @IsTest | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add bulk and permission-path tests. This test class has no 200-record trigger case and no As per path instructions: “Trigger and service changes need a bulk test with 200 records” and “User-facing code … needs System.runAs tests with and without the relevant permission set.” These are BF-TEST-5 and BF-TEST-6.
|
||
| static void testSearch() { | ||
| List<Account> results = AccountHealthController.searchAccounts('a', 'Name'); | ||
| Assert.isTrue(true); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert the returned business results.
As per path instructions: “Every Also applies to: 11-11, 22-22
|
||
| } | ||
|
|
||
| @IsTest | ||
| static void testPortfolio() { | ||
| AccountHealthController.getPortfolioHealth(); | ||
| } | ||
|
|
||
| @IsTest | ||
| static void testScore() { | ||
| Account acc = [ | ||
| SELECT Id, Credit_Limit__c, Open_Order_Value__c, Credit_Hold__c, Customer_Tier__c | ||
| FROM Account | ||
| LIMIT 1 | ||
| ]; | ||
| Decimal s = AccountHealthCalculator.score(acc, 0); | ||
| System.assert(s != null); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>65.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <CustomField xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <fullName>Health_Score__c</fullName> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Grant sales reps access to the new field.
As per path instructions: “New custom fields need a description, and the right permission set … needs matching fieldPermissions in the same PR.” This is BF-SEC-6.
|
||
| <externalId>false</externalId> | ||
| <label>Health Score</label> | ||
| <precision>5</precision> | ||
| <required>false</required> | ||
| <scale>0</scale> | ||
| <trackHistory>false</trackHistory> | ||
| <type>Number</type> | ||
| </CustomField> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| trigger AccountHealthTrigger on Account(before update) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this logic into the existing Account handler.
As per path instructions: “Exactly one trigger per SObject.” This is BF-ARCH-1 and BF-ARCH-4.
|
||
| for (Account acc : Trigger.new) { | ||
| Account oldAcc = Trigger.oldMap.get(acc.Id); | ||
| if (acc.Open_Order_Value__c != oldAcc.Open_Order_Value__c || acc.Credit_Limit__c != oldAcc.Credit_Limit__c) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recalculate when the customer tier changes.
BF-ARCH-5 requires the change check to cover relevant fields.
|
||
| Integer failedOrders = [ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bulkify the failed-order query. A 200-record update can execute 200 queries. Synchronous Apex permits 100 SOQL queries per transaction, so the update fails once the limit is exceeded. Collect Account IDs and query grouped failed-order counts once through an As per path instructions: “Flag any SOQL … that executes inside a loop.” This is BF-PERF-1 and BF-ARCH-3.
|
||
| SELECT COUNT() | ||
| FROM Order | ||
| WHERE AccountId = :acc.Id AND Fulfillment_Status__c = 'Failed' | ||
| ]; | ||
| acc.Health_Score__c = AccountHealthCalculator.score(acc, failedOrders); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>65.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexTrigger> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declare sharing explicitly.
Add
inherited sharingto this class. BF-SEC-1 requires an explicit sharing declaration for every class.As per path instructions: “Every top-level class must declare
with sharing,inherited sharingor a justifiedwithout sharing.”Source: Path instructions