Skip to content

refactor: cart controller logic update - #20

Merged
EmmanuelAdah merged 1 commit into
mainfrom
microdev
Apr 28, 2026
Merged

refactor: cart controller logic update#20
EmmanuelAdah merged 1 commit into
mainfrom
microdev

Conversation

@EmmanuelAdah

@EmmanuelAdah EmmanuelAdah commented Apr 28, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Bug Fixes
    • Fixed currency conversion in your cart for multi-currency product inventories. Items now properly convert to your base currency using each product's individual currency, rather than incorrectly assuming all prices are in USD. This ensures accurate cart totals when purchasing from multiple currency sellers.
    • Improved exchange rate handling by automatically refreshing rates when cached data becomes unavailable, preventing the display of stale exchange rates.

@coderabbitai

coderabbitai Bot commented Apr 28, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a0989b33-3d94-4d40-8cc3-0051a97e83c0

📥 Commits

Reviewing files that changed from the base of the PR and between 5eac05b and 7517613.

📒 Files selected for processing (4)
  • controllers/cartController.js
  • controllers/productController.js
  • coverage/lcov-report/PanAfrik_Store/controllers/cartController.js.html
  • test/cartController.test.js

📝 Walkthrough

Walkthrough

The cart controller's currency conversion logic is refactored to use product-specific currencies instead of assuming USD prices, fetch rates on Redis cache miss, and remove the USD default for base currency. Corresponding tests are updated with new fixtures and assertions.

Changes

Cohort / File(s) Summary
Cart Controller Logic
controllers/cartController.js
Modifies getCart to use req.user.baseCurrency directly instead of defaulting to USD, fetch fresh rates via fetchGrossRates() on cache miss, and rewrite per-item price conversion to respect each product's currency field instead of assuming USD-based pricing.
Coverage Report
coverage/lcov-report/PanAfrik_Store/controllers/cartController.js.html
Updates coverage report to reflect the renamed variable source in price conversion logic from priceUSD to merchantCurrency.
Cart Tests
test/cartController.test.js
Refactors test formatting for POST /cart validation, reworks GET /cart conversion test with new Redis rate mock (GHS → NGN, NGN → NGN), updates cart fixtures to include per-product currency, adds serialized user to request context, and adjusts assertions for revised totals and per-item prices. Renames second test to cover missing-rate fallback scenario.
Product Controller
controllers/productController.js
Removes extraneous blank line in createProduct validation error path.

Sequence Diagram

sequenceDiagram
    participant Client
    participant CartController
    participant RedisCache
    participant RateFetcher
    participant CurrencyConverter

    Client->>CartController: GET /cart
    CartController->>CartController: Get user.baseCurrency
    
    CartController->>RedisCache: Check for rates
    alt Cache Hit
        RedisCache-->>CartController: Return cached rates
    else Cache Miss
        CartController->>RateFetcher: fetchGrossRates()
        RateFetcher-->>CartController: Return fresh rates
        CartController->>RedisCache: Store rates
    end
    
    CartController->>CartController: For each cart item
    CartController->>CurrencyConverter: Check product.currency vs baseCurrency
    
    alt Currencies Match
        CurrencyConverter-->>CartController: Use original price
    else Currencies Differ
        CurrencyConverter->>CartController: Apply merchant → user currency rate
    end
    
    CartController-->>Client: Return cart with converted prices
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A cart hops through currencies with newfound grace,
No longer stuck in USD's embrace!
Rates refresh when caches fall short,
Each product's coin gets its proper support—
Conversions bloom across merchant lands! 🌍✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch microdev

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@EmmanuelAdah
EmmanuelAdah merged commit 34f05fe into main Apr 28, 2026
1 check was pending
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.

1 participant