Hello from MCP server
import { defineStore } from "pinia";
import { getDb } from "@/dataAccess/getDb";
import { storeCurrency, type CurrencyPrefs } from "@/framework/currencies";
import { Preferences } from "@capacitor/preferences";
export const useCurrencyStore = defineStore("currency", {
state: () => ({
currencies: [] as any[],
exchangeRates: [] as any[],
db: null as Awaited<ReturnType<typeof getDb>> | null,
currencyPrefs: null as CurrencyPrefs | null,
}),
actions: {
async initDb() {
if (!this.db) {
this.db = await getDb();
}
},
async getCurrencies() {
await this.initDb();
if (this.db) {
this.currencies = (await this.db.currencies.getAll()) || [];
}
},
async getRates() {
await this.initDb();
if (this.db) {
this.exchangeRates = (await this.db.currencies.getRates()) || [];
}
},
// Build and store full currency prefs to userPrefs table (one-way sync)
async syncCurrencyPrefs(targetCurrency: string) {
// Ensure currencies and rates are loaded first
if (this.currencies.length === 0) {
await this.getCurrencies();
}
if (this.exchangeRates.length === 0) {
await this.getRates();
}
// Find the currency record to get the symbol
const currencyRecord = this.currencies.find(
(c: any) => c.refId?.toLowerCase() === targetCurrency.toLowerCase()
);
// Find the exchange rate for the target currency
const rate = this.exchangeRates.find(
(r: any) => r.quoteCurrency?.toLowerCase() === targetCurrency.toLowerCase()
);
const prefs: CurrencyPrefs = {
baseCurrency: rate?.baseCurrency || "XAG",
targetCurrency: targetCurrency.toUpperCase(),
exchangeRate: rate?.rate ? parseFloat(rate.rate) : 1,
symbol: currencyRecord?.symbol || "$",
};
this.currencyPrefs = prefs;
await storeCurrency(prefs);
return prefs;
},
},
});