All files / src/controllers account.controller.ts

100% Statements 22/22
83.33% Branches 5/6
100% Functions 4/4
100% Lines 20/20

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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 958x                       8x 8x 8x       8x 8x       8x   8x               8x 1x                           8x     1x                             8x 2x 2x       2x 1x     1x                       8x 1x          
import {
  bearerAuth,
  controller,
  description,
  get, HttpError, HttpStatus,
  inject,
  MicroserviceRequest,
  pathParameter,
  response,
  responseBody,
  summary
} from "@waytrade/microservice-core";
import {firstValueFrom} from "rxjs";
import {AccountList} from "../models/account-list.model";
import {
  AccountSummary,
  AccountSummaryList
} from "../models/account-summary.model";
import {PositionList} from "../models/position-list.model";
import {IBApiService} from "../services/ib-api.service";
 
/** The account information controller. */
@controller("Account", "/account")
export class AccountController {
  @inject("IBApiService")
  private apiService!: IBApiService;
 
  @get("/managedAccounts")
  @summary("Get the managed accounts.")
  @description("Get the accounts to which the logged user has access to.")
  @response(HttpStatus.UNAUTHORIZED, "Missing or invalid authorization header.")
  @responseBody(AccountList)
  @bearerAuth([])
  async getManagedAccounts(req: MicroserviceRequest): Promise<AccountList> {
    return {
      accounts: await this.apiService.managedAccounts,
    };
  }
 
  @get("/accountSummaries")
  @summary("Get a snapshot the account summaries.")
  @description(
    "Get a snapshot of the account summaries of all managed accounts.</br> " +
      "Use Real-time Data endpoint for receiving realtime updates.",
  )
  @response(HttpStatus.UNAUTHORIZED, "Missing or invalid authorization header.")
  @responseBody(AccountSummaryList)
  @bearerAuth([])
  async getAccountSummaries(
    req: MicroserviceRequest,
  ): Promise<AccountSummaryList> {
    return {
      summaries: await firstValueFrom(this.apiService.accountSummaries)
    };
  }
 
  @get("/accountSummary/{account}")
  @pathParameter("account", String, "The account id")
  @summary("Get a snapshot of a account summary.")
  @description(
    "Get a snapshot of the current account summary of a given account.</br> " +
      "Use Real-time Data endpoint for receiving realtime updates.",
  )
  @response(HttpStatus.UNAUTHORIZED, "Missing or invalid authorization header.")
  @responseBody(AccountSummary)
  @bearerAuth([])
  async getAccountSummary(req: MicroserviceRequest): Promise<AccountSummary> {
    const paths = req.url.split("/");
    const summary = await firstValueFrom(
      this.apiService.getAccountSummary(paths[paths.length - 1])
    );
 
    if (!summary || !summary.baseCurrency) {
      throw new HttpError(HttpStatus.NOT_FOUND, "Invalid account id");
    }
 
    return summary;
  }
 
  @get("/positions")
  @summary("Get a snapshot of all open positions.")
  @description(
    "Get a snapshot of all open positions on all managed accounts.</br> " +
      "Use Real-time Data endpoint for receiving realtime updates.",
  )
  @response(HttpStatus.UNAUTHORIZED, "Missing or invalid authorization header.")
  @responseBody(PositionList)
  @bearerAuth([])
  async getPositions(req: MicroserviceRequest): Promise<PositionList> {
    return {
      positions: ((await firstValueFrom(this.apiService.positions))?.changed) ?? []
    };
  }
}