Skip to content

Commit

Permalink
Merge pull request #18 from LeKer29/feature/aggregation-duration-log
Browse files Browse the repository at this point in the history
[FEAT] add log for aggregation duration
  • Loading branch information
KhadijaBenAmmar authored Oct 7, 2021
2 parents c7d5436 + 0b82dfb commit 3e57fe3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/hooks/controllers/hooks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class HooksController {
@Post('/hooks')
@HttpCode(HttpStatus.NO_CONTENT)
public async controlHook(@Body() event: EventDTO, @Headers() headers: IHeaders): Promise<void> {
const aggregationStartDate: Date = new Date();

// Get subscription
const subscription: Subscription | undefined = this.serviceAccount.subscriptions.find(
(sub: Subscription) => sub.id === event.subscription.id,
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 8 additions & 5 deletions src/hooks/services/hooks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 18 additions & 2 deletions src/hooks/services/hooks.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -174,7 +179,10 @@ export class HooksService {
/**
* Handle Aggregator Link event
*/
public async handleBankDetailsRequiredEvent(payload: BankDetailsRequiredDTO): Promise<void> {
public async handleBankDetailsRequiredEvent(
payload: BankDetailsRequiredDTO,
aggregationStartDate: Date,
): Promise<void> {
try {
// Get client config
const clientConfig: ClientConfig | undefined = this.serviceAccount.config as ClientConfig | undefined;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 3e57fe3

Please sign in to comment.