File

src/chatbot/chatbot.controller.ts

Prefix

chat

Index

Methods

Methods

Async askBot
askBot(req, userMessage: UserMessage)
Decorators :
@Post('ask')
@UsePipes(new ValidationPipe())
Parameters :
Name Type Optional
req No
userMessage UserMessage No
Returns : unknown
findOne
findOne(id: string)
Decorators :
@Get(':id')
@ApiOkResponse({type: Chat})
@UsePipes(new ValidationPipe())
Parameters :
Name Type Optional
id string No
Async getChatsForUser
getChatsForUser(req, queryOptions: literal type)
Decorators :
@Get()
@ApiOkResponse({type: undefined})
Parameters :
Name Type Optional
req No
queryOptions literal type No
Returns : Promise<Chat[]>
Async getMessagesForChat
getMessagesForChat(chatId: string, queryOptions: literal type)
Decorators :
@Get('/:id/messages')
@ApiOkResponse({type: undefined})
Parameters :
Name Type Optional
chatId string No
queryOptions literal type No
Returns : Promise<Message[]>
Async update
update(id: string, updateChatDto: UpdateChatDto)
Decorators :
@Patch(':id')
@ApiOkResponse({type: Chat})
@UsePipes(new ValidationPipe())
Parameters :
Name Type Optional
id string No
updateChatDto UpdateChatDto No
Returns : Promise<Chat>
import {
  Body,
  Controller,
  Get,
  Param,
  Patch,
  Post,
  Query,
  Request,
  UsePipes,
  ValidationPipe,
} from "@nestjs/common";
import { ChatBotService } from "./chatbot.service";
import { UserMessage } from "./dto/userMessage.dto";
import { ChatService } from "./chat.service";
import { MessageService } from "./message.service";
import { UpdateChatDto } from "./dto/chat.dto";
import { Chat, ChatStatus } from "./entities/chat.entity";
import { Message } from "./entities/message.entity";
import { ApiBearerAuth, ApiOkResponse } from "@nestjs/swagger";

@ApiBearerAuth("access-token") // adds auth token with request in swagger
@Controller("chat")
export class ChatBotController {
  constructor(
    private readonly chatBotService: ChatBotService,
    private readonly chatService: ChatService,
    private readonly messageService: MessageService,
  ) {}

  @Post("ask")
  @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
  async askBot(@Request() req, @Body() userMessage: UserMessage) {
    const userId = req.user.id;
    return await this.chatBotService.ingestUserMessage(userMessage, userId);
  }

  @Get("/:id/messages")
  @ApiOkResponse({ type: [Message] })
  async getMessagesForChat(
    @Param("id") chatId: string,
    @Query()
    queryOptions: {
      pageSize?: number;
      pageOffset?: number;
      startId?: number;
    },
  ): Promise<Message[]> {
    try {
      return await this.chatService.findMessagesForChat(+chatId, queryOptions);
    } catch (error) {
      console.error(error);
      return [];
    }
  }

  @Patch(":id")
  @ApiOkResponse({ type: Chat })
  @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
  async update(
    @Param("id") id: string,
    @Body() updateChatDto: UpdateChatDto,
  ): Promise<Chat> {
    return await this.chatService.update(+id, updateChatDto);
  }

  @Get(":id")
  @ApiOkResponse({ type: Chat })
  @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
  findOne(@Param("id") id: string): Promise<Chat | null> {
    return this.chatService.findOne(+id);
  }

  @Get()
  @ApiOkResponse({ type: [Chat] })
  async getChatsForUser(
    @Request() req,
    @Query()
    queryOptions: {
      pageSize?: number;
      pageOffset?: number;
      startId?: number;
      status?: ChatStatus;
    },
  ): Promise<Chat[]> {
    const userId = req.user.id;
    const chats = await this.chatService.findForUser(userId, queryOptions);
    return chats;
  }
}

results matching ""

    No results matching ""