diff --git a/backend/src/modules/cron/bankstatementsync.service.ts b/backend/src/modules/cron/bankstatementsync.service.ts index 71aa17d..cded6be 100644 --- a/backend/src/modules/cron/bankstatementsync.service.ts +++ b/backend/src/modules/cron/bankstatementsync.service.ts @@ -52,6 +52,11 @@ const normalizeDate = (val: any) => { return isNaN(d.getTime()) ? null : d } +export const getBankAccountOwnerName = (account: any) => { + const ownerName = typeof account?.owner_name === "string" ? account.owner_name.trim() : "" + return ownerName || null +} + export function bankStatementService(server: FastifyInstance) { let accessToken: string | null = null @@ -110,6 +115,23 @@ export function bankStatementService(server: FastifyInstance) { } } + // ----------------------------------------------- + // ✔ Kontoinhaber laden + // ----------------------------------------------- + const getAccountData = async (accountId: string): Promise => { + if (useCentralBanking) return await centralServicesClient.getBankingAccount(accountId) + const {data} = await axios.get( + `${secrets.GOCARDLESS_BASE_URL}/accounts/${accountId}`, + { + headers: { + Authorization: `Bearer ${accessToken}`, + Accept: "application/json", + }, + } + ) + return data + } + // ----------------------------------------------- // ✔ Transaktionen laden // ----------------------------------------------- @@ -162,6 +184,22 @@ export function bankStatementService(server: FastifyInstance) { for (const account of accounts) { try { + // --------------------------- + // 0. KONTOINHABER SYNC + // --------------------------- + try { + const accountData = await getAccountData(account.accountId) + const ownerName = getBankAccountOwnerName(accountData) + if (ownerName && ownerName !== account.ownerName) { + await server.db + .update(bankaccounts) + .set({ownerName}) + .where(eq(bankaccounts.id, account.id)) + } + } catch (error: any) { + server.log.warn({err: error, accountId: account.accountId}, "Kontoinhaber konnte nicht synchronisiert werden") + } + // --------------------------- // 1. BALANCE SYNC // --------------------------- diff --git a/backend/tests/bankStatementSyncOwnerName.test.ts b/backend/tests/bankStatementSyncOwnerName.test.ts new file mode 100644 index 0000000..efc0e83 --- /dev/null +++ b/backend/tests/bankStatementSyncOwnerName.test.ts @@ -0,0 +1,17 @@ +import test from "node:test" +import assert from "node:assert/strict" + +import { getBankAccountOwnerName } from "../src/modules/cron/bankstatementsync.service" + +test("reads the GoCardless owner_name for a bank account", () => { + assert.equal( + getBankAccountOwnerName({ owner_name: "Neue Firma GmbH" }), + "Neue Firma GmbH" + ) +}) + +test("does not overwrite the stored owner with an empty provider value", () => { + assert.equal(getBankAccountOwnerName({ owner_name: "" }), null) + assert.equal(getBankAccountOwnerName({}), null) + assert.equal(getBankAccountOwnerName(null), null) +})