feat: add get chatbot conversation by id endpoint
This commit is contained in:
@@ -4,6 +4,8 @@ import {
|
||||
Get,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Post,
|
||||
Res,
|
||||
} from '@nestjs/common';
|
||||
@@ -15,6 +17,7 @@ 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';
|
||||
import { GetConversationDetailsDTO } from './dtos/get-conversation-details.dto';
|
||||
|
||||
@Controller('chatbot')
|
||||
@Throttle({ default: { limit: 30, ttl: 60_000 } })
|
||||
@@ -38,6 +41,21 @@ export class ChatbotController {
|
||||
return this.chatbotService.getConversations(user);
|
||||
}
|
||||
|
||||
@Get('conversations/:conversationId')
|
||||
@ApiAppResponse([
|
||||
{
|
||||
status: HttpStatus.OK,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
variants: [{ model: GetConversationDetailsDTO }],
|
||||
},
|
||||
])
|
||||
public async getConversation(
|
||||
@Param('conversationId', ParseIntPipe) conversationId: number,
|
||||
@ActiveUser() user: User,
|
||||
) {
|
||||
return this.chatbotService.getConversation(user, conversationId);
|
||||
}
|
||||
|
||||
@Post('chat')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiAppResponse([
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { MessageRole } from '@/modules/conversation/enums/message-role.enum';
|
||||
import { GetConversationDTO } from './get-conversation.dto';
|
||||
|
||||
export class GetConversationMessageDTO {
|
||||
@ApiProperty({ type: 'number', example: 1 })
|
||||
messageId!: number;
|
||||
|
||||
@ApiProperty({ enum: MessageRole })
|
||||
role!: MessageRole;
|
||||
|
||||
@ApiProperty({ type: 'string' })
|
||||
content!: string;
|
||||
|
||||
@ApiProperty({ type: 'string', format: 'date-time' })
|
||||
createdAt!: Date;
|
||||
}
|
||||
|
||||
export class GetConversationDetailsDTO extends GetConversationDTO {
|
||||
@ApiProperty({ type: 'string', format: 'date-time' })
|
||||
createdAt!: Date;
|
||||
|
||||
@ApiProperty({ type: GetConversationMessageDTO, isArray: true })
|
||||
messages!: GetConversationMessageDTO[];
|
||||
}
|
||||
@@ -1,2 +1,6 @@
|
||||
GET {{baseUrl}}/chatbot/conversations
|
||||
Authorization: Bearer {{$global.accessToken}}
|
||||
|
||||
### Get a conversation owned by the authenticated user
|
||||
GET {{baseUrl}}/chatbot/conversations/1
|
||||
Authorization: Bearer {{$global.accessToken}}
|
||||
|
||||
@@ -38,6 +38,27 @@ export class ChatbotService {
|
||||
}));
|
||||
}
|
||||
|
||||
public async getConversation(user: User, conversationId: number) {
|
||||
const conversation = await this.conversationService.findConversationById(
|
||||
conversationId,
|
||||
user,
|
||||
);
|
||||
const messages = await this.conversationService.getMessages(conversation);
|
||||
|
||||
return {
|
||||
conversationId: conversation.id,
|
||||
title: conversation.title,
|
||||
createdAt: conversation.createdAt,
|
||||
updatedAt: conversation.updatedAt,
|
||||
messages: messages.map((message) => ({
|
||||
messageId: message.id,
|
||||
role: message.role,
|
||||
content: message.content,
|
||||
createdAt: message.createdAt,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
private async getConversationMessages(
|
||||
user: User,
|
||||
message: string,
|
||||
|
||||
@@ -5,6 +5,7 @@ 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';
|
||||
import { HttpStatus } from '@nestjs/common';
|
||||
|
||||
describe('Conversation list', () => {
|
||||
const find = jest.fn<Repository<Conversation>['find']>();
|
||||
@@ -36,3 +37,50 @@ describe('Conversation list', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Conversation details access', () => {
|
||||
const findOne = jest.fn<Repository<Conversation>['findOne']>();
|
||||
const findMessages = jest.fn<Repository<Message>['find']>();
|
||||
const service = new ConversationService(
|
||||
{ findOne } as unknown as Repository<Conversation>,
|
||||
{ find: findMessages } as unknown as Repository<Message>,
|
||||
);
|
||||
const user = { id: 42 } as User;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('looks up a conversation by both its ID and its owner', async () => {
|
||||
const conversation = { id: 7, userId: 42 } as Conversation;
|
||||
findOne.mockResolvedValue(conversation);
|
||||
|
||||
await expect(service.findConversationById(7, user)).resolves.toBe(
|
||||
conversation,
|
||||
);
|
||||
expect(findOne).toHaveBeenCalledWith({
|
||||
where: { id: 7, user: { id: 42 } },
|
||||
});
|
||||
});
|
||||
|
||||
it('returns not found for missing conversations or conversations owned by another user', async () => {
|
||||
findOne.mockResolvedValue(null);
|
||||
|
||||
await expect(service.findConversationById(7, user)).rejects.toMatchObject({
|
||||
status: HttpStatus.NOT_FOUND,
|
||||
});
|
||||
expect(findMessages).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('loads only the conversation messages in chronological order with a stable tie breaker', async () => {
|
||||
findMessages.mockResolvedValue([]);
|
||||
|
||||
await expect(
|
||||
service.getMessages({ id: 7 } as Conversation),
|
||||
).resolves.toEqual([]);
|
||||
expect(findMessages).toHaveBeenCalledWith({
|
||||
where: { conversation: { id: 7 } },
|
||||
order: { createdAt: 'ASC', id: 'ASC' },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -112,6 +112,7 @@ export class ConversationService {
|
||||
},
|
||||
order: {
|
||||
createdAt: 'ASC',
|
||||
id: 'ASC',
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user