rc 1.0
This commit is contained in:
51
lib/currency.ts
Normal file
51
lib/currency.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Currency conversion utilities
|
||||
* Database stores prices in EUR
|
||||
* Countries in CHF_COUNTRIES see CHF (converted from EUR)
|
||||
* All other countries see EUR
|
||||
*/
|
||||
|
||||
// List of country codes that use CHF currency
|
||||
// Add or remove country codes here to change which countries get CHF pricing
|
||||
export const CHF_COUNTRIES = ['CH'] as const
|
||||
|
||||
// EUR to CHF exchange rate
|
||||
// Using a fixed rate - in production, you might want to fetch this from an API
|
||||
// Current approximate rate: 1 EUR ≈ 0.97 CHF (as of 2025)
|
||||
// Note: This is approximate. For production, consider using a real-time exchange rate API
|
||||
const EUR_TO_CHF_RATE = 0.97
|
||||
|
||||
/**
|
||||
* Convert EUR amount to CHF
|
||||
*/
|
||||
export function convertEurToChf(eurAmount: number): number {
|
||||
return eurAmount * EUR_TO_CHF_RATE
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the currency to use based on country code
|
||||
* Returns 'CHF' for countries in CHF_COUNTRIES, 'EUR' for all other countries
|
||||
*/
|
||||
export function getCurrencyForCountry(countryCode: string | null): 'CHF' | 'EUR' {
|
||||
return countryCode && CHF_COUNTRIES.includes(countryCode as any) ? 'CHF' : 'EUR'
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert price based on country
|
||||
* If country is in CHF_COUNTRIES, convert EUR to CHF
|
||||
* Otherwise, return EUR amount as-is
|
||||
*/
|
||||
export function convertPriceForCountry(priceInEur: number, countryCode: string | null): number {
|
||||
if (countryCode && CHF_COUNTRIES.includes(countryCode as any)) {
|
||||
return convertEurToChf(priceInEur)
|
||||
}
|
||||
return priceInEur
|
||||
}
|
||||
|
||||
/**
|
||||
* Get currency symbol for display
|
||||
*/
|
||||
export function getCurrencySymbol(currency: 'CHF' | 'EUR'): string {
|
||||
return currency === 'CHF' ? 'CHF' : 'EUR'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user