From 0b82dfbed9d1d645455154e881a145716d3f0fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KERGUTUIL=20Th=C3=A9o?= <58176270+LeKer29@users.noreply.github.com> Date: Wed, 6 Oct 2021 15:42:40 +0200 Subject: [PATCH] feat(hooks): add log for aggregation duration --- src/hooks/controllers/hooks.controller.ts | 4 +++- src/hooks/services/hooks.service.spec.ts | 13 ++++++++----- src/hooks/services/hooks.service.ts | 20 ++++++++++++++++++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/hooks/controllers/hooks.controller.ts b/src/hooks/controllers/hooks.controller.ts index 94f2fba..1f6e23c 100644 --- a/src/hooks/controllers/hooks.controller.ts +++ b/src/hooks/controllers/hooks.controller.ts @@ -27,6 +27,8 @@ export class HooksController { @Post('/hooks') @HttpCode(HttpStatus.NO_CONTENT) public async controlHook(@Body() event: EventDTO, @Headers() headers: IHeaders): Promise { + const aggregationStartDate: Date = new Date(); + // Get subscription const subscription: Subscription | undefined = this.serviceAccount.subscriptions.find( (sub: Subscription) => sub.id === event.subscription.id, @@ -56,7 +58,7 @@ export class HooksController { case EventName.BANK_DETAILS_REQUIRED: assertsTypeValidation(BankDetailsRequiredDTO, event.payload); - void this.hooksService.handleBankDetailsRequiredEvent(event.payload).catch((err) => { + void this.hooksService.handleBankDetailsRequiredEvent(event.payload, aggregationStartDate).catch((err) => { throw err; }); break; diff --git a/src/hooks/services/hooks.service.spec.ts b/src/hooks/services/hooks.service.spec.ts index f936ed3..7f35014 100644 --- a/src/hooks/services/hooks.service.spec.ts +++ b/src/hooks/services/hooks.service.spec.ts @@ -359,7 +359,7 @@ describe('HookService', () => { }); it('should do these steps in "snapshot" mode', async () => { - await hookService.handleBankDetailsRequiredEvent(bankDetailsRequiredMock); + await hookService.handleBankDetailsRequiredEvent(bankDetailsRequiredMock, new Date()); // Authenticate as user expect(tinkAuthenticateAsUserWithCodesSpy).toHaveBeenCalledWith( @@ -398,7 +398,10 @@ describe('HookService', () => { }); it('should do these steps in "refresh" mode', async () => { - await hookService.handleBankDetailsRequiredEvent({ ...bankDetailsRequiredMock, temporaryCode: undefined }); + await hookService.handleBankDetailsRequiredEvent( + { ...bankDetailsRequiredMock, temporaryCode: undefined }, + new Date(), + ); // Authenticate with the refresh token expect(getCustomerSpy).toHaveBeenCalledWith(bankDetailsRequiredMock.customerId); @@ -433,9 +436,9 @@ describe('HookService', () => { it('should throw error if client config missing', async () => { serviceAccount.config = undefined; - await expect(hookService.handleBankDetailsRequiredEvent(bankDetailsRequiredMock)).rejects.toThrowError( - `Missing information: clientConfig: undefined`, - ); + await expect( + hookService.handleBankDetailsRequiredEvent(bankDetailsRequiredMock, new Date()), + ).rejects.toThrowError(`Missing information: clientConfig: undefined`); expect(updateAnalysisSpy).toHaveBeenCalledWith( bankDetailsRequiredMock.customerId, diff --git a/src/hooks/services/hooks.service.ts b/src/hooks/services/hooks.service.ts index 3729181..360e94b 100644 --- a/src/hooks/services/hooks.service.ts +++ b/src/hooks/services/hooks.service.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/naming-convention, camelcase */ import { ServiceAccount } from '@algoan/rest'; -import { Injectable, Inject } from '@nestjs/common'; +import { Injectable, Inject, Logger } from '@nestjs/common'; import { Config } from 'node-config-ts'; import { AggregationDetailsMode } from '../../algoan/dto/customer.enums'; @@ -37,6 +37,11 @@ import { AnalysisStatus, ErrorCodes } from '../../algoan/dto/analysis.enum'; */ @Injectable() export class HooksService { + /** + * Class logger + */ + private readonly logger: Logger = new Logger(HooksService.name); + constructor( @Inject(CONFIG) private readonly config: Config, private readonly algoanHttpService: AlgoanHttpService, @@ -174,7 +179,10 @@ export class HooksService { /** * Handle Aggregator Link event */ - public async handleBankDetailsRequiredEvent(payload: BankDetailsRequiredDTO): Promise { + public async handleBankDetailsRequiredEvent( + payload: BankDetailsRequiredDTO, + aggregationStartDate: Date, + ): Promise { try { // Get client config const clientConfig: ClientConfig | undefined = this.serviceAccount.config as ClientConfig | undefined; @@ -225,6 +233,14 @@ export class HooksService { // Generate an Algoan analysis from data information const analysis: AnalysisUpdateInput = mapTinkDataToAlgoanAnalysis(accounts, transactions, providers); + const aggregationDuration: number = new Date().getTime() - aggregationStartDate.getTime(); + + this.logger.log({ + message: `Account aggregation completed in ${aggregationDuration} milliseconds for Customer ${payload.customerId} and Analysis ${payload.analysisId}.`, + aggregator: 'TINK', + duration: aggregationDuration, + }); + // Update the user analysis await this.algoanAnalysisService.updateAnalysis(payload.customerId, payload.analysisId, analysis);