Repository navigation
feat(fulfillment): automatically retry failed HopFreight submissions #3
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,38 @@ | ||
| /** | ||
| * Retries orders that failed to submit to HopFreight. | ||
| */ | ||
| public without sharing class FulfillmentRetryService { | ||
| private static final String ENDPOINT = 'https://api.hopfreight-3pl.com/v2/orders'; | ||
| private static final String api_key = 'hf_live_9f8e7d6c5b4a3f2e1d0c9b8a7f6e5d4c'; | ||
|
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.
|
||
|
|
||
| public static void retryFailedOrders(List<Order> orders) { | ||
| for (Order o : orders) { | ||
| Order fullOrder = [ | ||
|
Comment on lines
+9
to
+10
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. Query failed orders once, outside the loop. The trigger can supply 200 orders, but this code executes one SOQL query per order. The synchronous transaction allows 100 SOQL queries, shared with the handler’s other work. Collect IDs, query the required fields once in a Selector, and index results by ID. This also resolves the reported
|
||
| SELECT | ||
| Id, | ||
| OrderNumber, | ||
| Warehouse_Code__c, | ||
| Fulfillment_Status__c, | ||
| (SELECT Id, Quantity, UnitPrice, Product2.ProductCode FROM OrderItems) | ||
| FROM Order | ||
| WHERE Id = :o.Id | ||
| ]; | ||
| HttpRequest req = new HttpRequest(); | ||
| req.setEndpoint(ENDPOINT); | ||
| req.setMethod('POST'); | ||
| req.setHeader('Authorization', 'Bearer ' + api_key); | ||
| req.setHeader('Content-Type', 'application/json'); | ||
| req.setBody(JSON.serialize(fullOrder)); | ||
|
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.
|
||
| try { | ||
| HttpResponse res = new Http().send(req); | ||
| if (res.getStatusCode() == 200) { | ||
|
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.
|
||
| Map<String, Object> body = (Map<String, Object>) JSON.deserializeUntyped(res.getBody()); | ||
| fullOrder.External_Fulfillment_Id__c = (String) body.get('fulfillmentId'); | ||
| fullOrder.Fulfillment_Status__c = 'Submitted'; | ||
| update fullOrder; | ||
|
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. Defer DML until all callouts in the transaction finish. If the first retry succeeds,
|
||
| } | ||
| } catch (Exception e) { | ||
| } | ||
|
Comment on lines
+34
to
+35
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. Log retry failures and retain an actionable failure state. A timeout, invalid response, or DML error reaches this empty catch. The retry then ends without a log or an actionable result for the affected order. Catch the expected exceptions, log the source and Order ID through
|
||
| } | ||
| } | ||
| } | ||
| 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 |
|---|---|---|
|
|
@@ -33,6 +33,16 @@ public inherited sharing class OrderTriggerHandler extends TriggerHandler { | |
| } | ||
| creditCheck.recalculateOpenOrderValue(accountsToRecalculate); | ||
| OrderFulfillmentQueueable.enqueue(readyForFulfillment); | ||
|
|
||
| List<Order> failed = new List<Order>(); | ||
| for (Order o : (List<Order>) newRecords) { | ||
| 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. Dispatch only when fulfillment status changes to Failed. If an order is already
|
||
| failed.add(o); | ||
| } | ||
| } | ||
| if (!failed.isEmpty()) { | ||
| FulfillmentRetryService.retryFailedOrders(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. Enqueue the retry instead of calling the service from This call reaches
|
||
| } | ||
| Logger.flush(); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <RemoteSiteSetting xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <disableProtocolSecurity>false</disableProtocolSecurity> | ||
| <isActive>true</isActive> | ||
| <url>https://api.hopfreight-3pl.com</url> | ||
| </RemoteSiteSetting> |
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: 19886
Authorization Bypass
Reachability: Internal
Exploitability: Difficult
CWE: CWE-862 — Missing Authorization
View Security blast radius
Use
inherited sharingfor this service.BF-SEC-1 requires services to use
inherited sharing. The current caller isOrderTriggerHandler.afterUpdate, but the public method should not disable record-level sharing for other internal callers.Apply the sharing declaration change
Committable suggestion
Source: Path instructions