File

src/security/security.controller.ts

Prefix

api/v1/security

Index

Methods

Methods

Async create
create(request: AuthRequest, createFundDto: CreateFundDto)
Decorators :
@UseGuards(new RolesAuthGuard())
@ApiBody({type: undefined})
@Post('/fund')
Parameters :
Name Type Optional
request AuthRequest No
createFundDto CreateFundDto No
Returns : unknown
Async createInstrument
createInstrument(request: AuthRequest, fundId: string, createInstrumentDto: CreateInstrumentDto)
Decorators :
@UseGuards(new RolesAuthGuard())
@Post('/fund/:id/instrument/')
Parameters :
Name Type Optional
request AuthRequest No
fundId string No
createInstrumentDto CreateInstrumentDto No
Returns : unknown
Async findAll
findAll(request: AuthRequest, filterFundDto: FilterFundDto)
Decorators :
@UseInterceptors(CustomCacheInterceptor)
@UseGuards(undefined)
@Get('/fund')
Parameters :
Name Type Optional
request AuthRequest No
filterFundDto FilterFundDto No
Returns : unknown
Async findOne
findOne(request: AuthRequest, id: string)
Decorators :
@UseInterceptors(CustomCacheInterceptor)
@UseGuards(new RolesAuthGuard())
@Get('/fund/:id')
Parameters :
Name Type Optional
request AuthRequest No
id string No
Returns : unknown
Async getAllFundClients
getAllFundClients(request: AuthRequest, id: string)
Decorators :
@UseGuards(new RolesAuthGuard())
@UseInterceptors(CustomCacheInterceptor)
@Get('/fund/:id/clients')
Parameters :
Name Type Optional
request AuthRequest No
id string No
Returns : unknown
Async getAllInstruments
getAllInstruments(request: AuthRequest, fundId: string, filterPaginationDto: FilterInstrumentDto)
Decorators :
@UseGuards(undefined)
@UseInterceptors(CustomCacheInterceptor)
@Get('/fund/:id/instrument/')
Parameters :
Name Type Optional
request AuthRequest No
fundId string No
filterPaginationDto FilterInstrumentDto No
Returns : unknown
Async getFundOrder
getFundOrder(request: AuthRequest, id: string)
Decorators :
@UseInterceptors(CustomCacheInterceptor)
@UseGuards(new RolesAuthGuard())
@Get('/fund/:id/orderbook')
Parameters :
Name Type Optional
request AuthRequest No
id string No
Returns : unknown
Async getInstrumentById
getInstrumentById(request: AuthRequest, fundId: string, instrumentId: string)
Decorators :
@UseGuards(undefined)
@UseInterceptors(CustomCacheInterceptor)
@Get('/fund/:id/instrument/:instrumentId')
Parameters :
Name Type Optional
request AuthRequest No
fundId string No
instrumentId string No
Returns : unknown
Async getInstrumentOrderbook
getInstrumentOrderbook(request: AuthRequest, id: string, instrumentId: string)
Decorators :
@UseGuards(new RolesAuthGuard())
@UseInterceptors(CustomCacheInterceptor)
@Get('/fund/:id/instrument/:instrumentId/orderbook')
Parameters :
Name Type Optional
request AuthRequest No
id string No
instrumentId string No
Returns : unknown
Async getNavs
getNavs(request: AuthRequest, fundId: string, instrumentId: string)
Decorators :
@UseGuards(undefined)
@UseInterceptors(CustomCacheInterceptor)
@Get('/fund/:fundId/instrument/:id/nav')
Parameters :
Name Type Optional
request AuthRequest No
fundId string No
instrumentId string No
Returns : unknown
Async initializeInstrument
initializeInstrument(request: AuthRequest, instrumentId: string)
Decorators :
@UseGuards(new RolesAuthGuard())
@Post('/fund/:id/instrument/:instrumentId')
Parameters :
Name Type Optional
request AuthRequest No
instrumentId string No
Returns : unknown
Async update
update(request: AuthRequest, id: string, updateFundDto: UpdateFundDto)
Decorators :
@UseGuards(new RolesAuthGuard())
@Put('/fund/:id')
Parameters :
Name Type Optional
request AuthRequest No
id string No
updateFundDto UpdateFundDto No
Returns : unknown
Async updateInstrument
updateInstrument(instrumentId: string, updateInstrumentDto: UpdateInstrumentDto)
Decorators :
@UseGuards(new RolesAuthGuard())
@Put('/fund/:id/instrument/:instrumentId')
Parameters :
Name Type Optional
instrumentId string No
updateInstrumentDto UpdateInstrumentDto No
Returns : unknown
Async updateNav
updateNav(request: AuthRequest, fundId: string, instrumentId: string, updatedNavDto: UpdatedNavDto)
Decorators :
@UseGuards(new RolesAuthGuard())
@ApiBody({type: undefined})
@Post('/fund/:fundId/instrument/:id/nav')
Parameters :
Name Type Optional
request AuthRequest No
fundId string No
instrumentId string No
updatedNavDto UpdatedNavDto No
Returns : unknown
import {
  Body,
  Controller,
  Get,
  Inject,
  InternalServerErrorException,
  Param,
  Post,
  Put,
  Query,
  Req,
  ServiceUnavailableException,
  UseGuards,
  UseInterceptors,
} from "@nestjs/common";

