Odoo Shopify Connector - ajay3003/doc GitHub Wiki
The OdooShopifyConnector project is designed to synchronize data—especially product inventory—between Shopify and Odoo. Here's an overview of the data flow and key components in the project:
1. Configuration & Startup
-
Configuration Files:
The application reads settings from anappsettings.jsonfile. This file includes:- ShopifySettings: Contains the shop URL, access token, API version, rate limits, etc.
- OdooSettings: Contains the Odoo server URL, database name, credentials, and API key.
- SyncSettings: Controls sync behavior (batch size, retry settings, concurrency limits, etc.).
- Logging & EmailNotification: Provide logging details and email notification settings for alerts.
-
Dependency Injection (DI):
In theProgram.cs, the host is built and DI is configured. All configuration sections are bound to strongly typed classes (e.g.,ShopifySettings,OdooSettings, etc.) and services such asInventorySyncService,ShopifyServiceWrapper, andOdooServiceare registered. -
Hosted Services:
The project uses a background service (InventorySyncBackgroundService) that periodically triggers the synchronization process.
2. Data Flow During Synchronization
A. Triggering the Sync Process
-
Background Service:
TheInventorySyncBackgroundServiceruns continuously in the background. It triggers the sync process at intervals defined inSyncSettings(for example, every 15 minutes). -
Calling InventorySyncService:
The background service calls theSyncInventorymethod in theInventorySyncServiceclass.
B. Fetching Data from Shopify
-
GraphQL Query:
TheInventorySyncService.SyncInventorymethod constructs a GraphQL query to fetch a batch of products (as defined by theBatchSizesetting).- The query requests product details such as
id,title, and nested variant details (including variantid,title, andinventoryQuantity).
- The query requests product details such as
-
HTTP Request:
The query is sent via an HTTP POST request to Shopify's GraphQL endpoint. TheX-Shopify-Access-Tokenheader authenticates the request. -
Processing the Response:
The JSON response is deserialized dynamically. A list of products (and their variants) is extracted using LINQ (with appropriate casting for dynamic types).
C. Processing Each Product and Variant
-
Concurrent Processing:
ASemaphoreSlim(configured viaMaxConcurrentOperations) limits the number of products processed concurrently. For each product, the service calls a method to process the product. -
Processing Variants:
InsideProcessProduct, the service iterates over each variant of the product:- For each variant, it attempts (with retry logic as defined in
MaxRetriesandRetryDelaySeconds) to synchronize the variant data with Odoo.
- For each variant, it attempts (with retry logic as defined in
D. Synchronizing with Odoo
-
Searching for Existing Products:
TheOdooService.CreateOrUpdateOdooProductAsyncmethod is invoked with the ShopifyProductandProductVariantdata.- It constructs a “domain” filter (for example, where the
default_codeequals the variant’s ID) to check if the product already exists in Odoo. - The service uses the Odoo API client (
OdooRpcClient) to call thesearch_readmethod (via a custom or built-in method likeGetAsync) to fetch matching Odoo records.
- It constructs a “domain” filter (for example, where the
-
Creating or Updating Records:
- If no matching product is found:
The service creates a new product in Odoo using thecreatemethod. It passes along details such as product name, default code, list price, and standard price. - If a product exists:
The service updates the existing product (for instance, updating prices or inventory) using thewritemethod.
- If no matching product is found:
-
OdooRpcClient Role:
TheOdooRpcClienthandles:- Authentication: Logging into Odoo and caching the user ID.
- API Calls: Making JSON-RPC calls to perform operations like
search_read,create, andwrite.
3. Health Checks & Logging
-
Health Checks:
The project registers health checks for both Shopify and Odoo (via custom classes likeShopifyHealthCheckandOdooHealthCheck). These ensure that both external systems are available and responsive. -
Logging:
Throughout the process, detailed logging (via Serilog and the .NET logging abstractions) records:- Start and completion of the sync cycle.
- Details on processing each product/variant.
- Any errors or retry attempts that occur.
4. Error Handling & Notifications
-
Retry Logic:
For transient errors (e.g., network issues), the sync process includes retry logic for each variant operation. -
Exception Logging:
Exceptions are logged with details, allowing you to diagnose issues. -
Email Notifications (Optional):
If configured, the system could send email notifications in case of critical errors, though that part is separate from the core sync flow.
Summary
-
Configuration & Startup:
Read settings, configure DI, and start background services. -
Shopify Data Fetch:
The background service triggersInventorySyncService, which fetches products and variants using Shopify's GraphQL API. -
Product Processing:
Each product’s variants are processed concurrently under controlled conditions. -
Odoo Sync:
For each variant, Odoo is queried to check for existence, and then the product is created or updated accordingly using the OdooRpcClient. -
Monitoring & Logging:
Health checks and logging help ensure that the system runs reliably and any issues are promptly diagnosed.
This is the overall data flow in the OdooShopifyConnector project, ensuring that inventory data is synchronized between Shopify and Odoo seamlessly. Let me know if you need further details or clarifications!