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 an appsettings.json file. 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 the Program.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 as InventorySyncService, ShopifyServiceWrapper, and OdooService are 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:
    The InventorySyncBackgroundService runs continuously in the background. It triggers the sync process at intervals defined in SyncSettings (for example, every 15 minutes).

  • Calling InventorySyncService:
    The background service calls the SyncInventory method in the InventorySyncService class.

B. Fetching Data from Shopify

  • GraphQL Query:
    The InventorySyncService.SyncInventory method constructs a GraphQL query to fetch a batch of products (as defined by the BatchSize setting).

    • The query requests product details such as id, title, and nested variant details (including variant id, title, and inventoryQuantity).
  • HTTP Request:
    The query is sent via an HTTP POST request to Shopify's GraphQL endpoint. The X-Shopify-Access-Token header 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:
    A SemaphoreSlim (configured via MaxConcurrentOperations) limits the number of products processed concurrently. For each product, the service calls a method to process the product.

  • Processing Variants:
    Inside ProcessProduct, the service iterates over each variant of the product:

    • For each variant, it attempts (with retry logic as defined in MaxRetries and RetryDelaySeconds) to synchronize the variant data with Odoo.

D. Synchronizing with Odoo

  • Searching for Existing Products:
    The OdooService.CreateOrUpdateOdooProductAsync method is invoked with the Shopify Product and ProductVariant data.

    • It constructs a “domain” filter (for example, where the default_code equals the variant’s ID) to check if the product already exists in Odoo.
    • The service uses the Odoo API client (OdooRpcClient) to call the search_read method (via a custom or built-in method like GetAsync) to fetch matching Odoo records.
  • Creating or Updating Records:

    • If no matching product is found:
      The service creates a new product in Odoo using the create method. 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 the write method.
  • OdooRpcClient Role:
    The OdooRpcClient handles:

    • Authentication: Logging into Odoo and caching the user ID.
    • API Calls: Making JSON-RPC calls to perform operations like search_read, create, and write.

3. Health Checks & Logging

  • Health Checks:
    The project registers health checks for both Shopify and Odoo (via custom classes like ShopifyHealthCheck and OdooHealthCheck). 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

  1. Configuration & Startup:
    Read settings, configure DI, and start background services.

  2. Shopify Data Fetch:
    The background service triggers InventorySyncService, which fetches products and variants using Shopify's GraphQL API.

  3. Product Processing:
    Each product’s variants are processed concurrently under controlled conditions.

  4. Odoo Sync:
    For each variant, Odoo is queried to check for existence, and then the product is created or updated accordingly using the OdooRpcClient.

  5. 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!