Customer And Address LLD
What is purpose of this article?
In this article we will discuss implementation logic to populate ‘addresses’ and ‘customers’ table respectively
Steps to Achieve Customer and Address Cron Job
Cloud Scheduler will hit cron job API (
_ah/api/esb/v1/customers-cron
) with given request body{ "isHistoric":true, "startDate":"2023-11-23 15:57:50", "endDate":"2023-11-25 15:57:50", "interval": 7 }
From the cloud scheduler request body we will check whether it is historic sync or regular sync
If it is historic sync then we will get start date, end date and interval values from request body
If it is regular sync then we will set static 24hours time interval of the current date
Using time interval we will fetch customers details of Eshopbox shipping from big query using query given below
SELECT workspace, shipping_customer_name, shipping_email_id, shipping_contact_number FROM `analytics-340903.bq_export_sync.bq_sale_order_report_v2` WHERE order_journey_update_time IS NOT NULL AND order_processing_on BETWEEN DATETIME("2023-09-01") AND DATETIME_ADD("2023-09-01", INTERVAL 60 DAY) AND esb_shipping = '1' GROUP BY workspace, shipping_customer_name, shipping_email_id, shipping_contact_number
After fetching customers details from big query we fetch customer details from WMS Database using unique composite key workspace,email and phone number using query given below
SELECT accounts.account_slug, customers.email, customers.phone FROM customers LEFT JOIN accounts ON customers.account_id = accounts.id WHERE accounts.account_slug IN (farmely) AND customers.phone IN ( '7204634592', '9068070522', '7259034618', '9712919840' ) AND customers.email IN ( 'haifabanday@yahoo.com', 'rashmitejaswinir@gmail.com', 'deekshabarora@gmail.com', 'g.bhatnagar22@gmail.com' );
Using result from big query and wms database we prepare a look up table using workspace##email##phone as unique key and find out new data to be inserted
Using these new data we will fetch orders details from Big Query using query given below
SELECT channel_order_id, current_status, shipping_customer_name, shipping_email_id, shipping_contact_number, workspace, shipping_address, shipping_city, shipping_pin_code, shipping_state, current_status, current_status_remark, shipment_id, return_id, return_reason, reason_for_failed_delivery, return_sub_reason, return_item_condition, cancelled_by, cancellation_reason, cancellation_sub_reason FROM `analytics-340903.bq_export_sync.bq_sale_order_report_v2` WHERE order_journey_update_time IS NOT NULL AND workspace IN ('farmely') AND shipping_email_id IN ('shubhmkhajuriya786@gmail.com') AND shipping_contact_number IN ('8966055092');
We will create another lookup table using the data from above query where key will be worskpace##email##phone corresponding to this key there will be another map which contains details about orders, return, rejectcted percentage etc. we will perform following steps to prepare lookup table
To populate orders count we do followings
create key named “order_id”
create a HashSet which will contains order_id
If record is already present given unique key(workspace##email##phone) then go to inner map and get order_id and add order_id of big query record into set
size of the set will be the count of unique orders of a customer which identified using workspace##email and phone
In similar way we calculated order count we calculate return count
create key named “return_id”
create a HashSet which will contains return_id
If record is already present for given unique key(workspace##email##phone) then go to inner map and get return_id and add order_id of big query record into set
size of the set will be the count of unique returns of a customer which identified using workspace##email and phone
In similar way we calculate shipment count
create key named “shipment_id”
create a HashSet which will contains shipment_id
If record is already present for given unique key(workspace##email##phone) then go to inner map and get shipment_id and add shipment_id of the big query record into set
size of the set will be the count of unique returns of a customer which identified using workspace##email and phone
In similar way we calculate rejected shipment count
create key named “rejected_shipment_id”
create a HashSet which will contains shipment_id
To get rejected shipment_count we will get those shipment_id which satisfy following condtions
Where return_reason = RTO and
failed delivery reason will be “Rejected by customer“
current_status not delivered
If record is already present for given unique key(workspace##email##phone) then go to inner map and get shipment_id and add shipment_id of big query record into set
size of the set will be the count of unique returns of a customer which identified using workspace##email##phone
Similarly we will get count of Cancellation and Returned Item conditions count as well
After preparing above lookup table we will fetch address details of new customers from BigQuery using query given below
SELECT workspace, shipping_email_id, shipping_contact_number, shipping_address, shipping_city, shipping_pin_code, shipping_state, shipping_country FROM `analytics-340903.bq_export_sync.bq_sale_order_report_v2` WHERE order_journey_update_time IS NOT NULL AND workspace IN ('farmely') AND shipping_email_id IN ('shubhmkhajuriya786@gmail.com') AND shipping_contact_number IN ('8966055092'); GROUP BY workspace, shipping_email_id, shipping_contact_number, shipping_address, shipping_city, shipping_pin_code, shipping_state, shipping_country
After getting address details use order details lookup table to add address list into inner map
After we done with orders details lookup table and new customers lookup table we will populate all the additional details into customers Model class
After completing List of Model class for new customers we save these details into database
Then after we will call another Task Queue Which will fetch data from Customers table into batches and fetch orders details from Big Query and update existing customers details
Steps to achieve post API to add customers
We get hit from frontend on POST API:
_ah/api/esb/v1/addCustomers
with given request body{ "customers": { "workspace": "farmely", "name": "Rohit", "email": "rohitkumar@eshopbox.com", "phone": "9876543210", "status": "ACTIVE" }, "address": [ { "addressLine1": "Udhyog Vihar", "addressLine2": "Phase 3", "pincode": "122021", "city": "Gurgaon", "state": "Haryana", "Country": "India" } ] }
After getting request body, we check if customer already exist or not
If customer already exist we return existing customer details to frontend
Else we fetch order details of this customer from Big Query and prepare customer class model
Also we will fetch address details from big query and find unique address by concatenating in special order and converting it into lower case
and save customers details into table and get unique primary key of customers after saving successfully
we use this unique key to as foreign key to bulk insert address into addresses table in database
after saving we will prepare response and return to frontend
Steps to achieve PUT API to Update Customers
We get hit from frontend on PUI:
_ah/api/esb/v1/updateCustomerDetails/{customer_id}
with given request body{ "workspace": "farmely", "name": "Rohit", "email": "rohitkumar@eshopbox.com", "phone": "9876543210", "status": "ACTIVE" }
After getting request body, run update query using customer_id and return updated result to frontend
Steps to create GET API to get customer and address details
Frontend will hit get API:
_ah/api/esb/v1/shipping/customers
to fetch customers detailsWe will fetch customers details using page and per-page value from database
After getting data we will prepare response and return to frontend
The response body will be in following structure
{ "total": "71674", "perPage": "50", "data": [ { "id": 9823, "customerName": "John Doe", "account": "farmely", "status": "ACTIVE", "customerCreatedOn": "1700489886993", "customerUpdatedOn": "1700489886993" "email": "testing@email.com", "phone": "9876543210", "whatsAppStatus":Active, "orderDetails":{ "orderCount":10, "returnCount":2, "customerCancellation":2, "sellerCancellation": 3 } "customerAddresses":[ { "addressLine1":"Phase 3 Gurugram", "addressLine2":"Udhyog Vihar", "shippingCity": "Bengaluru", "shippingState": "Karnataka", "shippingPostalCode": "560037" }] } ] }