File

src/user/user.orderbook.service.ts

Index

Properties
Methods

Methods

Async getInvestorOrders
getInvestorOrders(undefined: IGetInvestorOrders)
Parameters :
Name Type Optional
IGetInvestorOrders No
Returns : Promise<literal type>

Properties

Private Readonly userService
Type : UserService
Decorators :
@Inject(UserService)
import { ApolloQueryResult } from "@apollo/client";
import {
  Injectable,
  Inject,
  ServiceUnavailableException,
  BadRequestException,
} from "@nestjs/common";
import { encodeBytes32String } from "ethers";
import { IGetOrdersQuery, EndUserRoles } from "src/common/interfaces";
import {
  apolloClient,
  getAllOrdersByInvestorIdQuery,
} from "src/common/provider";
import { IGetInvestorOrders } from "src/orderbook/orderbook.interface";
import { UserService } from "./user.service";
import type { User } from "./schemas/user.schema";

@Injectable()
export class UserOrderbookService {
  @Inject(UserService)
  private readonly userService: UserService;

  async getInvestorOrders({
    investorId,
    orderType,
    orderDirection,
    orderStatus,
    skip,
    limit,
  }: IGetInvestorOrders): Promise<{
    investorProfile: User;
    orders: IGetOrdersQuery;
  }> {
    try {
      const investorProfile: User = (await this.userService.findOne(
        investorId,
      )) as User;
      if (investorProfile.endUserRole !== EndUserRoles.investor) {
        throw new BadRequestException("This call for investors only");
      }
      const ordersGraph: ApolloQueryResult<IGetOrdersQuery> =
        await apolloClient.query({
          query: getAllOrdersByInvestorIdQuery,
          variables: {
            investorId: encodeBytes32String(investorId),
            status: orderStatus,
            type: orderType,
            first: limit,
            skip: skip * limit,
            orderDirection,
          },
        });
      if (ordersGraph.errors) {
        throw new ServiceUnavailableException(ordersGraph.errors[0]);
      }
      return { investorProfile, orders: ordersGraph.data };
    } catch (error) {
      console.error(error);
      if (error.status === 401) {
        throw new BadRequestException(error);
      }
      throw new ServiceUnavailableException(error);
    }
  }
}

results matching ""

    No results matching ""