Open source personal finance application
feat(rates): add daily fetch-rates edge function and cron
Ilya Komichev26f5634c8725
diff --git a/app/supabase/functions/fetch-rates/index.ts b/app/supabase/functions/fetch-rates/index.tsnew file mode 100644index 00000000..26e18890--- /dev/null+++ b/app/supabase/functions/fetch-rates/index.ts@@ -0,0 +1,141 @@+// Daily exchange-rate refresh.+//+// Pulls two USD-based sources, merges them into one map and writes a single+// `source = 'merged'` row into `public.rates`:+// - Coinbase: no API key, ~all fiat + crypto. Used as the base layer.+// - OER (Open Exchange Rates): better fiat precision. Overlaid on top.+//+// The client reads ONE row (`... ORDER BY date DESC LIMIT 1`), so we merge here+// and persist a single row per day rather than merging at read time.+//+// Writes use the service_role (auto-injected as SUPABASE_SERVICE_ROLE_KEY):+// `rates` has a global read policy but no client INSERT policy.++import { createClient } from 'jsr:@supabase/supabase-js@2'++const OER_API_URL = 'https://openexchangerates.org/api/latest.json'+const COINBASE_API_URL = 'https://api.coinbase.com/v2/exchange-rates'++async function fetchFromCoinbase(): Promise<Record<string, number> | null> {+ try {+ const res = await fetch(`${COINBASE_API_URL}?currency=USD`)++ if (!res.ok) {+ console.error(`Coinbase fetch failed: ${res.status} ${res.statusText}`)+ return null+ }++ const json = await res.json()+ const rawRates: Record<string, string> = json.data?.rates++ if (!rawRates) {+ console.error('Coinbase: unexpected response shape')+ return null+ }++ const parsed: Record<string, number> = {}+ for (const [code, value] of Object.entries(rawRates)) {+ const num = Number.parseFloat(value)+ if (Number.isFinite(num) && num > 0)+ parsed[code] = num+ }++ return parsed+ }+ catch (e) {+ console.error('Coinbase fetch error:', e)+ return null+ }+}++async function fetchFromOER(): Promise<Record<string, number> | null> {+ const appId = Deno.env.get('OPEN_EXCHANGE_RATES_KEY')++ if (!appId) {+ console.error('OPEN_EXCHANGE_RATES_KEY not set')+ return null+ }++ try {+ const res = await fetch(`${OER_API_URL}?app_id=${appId}`)++ if (!res.ok) {+ console.error(`OER fetch failed: ${res.status} ${res.statusText}`)+ return null+ }++ const data = await res.json()+ return data.rates ?? null+ }+ catch (e) {+ console.error('OER fetch error:', e)+ return null+ }+}++Deno.serve(async () => {+ const date = new Date().toISOString().slice(0, 10)++ const [coinbase, oer] = await Promise.all([fetchFromCoinbase(), fetchFromOER()])++ if (!coinbase && !oer) {+ console.error('All exchange rate sources failed')+ return new Response(JSON.stringify({ error: 'all sources failed' }), {+ status: 502,+ headers: { 'Content-Type': 'application/json' },+ })+ }++ // Base: Coinbase (broad + crypto). Overlay: OER fiat (more precise). Both USD-based.+ const merged: Record<string, number> = { ...(coinbase ?? {}), ...(oer ?? {}) }++ const supabase = createClient(+ Deno.env.get('SUPABASE_URL')!,+ Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!,+ )++ // The (date, source) unique index is on an expression (coalesce(source,'')),+ // which PostgREST upsert can't target - so select-then-write.+ const { data: existing, error: selErr } = await supabase+ .from('rates')+ .select('id')+ .eq('date', date)+ .eq('source', 'merged')+ .maybeSingle()++ if (selErr) {+ console.error('rates select failed:', selErr)+ return new Response(JSON.stringify({ error: selErr.message }), {+ status: 500,+ headers: { 'Content-Type': 'application/json' },+ })+ }++ const writeErr = existing+ ? (await supabase+ .from('rates')+ .update({ rates: merged, updatedAt: Date.now() })+ .eq('id', existing.id)).error+ : (await supabase+ .from('rates')+ .insert({ date, source: 'merged', rates: merged, updatedAt: Date.now() })).error++ if (writeErr) {+ console.error('rates write failed:', writeErr)+ return new Response(JSON.stringify({ error: writeErr.message }), {+ status: 500,+ headers: { 'Content-Type': 'application/json' },+ })+ }++ const summary = {+ date,+ count: Object.keys(merged).length,+ coinbase: coinbase ? Object.keys(coinbase).length : 0,+ oer: oer ? Object.keys(oer).length : 0,+ }+ console.log('rates saved:', summary)+ return new Response(JSON.stringify(summary), {+ headers: { 'Content-Type': 'application/json' },+ })+})diff --git a/app/supabase/migrations/20260613160000_schedule_fetch_rates.sql b/app/supabase/migrations/20260613160000_schedule_fetch_rates.sqlnew file mode 100644index 00000000..937b77e0--- /dev/null+++ b/app/supabase/migrations/20260613160000_schedule_fetch_rates.sql@@ -0,0 +1,32 @@+-- Daily exchange-rate refresh schedule.+--+-- Runs the `fetch-rates` edge function every day at 06:00 UTC. The function+-- pulls Coinbase + OER, merges them into one USD-based map and writes a single+-- `source = 'merged'` row into `rates`.+--+-- Portable: the function URL and anon key are read from Vault secrets rather+-- than hardcoded, so this migration is safe on local / dev / prod. Set them+-- once per project (the anon key is public - it is baked into the SPA build):+-- select vault.create_secret('https://<ref>.supabase.co', 'project_url');+-- select vault.create_secret('<anon-key>', 'anon_key');+-- Until the secrets exist the scheduled POST is a harmless no-op (null url).+-- cron.schedule upserts by job name, so re-running this is safe.++create extension if not exists pg_cron;+create extension if not exists pg_net;++select cron.schedule(+ 'fetch-rates-daily',+ '0 6 * * *',+ $$+ select net.http_post(+ url := (select decrypted_secret from vault.decrypted_secrets where name = 'project_url')+ || '/functions/v1/fetch-rates',+ headers := jsonb_build_object(+ 'Content-Type', 'application/json',+ 'Authorization', 'Bearer ' || (select decrypted_secret from vault.decrypted_secrets where name = 'anon_key')+ ),+ body := '{}'::jsonb+ );+ $$+);diff --git a/eslint.config.js b/eslint.config.jsindex f7e0c145..0c45ded4 100644--- a/eslint.config.js+++ b/eslint.config.js@@ -38,6 +38,7 @@ export default ['app/playwright/profiles/**','app/powersync/**','app/supabase/config.toml',+ 'app/supabase/functions/**', // Deno runtime, separate from the Nuxt toolchain'app/tests/e2e/.auth/**','app/utils/migrate/data.json',],