import { Controller, Body, Post, Get, Param, BadRequestException, UseFilters, } from "@nestjs/common"; import { ConnectionManagerClient } from "@ocm-engine/clients"; import { CONNECTION_ACCEPT, CONNECTION_CREATE, CONNECTION_GET, CONNECTION_LIST, CreateInvitationResponseDto, GatewayAcceptedResponseDto, GetConnectionRequestDto, } from "@ocm-engine/dtos"; import { AllExceptionsHandler } from "../exception.handler"; import { ApiBadRequestResponse, ApiBody, ApiInternalServerErrorResponse, ApiOperation, ApiResponse, ApiTags, } from "@nestjs/swagger"; @UseFilters(AllExceptionsHandler) @Controller("v1") export class ConnectionController { constructor(private readonly cmClient: ConnectionManagerClient) {} @Post("/invitation") @ApiResponse({ status: 201, description: "Request is accepted for execution, the response id will match the event id received from the web socket", type: GatewayAcceptedResponseDto, }) @ApiBadRequestResponse({ status: 400, description: "Error in sending data to connection manager. This error shows that connection manager could not convert request to event or connection manager could not send the event to the broker.", type: BadRequestException, }) @ApiInternalServerErrorResponse({ status: 500, description: "Unknown error", }) @ApiOperation({ summary: "Create invitation for connection", description: "Method will create invitation url. The id of the response will be matched when you receive event from the websocket", tags: ["Connections"], }) createInvitation(): Promise<GatewayAcceptedResponseDto> { return this.cmClient.sendPayload<null>({ pattern: "connections", payload: { source: "/invitation", data: null, type: CONNECTION_CREATE, }, }); } @Post("/invitation/accept") @ApiResponse({ status: 201, description: "Request is accepted for execution, the response id will match the event id received from the web socket", type: GatewayAcceptedResponseDto, }) @ApiBadRequestResponse({ status: 400, description: "Error in sending data to connection manager. This error shows that connection manager could not convert request to event or connection manager could not send the event to the broker.", type: BadRequestException, }) @ApiInternalServerErrorResponse({ status: 500, description: "Unknown error", }) @ApiOperation({ summary: "Accept invitation for connection", description: "Method will accept the invitation and will return connection thought the websocket. The id of the response will be matched when you receive event from the websocket", tags: ["Connections"], }) async acceptInvitation( @Body() createInvitationDto: CreateInvitationResponseDto, ) { try { return this.cmClient.sendPayload<CreateInvitationResponseDto>({ pattern: "connections", payload: { source: "/invitation/accept", data: createInvitationDto, type: CONNECTION_ACCEPT, }, }); } catch (e) { throw new BadRequestException(e); } } @Get("/connections") @ApiResponse({ status: 200, description: "Request is accepted for execution, the response id will match the event id received from the web socket", type: GatewayAcceptedResponseDto, }) @ApiBadRequestResponse({ status: 400, description: "Error in sending data to connection manager. This error shows that connection manager could not convert request to event or connection manager could not send the event to the broker.", type: BadRequestException, }) @ApiInternalServerErrorResponse({ status: 500, description: "Unknown error", }) @ApiOperation({ summary: "List all connections", description: "The id of the response will be matched when you receive event from the websocket", tags: ["Connections"], }) async list() { return this.cmClient.sendPayload<null>({ pattern: "connections", payload: { source: "/connections", data: null, type: CONNECTION_LIST, }, }); } @Get("/connections/:id") @ApiResponse({ status: 200, description: "Request is accepted for execution, the response id will match the event id received from the web socket", type: GatewayAcceptedResponseDto, }) @ApiBadRequestResponse({ status: 400, description: "Error in sending data to connection manager. This error shows that connection manager could not convert request to event or connection manager could not send the event to the broker.", type: BadRequestException, }) @ApiInternalServerErrorResponse({ status: 500, description: "Unknown error", }) @ApiOperation({ summary: "Get connection by id", description: "The method will search for connection id, if not found null will be returned. The id of the response will be matched when you receive event from the websocket", tags: ["Connections"], }) async getById(@Param("id") id: string) { const request = new GetConnectionRequestDto(); request.connectionId = id; return this.cmClient.sendPayload<GetConnectionRequestDto>({ pattern: "connections", payload: { source: "/connections/:id", data: request, type: CONNECTION_GET, }, }); } }