feat: add chatbot conversations endpoint
This commit is contained in:
@@ -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