- Auth: pessimistic locking on login tokens and refresh token rotation, backup code attempt counter, rate limiting verification - Schema: unique constraints on business numbers, FK relations, unsigned/signed alignment, attendance duplicate prevention - Invoices/PDFs: DOMPurify sanitization, bounded queries in stats and alerts, VAT rounding, Puppeteer error handling - Orders/Offers: transactional parent+child creation, Zod NaN refinement, status enums, uniqueness checks - Projects/Files: path traversal protection, streamed uploads, permission guards, query param validation - Attendance/HR: duplicate checks, ownership validation, GPS restrictions, trip distance validation - Frontend: modal lock reference counting, XSS escaping in print HTML, ref mutation fixes, accessibility attributes Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
/**
|
|
* Czech National Bank (ČNB) exchange rate service.
|
|
* Fetches daily rates and caches them.
|
|
* API: https://api.cnb.cz/cnbapi/exrates/daily
|
|
*/
|
|
|
|
interface CnbRate {
|
|
currencyCode: string;
|
|
rate: number;
|
|
amount: number;
|
|
}
|
|
|
|
const rateCache: Record<string, Record<string, number>> = {};
|
|
|
|
async function fetchRatesForDate(
|
|
date?: string,
|
|
): Promise<Record<string, number>> {
|
|
const key = date || "today";
|
|
if (rateCache[key]) return rateCache[key];
|
|
|
|
try {
|
|
let url = "https://api.cnb.cz/cnbapi/exrates/daily?lang=EN";
|
|
if (date) url += `&date=${date}`;
|
|
|
|
const response = await fetch(url);
|
|
if (!response.ok) throw new Error(`CNB API: ${response.status}`);
|
|
|
|
const data = (await response.json()) as { rates: CnbRate[] };
|
|
const rates: Record<string, number> = { CZK: 1 };
|
|
|
|
for (const r of data.rates) {
|
|
rates[r.currencyCode] = r.rate / r.amount;
|
|
}
|
|
|
|
rateCache[key] = rates;
|
|
return rates;
|
|
} catch (err) {
|
|
console.error("Failed to fetch CNB exchange rates:", err);
|
|
if (rateCache["today"]) return rateCache["today"];
|
|
throw new Error("Nepodařilo se získat aktuální kurzy z ČNB");
|
|
}
|
|
}
|
|
|
|
/** Convert an amount from a given currency to CZK using CNB rates */
|
|
export async function toCzk(
|
|
amount: number,
|
|
currency: string,
|
|
date?: string,
|
|
): Promise<number> {
|
|
if (currency === "CZK") return amount;
|
|
const rates = await fetchRatesForDate(date);
|
|
const rate = rates[currency];
|
|
if (!rate) throw new Error(`Neznámá měna: ${currency}`);
|
|
return Math.round(amount * rate * 100) / 100;
|
|
}
|
|
|
|
/** Get CNB rate for a currency (CZK per 1 unit), optionally for a specific date */
|
|
export async function getRate(
|
|
currency: string,
|
|
date?: string,
|
|
): Promise<number> {
|
|
if (currency === "CZK") return 1;
|
|
const rates = await fetchRatesForDate(date);
|
|
const rate = rates[currency];
|
|
if (!rate) throw new Error(`Neznámá měna: ${currency}`);
|
|
return rate;
|
|
}
|