Skip to content

utils/enh/roundAmountByCurrency#1240

Open
H-C-21 wants to merge 1 commit into
mainfrom
utils/enh/roundAmountByCurrency
Open

utils/enh/roundAmountByCurrency#1240
H-C-21 wants to merge 1 commit into
mainfrom
utils/enh/roundAmountByCurrency

Conversation

@H-C-21
Copy link
Copy Markdown
Contributor

@H-C-21 H-C-21 commented Apr 25, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Pricing amounts in API responses are now calculated with proper currency-specific accuracy, improving precision across all supported currencies.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 25, 2026

Walkthrough

The roundAmountByCurrency function in the Price module was refactored to dynamically apply currency-specific rounding accuracy instead of hardcoding EUR accuracy and returning original amounts for all other currencies, directly affecting PriceAPIEntity JSON serialization behavior.

Changes

Cohort / File(s) Summary
Price Rounding Logic
lib/mobility-core/src/Kernel/Types/Price.hs
Updated roundAmountByCurrency to use getAccuracy currency for all currencies, removing hardcoded EUR-specific logic and conditional fallback behavior. Changes JSON serialization of PriceAPIEntity amount field.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A currency tale, precise and true,
No hardcoded EUR in our view,
Each coin now rounds by its own measure,
Accuracy's a flexible treasure!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'utils/enh/roundAmountByCurrency' is a branch name format rather than a descriptive pull request title that clearly summarizes the main change. Use a descriptive title that explains the change, such as 'Apply currency-specific rounding to roundAmountByCurrency' or 'Support dynamic currency rounding in roundAmountByCurrency'.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch utils/enh/roundAmountByCurrency

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/mobility-core/src/Kernel/Types/Price.hs`:
- Around line 174-175: The ToJSON instance for PriceAPIEntity is currently using
roundAmountByCurrency (which applies getAccuracy and makes INR accuracy 0),
causing INR amounts to lose paise; change the ToJSON PriceAPIEntity
implementation to serialize PriceAPIEntity.amount using the raw HighPrecMoney
value (or a new identity-preserving serializer) instead of calling
roundAmountByCurrency, and move rounding logic into a separate display formatter
(e.g., roundForDisplay or formatAmountForCurrency) used only by UI/display
paths; ensure references to roundAmountByCurrency, getAccuracy,
PriceAPIEntity.amount and the ToJSON PriceAPIEntity instance are updated,
add/adjust unit or integration tests to assert full precision is preserved for
INR/USD serialization, and if the rounding change is intentional, add a release
note documenting the precision break for INR consumers.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0aef8d13-85f5-40fe-bed8-7b83ed1ffc9a

📥 Commits

Reviewing files that changed from the base of the PR and between 490d6ed and 63cab63.

📒 Files selected for processing (1)
  • lib/mobility-core/src/Kernel/Types/Price.hs

Comment on lines 174 to +175
roundAmountByCurrency :: Currency -> HighPrecMoney -> HighPrecMoney
roundAmountByCurrency EUR amount = roundHighPrecMoney @HighPrecMoney (getAccuracy EUR) amount
roundAmountByCurrency _ amount = amount -- for backward compatibility
roundAmountByCurrency currency amount = roundHighPrecMoney @HighPrecMoney (getAccuracy currency) amount
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find all usages of roundAmountByCurrency and PriceAPIEntity to assess blast radius.
rg -nP -C3 '\broundAmountByCurrency\b'
echo '----'
rg -nP -C2 '\bPriceAPIEntity\b' --type=hs
echo '----'
# Look for tests/snapshots that may pin amount formatting for INR/USD.
rg -nP -C2 '"amount"\s*[:=]' --type=hs -g '!**/dist*/**'
echo '----'
# Any explicit references to mkPriceFromAPIEntity (potential lossy round-trip).
rg -nP -C3 '\bmkPriceFromAPIEntity\b'

Repository: nammayatri/shared-kernel

Length of output: 3747


INR amounts will lose sub-unit precision in PriceAPIEntity JSON serialization.

roundAmountByCurrency now rounds all currency amounts using getAccuracy: INR=0 (whole rupees), USD=2, EUR=2. This affects the ToJSON PriceAPIEntity instance at line 170, which serializes every PriceAPIEntity.amount rounded.

For INR, this is a breaking change: any API consumer expecting fractional rupee amounts (paise) will now receive whole rupees, with sub-unit precision silently lost via rounding.

Verify:

  • Is this rounding intentional for the API contract, or should PriceAPIEntity JSON preserve full precision while using getAccuracy only for display formatting elsewhere?
  • Are there snapshot tests or integration tests that validate API response precision for INR/USD?
  • If intentional, add a release note explaining the precision change for INR consumers.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/mobility-core/src/Kernel/Types/Price.hs` around lines 174 - 175, The
ToJSON instance for PriceAPIEntity is currently using roundAmountByCurrency
(which applies getAccuracy and makes INR accuracy 0), causing INR amounts to
lose paise; change the ToJSON PriceAPIEntity implementation to serialize
PriceAPIEntity.amount using the raw HighPrecMoney value (or a new
identity-preserving serializer) instead of calling roundAmountByCurrency, and
move rounding logic into a separate display formatter (e.g., roundForDisplay or
formatAmountForCurrency) used only by UI/display paths; ensure references to
roundAmountByCurrency, getAccuracy, PriceAPIEntity.amount and the ToJSON
PriceAPIEntity instance are updated, add/adjust unit or integration tests to
assert full precision is preserved for INR/USD serialization, and if the
rounding change is intentional, add a release note documenting the precision
break for INR consumers.

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