Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

---

## [1.16.17] — 2026-11-02

### Fixed
- **Changing a file's "Read as", then reloading, showed its transactions under both accounts.**
- **When:** Settings → Import → Files in the folder: change a file's **Read as** (savings read as checking, say), then **Reload CSVs**. Also Choose CSV files with a different Read as for a file imported before. The rows stayed doubled, in memory, IndexedDB and `finlens-state.json`, until **Rebuild from your folder**. On the samples: 268 transactions instead of 257 for savings read as checking, 338 for checking read as savings.
- **Cause:** the account id is part of every transaction id (`mkTx()`, CLAUDE.md §10), so the file's rows came back with new ids. Since v1.16.3 an import keeps every loaded transaction a file doesn't replace (`mergeImported()`), so the old copies stayed in the old account. `master` has it in IndexedDB: its import replaces the loaded transactions, so memory and the state file are right, but the store keeps both copies, and a boot from IndexedDB (no folder, or access lapsed) shows them. It also keeps the old account, empty.
- **Fix:** before importing, `earlierCopies()` finds each file's earlier rows under another account. It looks only in accounts the file went into before, and finds each row by its exact id there, worked out from its new id (`IdMigration._rekey()`, the same step the v1.12.0 migration uses). `moveEarlierCopies()` then removes them from memory and IndexedDB; the autosave writes the state file.
- **Edits and plan links go with the row:** they move to its new id, since it's the same bank transaction in another account. The id keeps the bank's time (or the card's post date), so two rows with the same date, description and amount keep their own edits, which a match on those three couldn't promise.
- **An account left empty is removed** (memory and IndexedDB), unless a file in the same import went into it. Its name, balances, card details and place in the order are kept by id, so they come back if the file goes back.
- **A kind that can't read the file** (a card export read as checking) moves nothing: the file isn't imported, Last import says why, and its rows stay where they are.
- **Duplicates left by earlier versions** go at the next reload too. Accounts saved before v1.16.17 record only their last file, so the import log also counts as "the file went into this account".
- **Settings → Import listed one file per account.** A second file read into an account (with Read as, or two exports of one account) wasn't listed, so after moving a file it could disappear from the list, with no way to change it back. Each account now records every CSV imported into it (`files`), and the list shows them all; an account's rows and dates span its files' rows. Rebuild from your folder's check uses the same list (`_rebuildBlockers()`), so a file that wasn't an account's last one still counts as imported before, and Rebuild won't silently drop it if FinLens can no longer read it.

### Changed
- **The "Read as" message** says what the next reload does: the file's transactions move to that account with their edits, and stay where they are if FinLens can't read the file that way. It no longer sends you to Rebuild.
- **Last import** says how many rows moved, and from which account ("11 moved from Bank Savings").

### Docs
- **CLAUDE.md:** §1 version; §4.3 the account's `files`; §4.5 an import moves a file's rows when its account changes; §8 `accountFiles()`, `earlierCopies()`, `moveEarlierCopies()`; §10 Read as changes the ids; §19 `DB.removeTransactions()`, `DB.patchAccount()`; §22 `_rekey()` is also used by imports.

---

## [1.16.16] — 2026-11-02

### Fixed
Expand Down
28 changes: 24 additions & 4 deletions CLAUDE.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> A zero-install, browser-native finance dashboard. Drop in your bank CSVs, get instant clarity on your money.

**Version:** v1.16.16  ·  **Stack:** Vanilla JS, HTML, CSS  ·  **Install:** None
**Version:** v1.16.17  ·  **Stack:** Vanilla JS, HTML, CSS  ·  **Install:** None

---

Expand Down Expand Up @@ -261,4 +261,4 @@ See [CHANGELOG.md](CHANGELOG.md) for the full release history.

---

*FinLens v1.16.16 — Built for one person's finances, designed to be useful for anyone's.*
*FinLens v1.16.17 — Built for one person's finances, designed to be useful for anyone's.*
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@
<div class="folder-banner-wrap" id="folderBanner" hidden></div>
<div class="content" id="content"></div>
</main>
<footer class="appfoot">v1.16.16 · FinLens</footer>
<footer class="appfoot">v1.16.17 · FinLens</footer>

<!-- side drawer: transaction detail, bill and plan forms (ui.js openDrawer) -->
<div class="drawer-scrim" id="drawerScrim" onclick="closeDrawer()" hidden></div>
Expand Down
125 changes: 123 additions & 2 deletions js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,15 @@ function defaultAccountName(a) {
return file;
}

// The CSV files an account's transactions came from (v1.16.17): its `files`, kept by
// importParsedCsv() and by a file moving to another account (moveEarlierCopies()). An
// account saved before v1.16.17 has no list: its sourceFile, the last file imported into it.
function accountFiles(a) {
if (!a) return [];
if (Array.isArray(a.files)) return a.files.slice();
return a.sourceFile && a.sourceFile !== 'manual' ? [a.sourceFile] : [];
}

// Reads one CSV file and turns its rows into transactions. It changes nothing: no State,
// browser storage or IndexedDB writes (v1.16.2). loadFiles() reads every file this way
// first and imports (importParsedCsv()) only when at least one transaction was read.
Expand Down Expand Up @@ -786,8 +795,15 @@ async function importParsedCsv(p) {
State.accounts[accountId] = State.accounts[accountId] || {
id: accountId, name: accountName, kind, balance: null, limit: null, sourceFile: file.name
};
State.accounts[accountId].sourceFile = file.name;
State.accounts[accountId].name = accountName;
const acct = State.accounts[accountId];
// Every CSV imported into the account, not only the last one (sourceFile): Settings →
// Import lists them all, and a file re-read under another account ("Read as") takes its
// rows out of this one (earlierCopies(), v1.16.17). An account saved before has none
// and starts from its sourceFile.
const files = accountFiles(acct);
acct.files = files.includes(file.name) ? files : files.concat(file.name);
acct.sourceFile = file.name;
acct.name = accountName;

// Persist to IndexedDB (if DB is ready)
let importResult = null;
Expand All @@ -799,6 +815,7 @@ async function importParsedCsv(p) {
kind,
schemaId: schema.id,
sourceFile: file.name,
files: acct.files,
displayOrder: State.accountOrder.indexOf(accountId),
manualBalance: State.manualBalances[accountId] != null ? Number(State.manualBalances[accountId]) : null,
creditLimit: State.manualBalances[accountId+'_limit'] != null ? Number(State.manualBalances[accountId+'_limit']) : null,
Expand Down Expand Up @@ -906,11 +923,16 @@ async function loadFiles(fileList, assignments, opts = {}) {
if (typeof updateFolderBanner === 'function') updateFolderBanner();
}
State.fileMeta = [];
// A file read as another kind than before ("Read as") gives its rows under another
// account, so with other ids. Its earlier rows are found before anything is written
// (v1.16.17), then leave their old account below.
const moves = await earlierCopies(read);
const fresh = [];
for (const p of read) {
if (!p.schema || p.problem) continue; // a file that gave no transactions creates no account
fresh.push(...await importParsedCsv(p));
}
if (moves.size) await moveEarlierCopies(moves, read, fresh);
// The files' transactions join the loaded ones; none of the others is dropped (v1.16.3).
State.transactions = await mergeImported(State.transactions, fresh);
// Edits still saved under a transaction's id from before v1.12.0 (e.g. kept while the
Expand Down Expand Up @@ -990,6 +1012,105 @@ function undoTransferDetection(t, row) {
t.categories = ov.categories && ov.categories.length ? [...ov.categories] : (ov.category ? [ov.category] : [...cats]);
}

// A file's earlier rows under another account (v1.16.17, for loadFiles(), before anything is
// written). The account id is part of every transaction id (mkTx(), CLAUDE.md §10), so a file
// read as another kind than before ("Read as" savings → checking, say) gives the same rows
// with new ids. Up to v1.16.16 the merge kept the old ones too, as it keeps every loaded
// transaction a file doesn't replace (mergeImported()): the file's rows showed under both
// accounts, in memory, IndexedDB and finlens-state.json, until Rebuild from your folder.
// - The accounts that held the file: those listing it in `files` (accountFiles()); an account
// saved before v1.16.17 has no list, so its sourceFile or an import-log entry counts.
// - The rows: each of the file's transactions gives the id it had in such an account, worked
// out from its own id (IdMigration._rekey(): the hash takes the account id last, CLAUDE.md
// §22). The id keeps the bank's time or the card's post date, so two rows with the same
// date, description and amount stay apart. A loaded row with that id is the file's earlier
// copy, unless a file in this import gives that id too: then it stays, and the merge
// replaces it as usual.
// A file that gave no transactions (a kind that can't read it) moves nothing.
// Returns Map(old id → { id: the new id, from: the old account, file }).
async function earlierCopies(read) {
const moves = new Map();
const loaded = new Set(State.transactions.map(t => t.id));
const files = read.filter(p => p.schema && !p.problem && p.txs.length);
if (!loaded.size || !files.length) return moves;
const fresh = new Set(files.flatMap(p => p.txs.map(t => t.id)));
const accts = Object.values(State.accounts);
let log = [];
if (accts.some(a => !Array.isArray(a.files)) && typeof DB !== 'undefined' && DB._db) {
try { log = await DB.getImportLog(); } catch (e) { log = []; }
}
const held = (a, name) => Array.isArray(a.files) ? a.files.includes(name)
: a.sourceFile === name || log.some(e => e.filename === name && e.accountId === a.id);
for (const p of files) {
const from = accts.filter(a => a.id !== p.accountId && held(a, p.name)).map(a => a.id);
for (const t of p.txs) {
for (const a of from) {
const old = IdMigration._rekey(t, p.accountId, a);
if (old && loaded.has(old) && !fresh.has(old)) moves.set(old, { id: t.id, from: a, file: p.name });
}
}
}
return moves;
}

// Takes a file's earlier rows out of their old account (v1.16.17, for loadFiles(), after the
// files are imported). `moves` is earlierCopies()'s result; `fresh` the files' transactions.
// - Edits go with the row: description, category, type, flags, notes and plan link move to
// the new id, since it's the same bank transaction in another account. If the new id has
// an edit already (only a copy left by an earlier version can), its fields win.
// - Memory: the old rows leave State.transactions. The files' rows that got an edit are
// rebuilt with it (in `fresh`, in place).
// - Accounts: the file leaves the old account's `files`. An account left with no transaction,
// that no file of this import went into, is removed (memory and IndexedDB). Its name,
// balances, card details and place in the order stay, by id, for when a file goes back.
// - IndexedDB: the old rows are deleted from the transactions store.
// loadFiles()'s autosave writes it all to finlens-state.json. State.fileMeta notes what moved
// (the Last import card shows it).
async function moveEarlierCopies(moves, read, fresh) {
const ovs = State.txOverrides;
const edited = new Set();
for (const [old, m] of moves) {
if (!ovs[old]) continue;
ovs[m.id] = { ...ovs[old], ...(ovs[m.id] || {}) };
delete ovs[old];
edited.add(m.id);
}
if (edited.size) {
lsSet('txOverrides', ovs);
fresh.forEach((t, i) => { if (edited.has(t.id)) fresh[i] = reconstituteTx({ ...t, description: t._originalDesc }, ovs[t.id]); });
}
State.transactions = State.transactions.filter(t => !moves.has(t.id));
// per old account: the files that left it; per file: how many rows moved, and from where
const left = new Map(), perFile = new Map();
for (const m of moves.values()) {
if (!left.has(m.from)) left.set(m.from, new Set());
left.get(m.from).add(m.file);
const f = perFile.get(m.file) || { count: 0, from: new Set() };
f.count++; f.from.add(m.from); perFile.set(m.file, f);
}
const names = new Map([...left.keys()].map(id => [id, typeof acctName === 'function' ? acctName(id) : id]));
const into = new Set(read.filter(p => p.schema && !p.problem).map(p => p.accountId));
const db = typeof DB !== 'undefined' && DB._db;
if (db) await DB.removeTransactions([...moves].map(([id, m]) => ({ id, account: m.from })));
for (const [id, gone] of left) {
const a = State.accounts[id];
if (!a) continue;
a.files = accountFiles(a).filter(f => !gone.has(f));
if (!into.has(id) && !State.transactions.some(t => t.account === id)) {
delete State.accounts[id];
if (db) await DB.patchAccount(id, null);
continue;
}
if (gone.has(a.sourceFile) && a.files.length) a.sourceFile = a.files[a.files.length - 1];
if (db) await DB.patchAccount(id, { files: a.files, sourceFile: a.sourceFile });
}
for (const fm of State.fileMeta) {
const f = perFile.get(fm.name);
if (f) fm.moved = { count: f.count, from: [...f.from].map(id => names.get(id)) };
}
console.log(`[FinLens] Import: ${moves.size} transaction${moves.size === 1 ? '' : 's'} moved to the account ${perFile.size === 1 ? 'its file is' : 'their files are'} read as now (${edited.size} with edits)`);
}

function detectTransfers() {
// Build keyword regex from user-editable list (falls back to defaults if empty)
const kws = (State.transferKeywords && State.transferKeywords.length)
Expand Down
49 changes: 49 additions & 0 deletions js/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,55 @@ const DB = {
return { inserted, skipped, removed, dateRangeMin: fileMinDate, dateRangeMax: fileMaxDate, gapWarning };
},

/**
* Deletes the rows a file gave under its old account once it's read as another kind
* (v1.16.17, moveEarlierCopies() in core.js). `rows` is [{ id, account }]; a row the store
* doesn't hold is skipped (after a restore from finlens-state.json it may hold none).
* Each account's txCount goes down by the rows deleted from it; its date range is left as
* it is. Resolves the number of rows deleted.
*/
async removeTransactions(rows) {
if (!this._db || !rows.length) return 0;
let have;
try { have = new Set(await this._req(this._getStore('transactions').getAllKeys())); }
catch (e) { console.warn('DB.removeTransactions:', e); return 0; }
const del = rows.filter(r => have.has(r.id));
if (!del.length) return 0;
const ok = await new Promise((res) => {
const t = this._db.transaction('transactions', 'readwrite');
const store = t.objectStore('transactions');
del.forEach(r => store.delete(r.id));
t.oncomplete = () => res(true);
t.onerror = t.onabort = () => res(false);
});
if (!ok) { console.warn('DB.removeTransactions: the delete failed'); return 0; }
const per = new Map();
del.forEach(r => per.set(r.account, (per.get(r.account) || 0) + 1));
for (const [id, n] of per) {
try {
const store = this._db.transaction('accounts', 'readwrite').objectStore('accounts');
const a = await this._req(store.get(id));
if (a) { a.txCount = Math.max(0, (a.txCount || 0) - n); await this._req(store.put(a)); }
} catch (e) { console.warn('DB.removeTransactions: account count', id, e); }
}
return del.length;
},

/**
* Changes fields of a stored account record, or deletes it when `patch` is null
* (v1.16.17, moveEarlierCopies()). A record the store doesn't have isn't created: an
* import (upsertAccount()) writes whole records.
*/
async patchAccount(id, patch) {
if (!this._db) return;
try {
const store = this._db.transaction('accounts', 'readwrite').objectStore('accounts');
if (patch === null) { await this._req(store.delete(id)); return; }
const a = await this._req(store.get(id));
if (a) await this._req(store.put({ ...a, ...patch }));
} catch (e) { console.warn('DB.patchAccount:', id, e); }
},

async _updateAccountCoverage(accountId, fileMin, fileMax, newlyInserted) {
if (!this._db) return;
try {
Expand Down
4 changes: 3 additions & 1 deletion js/views-cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ const DataCleanup = {
// Other CSVs in a format it doesn't read are skipped, as every import skips them.
// `r` is loadFiles()'s result, before anything is cleared.
_rebuildBlockers(r) {
const imported = new Set(Object.values(State.accounts).map(a => a.sourceFile).filter(Boolean));
// Every file of every account (accountFiles(), v1.16.17), not only each one's last: a file
// read into an account that another file fed last is imported too.
const imported = new Set(Object.values(State.accounts).flatMap(a => accountFiles(a)));
return r.files.filter(f => f.problem === 'unreadable' || f.problem === 'no-rows'
|| (f.problem === 'unknown-format' && imported.has(f.name)));
},
Expand Down
Loading