All files / src/controllers contracts.controller.ts

100% Statements 36/36
100% Branches 10/10
100% Functions 4/4
100% Lines 34/34

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 1528x                       8x 8x 8x 8x 8x 8x       8x   8x                   8x     4x 4x 1x           3x 3x       1x                             8x       3x 3x       1x                               8x         5x 5x 1x               4x 4x           1x                               8x         3x 3x 1x           2x 2x     1x              
import {
  bearerAuth,
  controller,
  description,
  get,
  HttpError,
  HttpStatus,
  inject,
  MicroserviceRequest, post, queryParameter, requestBody, response,
  responseBody,
  summary
} from "@waytrade/microservice-core";
import {ContractDescriptionList} from "../models/contract-description.model";
import {ContractDetailsList} from "../models/contract-details.model";
import {Contract} from "../models/contract.model";
import {HistoricDataRequestArguments} from "../models/historic-data-request.model";
import {OHLCBars} from "../models/ohlc-bar.model";
import {IBApiService} from "../services/ib-api.service";
 
/** The contracts database controller. */
@controller("Contracts", "/contracts")
export class ContractsController {
  @inject("IBApiService")
  private apiService!: IBApiService;
 
  @get("/search")
  @summary("Search contracts.")
  @description("Search contracts where name or symbol matches the given text pattern.")
  @queryParameter("pattern", String, true, "The  text pattern.")
  @responseBody(ContractDescriptionList)
  @response(HttpStatus.BAD_REQUEST)
  @response(HttpStatus.UNAUTHORIZED, "Missing or invalid authorization header.")
  @bearerAuth([])
  async searchContract(
    req: MicroserviceRequest,
  ): Promise<ContractDescriptionList> {
    const pattern = req.queryParams.pattern as string;
    if (pattern === undefined) {
      throw new HttpError(
        HttpStatus.BAD_REQUEST,
        "Missing pattern parameter on query",
      );
    }
 
    try {
      return {
        descs: await this.apiService.searchContracts(pattern)
      } as ContractDescriptionList;
    } catch (e) {
      throw new HttpError(
        HttpStatus.INTERNAL_SERVER_ERROR,
        (<Error>e).message,
      );
    }
  }
 
  @post("/details")
  @summary("Get contract details.")
  @description("Get the contract details of a given contract ID.")
  @response(HttpStatus.BAD_REQUEST)
  @response(HttpStatus.UNAUTHORIZED, "Missing or invalid authorization header.")
  @requestBody(Contract)
  @responseBody(ContractDetailsList)
  @bearerAuth([])
  async getContractDetails(
    req: MicroserviceRequest,
    contract: Contract,
  ): Promise<ContractDetailsList> {
    try {
      return {
        details: await this.apiService.getContractDetails(contract)
      } as ContractDetailsList;
    } catch (e) {
      throw new HttpError(
        HttpStatus.BAD_REQUEST,
        (<Error>e).message,
      );
    }
  }
 
  @get("/detailsById")
  @summary("Get contract details by conId.")
  @description("Get the contract details of a given contract ID.")
  @queryParameter("conId", Number, true, "The IB contract ID.")
  @response(HttpStatus.BAD_REQUEST)
  @response(HttpStatus.NOT_FOUND, "Contract not found.")
  @response(HttpStatus.UNAUTHORIZED, "Missing or invalid authorization header.")
  @responseBody(ContractDetailsList)
  @bearerAuth([])
  async getContractDetailsById(
    req: MicroserviceRequest,
  ): Promise<ContractDetailsList> {
    // verify arguments
 
    const conId = Number(req.queryParams.conId);
    if (conId === undefined || isNaN(conId)) {
      throw new HttpError(
        HttpStatus.BAD_REQUEST,
        "Missing conId parameter on query",
      );
    }
 
    // get contract details
 
    try {
      return {
        details: await this.apiService.getContractDetails({
          conId,
        })
      } as ContractDetailsList;
    } catch (e) {
      throw new HttpError(
        HttpStatus.INTERNAL_SERVER_ERROR,
        (<Error>e).message,
      );
    }
  }
 
  @post("/historicData")
  @summary("Get historic data of a contract.")
  @description("Get historic OHLC data of a contract of a given contract ID.")
  @requestBody(HistoricDataRequestArguments)
  @response(HttpStatus.BAD_REQUEST)
  @response(HttpStatus.NOT_FOUND, "Contract not found.")
  @response(HttpStatus.UNAUTHORIZED, "Missing or invalid authorization header.")
  @responseBody(OHLCBars)
  @bearerAuth([])
  async getHistoricData(
    req: MicroserviceRequest,
    args: HistoricDataRequestArguments): Promise<OHLCBars> {
    // verify arguments
 
    const conId = Number(args.conId);
    if (conId === undefined || isNaN(conId)) {
      throw new HttpError(
        HttpStatus.BAD_REQUEST,
        "Missing conId on HistoricDataRequestArguments",
      );
    }
 
    try {
      return await this.apiService.getHistoricData(
        conId, args.endDate, args.duration, args.barSize, args.whatToShow);
    } catch (e) {
      throw new HttpError(
        HttpStatus.BAD_REQUEST,
        (<Error>e).message,
      );
    }
  }
}