From bdfa57519626e3ec82c24aad853ce92fc7dae99e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KERGUTUIL=20Th=C3=A9o?= <58176270+LeKer29@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:12:38 +0200 Subject: [PATCH] feat(hooks): add feature flag for account ownerships --- config/default.json | 3 +- src/hooks/services/hooks.service.spec.ts | 27 ++++++++------ src/hooks/services/hooks.service.ts | 47 +++++++++++++++++------- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/config/default.json b/config/default.json index 8640e2ff..c8da8c56 100644 --- a/config/default.json +++ b/config/default.json @@ -17,7 +17,8 @@ "waitingTime": 1000, "apiVersion": "2.0", "ignoredConnectionStates": ["wrongpass", "bug", "passwordExpired", "websiteUnavailable"], - "paginationLimit": 20 + "paginationLimit": 20, + "enableAccountOwnerships": false }, "targetUrl": "http://localhost:8080/hooks", "eventList": [ diff --git a/src/hooks/services/hooks.service.spec.ts b/src/hooks/services/hooks.service.spec.ts index cbb9867e..0b1d1c28 100644 --- a/src/hooks/services/hooks.service.spec.ts +++ b/src/hooks/services/hooks.service.spec.ts @@ -648,6 +648,9 @@ describe('HooksService', () => { const userInfoSpy = jest.spyOn(aggregatorService, 'getInfo').mockResolvedValue({ owner: { name: 'JOHN DOE' } }); const transactionSpy = jest.spyOn(aggregatorService, 'getTransactions').mockResolvedValue([mockTransaction]); const categorySpy = jest.spyOn(aggregatorService, 'getCategory').mockResolvedValue(mockCategory); + const accountOwnershipsSpy = jest + .spyOn(aggregatorService, 'getAccountOwnerships') + .mockResolvedValue(mockAccountOwnerships); const analysisSpy = jest .spyOn(algoanAnalysisService, 'updateAnalysis') .mockReturnValue(Promise.resolve({} as unknown as Analysis)); @@ -684,6 +687,7 @@ describe('HooksService', () => { expect(userInfoSpy).toBeCalledTimes(1); expect(transactionSpy).toBeCalledWith('fakeToken', 1, saConfig); expect(categorySpy).toBeCalledWith('fakeToken', mockTransaction.id_category, saConfig); + expect(accountOwnershipsSpy).toBeCalledTimes(0); expect(analysisSpy).toBeCalledWith('mockCustomerId', 'mockAnalysisId', { connections: [ { @@ -750,24 +754,25 @@ describe('HooksService', () => { } as unknown as Subscription; const spyGetCustomer = jest.spyOn(algoanCustomerService, 'getCustomerById').mockReturnValue( - Promise.resolve({ - id: 'mockCustomerId', - aggregationDetails: { mode: AggregationDetailsMode.API, token: 'fakeToken' }, - } as unknown as Customer), + Promise.resolve({ + id: 'mockCustomerId', + aggregationDetails: { mode: AggregationDetailsMode.API, token: 'fakeToken' }, + } as unknown as Customer), ); const connectionSpy = jest - .spyOn(aggregatorService, 'getConnections') - .mockReturnValue(Promise.resolve([{ ...connection, state: ConnectionErrorState.WRONGPASS }])); + .spyOn(aggregatorService, 'getConnections') + .mockReturnValue(Promise.resolve([{ ...connection, state: ConnectionErrorState.WRONGPASS }])); const accountSpy = jest.spyOn(aggregatorService, 'getAccounts').mockResolvedValue([mockAccount]); + const userInfoSpy = jest.spyOn(aggregatorService, 'getInfo').mockResolvedValue({ owner: { name: 'JOHN DOE' } }); const transactionSpy = jest.spyOn(aggregatorService, 'getTransactions').mockResolvedValue([mockTransaction]); const categorySpy = jest.spyOn(aggregatorService, 'getCategory').mockResolvedValue(mockCategory); const accountOwnershipsSpy = jest - .spyOn(aggregatorService, 'getAccountOwnerships') - .mockResolvedValue(mockAccountOwnerships); + .spyOn(aggregatorService, 'getAccountOwnerships') + .mockResolvedValue(mockAccountOwnerships); const analysisSpy = jest - .spyOn(algoanAnalysisService, 'updateAnalysis') - .mockReturnValue(Promise.resolve({} as unknown as Analysis)); + .spyOn(algoanAnalysisService, 'updateAnalysis') + .mockReturnValue(Promise.resolve({} as unknown as Analysis)); const event: EventDTO = { ...mockEvent, @@ -797,6 +802,7 @@ describe('HooksService', () => { expect(spyGetCustomer).toBeCalledWith('mockCustomerId'); expect(connectionSpy).toBeCalledWith('fakeToken', saConfig); expect(accountSpy).toBeCalledWith('fakeToken', saConfig); + expect(userInfoSpy).toBeCalledTimes(0); expect(transactionSpy).toBeCalledWith('fakeToken', 1, saConfig); expect(categorySpy).toBeCalledWith('fakeToken', mockTransaction.id_category, saConfig); expect(accountOwnershipsSpy).toBeCalledWith('fakeToken', saConfig); @@ -856,7 +862,6 @@ describe('HooksService', () => { }); config.budgetInsight.enableAccountOwnerships = false; - }); it('should fetch bank details if there is at least a finished connection with a warning (after timeout)', async () => { diff --git a/src/hooks/services/hooks.service.ts b/src/hooks/services/hooks.service.ts index 942cf4d1..2873c2e7 100644 --- a/src/hooks/services/hooks.service.ts +++ b/src/hooks/services/hooks.service.ts @@ -316,7 +316,7 @@ export class HooksService { return; } - const accountOwnerships: AccountOwnership[] = await this.aggregator.getAccountOwnerships( + const accountOwnerships: AccountOwnership[] | undefined = await this.getAccountOwnerships( permanentToken, serviceAccount.config as ClientConfig, ); @@ -370,6 +370,23 @@ export class HooksService { } } + /** + * Get account ownerships + * @param token + * @param clientConfig + * @private + */ + private async getAccountOwnerships( + token: string, + clientConfig: ClientConfig, + ): Promise { + if (!this.config.budgetInsight.enableAccountOwnerships) { + return undefined; + } + + return this.aggregator.getAccountOwnerships(token, clientConfig); + } + /** * Handles the service_account_created event * @param payload the new service account id @@ -479,19 +496,21 @@ export class HooksService { * 3.b. Get personal information from every connection */ const connectionsInfo: { [key: string]: BudgetInsightOwner } = {}; - for (const connection of connections) { - try { - connectionsInfo[connection.id] = await this.aggregator.getInfo( - permanentToken, - `${connection.id}`, - serviceAccountConfig, - ); - } catch (err) { - this.logger.warn({ - message: `Unable to get user personal information`, - error: err, - connection, - }); + if (!this.config.budgetInsight.enableAccountOwnerships) { + for (const connection of connections) { + try { + connectionsInfo[connection.id] = await this.aggregator.getInfo( + permanentToken, + `${connection.id}`, + serviceAccountConfig, + ); + } catch (err) { + this.logger.warn({ + message: `Unable to get user personal information`, + error: err, + connection, + }); + } } }