Skip to content

A local-first personal finance app

[AI] Fix bank sync showing the wrong bank for a second connection (#8625)

Junyuan Zheng53dae19f70d4
diff --git a/packages/loot-core/src/server/accounts/link.test.ts b/packages/loot-core/src/server/accounts/link.test.ts
new file mode 100644
index 0000000000..4d5667ba81
--- /dev/null
+++ b/packages/loot-core/src/server/accounts/link.test.ts
@@ -0,0 +1,89 @@
+import * as db from '#server/db';
+import { loadMappings } from '#server/db/mappings';
+
+import { findOrCreateBank } from './link';
+
+beforeEach(async () => {
+ await global.emptyDatabase()();
+ await loadMappings();
+});
+
+async function getBanks() {
+ return await db.all<db.DbBank>(
+ 'SELECT id, bank_id, name FROM banks ORDER BY name',
+ );
+}
+
+describe('findOrCreateBank', () => {
+ it('creates a bank when none exists for the connection', async () => {
+ const bank = await findOrCreateBank({ name: 'Robinhood' }, 'robinhood.com');
+
+ expect(await getBanks()).toEqual([
+ expect.objectContaining({
+ id: bank.id,
+ bank_id: 'robinhood.com',
+ name: 'Robinhood',
+ }),
+ ]);
+ });
+
+ it('reuses the existing bank when the same connection is linked again', async () => {
+ const first = await findOrCreateBank(
+ { name: 'Robinhood' },
+ 'robinhood.com',
+ );
+ const second = await findOrCreateBank(
+ { name: 'Robinhood' },
+ 'robinhood.com',
+ );
+
+ expect(second.id).toBe(first.id);
+ expect(await getBanks()).toHaveLength(1);
+ });
+
+ it('keeps connections apart when they share an id but report different institutions', async () => {
+ const first = await findOrCreateBank(
+ { name: 'Robinhood (Person A)' },
+ 'robinhood.com',
+ );
+ const second = await findOrCreateBank(
+ { name: 'Robinhood (Person B)' },
+ 'robinhood.com',
+ );
+
+ expect(second.id).not.toBe(first.id);
+ expect(await getBanks()).toEqual([
+ expect.objectContaining({
+ id: first.id,
+ bank_id: 'robinhood.com',
+ name: 'Robinhood (Person A)',
+ }),
+ expect.objectContaining({
+ id: second.id,
+ bank_id: 'robinhood.com',
+ name: 'Robinhood (Person B)',
+ }),
+ ]);
+ });
+
+ it('reuses the existing bank when the provider reports no institution', async () => {
+ const first = await findOrCreateBank(null, 'robinhood.com');
+ const second = await findOrCreateBank({ name: null }, 'robinhood.com');
+
+ expect(second.id).toBe(first.id);
+ expect(await getBanks()).toEqual([
+ expect.objectContaining({ bank_id: 'robinhood.com', name: null }),
+ ]);
+ });
+
+ it('accepts an institution reported as a plain string', async () => {
+ const first = await findOrCreateBank('Robinhood', 'robinhood.com');
+ const second = await findOrCreateBank(
+ { name: 'Robinhood' },
+ 'robinhood.com',
+ );
+
+ expect(second.id).toBe(first.id);
+ expect(await getBanks()).toHaveLength(1);
+ });
+});
diff --git a/packages/loot-core/src/server/accounts/link.ts b/packages/loot-core/src/server/accounts/link.ts
index 202ec411ba..19f9163e58 100644
--- a/packages/loot-core/src/server/accounts/link.ts
+++ b/packages/loot-core/src/server/accounts/link.ts
@@ -3,10 +3,37 @@ import { v4 as uuidv4 } from 'uuid';
import * as db from '#server/db';
-export async function findOrCreateBank(institution, requisitionId) {
+type Institution = string | { name?: string | null };
+
+function getInstitutionName(institution: Institution | null | undefined) {
+ if (typeof institution === 'string') {
+ return institution;
+ }
+ return institution?.name ?? null;
+}
+
+/**
+ * Finds the bank record for a bank-sync connection, creating one if needed.
+ *
+ * The provider identifier alone is not enough to tell connections apart: some
+ * providers (SimpleFIN in particular) report the institution's domain as that
+ * identifier. Two separate logins at the same bank would then share a single
+ * bank record, and accounts linked through the second connection would be
+ * displayed under the first connection's name. Matching on the institution
+ * name as well keeps those connections apart, while still reusing the record
+ * when the same connection is linked again.
+ */
+export async function findOrCreateBank(
+ institution: Institution | null | undefined,
+ bankId: db.DbBank['bank_id'] | undefined,
+): Promise<Pick<db.DbBank, 'id' | 'bank_id'>> {
+ const name = getInstitutionName(institution);
+
+ // `IS` instead of `=` so records with a null name (providers that don't
+ // report an institution) still match.
const bank = await db.first<Pick<db.DbBank, 'id' | 'bank_id'>>(
- 'SELECT id, bank_id FROM banks WHERE bank_id = ?',
- [requisitionId],
+ 'SELECT id, bank_id FROM banks WHERE bank_id = ? AND name IS ?',
+ [bankId, name],
);
if (bank) {
@@ -15,8 +42,8 @@ export async function findOrCreateBank(institution, requisitionId) {
const bankData = {
id: uuidv4(),
- bank_id: requisitionId,
- name: institution.name,
+ bank_id: bankId,
+ name,
};
await db.insertWithUUID('banks', bankData);
diff --git a/upcoming-release-notes/fix-bank-sync-duplicate-institution-name.md b/upcoming-release-notes/fix-bank-sync-duplicate-institution-name.md
new file mode 100644
index 0000000000..98f2077643
--- /dev/null
+++ b/upcoming-release-notes/fix-bank-sync-duplicate-institution-name.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [junyuanz1]
+---
+
+Bank sync: show the correct bank name when two accounts at the same bank are connected through separate logins