Skip to content

Commit

Permalink
Merge pull request #306 from qhdinh/fix/filter_duplicated_accounts
Browse files Browse the repository at this point in the history
[FIX] Keep only distinct accounts when fetching from BI
  • Loading branch information
KhadijaBenAmmar authored Oct 22, 2021
2 parents 21487b1 + 4ff6a3c commit 1a37f88
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 6 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ module.exports = {
],
camelcase: ['off'],
'nestjs/use-validation-pipe': ['off'],
'no-null/no-null': 'off',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const fromBIToAlgoanAccounts = (
type: mapAccountType(account.type),
usage: mapUsageType(account.usage),
owners: !isNil(ownerInfo) && !isNil(ownerInfo?.owner?.name) ? [{ name: ownerInfo.owner.name }] : undefined,
// eslint-disable-next-line no-null/no-null
iban: account.iban === null ? undefined : account.iban,
bic: account.bic,
name: account.name,
Expand All @@ -63,12 +62,10 @@ const fromBIToAlgoanAccounts = (
name: bank?.name,
logoUrl,
},
// eslint-disable-next-line no-null/no-null
coming: account.coming === null ? undefined : account.coming,
details: {
savings: mapAccountType(account.type) === AccountType.SAVINGS ? {} : undefined,
loan:
// eslint-disable-next-line no-null/no-null
account.loan !== null && account.loan !== undefined
? {
amount: account.loan.total_amount,
Expand Down
130 changes: 128 additions & 2 deletions src/hooks/services/hooks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ describe('HooksService', () => {
});
});

it('without token in the customer but a userId in the customer', async () => {
it('without token in the customer but a userId in the customer - duplicated fetched accounts', async () => {
const mockSubscription: Subscription = ({
event: (_id: string) => ({
update: async ({ status }) => {
Expand All @@ -545,7 +545,107 @@ describe('HooksService', () => {
.spyOn(aggregatorService, 'getConnections')
.mockReturnValueOnce(Promise.resolve([connection]))
.mockReturnValueOnce(Promise.resolve([{ ...connection, last_update: 'mockLastUpdate' }]));
const accountSpy = jest.spyOn(aggregatorService, 'getAccounts').mockResolvedValue([mockAccount]);
const accountSpy = jest.spyOn(aggregatorService, 'getAccounts').mockResolvedValue([mockAccount, 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 analysisSpy = jest
.spyOn(algoanAnalysisService, 'updateAnalysis')
.mockReturnValue(Promise.resolve(({} as unknown) as Analysis));

const event: EventDTO = ({
...mockEvent,
subscription: {
...mockEvent,
eventName: EventName.BANK_DETAILS_REQUIRED,
},
payload: { customerId: 'mockCustomerId', analysisId: 'mockAnalysisId' },
} as unknown) as EventDTO;

const fakeServiceAccount: ServiceAccount = {
...mockServiceAccount,
config: {
baseUrl: 'https://fake-base-url.url',
clientId: 'fakeClientId',
},
} as ServiceAccount;

await hooksService.dispatchAndHandleWebhook(event, mockSubscription, fakeServiceAccount, new Date());

const saConfig = {
baseUrl: 'https://fake-base-url.url',
clientId: 'fakeClientId',
};

expect(spyHttpService).toBeCalled();
expect(spyGetCustomer).toBeCalledWith('mockCustomerId');
expect(jwtTokenSpy).toBeCalledWith(saConfig, 'mockUserId');
expect(connectionSpy).toBeCalledWith('mockPermToken', saConfig);
expect(connectionSpy).toBeCalledTimes(2);
expect(accountSpy).toBeCalledWith('mockPermToken', saConfig);
expect(userInfoSpy).toBeCalledWith('mockPermToken', '4', saConfig);
expect(userInfoSpy).toBeCalledTimes(1);
expect(transactionSpy).toBeCalledWith('mockPermToken', 1, saConfig);
expect(categorySpy).toBeCalledWith('mockPermToken', mockTransaction.id_category, saConfig);
expect(analysisSpy).toBeCalledWith('mockCustomerId', 'mockAnalysisId', {
accounts: [
{
aggregator: { id: '1' },
balance: 100,
balanceDate: '2011-10-05T14:48:00.000Z',
bank: { id: undefined, name: undefined },
bic: 'mockBic',
coming: 0,
currency: 'id1',
details: { loan: undefined, savings: undefined },
iban: 'mockIban',
name: 'mockName',
number: 'mockNumber',
owners: undefined,
transactions: [
{
aggregator: { category: 'mockCategoryName', id: 'mockId', type: 'BANK_FEE' },
amount: 50,
currency: 'USD',
dates: { bookedAt: null, debitedAt: null },
description: 'mockOriginalWording',
isComing: false,
},
],
type: 'CHECKING',
usage: 'PERSONAL',
},
],
});
});

it('without token in the customer but a userId in the customer - there is an account with number null', async () => {
const mockSubscription: Subscription = ({
event: (_id: string) => ({
update: async ({ status }) => {
expect(status).toEqual('PROCESSED');
},
}),
} as unknown) as Subscription;

const spyGetCustomer = jest.spyOn(algoanCustomerService, 'getCustomerById').mockReturnValue(
Promise.resolve(({
id: 'mockCustomerId',
aggregationDetails: { mode: AggregationDetailsMode.API, userId: 'mockUserId' },
} as unknown) as Customer),
);

const jwtTokenSpy = jest
.spyOn(aggregatorService, 'getJWToken')
.mockResolvedValue({ jwt_token: 'mockPermToken', payload: { domain: 'mockDomain', id_user: 'userId' } });

const connectionSpy = jest
.spyOn(aggregatorService, 'getConnections')
.mockReturnValueOnce(Promise.resolve([connection]))
.mockReturnValueOnce(Promise.resolve([{ ...connection, last_update: 'mockLastUpdate' }]));
const accountSpy = jest
.spyOn(aggregatorService, 'getAccounts')
.mockResolvedValue([mockAccount, { ...mockAccount, number: null }]);
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);
Expand Down Expand Up @@ -615,6 +715,32 @@ describe('HooksService', () => {
type: 'CHECKING',
usage: 'PERSONAL',
},
{
aggregator: { id: '1' },
balance: 100,
balanceDate: '2011-10-05T14:48:00.000Z',
bank: { id: undefined, name: undefined },
bic: 'mockBic',
coming: 0,
currency: 'id1',
details: { loan: undefined, savings: undefined },
iban: 'mockIban',
name: 'mockName',
number: undefined,
owners: undefined,
transactions: [
{
aggregator: { category: 'mockCategoryName', id: 'mockId', type: 'BANK_FEE' },
amount: 50,
currency: 'USD',
dates: { bookedAt: null, debitedAt: null },
description: 'mockOriginalWording',
isComing: false,
},
],
type: 'CHECKING',
usage: 'PERSONAL',
},
],
});
});
Expand Down
19 changes: 18 additions & 1 deletion src/hooks/services/hooks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,23 @@ export class HooksService {
accounts,
});

/**
* Remove accounts with duplicated number
*/
const accountNumberMap: Record<string, boolean | undefined> = {};
const uniqueAccounts: BudgetInsightAccount[] = [];

for (const account of accounts) {
// Add accounts without number
if (account.number === null) {
uniqueAccounts.push(account);
// Add accounts with number, only if it's the first time we see it
} else if (accountNumberMap[account.number as string] === undefined) {
accountNumberMap[account.number as string] = true;
uniqueAccounts.push(account);
}
}

/**
* 3.b. Get personal information from every connection
*/
Expand All @@ -373,7 +390,7 @@ export class HooksService {
}

const algoanAccounts: Account[] = mapBudgetInsightAccount(
accounts,
uniqueAccounts,
this.aggregator,
connections,
connectionsInfo,
Expand Down

0 comments on commit 1a37f88

Please sign in to comment.