From d572f6b7a2ef7293c8018bc4663c12ce39b857d9 Mon Sep 17 00:00:00 2001 From: radmehr Date: Sat, 14 Nov 2026 23:24:10 +0330 Subject: [PATCH] feat: add chatbot conversations endpoint --- src/modules/chatbot/chatbot.controller.ts | 14 +++++++ .../chatbot/dtos/get-conversation.dto.ts | 12 ++++++ .../chatbot/http/chatbot.get.endpoints.http | 2 + .../chatbot/providers/chatbot.service.ts | 11 ++++++ .../providers/conversation.service.spec.ts | 38 +++++++++++++++++++ .../providers/conversation.service.ts | 12 ++++++ 6 files changed, 89 insertions(+) create mode 100644 src/modules/chatbot/dtos/get-conversation.dto.ts create mode 100644 src/modules/chatbot/http/chatbot.get.endpoints.http create mode 100644 src/modules/conversation/providers/conversation.service.spec.ts diff --git a/src/modules/chatbot/chatbot.controller.ts b/src/modules/chatbot/chatbot.controller.ts index 3cc0929..2b6fe0c 100644 --- a/src/modules/chatbot/chatbot.controller.ts +++ b/src/modules/chatbot/chatbot.controller.ts @@ -1,6 +1,7 @@ import { Body, Controller, + Get, HttpCode, HttpStatus, Post, @@ -13,6 +14,7 @@ import { ActiveUser } from '@/common/decorators/active-user.decorator'; import { User } from '../users/entities/user.entity'; import { ApiAppResponse } from '@/common/decorators/api-app-response.decorator'; import { Throttle } from '@nestjs/throttler'; +import { GetConversationDTO } from './dtos/get-conversation.dto'; @Controller('chatbot') @Throttle({ default: { limit: 30, ttl: 60_000 } }) @@ -24,6 +26,18 @@ export class ChatbotController { private readonly chatbotService: ChatbotService, ) {} + @Get('conversations') + @ApiAppResponse([ + { + status: HttpStatus.OK, + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + variants: [{ model: GetConversationDTO, isArray: true }], + }, + ]) + public async getConversations(@ActiveUser() user: User) { + return this.chatbotService.getConversations(user); + } + @Post('chat') @HttpCode(HttpStatus.OK) @ApiAppResponse([ diff --git a/src/modules/chatbot/dtos/get-conversation.dto.ts b/src/modules/chatbot/dtos/get-conversation.dto.ts new file mode 100644 index 0000000..d593ddf --- /dev/null +++ b/src/modules/chatbot/dtos/get-conversation.dto.ts @@ -0,0 +1,12 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class GetConversationDTO { + @ApiProperty({ type: 'number', example: 1 }) + conversationId!: number; + + @ApiProperty({ type: 'string', nullable: true }) + title!: string | null; + + @ApiProperty({ type: 'string', format: 'date-time' }) + updatedAt!: Date; +} diff --git a/src/modules/chatbot/http/chatbot.get.endpoints.http b/src/modules/chatbot/http/chatbot.get.endpoints.http new file mode 100644 index 0000000..fa42bba --- /dev/null +++ b/src/modules/chatbot/http/chatbot.get.endpoints.http @@ -0,0 +1,2 @@ +GET {{baseUrl}}/chatbot/conversations +Authorization: Bearer {{$global.accessToken}} diff --git a/src/modules/chatbot/providers/chatbot.service.ts b/src/modules/chatbot/providers/chatbot.service.ts index b83b843..3eb9ebe 100644 --- a/src/modules/chatbot/providers/chatbot.service.ts +++ b/src/modules/chatbot/providers/chatbot.service.ts @@ -27,6 +27,17 @@ export class ChatbotService { private readonly openRouterService: OpenRouterService, ) {} + public async getConversations(user: User) { + const conversations = + await this.conversationService.findConversationsByUser(user); + + return conversations.map((conversation) => ({ + conversationId: conversation.id, + title: conversation.title, + updatedAt: conversation.updatedAt, + })); + } + private async getConversationMessages( user: User, message: string, diff --git a/src/modules/conversation/providers/conversation.service.spec.ts b/src/modules/conversation/providers/conversation.service.spec.ts new file mode 100644 index 0000000..4f5ab33 --- /dev/null +++ b/src/modules/conversation/providers/conversation.service.spec.ts @@ -0,0 +1,38 @@ +import { Repository } from 'typeorm'; +import { beforeEach, describe, expect, it, jest } from '@jest/globals'; +import { ConversationService } from './conversation.service'; +import { Conversation } from '../entities/conversation.entity'; +import { Message } from '../entities/message.entity'; +import { User } from '@/modules/users/entities/user.entity'; +import { AppTimeoutException } from '@/common/exceptions/app-timeout.exception'; + +describe('Conversation list', () => { + const find = jest.fn['find']>(); + const service = new ConversationService( + { find } as unknown as Repository, + {} as Repository, + ); + const user = { id: 42 } as User; + + // eslint-disable-next-line @typescript-eslint/require-await + beforeEach(async () => jest.resetAllMocks()); + + it('restricts results to the authenticated user and orders newest first', async () => { + find.mockResolvedValue([]); + + await expect(service.findConversationsByUser(user)).resolves.toEqual([]); + expect(find).toHaveBeenCalledWith({ + select: { id: true, title: true, updatedAt: true }, + where: { userId: 42 }, + order: { updatedAt: 'DESC', id: 'DESC' }, + }); + }); + + it('handles repository failures using the application exception', async () => { + find.mockRejectedValue(new Error('Database unavailable')); + + await expect(service.findConversationsByUser(user)).rejects.toBeInstanceOf( + AppTimeoutException, + ); + }); +}); diff --git a/src/modules/conversation/providers/conversation.service.ts b/src/modules/conversation/providers/conversation.service.ts index 2dfeec6..3a8007f 100644 --- a/src/modules/conversation/providers/conversation.service.ts +++ b/src/modules/conversation/providers/conversation.service.ts @@ -24,6 +24,18 @@ export class ConversationService { private readonly messageRepository: Repository, ) {} + public async findConversationsByUser(user: User): Promise { + try { + return await this.conversationRepository.find({ + select: { id: true, title: true, updatedAt: true }, + where: { userId: user.id }, + order: { updatedAt: 'DESC', id: 'DESC' }, + }); + } catch (err) { + throw new AppTimeoutException(err); + } + } + public async createConversation( user: User, title?: string,