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:
parent
6b0c80bb50
commit
62c19192a7
13 changed files with 160 additions and 27 deletions
|
|
@ -9,7 +9,7 @@ import { createApp } from '../src/app.ts';
|
|||
import { loadConfig } from '../src/config.ts';
|
||||
import { Store } from '../src/db.ts';
|
||||
import { encryptPayload, isAllowedEndpoint, loadVapidKeys } from '../src/push.ts';
|
||||
import { parseEcbXml } from '../src/rates.ts';
|
||||
import { parseEcbXml, parseExtraRates } from '../src/rates.ts';
|
||||
|
||||
const b64u = (b: Buffer) => b.toString('base64url');
|
||||
const newGroup = () => {
|
||||
|
|
@ -35,6 +35,7 @@ before(async () => {
|
|||
});
|
||||
store = new Store(cfg.dataDir);
|
||||
store.saveRates('2026-10-08', { EUR: '1', CZK: '24.4', USD: '1.12' });
|
||||
store.saveExtraRates('2026-10-09', 'xr', { EUR: '1', CZK: '24.9', ALL: '98.38' });
|
||||
server = createServer(createApp(cfg, store));
|
||||
await new Promise<void>((r) => server.listen(0, '127.0.0.1', r));
|
||||
const addr = server.address() as { port: number };
|
||||
|
|
@ -142,6 +143,27 @@ describe('rates and static', () => {
|
|||
assert.equal((await api('GET', '/api/rates?date=2020-01-01')).status, 404);
|
||||
});
|
||||
|
||||
it('fills in currencies the ECB lacks from the extra source, ECB first', async () => {
|
||||
const r = await api('GET', '/api/rates?date=2026-10-11');
|
||||
assert.equal(r.data.rates.ALL, '98.38');
|
||||
assert.equal(r.data.rates.CZK, '24.4');
|
||||
assert.deepEqual(r.data.sources, { ALL: 'xr:2026-10-09' });
|
||||
// A snapshot more than a week old is not used; ECB rates still are.
|
||||
const late = await api('GET', '/api/rates?date=2026-10-20');
|
||||
assert.equal(late.data.rates.ALL, undefined);
|
||||
assert.equal(late.data.rates.CZK, '24.4');
|
||||
assert.equal((await api('GET', '/api/rates?date=2026-02-30x')).status, 400);
|
||||
assert.equal((await api('GET', '/api/rates?date=2026-13-45')).status, 400);
|
||||
});
|
||||
|
||||
it('parses the extra rates feed', () => {
|
||||
const t = Date.UTC(2026, 10, 6, 0, 2) / 1000;
|
||||
const p = parseExtraRates({ result: 'success', base_code: 'EUR', time_last_update_unix: t, rates: { EUR: 1, ALL: 98.38, BAD: -1, XYZ: 1e-9, lower: 2 } });
|
||||
assert.deepEqual(p, { date: '2026-11-06', rates: { EUR: '1', ALL: '98.38' } });
|
||||
assert.throws(() => parseExtraRates({ result: 'error' }));
|
||||
assert.throws(() => parseExtraRates({ result: 'success', base_code: 'USD', time_last_update_unix: t, rates: { EUR: 1 } }));
|
||||
});
|
||||
|
||||
it('parses ECB XML', () => {
|
||||
const xml = '<Cube><Cube time="2026-10-09"><Cube currency="USD" rate="1.1206"/><Cube currency="CZK" rate="24.369"/></Cube></Cube>';
|
||||
assert.deepEqual(parseEcbXml(xml).get('2026-10-09'), { EUR: '1', USD: '1.1206', CZK: '24.369' });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue