Add a supplementary rate feed for currencies the ECB lacks (e.g. ALL)

The server also fetches ExchangeRate-API's free EUR-based endpoint
(RATES_EXTRA_URL, empty = off), stored apart from ECB rates; ECB always wins
for a currency both publish. /api/rates returns sources for supplementary
rates, expenses record them as xr:<date>, and the editor shows the required
attribution link. Snapshots older than a week before the requested date are
not used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gVKYyo9BHLHPW4qr4x
This commit is contained in:
agent 2026-11-06 18:13:31 +00:00
parent 6b0c80bb50
commit 62c19192a7
13 changed files with 160 additions and 27 deletions

View file

@ -19,6 +19,12 @@ export interface PushSub {
deviceId: string;
}
export interface RatesDay {
date: string;
rates: Record<string, string>;
sources: Record<string, string>;
}
export class Store {
readonly db: DatabaseSync;
@ -65,6 +71,13 @@ export class Store {
date TEXT PRIMARY KEY,
json TEXT NOT NULL
);
-- Supplementary rates (see rates.ts), kept apart so ECB refreshes never
-- overwrite them and ECB always wins for currencies both publish.
CREATE TABLE IF NOT EXISTS rates_extra (
date TEXT PRIMARY KEY,
source TEXT NOT NULL,
json TEXT NOT NULL
);
`);
}
@ -160,12 +173,37 @@ export class Store {
this.db.prepare('INSERT OR REPLACE INTO rates (date, json) VALUES (?, ?)').run(date, JSON.stringify(rates));
}
/** Rates for the latest published date on or before `date`. */
ratesOn(date: string): { date: string; rates: Record<string, string> } | undefined {
saveExtraRates(date: string, source: string, rates: Record<string, string>): void {
this.db
.prepare('INSERT OR REPLACE INTO rates_extra (date, source, json) VALUES (?, ?, ?)')
.run(date, source, JSON.stringify(rates));
}
/**
* ECB rates for the latest published date on or before `date`, plus
* supplementary rates for currencies the ECB doesn't publish. `sources`
* names the origin of each supplementary rate ("<source>:<date>").
* Supplementary rates older than a week before `date` are not used.
*/
ratesOn(date: string): RatesDay | undefined {
const r = this.db.prepare('SELECT date, json FROM rates WHERE date <= ? ORDER BY date DESC LIMIT 1').get(date) as
| { date: string; json: string }
| undefined;
return r && { date: r.date, rates: JSON.parse(r.json) };
if (!r) return undefined;
const rates: Record<string, string> = JSON.parse(r.json);
const sources: Record<string, string> = {};
const weekBefore = new Date(Date.parse(date) - 7 * 86400_000).toISOString().slice(0, 10);
const x = this.db
.prepare('SELECT date, source, json FROM rates_extra WHERE date <= ? AND date >= ? ORDER BY date DESC LIMIT 1')
.get(date, weekBefore) as { date: string; source: string; json: string } | undefined;
if (x) {
for (const [code, rate] of Object.entries(JSON.parse(x.json) as Record<string, string>)) {
if (code in rates) continue;
rates[code] = rate;
sources[code] = `${x.source}:${x.date}`;
}
}
return { date: r.date, rates, sources };
}
latestRatesDate(): string | undefined {