import { CreateFundDto } from "./dto/create-fund.dto";
import { FundService } from "./fund.service";
import { UpdateFundDto } from "./dto/update-fund.dto";
import { RolesAuthGuard } from "src/auth/guards/roles-auth.guard";
import { AuthRequest } from "src/common/interfaces";
import { FilterFundDto } from "./dto/filter-fund.dto";
import { ApiBody } from "@nestjs/swagger";
import { UserService } from "src/user/user.service";
import { FundWeb3Service } from "./fund.web3.service";
import { UpdatedNavDto } from "./dto/update-nav.dto";
import { AuthGuard } from "@nestjs/passport";
import { CreateInstrumentDto } from "./dto/create-instrument.dto";
import { InstrumentService } from "./instrument.service";
import { UpdateInstrumentDto } from "./dto/update-instrument.dto";
import { encodeBytes32String } from "ethers";

import { CustomCacheInterceptor } from "src/interceptor/cache.interceptor";
import { FilterInstrumentDto } from "./dto/filter-instrument.dto";

@Controller("api/v1/security")
export class SecurityController {
  @Inject(UserService)
  private readonly userService: UserService;

  constructor(
    private readonly fundService: FundService,
    private readonly fundWeb3Service: FundWeb3Service,
    private readonly instrumentService: InstrumentService,
  ) {}

