Files
token-circles/shared/bankFormats.ts
Komediruzecki ce0561e5f3 feat(imports): an import log keeps the bank formats its session read
Migration 0038 adds import_logs.bank_formats (TEXT, nullable, no default): the adapter ids of the
statements an import read, joined by commas, in a column of their own because both runtimes cut
details at 8000 characters. Every writer goes through storedBankFormats (shared/bankFormats.ts),
which keeps the ids the import reads, each once, and drops any other.

POST /api/import-logs stores them in both runtimes; the Import page sends the recognised
statements' formats from the Bank Imports tab and none from the other tabs. /api/v1/import names
the adapter that read the file. The email import and the Google Sheet sync read plain tables and
write null. Local-first lists a log from before the column with bank_formats null, as the Worker
answers. Nothing reads the column yet.
2026-10-11 22:03:03 +02:00

53 lines
2.1 KiB
TypeScript

/**
* The bank formats an import read, as `import_logs.bank_formats` holds them (migration 0038).
*
* The Bank Imports tab and the API's import recognise a statement by its bank's format
* (shared/bankImport/registry.ts). Which formats a profile's imports came in is a hint to where
* the person lives, for the official figures their numbers are set beside. It is kept in a column
* of its own, the adapter ids joined by commas, because both runtimes cut an import log's
* `details` at 8000 characters, and a cut leaves JSON that no longer parses.
*
* Every writer reads what it is given through `storedBankFormats`, so an id the app does not read
* (a later build's adapter, or a client's guess) never reaches the column.
*/
import type { BankId } from './bankImport/types';
/** Every format the import reads, by adapter id, in the registry's order. */
export const BANK_FORMAT_IDS = [
'revolut',
'erste',
'pbz',
'n26',
'wise',
'ing',
'sparkasse',
'dkb',
'ynab',
] as const satisfies readonly BankId[];
/** Whether `value` is the id of a format the import reads, exactly as stored. */
export function isBankFormat(value: unknown): value is BankId {
return typeof value === 'string' && (BANK_FORMAT_IDS as readonly string[]).includes(value);
}
/**
* The formats a writer was given, as the column stores them: the ones the app reads, lowercased,
* each once, in the order given, joined by commas. Null for none. Takes a list or text with the
* ids separated by commas; anything else is none.
*/
export function storedBankFormats(raw: unknown): string | null {
const given = Array.isArray(raw) ? raw : typeof raw === 'string' ? raw.split(',') : [];
const known: BankId[] = [];
for (const entry of given) {
const id = typeof entry === 'string' ? entry.trim().toLowerCase() : '';
if (isBankFormat(id) && !known.includes(id)) known.push(id);
}
return known.length > 0 ? known.join(',') : null;
}
/** The formats a stored value names, the ones the app reads only. */
export function bankFormatsOf(stored: unknown): BankId[] {
const value = storedBankFormats(stored);
return value === null ? [] : (value.split(',') as BankId[]);
}