feat: add get chatbot conversation by id endpoint

This commit is contained in:
2026-11-14 23:58:34 +03:30
parent d572f6b7a2
commit 88b4a99908
6 changed files with 117 additions and 0 deletions
@@ -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) {