  @UseGuards(new RolesAuthGuard(["admin"]))
  @ApiBody({ type: [CreateFundDto] })
  @Post("/fund")
  async create(
    @Req() request: AuthRequest,
    @Body() createFundDto: CreateFundDto,
  ) {
    try {
      return await this.fundService.create(createFundDto, request.user.email);
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }

  @UseInterceptors(CustomCacheInterceptor)
  @UseGuards(AuthGuard("jwt"))
  @Get("/fund")
  async findAll(
    @Req() request: AuthRequest,
    @Query() filterFundDto: FilterFundDto,
  ) {
    try {
      if (request.user.role === "admin") {
        return await this.fundService.findFundsByFilter(filterFundDto);
      }
      const user = await this.userService.findUserByProperty({
        email: request.user.email,
      });
      const results = await this.fundService.findByIds({
        ids: user.subscribedFunds.map((fundId) => fundId.toString()),
        filterFundDto,
      });
      console.log({ results });
      return results;
    } catch (error) {
      console.error(error);
      throw new InternalServerErrorException(error);
    }
  }

  @UseInterceptors(CustomCacheInterceptor)
  @UseGuards(new RolesAuthGuard(["admin", "fundAdmin", "dealer", "investor"]))
  @Get("/fund/:id")
  async findOne(@Req() request: AuthRequest, @Param("id") id: string) {
    try {
      await this.userService.checkIfUserIsAllowedToFund(request.user, id);
      return this.fundService.findOne(id);
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }

  @UseGuards(new RolesAuthGuard(["admin", "fundAdmin"]))
  @Put("/fund/:id")
  async update(
    @Req() request: AuthRequest,
    @Param("id") id: string,
    @Body() updateFundDto: UpdateFundDto,
  ) {
    try {
      await this.userService.checkIfUserIsAllowedToFund(request.user, id);
      const file = await this.fundService.update(id, updateFundDto);
      return file;
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }

  @UseInterceptors(CustomCacheInterceptor)
  @UseGuards(new RolesAuthGuard(["admin", "dealer", "investor", "fundAdmin"]))
  @Get("/fund/:id/orderbook")
  async getFundOrder(@Req() request: AuthRequest, @Param("id") id: string) {
    await this.userService.checkIfUserIsAllowedToFund(request.user, id);
    try {
      return await this.fundWeb3Service.getFundOrders(id);
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException("Can't fetch from the subgraph");
    }
  }

  @UseGuards(new RolesAuthGuard(["admin", "fundAdmin"]))
  @UseInterceptors(CustomCacheInterceptor)
  @Get("/fund/:id/clients")
  async getAllFundClients(
    @Req() request: AuthRequest,
    @Param("id") id: string,
  ) {
    await this.userService.checkIfUserIsAllowedToFund(request.user, id);
    try {
      return await this.fundWeb3Service.getAllFundClients(id);
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException("Can't fetch from the subgraph");
    }
  }

  @UseGuards(new RolesAuthGuard(["admin"]))
  @Post("/fund/:id/instrument/")
  async createInstrument(
    @Req() request: AuthRequest,
    @Param("id") fundId: string,
    @Body() createInstrumentDto: CreateInstrumentDto,
  ) {
    try {
      return await this.instrumentService.create({
        adminEmail: request.user.email,
        ...createInstrumentDto,
        fundId,
      });
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }

  @UseGuards(new RolesAuthGuard(["admin", "fundAdmin"]))
  @Post("/fund/:id/instrument/:instrumentId")
  async initializeInstrument(
    @Req() request: AuthRequest,
    @Param("instrumentId") instrumentId: string,
  ) {
    try {
      return await this.instrumentService.initializeInstrument({
        userJwtPayload: request.user,
        instrumentId,
      });
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }

  @UseGuards(new RolesAuthGuard(["admin", "fundAdmin"]))
  @Put("/fund/:id/instrument/:instrumentId")
  async updateInstrument(
    @Param("instrumentId") instrumentId: string,
    @Body() updateInstrumentDto: UpdateInstrumentDto,
  ) {
    try {
      return await this.instrumentService.update(
        instrumentId,
        updateInstrumentDto,
      );
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }

  @UseGuards(AuthGuard("jwt"))
  @UseInterceptors(CustomCacheInterceptor)
  @Get("/fund/:id/instrument/")
  async getAllInstruments(
    @Req() request: AuthRequest,
    @Param("id") fundId: string,
    @Query() filterPaginationDto: FilterInstrumentDto,
  ) {
    try {
      await this.userService.checkIfUserIsAllowedToFund(request.user, fundId);
      const [subgraphInstruments, dbInstruments] = await Promise.all([
        this.fundWeb3Service.getInstrumentsByFundId(
          fundId,
          filterPaginationDto.limit,
          filterPaginationDto.skip,
          filterPaginationDto.orderDirection,
          request.user,
        ),
        this.instrumentService.findAllByFilter({
          fundId,
          ...filterPaginationDto,
        }),
      ]);
      return {
        subgraphInstruments: filterPaginationDto.selectors
          ? []
          : subgraphInstruments,
        dbInstruments: dbInstruments.filter((instrument) => {
          const index = subgraphInstruments.findIndex(
            (subgraphInstrument) =>
              subgraphInstrument.id ===
              encodeBytes32String(instrument._id.toString()),
          );
          return index !== -1;
        }),
      };
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }

  @UseGuards(AuthGuard("jwt"))
  @UseInterceptors(CustomCacheInterceptor)
  @Get("/fund/:id/instrument/:instrumentId")
  async getInstrumentById(
    @Req() request: AuthRequest,
    @Param("id") fundId: string,
    @Param("instrumentId") instrumentId: string,
  ) {
    try {
      await this.userService.checkIfUserIsAllowedToFund(request.user, fundId);
      const [subgraphInstrument, dbInstrument] = await Promise.all([
        this.fundWeb3Service.getInstrumentById(instrumentId),
        this.instrumentService.findOne(instrumentId),
      ]);
      return {
        subgraphInstrument,
        dbInstrument,
      };
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }

  @UseGuards(new RolesAuthGuard(["admin", "dealer", "investor", "fundAdmin"]))
  @UseInterceptors(CustomCacheInterceptor)
  @Get("/fund/:id/instrument/:instrumentId/orderbook")
  async getInstrumentOrderbook(
    @Req() request: AuthRequest,
    @Param("id") id: string,
    @Param("instrumentId") instrumentId: string,
  ) {
    try {
      if (request.user.role !== "admin") {
        await this.userService.checkIfUserIsAllowedToFund(
          request.user,
          instrumentId,
        );
      }
      return await this.instrumentService.getInstrumentOrders(instrumentId);
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException("Can't fetch from the subgraph");
    }
  }

  @UseGuards(new RolesAuthGuard(["admin", "fundAdmin"]))
  @ApiBody({ type: [UpdatedNavDto] })
  @Post("/fund/:fundId/instrument/:id/nav")
  async updateNav(
    @Req() request: AuthRequest,
    @Param("fundId") fundId: string,
    @Param("id") instrumentId: string,
    @Body() updatedNavDto: UpdatedNavDto,
  ) {
    try {
      if (request.user.role !== "admin") {
        await this.userService.checkIfUserIsAllowedToFund(request.user, fundId);
      }
      return await this.fundWeb3Service.updateNav({
        ...updatedNavDto,
        instrumentId,
        userJwtPayload: request.user,
      });
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }

  @UseGuards(AuthGuard("jwt"))
  @UseInterceptors(CustomCacheInterceptor)
  @Get("/fund/:fundId/instrument/:id/nav")
  async getNavs(
    @Req() request: AuthRequest,
    @Param("fundId") fundId: string,
    @Param("id") instrumentId: string,
  ) {
    try {
      if (request.user.role !== "admin") {
        await this.userService.checkIfUserIsAllowedToFund(request.user, fundId);
      }
      return await this.fundWeb3Service.getNavs(instrumentId);
    } catch (error) {
      console.error(error);
      throw new ServiceUnavailableException(error);
    }
  }
}

results matching ""

    No results matching ""