diff --git a/Amazon-SP-API-Steps-involved-in-fetching-reports-and-persisting-orders-in-the-system-using-Job-Queues.-.md b/Amazon-SP-API-Steps-involved-in-fetching-reports-and-persisting-orders-in-the-system-using-Job-Queues.-.md new file mode 100644 index 0000000..559b989 --- /dev/null +++ b/Amazon-SP-API-Steps-involved-in-fetching-reports-and-persisting-orders-in-the-system-using-Job-Queues.-.md @@ -0,0 +1,49 @@ +Fetching reports and persisting orders into the system involves many steps and job queues, It becomes complicated very quickly, so its the right time to take notes on these steps. This documentation will explain step by step code flow which will make easy to understand this logic in future whenever required. + +# Initializaton +eStack system has implemented Cron Job in a very decent way and eStack runs so many cron jobs for such complicated tasks. Implemented of cron jobs and job scheduling is high priority task, becuse reports processing and order fetching is time taking process. We cannot keep the system busy in browser for processing such big task, so for all Order Fetching Systems we have cron jobs such as **amazon, ebay, shopify, walmart** + +Minimum command syntax to run cron job for **Amazon SP API** + +`bin/pms3 amazon fetch spapi-orders --custId=cb --minutesAgo=1440` + +This command will executes a method `spApiScheduledReportAction()` from `module/AmazonOrder/src/Controller/CliController.php` + +This is the initial method of the process, This method will push first job `[CheckSpApiScheduledOrderDataReports] module/Meta/src/Job/Amazon/CheckSpApiScheduledOrderDataReports.php` to queue. + +# CheckSpApiScheduledOrderDataReports Job + +As name suggests this job will checks all SP API scheduled reports. Logic related to checking Scheduled report is available in the `[spApiReportService] module/AmazonOrder/src/Service/SpApiReportsService.php` service. This service is executed from within the method. + +### spApiReportsService Service + +This service called from the previous method and holds the logic for making list of reports fetched from the Amazon. + +An instance of Amazon SP API Reports is created in another function `checkForScheduledReportsForChannelAccount` within the same service, and using the instance of the Reports, eStack system fetches the reports from the Amazon server. All fetched reports are filtered for `processingStatus` with `DONE`. These filtered reports will be stored inside the system within `Report` database table, at the end, this method returns a list of `reportEntities` for further processing. + +Once `reportEntities` are returned, then those reports are pushed to `SpApiFetchReport` job to download the process Orders. + +# SpApiFetchReport Job + +All the `reportEntities` are looped to push each report to **SpApiFetchReport**. This Fetch Report job again processes **SpApiReportService** to fetch actual reports through method **fetchReport** from the amazon and save them into the eStack at following address. + +`data/customer-data//amzn-reports/.txt` + +Report content also saved inside the `Report` database table. + +When one report is saved in the database, then FetchReport job executes another **SpApiProcessAmazonReport** + +#SpApiProcessAmazonReport Job + + This job checks type of report and we will have separate job for different types of Reports, so that that Job will be responsible for reading content of the job and processing that into the system. + +e.g. we may have CSV and XML reports or may be some other format may be JSON. Based on that system will implement specific job and code. + +This method calls the last Job of the queue which is final processing Job. + +# Process Report Jobs + +This section will execute job as per the type of the Job e.g. we have report type `GET_AMAZON_FULFILLED_SHIPMENTS_DATA_GENERAL` and for this type of report, the system will execute `GetAmazonFulfilledShipmentDataGeneral`. + +Jobs executed at this step will have different services implemented as per the requirements. In case of `GetAmazonFulfilledShipmentDataGeneral` OrderImportService will be used to read content from the report and save all those Orders in the system. +