feat: add chatbot conversations endpoint
This commit is contained in:
@@ -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([
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
GET {{baseUrl}}/chatbot/conversations
|
||||
Authorization: Bearer {{$global.accessToken}}
|
||||
@@ -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,
|
||||
|
||||
@@ -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<Repository<Conversation>['find']>();
|
||||
const service = new ConversationService(
|
||||
{ find } as unknown as Repository<Conversation>,
|
||||
{} as Repository<Message>,
|
||||
);
|
||||
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,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -24,6 +24,18 @@ export class ConversationService {
|
||||
private readonly messageRepository: Repository<Message>,
|
||||
) {}
|
||||
|
||||
public async findConversationsByUser(user: User): Promise<Conversation[]> {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user