diff --git a/src/lib/components/SplitMethodSelector.svelte b/src/lib/components/SplitMethodSelector.svelte index 4be72dd..e364209 100644 --- a/src/lib/components/SplitMethodSelector.svelte +++ b/src/lib/components/SplitMethodSelector.svelte @@ -9,6 +9,7 @@ export let personalAmounts = {}; export let currentUser = ''; export let predefinedMode = false; + export let currency = 'CHF'; let personalTotalError = false; @@ -173,8 +174,8 @@ {/each} {#if amount}
{getErrorDescription(status)}
@@ -163,7 +163,7 @@ \ No newline at end of file + diff --git a/src/routes/api/cospend/exchange-rates/+server.ts b/src/routes/api/cospend/exchange-rates/+server.ts new file mode 100644 index 0000000..f3c47cd --- /dev/null +++ b/src/routes/api/cospend/exchange-rates/+server.ts @@ -0,0 +1,115 @@ +import type { RequestHandler } from '@sveltejs/kit'; +import { ExchangeRate } from '../../../../models/ExchangeRate'; +import { dbConnect } from '../../../../utils/db'; +import { error, json } from '@sveltejs/kit'; + +export const GET: RequestHandler = async ({ locals, url }) => { + const auth = await locals.auth(); + if (!auth || !auth.user?.nickname) { + throw error(401, 'Not logged in'); + } + + const fromCurrency = url.searchParams.get('from')?.toUpperCase(); + const date = url.searchParams.get('date'); + const action = url.searchParams.get('action') || 'rate'; + + if (action === 'currencies') { + return await getSupportedCurrencies(); + } + + if (!fromCurrency || !date) { + throw error(400, 'Missing required parameters: from and date'); + } + + if (!isValidCurrencyCode(fromCurrency)) { + throw error(400, 'Invalid currency code'); + } + + try { + const rate = await getExchangeRate(fromCurrency, date); + return json({ rate, fromCurrency, toCurrency: 'CHF', date }); + } catch (e) { + console.error('Error getting exchange rate:', e); + throw error(500, 'Failed to get exchange rate'); + } +}; + +async function getExchangeRate(fromCurrency: string, date: string): Promise