All files / libs/lti/core/src/lib/use-cases/lib-custody get-custody.use-case.ts

0% Statements 0/78
0% Branches 0/1
0% Functions 0/1
0% Lines 0/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                                                                                                             
import { ForbiddenException, Injectable, NotFoundException } from '@nestjs/common';

import { type LtiPlan } from '@amalia/core/models';
import { assert } from '@amalia/ext/typescript';
import { canViewLtiPlans, defineAbilityFor } from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { IntlService } from '@amalia/kernel/intl/module';
import { type LtiCustodyResponse } from '@amalia/lti/types';

import { configuration } from '../../configs';

import { CustodyClient } from './custody.client';

interface GetCustodyUseCaseArgs {
  authenticatedContext: AuthenticatedContext;
  // Normally this endpoint should be filtered by instrumentId but I don't know how to do it yet.
  planId: LtiPlan['id'];
}

@Injectable()
export class GetCustodyUseCase {
  public constructor(private readonly intl: IntlService) {}

  public async execute({ authenticatedContext, planId: _planId }: GetCustodyUseCaseArgs): Promise<LtiCustodyResponse> {
    this.assertCanExecute(authenticatedContext);

    const custodyClient = new CustodyClient();

    const { custodyApiKey, custodyApiUrl } = configuration.allShares;
    assert(
      custodyApiKey,
      new NotFoundException(
        this.intl.formatMessage({
          defaultMessage: 'Custody API key not found - cannot get custody.',
          id: 'NuyB6I',
        }),
      ),
    );
    assert(
      custodyApiUrl,
      new NotFoundException(
        this.intl.formatMessage({
          defaultMessage: 'Custody API URL not found - cannot get custody.',
          id: 'Yl3aGw',
        }),
      ),
    );

    const holderId = authenticatedContext.user.holderId;
    assert(
      holderId,
      new NotFoundException(
        this.intl.formatMessage({
          defaultMessage: 'User holderId not found - cannot get custody.',
          id: '4fQrwj',
        }),
      ),
    );

    const { payload } = await custodyClient.getCustomerPortfolio(holderId);

    return { portfolios: payload };
  }

  private assertCanExecute(authenticatedContext: AuthenticatedContext): void {
    const ability = defineAbilityFor(authenticatedContext);

    assert(
      canViewLtiPlans(ability),
      new ForbiddenException(
        this.intl.formatMessage({
          defaultMessage: 'You do not have permission to list the documents in this plan.',
          id: 'CR3gqi',
        }),
      ),
    );
  }
}