Skip to content

Commit

Permalink
feat(hooks): add feature flag for account ownerships
Browse files Browse the repository at this point in the history
  • Loading branch information
LeKer29 committed Aug 10, 2023
1 parent 02d5e7e commit bdfa575
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
27 changes: 16 additions & 11 deletions src/hooks/services/hooks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 () => {
Expand Down
47 changes: 33 additions & 14 deletions src/hooks/services/hooks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -370,6 +370,23 @@ export class HooksService {
}
}

/**
* Get account ownerships
* @param token
* @param clientConfig
* @private
*/
private async getAccountOwnerships(
token: string,
clientConfig: ClientConfig,
): Promise<AccountOwnership[] | undefined> {
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
Expand Down Expand Up @@ -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,
});
}
}
}

Expand Down

0 comments on commit bdfa575

Please sign in to comment.