Skip to content

review code haravan connector and yuhtfarm haravan (vibe-kanban) - #24

Merged
ducminh-1 merged 3 commits into
19.0from
vk/b99a-review-code-hara
Jun 19, 2026
Merged

review code haravan connector and yuhtfarm haravan (vibe-kanban)#24
ducminh-1 merged 3 commits into
19.0from
vk/b99a-review-code-hara

Conversation

@ducminh-1

Copy link
Copy Markdown
Owner

No description provided.

leminh64 added 3 commits June 10, 2026 16:54
…sing several critical issues to improve its reliability and performance:

1. **Optimized Webhook Processing (`queue_job`)**:
   - I moved the webhook processing logic from `webhook_controller.py` into the `haravan.webhook` model.
   - The controller now delegates execution using Odoo's `.with_delay()` which leverages `queue_job`. This ensures that incoming webhooks return a 200 OK immediately and process payload in the background, preventing slow transactions or timeouts from breaking webhook integrations.

2. **Fixed Lexicographical Sorting Bugs**:
   - In both `haravan_product.py` and `haravan_order.py`, the `since_id` calculation incorrectly queried Odoo string fields (`haravan_product_id` and `haravan_order_id`) with `desc` and `asc` respectively. Because Haravan IDs are huge numbers stored as strings, ordering lexicographically causes issues (e.g. `'9'` > `'10'`).
   - I updated the queries to order by Odoo's internal `id desc` instead, securely fetching the most recently imported mapping to determine the correct `since_id`.

3. **Multi-Location Inventory Support**:
   - The stock push methods (`_upload_stock_to_haravan` in `haravan_config.py` and `_push_inventory_to_haravan` in `haravan_product.py`) were hardcoded to only pull from the first index (`mapping[0]`).
   - I refactored these functions to iterate over all valid location mappings assigned to a configuration so it correctly aggregates or pushes per-location inventory.

4. **Code Cleanups**:
   - Refactored `_get_api_url` in `haravan_config.py` to correctly utilize the globally defined `HARAVAN_API_BASE` constant instead of redefining the string internally.
…hronization functions (`sync_orders_from_haravan`, `sync_products_from_haravan`, `sync_customers_from_haravan`, and `sync_revenue_from_haravan`).

Here is a summary of the critical logic bugs fixed and the structural improvements made:

1. **Decoupled Sync Timestamps (`last_sync_date`)**:
   - **Issue:** Previously, `haravan.config` used a single `last_sync_date` for products, orders, and customers. If orders synced first, it would advance `last_sync_date`. When products synced next, they would use the new timestamp, completely missing any products that were updated in Haravan between the previous sync cycle and the order sync.
   - **Fix:** Introduced separate `last_order_sync_date`, `last_product_sync_date`, and `last_customer_sync_date` fields to the model and views, ensuring independent and accurate sync cycles.

2. **Fixed Flawed Sort Order (`id desc` vs Lexicographical)**:
   - **Issue:** The fallback mechanism fetched the `since_id` by sorting string fields (`haravan_order_id desc`, `haravan_product_id desc`). In SQL, sorting numeric strings lexicographically evaluates "999" as greater than "1000", meaning the system frequently pulled the wrong `since_id`.
   - **Fix:** Updated queries to accurately sort by the internal database sequence `order="id desc"`.

3. **Corrected Incremental Sync Parameter Logic (`since_id` vs `updated_at_min`)**:
   - **Issue:** The sync algorithms were simultaneously querying Haravan using both `since_id` and `updated_at_min` (and using `page`), which resulted in skipped updates (e.g., if an older order was updated to "paid", its ID is lower than `since_id`, thus the API would omit it).
   - **Fix:** Refactored the methods to strictly prioritize `updated_at_min`. `since_id` is now exclusively used as a fallback if the sync runs for the very first time. Additionally, updated `sync_revenue` to use `updated_at_min` instead of `created_at_min` to capture orders that get fulfilled or paid later.

4. **Captured Pre-Sync Start Timestamps**:
   - **Issue:** The functions did not update the `last_sync_date` or updated it inaccurately at the end of execution. Any records updated in Haravan while the sync process was actively running would be permanently missed.
   - **Fix:** Each sync function now tracks `sync_start = fields.Datetime.now()` at the exact moment execution begins. It writes this exact timestamp to the configuration when complete, guaranteeing 100% gapless coverage during the next sync run.
@ducminh-1
ducminh-1 merged commit f4205aa into 19.0 Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants