Newer
Older
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,
ApiInternalServerErrorResponse,
ApiOperation,
ApiResponse,
} from "@nestjs/swagger";
@UseFilters(AllExceptionsHandler)
@Controller("v1")
export class ConnectionController {
constructor(private readonly cmClient: ConnectionManagerClient) {}
@ApiResponse({
status: 201,
description:
"Request is accepted for execution, the response id will match the event id received from the web socket",
type: GatewayAcceptedResponseDto,
})
@ApiInternalServerErrorResponse({
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.",
content: {
"application/json": {
schema: {
type: "object",
properties: {
statusCode: {
type: "number",
example: 500,
},
message: {
type: "string",
example: "connect ECONNREFUSED 0.0.0.0.0:4312",
},
},
},
},
},
})
@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,
},
});
}
@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({
description: "Validation error",
content: {
"application/json": {
schema: {
type: "object",
properties: {
statusCode: {
type: "number",
example: 400,
},
message: {
type: "array",
example: [
"invitationUrl must be a string",
"invitationUrl should not be empty",
],
},
error: {
type: "string",
example: "Bad Request",
},
},
},
},
},
})
@ApiInternalServerErrorResponse({
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.",
content: {
"application/json": {
schema: {
type: "object",
properties: {
statusCode: {
type: "number",
example: 500,
},
message: {
type: "string",
example: "connect ECONNREFUSED 0.0.0.0.0:4312",
},
},
},
},
},
summary: "Accept invitation long and short urls for connection",
"Method will accept long and short invitation urls 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: "/invitations/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,
})
@ApiInternalServerErrorResponse({
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.",
content: {
"application/json": {
schema: {
type: "object",
properties: {
statusCode: {
type: "number",
example: 500,
},
message: {
type: "string",
example: "connect ECONNREFUSED 0.0.0.0.0:4312",
},
},
},
},
},
})
@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,
})
@ApiInternalServerErrorResponse({
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.",
content: {
"application/json": {
schema: {
type: "object",
properties: {
statusCode: {
type: "number",
example: 500,
},
message: {
type: "string",
example: "connect ECONNREFUSED 0.0.0.0.0:4312",
},
},
},
},
},
})
@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,
},
});
}
}