/
Order Count : Backend LLD

Order Count : Backend LLD

Figma: Order Count Figma

Introduction - This article contains all the logical steps involved to create the order count API in order to implement segments and smart segments.

Backend Module Implementation:

  • The shipping team can implement a lightweight method within the order service in the existing repository for order count.

  • This method should ensure minimal latency.

API

Service: Esb Client Order Return

API Method: POST

URL : http://localhost:8080/_ah/api/esb/v1/orders/count

Request

{ "filterQuery": { { "61d2d83019e2b31cf7d350a1": "(orderReceivedOn%3E=1708194600000)AND(orderReceivedOn%3C=1710786599999)AND(%27currentOrderStatus%27:%27Delivered%27)", "61d2d83019e2b31cf7d350a2": "(orderReceivedOn%3E=1708194600000)AND(orderReceivedOn%3C=1710786599999)AND(%27currentOrderStatus%27:%27Delivered%27)" } }

Response:

{ "61d2d83019e2b31cf7d350a1":20, "61d2d83019e2b31cf7d350a2":40, "updated_at":'2024-03-01 00:00:00' }

How to achieve this:

  1. We will get a map containing the list of segments and their corresponding filters.

  2. We will iterate through this map and fetch the details from algolia for that particular filter.

    Query query = new Query().setFilters(filter); query.setFacets(Arrays.asList("currentOrderStatus")); RequestOptions requestOptions = new RequestOptions().addExtraHeader("X-Algolia-UserToken", "order_search_v2") .setTimeout(15000);

     

  3. Here we will extract 2 fields that we need i.e. total which includes the total number of records and currentOrderStatus which keep data of multiple status for that particular filter. (TBD, for now we are only returning the number)

  4. The number we get from algolia will be put in a map corresponding to its segementId along with the current timestamp as updated_at and then we will return the map in the required format.

 

Curl for Staging :

curl --location 'https://order-return-dot-eshopbox-portal-dev.el.r.appspot.com/_ah/api/esb/v1/orders/count' \ --header 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlJVVXdSREZCUVRSRFFqQkdORFUxTVVZeE16ZEdPRFJHTnpORk5EaEJSVEU0TVVORk5qVTJOdyJ9.eyJodHRwczovL2FwcERhdGEiOnt9LCJodHRwczovL3VzZXJEZXRhaWxzIjp7ImlkIjoyOTc2LCJ1c2VyVHlwZSI6ImRlZmF1bHQiLCJlb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CvR5WYdBHxw8UsA6ckFh5dsHMiv6KTpYhaDKu0bYfNc7tSo6DdgkYMArriKnrZFq43PiNJGvyABoxv8x9O4DpW4e5Oiay6wfhuW2cpBPU1e4jtvOwRyaQPFLoT8OtXtspc7QnSce4HgxNUrj9XjNPm0pFkVKdGemJTcpsBrwxc_8qFRy5aaAuk6CQ8WLWizLwdeInEO3bnBrNAMpP8-B7vSige6MkOBWl0WRsRRBE49Vqcr6DyJeSz4cEcvtAYM9qQ1iBIvjGj1hiUkiM60OQg5uij7-lOp4diOMdgpBMjg' \ --header 'Content-Type: application/json' \ --header 'Cookie: JSESSIONID=PzX7jHdyk_9Ojjio2cSxeA' \ --data '{ "filterQuery": { "61d2d83019e2b31cf7d350a2": "(orderReceivedOn>=1711823400000) AND (orderReceivedOn<=1714415399999) OR (returns.returnDetails.createdOnTimestamp>=1711823400000) AND (returns.returnDetails.createdOnTimestamp<=1714415399999) ", "61d2d83019e2b31cf7d350a1": "(orderReceivedOn>=1697999400000)AND(orderReceivedOn<=1713551399999)AND( '\''shipmentDetails.failedToRts'\'':'\''Yes'\'' )AND( '\''currentOrderStatus'\'':'\''Processing'\'' )", "61d2d83019e2b31cf7d350a3" : "(returns.returnDetails.createdOnTimestamp>=1711823400000)AND(returns.returnDetails.createdOnTimestamp<=1714415399999)", "61d2d83019e2b31cf7d350a4" : "(orderReceivedOn>=1710873000000)AND(orderReceivedOn<=1713464999999)AND( '\''currentOrderStatus'\'':'\''Processing'\'' )AND( '\''shipmentDetails.failedToHandover'\'':'\''Yes'\'' )", "61d2d83019e2b31cf7d350a5" : "(orderReadyToShipTimestamp>=1710873000000)AND(orderReadyToShipTimestamp<=1713464999999)AND( '\''shipmentDetails.forwardShippingMode'\'':'\''Express'\'' OR '\''shipmentDetails.forwardShippingMode'\'':'\''Priority'\'' OR '\''shipmentDetails.forwardShippingMode'\'':'\''Standard'\'' )AND( '\''currentOrderStatus'\'':'\''Handover done'\'' ) OR ('\''currentOrderStatus'\'':'\''On the way'\'' ) OR ('\''currentOrderStatus'\'':'\''Out for delivery'\'' )", "61d2d83019e2b31cf7d350a6" : "(orderReadyToShipTimestamp>=1710873000000)AND(orderReadyToShipTimestamp<=1713464999999)AND( '\''currentOrderStatus'\'':'\''Handover done'\'' )AND( '\''shipmentDetails.forwardShippingMode'\'':'\''Express'\'' OR '\''shipmentDetails.forwardShippingMode'\'':'\''Priority'\'' OR '\''shipmentDetails.forwardShippingMode'\'':'\''Standard'\'' )", "61d2d83019e2b31cf7d350a7" : "(orderReadyToShipTimestamp>=1710873000000)AND(orderReadyToShipTimestamp<=1713464999999)AND( '\''currentOrderStatus'\'':'\''Cancelled'\'' )", "61d2d83019e2b31cf7d350a8" : "(returns.returnDetails.createdOnTimestamp>=1710959400000)AND(returns.returnDetails.createdOnTimestamp<=1713551399999)AND( '\''currentOrderStatus'\'':'\''Awaiting pickup'\'' )", "61d2d83019e2b31cf7d350a9" : "(returns.returnDetails.createdOnTimestamp>=1710959400000)AND(returns.returnDetails.createdOnTimestamp<=1713551399999)AND( '\''currentOrderStatus'\'':'\''Return on the way'\'' )", "61d2d83019e2b31cf7d350a10" : "(returns.returnDetails.createdOnTimestamp>=1710959400000)AND(returns.returnDetails.createdOnTimestamp<=1713551399999)AND( '\''currentOrderStatus'\'':'\''Return on the way'\'' )AND( '\''shipmentDetails.stuckIntransit'\'':'\''Yes'\'' )", "61d2d83019e2b31cf7d350a11" : "(returns.returnDetails.createdOnTimestamp>=1710959400000)AND(returns.returnDetails.createdOnTimestamp<=1713551399999)AND( '\''currentOrderStatus'\'':'\''Return lost in transit'\'' )", "61d2d83019e2b31cf7d350a12" : "(returns.returnDetails.createdOnTimestamp>=1710959400000)AND(returns.returnDetails.createdOnTimestamp<=1713551399999)AND( '\''currentOrderStatus'\'':'\''Return on the way'\'' ) OR ('\''currentOrderStatus'\'':'\''Return in processing'\'' ) OR ('\''currentOrderStatus'\'':'\''Return lost in transit'\'' )", "61d2d83019e2b31cf7d350a13" : "(orderReceivedOn>=1711391400000)AND(orderReceivedOn<=1713983399999)AND( '\''currentOrderStatus'\'':'\''Return in processing'\'' )", "61d2d83019e2b31cf7d350a14" : "(returns.returnDetails.receivedOnTimestamp>=1710959400000)AND(returns.returnDetails.receivedOnTimestamp<=1713551399999)", "61d2d83019e2b31cf7d350a15" : "(orderReceivedOn>=1710959400000)AND(orderReceivedOn<=1713551399999)AND( '\''currentOrderStatus'\'':'\''Restocked'\'' )", "61d2d83019e2b31cf7d350a16" : "(orderReceivedOn>=1710959400000)AND(orderReceivedOn<=1713551399999)AND( '\''currentOrderStatus'\'':'\''Damaged'\'' )", "61d2d83019e2b31cf7d350a17" : "(orderReceivedOn>=1710959400000)AND(orderReceivedOn<=1713551399999)AND( '\''currentOrderStatus'\'':'\''Restocked'\'' ) OR ('\''currentOrderStatus'\'':'\''Quarantined'\'' ) OR ('\''currentOrderStatus'\'':'\''Damaged'\'' ) OR ('\''currentOrderStatus'\'':'\''Lost in processing'\'' )", "61d2d83019e2b31cf7d350a18" : "(orderReceivedOn>=1710959400000)AND(orderReceivedOn<=1713551399999)AND(returns.returnDetails.createdOnTimestamp>=1710959400000)AND(returns.returnDetails.createdOnTimestamp<=1713551399999)", "61d2d83019e2b31cf7d350a19" : "(orderReceivedOn>=1697999400000)AND(orderReceivedOn<=1713551399999)AND( '\''currentOrderStatus'\'':'\''Handover done'\'' )AND( '\''shipmentDetails.forwardShippingMode'\'':'\''Express'\'' OR '\''shipmentDetails.forwardShippingMode'\'':'\''Priority'\'' OR '\''shipmentDetails.forwardShippingMode'\'':'\''Standard'\'' )", "61d2d83019e2b31cf7d350a20" : "(orderReadyToShipTimestamp>=1710873000000)AND(orderReadyToShipTimestamp<=1713464999999)AND( '\''shipmentDetails.forwardShippingMode'\'':'\''Express'\'' OR '\''shipmentDetails.forwardShippingMode'\'':'\''Priority'\'' OR '\''shipmentDetails.forwardShippingMode'\'':'\''Standard'\'' )AND( '\''currentOrderStatus'\'':'\''Handover done'\'' ) OR ('\''currentOrderStatus'\'':'\''On the way'\'' ) OR ('\''currentOrderStatus'\'':'\''Out for delivery'\'' )" } }'

 

Response Time for 20 filterQuery as tested on local ~ 2.5s

 

Add RTS urgency, Handover urgency, Delivery urgency to Algolia as well as exports.

 

Add label

Related content