Skip to content

Commit

Permalink
feat(hooks): add account ownerships to PATCH /analyses/:id body
Browse files Browse the repository at this point in the history
  • Loading branch information
LeKer29 committed Aug 10, 2023
1 parent c212673 commit 02d5e7e
Showing 1 changed file with 119 additions and 8 deletions.
127 changes: 119 additions & 8 deletions src/hooks/services/hooks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AggregationDetailsMode } from '../../algoan/dto/customer.enums';
import { Customer } from '../../algoan/dto/customer.objects';
import { Analysis } from '../../algoan/dto/analysis.objects';
import { AggregatorModule } from '../../aggregator/aggregator.module';
import { config } from 'node-config-ts';
import {
mockAccount,
mockTransaction,
Expand Down Expand Up @@ -647,9 +648,6 @@ 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 @@ -686,6 +684,121 @@ describe('HooksService', () => {
expect(userInfoSpy).toBeCalledTimes(1);
expect(transactionSpy).toBeCalledWith('fakeToken', 1, saConfig);
expect(categorySpy).toBeCalledWith('fakeToken', mockTransaction.id_category, saConfig);
expect(analysisSpy).toBeCalledWith('mockCustomerId', 'mockAnalysisId', {
connections: [
{
accounts: [
{
id: 1,
id_connection: 2,
id_user: 3,
id_source: 4,
id_parent: 5,
number: 'mockNumber',
original_name: 'mockOrginalName',
coming: 0,
currency: { id: 'id1' },
balance: 100,
name: 'mockName',
last_update: '2011-10-05T14:48:00.000Z',
type: 'checking',
iban: 'mockIban',
bic: 'mockBic',
disabled: false,
company_name: 'mockCompany',
usage: 'PRIV',
ownership: 'owner',
transactions: [
{
id_account: 5,
id: 'mockId',
application_date: 'mockApplicationDate',
date: 'mockDate',
rdate: 'mockRDate',
simplified_wording: 'mockSimplifiedWording',
value: 50,
card: 'mockCard',
wording: 'mockWording',
id_category: 10,
type: 'bank',
original_wording: 'mockOriginalWording',
original_currency: { id: 'USD' },
coming: false,
category: { name: 'mockCategoryName' },
},
],
},
],
connector: {
logoUrl: undefined,
},
information: undefined,
},
],
format: 'BUDGET_INSIGHT_V2_0',
});
});

it('should fetch bank details - account ownerships enabled', async () => {
config.budgetInsight.enableAccountOwnerships = true;
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, token: 'fakeToken' },
} as unknown as Customer),
);

const connectionSpy = jest
.spyOn(aggregatorService, 'getConnections')
.mockReturnValue(Promise.resolve([{ ...connection, state: ConnectionErrorState.WRONGPASS }]));
const accountSpy = jest.spyOn(aggregatorService, 'getAccounts').mockResolvedValue([mockAccount]);
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));

const event: EventDTO = {
...mockEvent,
subscription: {
...mockEvent,
eventName: EventName.BANK_DETAILS_REQUIRED,
},
payload: { customerId: 'mockCustomerId', analysisId: 'mockAnalysisId', temporaryCode: 'mockTempCode' },
} 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(connectionSpy).toBeCalledWith('fakeToken', saConfig);
expect(accountSpy).toBeCalledWith('fakeToken', saConfig);
expect(transactionSpy).toBeCalledWith('fakeToken', 1, saConfig);
expect(categorySpy).toBeCalledWith('fakeToken', mockTransaction.id_category, saConfig);
expect(accountOwnershipsSpy).toBeCalledWith('fakeToken', saConfig);
expect(analysisSpy).toBeCalledWith('mockCustomerId', 'mockAnalysisId', {
connections: [
Expand Down Expand Up @@ -741,6 +854,9 @@ describe('HooksService', () => {
accountOwnerships: mockAccountOwnerships,
format: 'BUDGET_INSIGHT_V2_0',
});

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 Expand Up @@ -778,9 +894,6 @@ 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 @@ -817,7 +930,6 @@ describe('HooksService', () => {
expect(userInfoSpy).toBeCalledTimes(2);
expect(transactionSpy).toBeCalledWith('fakeToken', 1, saConfig);
expect(categorySpy).toBeCalledWith('fakeToken', mockTransaction.id_category, saConfig);
expect(accountOwnershipsSpy).toBeCalledWith('fakeToken', saConfig);
expect(analysisSpy).toBeCalledWith('mockCustomerId', 'mockAnalysisId', {
connections: [
{
Expand Down Expand Up @@ -869,7 +981,6 @@ describe('HooksService', () => {
information: undefined,
},
],
accountOwnerships: mockAccountOwnerships,
format: 'BUDGET_INSIGHT_V2_0',
});
expect(warnLoggerSpy).toBeCalledWith({
Expand Down

0 comments on commit 02d5e7e

Please sign in to comment.