mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 06:57:47 +01:00
feat(backend): Moved the unirate API to the backend
This commit is contained in:
4
.github/workflows/deploy-prod.yaml
vendored
4
.github/workflows/deploy-prod.yaml
vendored
@@ -99,6 +99,7 @@ jobs:
|
|||||||
SMTP_USE_TLS: ${{ secrets.SMTP_USE_TLS }}
|
SMTP_USE_TLS: ${{ secrets.SMTP_USE_TLS }}
|
||||||
SMTP_USE_SSL: ${{ secrets.SMTP_USE_SSL }}
|
SMTP_USE_SSL: ${{ secrets.SMTP_USE_SSL }}
|
||||||
SMTP_FROM: ${{ secrets.SMTP_FROM }}
|
SMTP_FROM: ${{ secrets.SMTP_FROM }}
|
||||||
|
UNIRATE_API_KEY: ${{ secrets.UNIRATE_API_KEY }}
|
||||||
run: |
|
run: |
|
||||||
helm upgrade --install myapp ./7project/charts/myapp-chart \
|
helm upgrade --install myapp ./7project/charts/myapp-chart \
|
||||||
-n prod --create-namespace \
|
-n prod --create-namespace \
|
||||||
@@ -125,4 +126,5 @@ jobs:
|
|||||||
--set-string smtp.password="$SMTP_PASSWORD" \
|
--set-string smtp.password="$SMTP_PASSWORD" \
|
||||||
--set-string smtp.tls="$SMTP_USE_TLS" \
|
--set-string smtp.tls="$SMTP_USE_TLS" \
|
||||||
--set-string smtp.ssl="$SMTP_USE_SSL" \
|
--set-string smtp.ssl="$SMTP_USE_SSL" \
|
||||||
--set-string smtp.from="$SMTP_FROM"
|
--set-string smtp.from="$SMTP_FROM" \
|
||||||
|
--set-string unirate.key="$UNIRATE_API_KEY"
|
||||||
66
7project/backend/app/api/exchange_rates.py
Normal file
66
7project/backend/app/api/exchange_rates.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import os
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
from fastapi import APIRouter, HTTPException, Query, status
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/exchange-rates", tags=["exchange-rates"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/", status_code=status.HTTP_200_OK)
|
||||||
|
async def get_exchange_rates(symbols: str = Query("EUR,USD,NOK", description="Comma-separated currency codes to fetch vs CZK")):
|
||||||
|
"""
|
||||||
|
Fetch exchange rates from UniRate API on the backend and return CZK-per-target rates.
|
||||||
|
- Always requests CZK in addition to requested symbols to compute conversion from USD-base.
|
||||||
|
- Returns a list of {currencyCode, rate} where rate is CZK per 1 unit of the target currency.
|
||||||
|
"""
|
||||||
|
api_key = os.getenv("UNIRATE_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
raise HTTPException(status_code=500, detail="Server is not configured with UNIRATE_API_KEY")
|
||||||
|
|
||||||
|
# Ensure CZK is included for conversion
|
||||||
|
requested = [s.strip().upper() for s in symbols.split(",") if s.strip()]
|
||||||
|
if "CZK" not in requested:
|
||||||
|
requested.append("CZK")
|
||||||
|
query_symbols = ",".join(sorted(set(requested)))
|
||||||
|
|
||||||
|
url = f"https://unirateapi.com/api/rates?api_key={api_key}&symbols={query_symbols}"
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient(timeout=httpx.Timeout(15.0)) as client:
|
||||||
|
resp = await client.get(url)
|
||||||
|
if resp.status_code != httpx.codes.OK:
|
||||||
|
raise HTTPException(status_code=502, detail=f"Upstream UniRate error: HTTP {resp.status_code}")
|
||||||
|
data = resp.json()
|
||||||
|
except httpx.HTTPError as e:
|
||||||
|
raise HTTPException(status_code=502, detail=f"Failed to contact UniRate: {str(e)}")
|
||||||
|
|
||||||
|
# Validate response structure
|
||||||
|
rates = data.get("rates") if isinstance(data, dict) else None
|
||||||
|
base = data.get("base") if isinstance(data, dict) else None
|
||||||
|
if not rates or base != "USD" or "CZK" not in rates:
|
||||||
|
# Prefer upstream message when available
|
||||||
|
detail = data.get("message") if isinstance(data, dict) else None
|
||||||
|
if not detail and isinstance(data, dict):
|
||||||
|
err = data.get("error")
|
||||||
|
if isinstance(err, dict):
|
||||||
|
detail = err.get("info")
|
||||||
|
raise HTTPException(status_code=502, detail=detail or "Invalid response from UniRate API")
|
||||||
|
|
||||||
|
czk_per_usd = rates["CZK"]
|
||||||
|
|
||||||
|
# Build result excluding CZK itself
|
||||||
|
result = []
|
||||||
|
for code in requested:
|
||||||
|
if code == "CZK":
|
||||||
|
continue
|
||||||
|
target_per_usd = rates.get(code)
|
||||||
|
if target_per_usd in (None, 0):
|
||||||
|
# Skip unavailable or invalid
|
||||||
|
continue
|
||||||
|
czk_per_target = czk_per_usd / target_per_usd
|
||||||
|
result.append({"currencyCode": code, "rate": czk_per_target})
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
@@ -21,6 +21,7 @@ from app.api.auth import router as auth_router
|
|||||||
from app.api.csas import router as csas_router
|
from app.api.csas import router as csas_router
|
||||||
from app.api.categories import router as categories_router
|
from app.api.categories import router as categories_router
|
||||||
from app.api.transactions import router as transactions_router
|
from app.api.transactions import router as transactions_router
|
||||||
|
from app.api.exchange_rates import router as exchange_rates_router
|
||||||
from app.services.user_service import auth_backend, current_active_verified_user, fastapi_users, get_oauth_provider, \
|
from app.services.user_service import auth_backend, current_active_verified_user, fastapi_users, get_oauth_provider, \
|
||||||
UserManager, get_jwt_strategy
|
UserManager, get_jwt_strategy
|
||||||
from app.core.security import extract_bearer_token, is_token_revoked, decode_and_verify_jwt
|
from app.core.security import extract_bearer_token, is_token_revoked, decode_and_verify_jwt
|
||||||
@@ -74,6 +75,7 @@ if not os.getenv("PYTEST_RUN_CONFIG"):
|
|||||||
fastApi.include_router(auth_router)
|
fastApi.include_router(auth_router)
|
||||||
fastApi.include_router(categories_router)
|
fastApi.include_router(categories_router)
|
||||||
fastApi.include_router(transactions_router)
|
fastApi.include_router(transactions_router)
|
||||||
|
fastApi.include_router(exchange_rates_router)
|
||||||
|
|
||||||
for h in list(logging.root.handlers):
|
for h in list(logging.root.handlers):
|
||||||
logging.root.removeHandler(h)
|
logging.root.removeHandler(h)
|
||||||
|
|||||||
@@ -90,6 +90,11 @@ spec:
|
|||||||
secretKeyRef:
|
secretKeyRef:
|
||||||
name: prod
|
name: prod
|
||||||
key: CSAS_CLIENT_SECRET
|
key: CSAS_CLIENT_SECRET
|
||||||
|
- name: UNIRATE_API_KEY
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: prod
|
||||||
|
key: UNIRATE_API_KEY
|
||||||
- name: DOMAIN
|
- name: DOMAIN
|
||||||
value: {{ required "Set .Values.domain" .Values.domain | quote }}
|
value: {{ required "Set .Values.domain" .Values.domain | quote }}
|
||||||
- name: DOMAIN_SCHEME
|
- name: DOMAIN_SCHEME
|
||||||
|
|||||||
@@ -26,3 +26,4 @@ stringData:
|
|||||||
SMTP_USE_TLS: {{ .Values.smtp.tls | default false | quote }}
|
SMTP_USE_TLS: {{ .Values.smtp.tls | default false | quote }}
|
||||||
SMTP_USE_SSL: {{ .Values.smtp.ssl | default false | quote }}
|
SMTP_USE_SSL: {{ .Values.smtp.ssl | default false | quote }}
|
||||||
SMTP_FROM: {{ .Values.smtp.from | default "" | quote }}
|
SMTP_FROM: {{ .Values.smtp.from | default "" | quote }}
|
||||||
|
UNIRATE_API_KEY: {{ .Values.unirate.key | default "" | quote }}
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ deployment: ""
|
|||||||
domain: ""
|
domain: ""
|
||||||
domain_scheme: ""
|
domain_scheme: ""
|
||||||
|
|
||||||
|
unirate:
|
||||||
|
key: ""
|
||||||
|
|
||||||
frontend_domain: ""
|
frontend_domain: ""
|
||||||
frontend_domain_scheme: ""
|
frontend_domain_scheme: ""
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import BalanceChart from './BalanceChart';
|
|||||||
import ManualManagement from './ManualManagement';
|
import ManualManagement from './ManualManagement';
|
||||||
import CategoryPieChart from './CategoryPieChart';
|
import CategoryPieChart from './CategoryPieChart';
|
||||||
import MockBankModal, { type MockGenerationOptions } from './MockBankModal';
|
import MockBankModal, { type MockGenerationOptions } from './MockBankModal';
|
||||||
import { BACKEND_URL, VITE_UNIRATE_API_KEY } from '../config';
|
import { BACKEND_URL } from '../config';
|
||||||
|
|
||||||
function formatAmount(n: number) {
|
function formatAmount(n: number) {
|
||||||
return new Intl.NumberFormat(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(n);
|
return new Intl.NumberFormat(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(n);
|
||||||
@@ -45,49 +45,20 @@ function CurrencyRates() {
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
const API_KEY = VITE_UNIRATE_API_KEY;
|
|
||||||
|
|
||||||
// We need to get the CZK rate as well, to use it for conversion
|
|
||||||
const allSymbols = [...TARGET_CURRENCIES, 'CZK'].join(',');
|
|
||||||
|
|
||||||
// We remove the `base` param, as the API seems to force base=USD
|
|
||||||
const UNIRATE_API_URL = `https://unirateapi.com/api/rates?api_key=${API_KEY}&symbols=${allSymbols}`;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(UNIRATE_API_URL);
|
const base = BACKEND_URL.replace(/\/$/, '');
|
||||||
const data: UnirateApiResponse = await res.json();
|
const url = `${base}/exchange-rates?symbols=${TARGET_CURRENCIES.join(',')}`;
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
// --- THIS IS THE NEW, CORRECTED LOGIC ---
|
const res = await fetch(url, {
|
||||||
|
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
||||||
// 1. Check if the 'rates' object exists. If not, it's an error.
|
credentials: 'include',
|
||||||
if (!data.rates) {
|
|
||||||
let errorMessage = data.message || (data.error ? data.error.info : 'Invalid API response');
|
|
||||||
throw new Error(errorMessage || 'Could not load rates');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Check that we got the base currency (USD) and our conversion currency (CZK)
|
|
||||||
if (data.base !== 'USD' || !data.rates.CZK) {
|
|
||||||
throw new Error('API response is missing required data for conversion (USD or CZK)');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Get our main conversion factor
|
|
||||||
const czkPerUsd = data.rates.CZK; // e.g., 23.0
|
|
||||||
|
|
||||||
// 4. Calculate the rates for our target currencies
|
|
||||||
const formattedRates = TARGET_CURRENCIES.map(code => {
|
|
||||||
const targetPerUsd = data.rates[code]; // e.g., 0.9 for EUR
|
|
||||||
|
|
||||||
// This calculates: (CZK per USD) / (TARGET per USD) = CZK per TARGET
|
|
||||||
// e.g. (23.0 CZK / 1 USD) / (0.9 EUR / 1 USD) = 25.55 CZK / 1 EUR
|
|
||||||
const rate = czkPerUsd / targetPerUsd;
|
|
||||||
|
|
||||||
return {
|
|
||||||
currencyCode: code,
|
|
||||||
rate: rate,
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
if (!res.ok) {
|
||||||
setRates(formattedRates);
|
const text = await res.text();
|
||||||
|
throw new Error(text || `Failed to load rates (${res.status})`);
|
||||||
|
}
|
||||||
|
const data: RateData[] = await res.json();
|
||||||
|
setRates(data);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setError(err.message || 'Could not load rates');
|
setError(err.message || 'Could not load rates');
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user