feat: add chatbot conversations endpoint

This commit is contained in:
2026-11-14 23:24:10 +03:30
parent 8d6834f44c
commit d572f6b7a2
6 changed files with 89 additions and 0 deletions
@@ -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,