A local-first personal finance app
:sparkles: (bank-sync) meaningful default balance calculation (#144)
Matiss Janis Aboltinsaf9713d5b165
diff --git a/src/app-nordigen/app-nordigen.js b/src/app-nordigen/app-nordigen.jsindex 10ab8adf60..ca278afb76 100644--- a/src/app-nordigen/app-nordigen.js+++ b/src/app-nordigen/app-nordigen.js@@ -74,26 +74,14 @@ app.post(app.post('/get-banks',handleError(async (req, res) => {+ let { country } = req.body;++ await nordigenService.setToken();+ const data = await nordigenService.getInstitutions(country);+res.send({status: 'ok',- // TODO: enable all banks- data: [- {- id: 'ING_PL_INGBPLPW',- name: 'ING PL',- country: ''- },- {- id: 'MBANK_RETAIL_BREXPLPW',- name: 'MBANK',- country: ''- },- {- id: 'SANDBOXFINANCE_SFIN0000',- name: 'DEMO - TEST',- country: ''- }- ]+ data});}));diff --git a/src/app-nordigen/banks/integration-bank.js b/src/app-nordigen/banks/integration-bank.jsindex c421082427..19b3ac8409 100644--- a/src/app-nordigen/banks/integration-bank.js+++ b/src/app-nordigen/banks/integration-bank.js@@ -1,4 +1,14 @@-import { sortByBookingDate } from '../utils.js';+import { sortByBookingDate, amountToInteger, printIban } from '../utils.js';++const SORTED_BALANCE_TYPE_LIST = [+ 'closingBooked',+ 'expected',+ 'forwardAvailable',+ 'interimAvailable',+ 'interimBooked',+ 'nonInvoiced',+ 'openingBooked'+];/** @type {import('./bank.interface.js').IBank} */export default {@@ -13,7 +23,7 @@ export default {account_id: account.id,institution: account.institution,mask: (account?.iban || '0000').slice(-4),- name: `integration-${account.institution_id}`,+ name: [account.name, printIban(account)].filter(Boolean).join(' '),official_name: `integration-${account.institution_id}`,type: 'checking'};@@ -33,6 +43,16 @@ export default {top10SortedTransactions: JSON.stringify(sortedTransactions.slice(0, 10))});- return 0;++ const currentBalance = balances+ .filter((item) => SORTED_BALANCE_TYPE_LIST.includes(item.balanceType))+ .sort(+ (a, b) =>+ SORTED_BALANCE_TYPE_LIST.indexOf(a.balanceType) -+ SORTED_BALANCE_TYPE_LIST.indexOf(b.balanceType)+ )[0];+ return sortedTransactions.reduce((total, trans) => {+ return total - amountToInteger(trans.transactionAmount.amount);+ }, amountToInteger(currentBalance?.balanceAmount?.amount || 0));}};diff --git a/src/app-nordigen/banks/tests/integration-bank.spec.js b/src/app-nordigen/banks/tests/integration-bank.spec.jsindex 6506217d0f..0df6b51012 100644--- a/src/app-nordigen/banks/tests/integration-bank.spec.js+++ b/src/app-nordigen/banks/tests/integration-bank.spec.js@@ -21,7 +21,7 @@ describe('IntegrationBank', () => {account_id: account.id,institution: mockInstitution,mask: '4321',- name: 'integration-SANDBOXFINANCE_SFIN0000',+ name: 'account-example-one (XXX 4321)',official_name: 'integration-SANDBOXFINANCE_SFIN0000',type: 'checking'});@@ -36,7 +36,7 @@ describe('IntegrationBank', () => {account_id: account.id,institution: mockInstitution,mask: '0000',- name: 'integration-SANDBOXFINANCE_SFIN0000',+ name: 'account-example-one',official_name: 'integration-SANDBOXFINANCE_SFIN0000',type: 'checking'});@@ -133,12 +133,12 @@ describe('IntegrationBank', () => {expect(startingBalance).toEqual(0);});- it('should return 0 when transactions and balances are provided', () => {+ it('should return 70000 when transactions and balances are provided', () => {const startingBalance = IntegrationBank.calculateStartingBalance(transactions,balances);- expect(startingBalance).toEqual(0);+ expect(startingBalance).toEqual(70000);});it('logs available transactions and balances properties', () => {diff --git a/src/app-nordigen/services/nordigen-service.js b/src/app-nordigen/services/nordigen-service.jsindex b81b9097c1..67e578f297 100644--- a/src/app-nordigen/services/nordigen-service.js+++ b/src/app-nordigen/services/nordigen-service.js@@ -14,7 +14,6 @@ import {import * as nordigenNode from 'nordigen-node';import * as uuid from 'uuid';import config from '../../load-config.js';-import { sortByBookingDate } from '../utils.js';const NordigenClient = nordigenNode.default;const nordigenClient = new NordigenClient({@@ -302,6 +301,27 @@ export const nordigenService = {};},+ /**+ * Retrieve details about all Institutions in a specific country+ * @param country+ * @throws {InvalidInputDataError}+ * @throws {InvalidNordigenTokenError}+ * @throws {AccessDeniedError}+ * @throws {NotFoundError}+ * @throws {ResourceSuspended}+ * @throws {RateLimitError}+ * @throws {UnknownError}+ * @throws {ServiceError}+ * @returns {Promise<Array<import('../nordigen-node.types.js').Institution>>}+ */+ getInstitutions: async (country) => {+ const response = await client.getInstitutions(country);++ handleNordigenError(response);++ return response;+ },+/*** Retrieve details about a specific Institution* @param institutionId@@ -404,6 +424,8 @@ export const client = {dateTo,country: undefined}),+ getInstitutions: async (country) =>+ await nordigenClient.institution.getInstitutions({ country }),getInstitutionById: async (institutionId) =>await nordigenClient.institution.getInstitutionById(institutionId),getDetails: async (accountId) =>diff --git a/src/app-nordigen/services/tests/nordigen-service.spec.js b/src/app-nordigen/services/tests/nordigen-service.spec.jsindex fa098d4527..ac43bb9fec 100644--- a/src/app-nordigen/services/tests/nordigen-service.spec.js+++ b/src/app-nordigen/services/tests/nordigen-service.spec.js@@ -43,6 +43,7 @@ describe('nordigenService', () => {let getTransactionsSpy;let getDetailsSpy;let getMetadataSpy;+ let getInstitutionsSpy;let getInstitutionSpy;let getRequisitionsSpy;let deleteRequisitionsSpy;@@ -50,6 +51,7 @@ describe('nordigenService', () => {let setTokenSpy;beforeEach(() => {+ getInstitutionsSpy = jest.spyOn(client, 'getInstitutions');getInstitutionSpy = jest.spyOn(client, 'getInstitutionById');getRequisitionsSpy = jest.spyOn(client, 'getRequisitionById');deleteRequisitionsSpy = jest.spyOn(client, 'deleteRequisition');@@ -165,7 +167,7 @@ describe('nordigenService', () => {expect.objectContaining({balances: mockedBalances.balances,institutionId: mockRequisition.institution_id,- startingBalance: 0,+ startingBalance: expect.any(Number),transactions: {booked: expect.arrayContaining([expect.objectContaining({@@ -333,6 +335,26 @@ describe('nordigenService', () => {});});+ describe('#getInstitutions', () => {+ const country = 'IE';+ it('calls nordigenClient and fetch institution details', async () => {+ getInstitutionsSpy.mockResolvedValue([mockInstitution]);++ expect(await nordigenService.getInstitutions({ country })).toEqual([+ mockInstitution+ ]);+ expect(getInstitutionsSpy).toBeCalledTimes(1);+ });++ it('handle error if status_code present in the response', async () => {+ getInstitutionsSpy.mockResolvedValue(mockUnknownError);++ await expect(() =>+ nordigenService.getInstitutions({ country })+ ).rejects.toThrow(UnknownError);+ });+ });+describe('#getInstitution', () => {const institutionId = 'fake-institution-id';it('calls nordigenClient and fetch institution details', async () => {diff --git a/yarn.lock b/yarn.lockindex 9c658e52e2..9e6ba3a563 100644--- a/yarn.lock+++ b/yarn.lock@@ -2919,7 +2919,7 @@ __metadata:"fsevents@patch:fsevents@^2.3.2#~builtin<compat/fsevents>":version: 2.3.2- resolution: "fsevents@patch:fsevents@npm%3A2.3.2#~builtin<compat/fsevents>::version=2.3.2&hash=18f3a7"+ resolution: "fsevents@patch:fsevents@npm%3A2.3.2#~builtin<compat/fsevents>::version=2.3.2&hash=df0bf1"dependencies:node-gyp: latestconditions: os=darwin@@ -5086,7 +5086,7 @@ __metadata:"resolve@patch:resolve@^1.20.0#~builtin<compat/resolve>":version: 1.22.1- resolution: "resolve@patch:resolve@npm%3A1.22.1#~builtin<compat/resolve>::version=1.22.1&hash=07638b"+ resolution: "resolve@patch:resolve@npm%3A1.22.1#~builtin<compat/resolve>::version=1.22.1&hash=c3c19d"dependencies:is-core-module: ^2.9.0path-parse: ^1.0.7@@ -5744,11 +5744,11 @@ __metadata:"typescript@patch:typescript@^4.9.5#~builtin<compat/typescript>":version: 4.9.5- resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin<compat/typescript>::version=4.9.5&hash=7ad353"+ resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin<compat/typescript>::version=4.9.5&hash=23ec76"bin:tsc: bin/tsctsserver: bin/tsserver- checksum: 2eee5c37cad4390385db5db5a8e81470e42e8f1401b0358d7390095d6f681b410f2c4a0c496c6ff9ebd775423c7785cdace7bcdad76c7bee283df3d9718c0f20+ checksum: ab417a2f398380c90a6cf5a5f74badd17866adf57f1165617d6a551f059c3ba0a3e4da0d147b3ac5681db9ac76a303c5876394b13b3de75fdd5b1eaa06181c9dlanguageName: nodelinkType: hard