Hello from MCP server

List Files | Just Commands | Repo | Logs

← back |
import { useCurrencyStore } from "@/stores/currency";
import { usePreferencesStore } from "@/stores/preferences";

/**
 * Converts user input currency to base currency
 * @param userCurrencyValue - The value in user's display currency
 * @returns The value converted to base currency
 */
export function convertToBaseCurrency(userCurrencyValue: number | string): number {
  const currencyStore = useCurrencyStore();
  const preferences = usePreferencesStore();
  
  const numericValue = Number(userCurrencyValue);
  if (isNaN(numericValue)) {
    return 0;
  }
  
  // Find the exchange rate for the user's currency
  const rate = currencyStore.exchangeRates.find(
    (e) => e.quoteCurrency == preferences.currency,
  ) || { rate: 1 };
  
  // Convert from user currency to base currency (inverse of ShowCurrency logic)
  const baseCurrencyValue = numericValue / Number(rate.rate);
  
  return baseCurrencyValue; // Keep full precision for base currency
}

/**
 * Converts base currency to user display currency (same logic as ShowCurrency)
 * @param baseCurrencyValue - The value in base currency
 * @returns The value converted to user's display currency
 */
export function convertFromBaseCurrency(baseCurrencyValue: number | string): number {
  const currencyStore = useCurrencyStore();
  const preferences = usePreferencesStore();
  
  const numericValue = Number(baseCurrencyValue);
  if (isNaN(numericValue)) {
    return 0;
  }
  
  // Find the exchange rate for the user's currency
  const rate = currencyStore.exchangeRates.find(
    (e) => e.quoteCurrency == preferences.currency,
  ) || { rate: 1 };
  
  // Convert from base currency to user currency (same as ShowCurrency logic)
  const userCurrencyValue = numericValue * Number(rate.rate);
  
  return Math.ceil(userCurrencyValue);
}