-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat(kit): add reporting currency setting #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
63cfe06
feat(kit): add reporting currency setting
hyochan 33cb443
docs(kit): clarify reporting currency scope
hyochan a01672a
fix(kit): address reporting currency review
hyochan 2e135cc
fix(kit): address reporting currency follow-up
hyochan dda1cd5
fix(kit): resolve reporting currency review
hyochan ca96a60
fix(kit): address analytics review follow-up
hyochan 29a2203
fix(kit): address CodeRabbit review
hyochan d0f93a4
test(kit): cover compact micros rounding
hyochan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { selectReportingMrr } from "./query"; | ||
|
|
||
| describe("selectReportingMrr", () => { | ||
| it("uses only the reporting currency for the headline MRR", () => { | ||
| const result = selectReportingMrr( | ||
| [ | ||
| { currency: "EUR", mrrMicros: 8_500_000 }, | ||
| { currency: "USD", mrrMicros: 9_990_000 }, | ||
| { currency: "HUF", mrrMicros: 12_000_000 }, | ||
| ], | ||
| "USD", | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| currency: "USD", | ||
| mrrMicros: 9_990_000, | ||
| excludedMrrByCurrency: [ | ||
| { currency: "EUR", mrrMicros: 8_500_000 }, | ||
| { currency: "HUF", mrrMicros: 12_000_000 }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("returns zero when the reporting currency has no matching MRR", () => { | ||
| const result = selectReportingMrr( | ||
| [ | ||
| { currency: "EUR", mrrMicros: 8_500_000 }, | ||
| { currency: "HUF", mrrMicros: 12_000_000 }, | ||
| ], | ||
| "USD", | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| currency: "USD", | ||
| mrrMicros: 0, | ||
| excludedMrrByCurrency: [ | ||
| { currency: "EUR", mrrMicros: 8_500_000 }, | ||
| { currency: "HUF", mrrMicros: 12_000_000 }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("falls back to USD for invalid reporting currency input", () => { | ||
| const result = selectReportingMrr( | ||
| [ | ||
| { currency: "USD", mrrMicros: 9_990_000 }, | ||
| { currency: "EUR", mrrMicros: 8_500_000 }, | ||
| ], | ||
| "US", | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| currency: "USD", | ||
| mrrMicros: 9_990_000, | ||
| excludedMrrByCurrency: [{ currency: "EUR", mrrMicros: 8_500_000 }], | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { createError, ErrorCode } from "./errors"; | ||
|
|
||
| export const DEFAULT_REPORTING_CURRENCY = "USD"; | ||
|
|
||
| const currencyCodePattern = /^[A-Z]{3}$/; | ||
|
|
||
| export function isValidCurrencyCode(code: string): boolean { | ||
| return currencyCodePattern.test(code); | ||
| } | ||
|
|
||
| function normalizeCurrencyCandidate(input: string | null | undefined): string { | ||
| return input?.trim().toUpperCase() ?? ""; | ||
| } | ||
|
|
||
| export function normalizeReportingCurrencyOrDefault( | ||
| input: string | null | undefined, | ||
| ): string { | ||
| const normalized = normalizeCurrencyCandidate(input); | ||
| return isValidCurrencyCode(normalized) | ||
| ? normalized | ||
| : DEFAULT_REPORTING_CURRENCY; | ||
| } | ||
|
|
||
| export function normalizeReportingCurrency(input: string): string { | ||
| const normalized = normalizeCurrencyCandidate(input); | ||
| if (!isValidCurrencyCode(normalized)) { | ||
| throw createError( | ||
| ErrorCode.INVALID_INPUT, | ||
| "Reporting currency must be a 3-letter ISO 4217 code (e.g. USD, EUR, GBP).", | ||
| ); | ||
| } | ||
| return normalized; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| DEFAULT_REPORTING_CURRENCY, | ||
| formatMicros, | ||
| normalizeCurrencyCode, | ||
| } from "./utils"; | ||
|
|
||
| describe("normalizeCurrencyCode", () => { | ||
| it("trims and uppercases valid ISO currency codes", () => { | ||
| expect(normalizeCurrencyCode(" usd ")).toBe("USD"); | ||
| }); | ||
|
|
||
| it("falls back when the currency code is invalid", () => { | ||
| expect(normalizeCurrencyCode("usdollar", "EUR")).toBe("EUR"); | ||
| }); | ||
|
|
||
| it("falls back to DEFAULT_REPORTING_CURRENCY for nullish input", () => { | ||
| expect(normalizeCurrencyCode(null)).toBe(DEFAULT_REPORTING_CURRENCY); | ||
| expect(normalizeCurrencyCode(undefined)).toBe(DEFAULT_REPORTING_CURRENCY); | ||
| }); | ||
|
|
||
| it("uses the explicit fallback for undefined input", () => { | ||
| expect(normalizeCurrencyCode(undefined, "GBP")).toBe("GBP"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("formatMicros", () => { | ||
| it("formats zero as a currency amount in non-compact mode", () => { | ||
| expect(formatMicros(0, { currency: "USD" })).toBe("USD 0.00"); | ||
| }); | ||
|
|
||
| it("can hide zero values when a metric is empty", () => { | ||
| expect(formatMicros(0, { currency: "USD", emptyWhenZero: true })).toBe("—"); | ||
| }); | ||
|
|
||
| it("keeps compact formatting for chart axes", () => { | ||
| expect(formatMicros(1_200_000_000, { compact: true })).toBe("1.2k"); | ||
| }); | ||
|
|
||
| it("formats compact values between ten and one thousand without decimals", () => { | ||
| expect(formatMicros(10_500_000, { compact: true })).toBe("11"); | ||
| expect(formatMicros(999_400_000, { compact: true })).toBe("999"); | ||
| }); | ||
|
|
||
| it("preserves cents for compact values below ten", () => { | ||
| expect(formatMicros(500_000, { compact: true })).toBe("0.50"); | ||
| }); | ||
| }); | ||
|
hyochan marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.