Repository navigation
feat(credit): release held orders when credit limit increases #1
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 |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| * resulting Account update lets AccountTriggerHandler derive Credit_Hold__c. | ||
| */ | ||
| public inherited sharing class CreditCheckService { | ||
| private static final String ORDER_HANDLER = 'Order'; | ||
|
|
||
| private final CreditPolicy policy; | ||
|
|
||
| public CreditCheckService() { | ||
|
|
@@ -50,6 +52,48 @@ public inherited sharing class CreditCheckService { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Releases held orders that now fit within the account's credit limit, oldest first, and | ||
| * hands them to fulfillment. | ||
| * | ||
| * Called from Account before-update with the in-flight Trigger.new records: the released | ||
| * value is added to Open_Order_Value__c on those records directly, so no Account DML is | ||
| * needed. The Order handler is bypassed while statuses are updated because its after-update | ||
| * recalculation would try to update these same accounts mid-trigger | ||
| * (SELF_REFERENCE_FROM_TRIGGER). | ||
| */ | ||
| public void releaseHeldOrders(List<Account> customers) { | ||
| if (customers == null || customers.isEmpty()) { | ||
| return; | ||
| } | ||
| Map<Id, Account> customersById = new Map<Id, Account>(customers); | ||
| // Releasing holds is automation triggered by a limit change, not a user query. | ||
| List<Order> heldOrders = new OrdersSelector(System.AccessLevel.SYSTEM_MODE) | ||
| .selectHeldByAccountIds(customersById.keySet()); | ||
|
|
||
| List<Order> released = new List<Order>(); | ||
| for (Order held : heldOrders) { | ||
| Account customer = customersById.get(held.AccountId); | ||
| Decimal projected = | ||
| CreditPolicy.nullToZero(customer.Open_Order_Value__c) + CreditPolicy.nullToZero(held.TotalAmount); | ||
| if (!policy.isOverLimit(projected, customer.Credit_Limit__c)) { | ||
| customer.Open_Order_Value__c = projected; | ||
| released.add(new Order(Id = held.Id, Credit_Check_Status__c = OrderConstants.CREDIT_APPROVED)); | ||
| } | ||
| } | ||
| if (released.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| TriggerHandler.bypass(ORDER_HANDLER); | ||
| try { | ||
| update as system released; | ||
| } finally { | ||
| TriggerHandler.clearBypass(ORDER_HANDLER); | ||
| } | ||
| OrderFulfillmentQueueable.enqueue(new Map<Id, Order>(released).keySet()); | ||
|
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.
|
||
| } | ||
|
|
||
| /** | ||
| * Recomputes Open_Order_Value__c for the given accounts and updates only those that changed. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| @IsTest | ||
| private class CreditHoldReleaseTest { | ||
| private static final Decimal ORDER_VALUE = 1000; | ||
| private static final Decimal LOW_LIMIT = 500; | ||
|
|
||
| private static void stubCollaborators(FakeFulfillmentClient fake) { | ||
| BunnyforceSettings.overrides.put(BunnyforceSettings.CREDIT_HOLD_GRACE_PERCENT, '0'); | ||
| Application.setMock(IFulfillmentClient.class, fake); | ||
| } | ||
|
|
||
| @IsTest | ||
| static void raisingLimitReleasesOldestOrdersThatFit() { | ||
| stubCollaborators(new FakeFulfillmentClient()); | ||
| Account customer = TestDataFactory.createAccounts(1, 'Bronze', LOW_LIMIT, true)[0]; | ||
| TestDataFactory.createHeldOrders(new List<Account>{ customer }, 3, ORDER_VALUE); | ||
|
|
||
| update new Account(Id = customer.Id, Credit_Limit__c = 2000); | ||
|
|
||
| Integer approved = [ | ||
| SELECT COUNT() | ||
| FROM Order | ||
| WHERE AccountId = :customer.Id AND Credit_Check_Status__c = :OrderConstants.CREDIT_APPROVED | ||
| ]; | ||
| Assert.areEqual(2, approved, 'Two 1000 orders fit in a 2000 limit; the third stays on hold'); | ||
|
Comment on lines
+19
to
+24
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.
|
||
| } | ||
|
|
||
| @IsTest | ||
| static void releasedValueCountsTowardOpenOrderValue() { | ||
| stubCollaborators(new FakeFulfillmentClient()); | ||
| Account customer = TestDataFactory.createAccounts(1, 'Bronze', LOW_LIMIT, true)[0]; | ||
| TestDataFactory.createHeldOrders(new List<Account>{ customer }, 3, ORDER_VALUE); | ||
|
|
||
| update new Account(Id = customer.Id, Credit_Limit__c = 2000); | ||
|
|
||
| Account saved = [SELECT Open_Order_Value__c, Credit_Hold__c FROM Account WHERE Id = :customer.Id]; | ||
| Assert.areEqual(Decimal.valueOf(2000), saved.Open_Order_Value__c, 'Released orders consume credit'); | ||
| Assert.isFalse(saved.Credit_Hold__c, 'Account is exactly at its limit, not over it'); | ||
| } | ||
|
|
||
| @IsTest | ||
| static void releasedOrdersAreSubmittedForFulfillment() { | ||
| FakeFulfillmentClient fake = new FakeFulfillmentClient(); | ||
| stubCollaborators(fake); | ||
| Account customer = TestDataFactory.createAccounts(1, 'Bronze', LOW_LIMIT, true)[0]; | ||
| TestDataFactory.createHeldOrders(new List<Account>{ customer }, 1, ORDER_VALUE); | ||
|
|
||
| Test.startTest(); | ||
| update new Account(Id = customer.Id, Credit_Limit__c = 5000); | ||
| Test.stopTest(); | ||
|
|
||
| Assert.areEqual(1, fake.received.size(), 'Released order should be sent to the 3PL'); | ||
| } | ||
|
|
||
| @IsTest | ||
| static void loweringLimitReleasesNothing() { | ||
| stubCollaborators(new FakeFulfillmentClient()); | ||
| Account customer = TestDataFactory.createAccounts(1, 'Bronze', LOW_LIMIT, true)[0]; | ||
| TestDataFactory.createHeldOrders(new List<Account>{ customer }, 1, ORDER_VALUE); | ||
|
|
||
| update new Account(Id = customer.Id, Credit_Limit__c = 100); | ||
|
|
||
| Assert.areEqual( | ||
| OrderConstants.CREDIT_ON_HOLD, | ||
| [SELECT Credit_Check_Status__c FROM Order WHERE AccountId = :customer.Id].Credit_Check_Status__c, | ||
| 'A lower limit must not release held orders' | ||
| ); | ||
| } | ||
|
Comment on lines
+54
to
+67
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 a test for removing the credit limit. The handler has an explicit branch for Add a test that clears the limit and asserts that all held orders are released. 💚 Proposed test
|
||
|
|
||
| @IsTest | ||
| static void bulkLimitIncreaseReleasesAllAccounts() { | ||
| stubCollaborators(new FakeFulfillmentClient()); | ||
| List<Account> accounts = TestDataFactory.createAccounts(200, 'Bronze', LOW_LIMIT, true); | ||
| TestDataFactory.createHeldOrders(accounts, 1, ORDER_VALUE); | ||
|
|
||
| for (Account customer : accounts) { | ||
| customer.Credit_Limit__c = 5000; | ||
| } | ||
| update accounts; | ||
|
|
||
| Assert.areEqual( | ||
| 200, | ||
| [SELECT COUNT() FROM Order WHERE Credit_Check_Status__c = :OrderConstants.CREDIT_APPROVED], | ||
| 'Every account should have its held order released' | ||
| ); | ||
| } | ||
|
Comment on lines
+70
to
+85
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.
|
||
| } | ||
| 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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -68,6 +68,28 @@ public inherited sharing class OrdersSelector { | |||||||||||||||
| ); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Activated orders on credit hold for the given accounts, oldest first so holds are | ||||||||||||||||
| * released in the order the customer placed them. | ||||||||||||||||
| */ | ||||||||||||||||
| public List<Order> selectHeldByAccountIds(Set<Id> accountIds) { | ||||||||||||||||
| if (accountIds == null || accountIds.isEmpty()) { | ||||||||||||||||
| return new List<Order>(); | ||||||||||||||||
| } | ||||||||||||||||
| return (List<Order>) Database.queryWithBinds( | ||||||||||||||||
| 'SELECT ' + | ||||||||||||||||
| BASE_FIELDS + | ||||||||||||||||
| ' FROM Order WHERE AccountId IN :accountIds AND StatusCode = :activated' + | ||||||||||||||||
| ' AND Credit_Check_Status__c = :onHold ORDER BY EffectiveDate ASC, OrderNumber ASC', | ||||||||||||||||
|
Comment on lines
+80
to
+83
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. Narrow the field list in The query uses Select only the fields the caller needs. ♻️ Proposed fix
The null/empty guard, the bind variables and the constructor-supplied access mode comply with BF-SEC-3. Committable suggestion
|
||||||||||||||||
| new Map<String, Object>{ | ||||||||||||||||
| 'accountIds' => accountIds, | ||||||||||||||||
| 'activated' => OrderConstants.STATUS_ACTIVATED, | ||||||||||||||||
| 'onHold' => OrderConstants.CREDIT_ON_HOLD | ||||||||||||||||
| }, | ||||||||||||||||
| mode | ||||||||||||||||
| ); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Sum of credit-approved, activated orders that have not been delivered, keyed by AccountId. | ||||||||||||||||
| * Accounts without open orders are absent from the map. | ||||||||||||||||
|
|
||||||||||||||||
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.
Script executed:
Repository: mtyates-coderabbit/bunnyforce
Length of output: 12292
Script executed:
Repository: mtyates-coderabbit/bunnyforce
Length of output: 26718
Script executed:
Repository: mtyates-coderabbit/bunnyforce
Length of output: 13819
Document the
SYSTEM_MODEDML (BF-SEC-2).The automation comment above the query does not cover
update as system released. Explain why this Order update must bypass the initiating user's Order permissions.🛡️ Suggested fix
Committable suggestion