diff --git a/backend/src/modules/chat/chat.module.ts b/backend/src/modules/chat/chat.module.ts index 88ee0b2..e21bc66 100644 --- a/backend/src/modules/chat/chat.module.ts +++ b/backend/src/modules/chat/chat.module.ts @@ -30,6 +30,6 @@ import { User, UserSchema } from '../user/user.schema'; { provide: 'IChatRepository', useExisting: ChatRepositoryMongodb }, { provide: 'IMessageRepository', useExisting: MessageRepositoryMongodb }, ], - exports: [ChatService], + exports: [ChatService, 'IChatRepository'], }) export class ChatModule {} \ No newline at end of file diff --git a/backend/src/modules/chat/domain/chat.repository.ts b/backend/src/modules/chat/domain/chat.repository.ts index b1113ed..fed5979 100644 --- a/backend/src/modules/chat/domain/chat.repository.ts +++ b/backend/src/modules/chat/domain/chat.repository.ts @@ -1,20 +1,63 @@ -import { ChatDetails, ChatStatus } from './chat.entity'; +/* eslint-disable @typescript-eslint/unbound-method */ +import { randomUUID } from 'crypto'; +import { ITicketRepository } from '../../../domain/repository/ticket.repository.interface'; +import { Ticket, TicketStatus } from '../../../domain/entities/ticket.entity'; +import { NewAgentTicketUseCase } from './newAgent.usecase'; +import { ChatService } from '../../../../chat/application/chat.service'; -export interface IChatRepository { - create(data: { - ticketId: string; - clientId: string; - agentId: string; - groupId: string; - }): Promise; +describe('NewAgentTicketUseCase', () => { + let repository: jest.Mocked; + let chatService: jest.Mocked; + let useCase: NewAgentTicketUseCase; + let ticket: Ticket; - findById(id: string): Promise; + beforeEach(() => { + ticket = Ticket.create({ + title: 'titulo do ticket', + category: 'bi', + description: 'descricao do ticket', + clientId: randomUUID(), + }); - findByParticipant(userId: string): Promise; + repository = { + readById: jest.fn(), + save: jest.fn(), + } as unknown as jest.Mocked; - findByTicketId(ticketId: string): Promise; + // Usando o mock do ChatService conforme o padrão da develop + chatService = { + updateAgentByTicketId: jest.fn(), + } as unknown as jest.Mocked; - updateStatus(id: string, status: ChatStatus): Promise; + useCase = new NewAgentTicketUseCase(repository, chatService); + }); - updateAgent(ticketId: string, agentId: string | null): Promise; -} + it('should assign a new agent to a ticket successfully and update the chat', async () => { + const input = { + id: ticket.id, + agentId: randomUUID(), + }; + + repository.readById.mockResolvedValue(ticket); + ticket.assignToAgent(input.agentId); + repository.save.mockResolvedValue(ticket); + + const output = await useCase.execute(input); + + expect(output).toBeDefined(); + expect(output.id).toBe(ticket.id); + expect(output.agentId).toBe(input.agentId); + expect(output.status).toBe(TicketStatus.IN_PROGRESS); + + expect(repository.readById).toHaveBeenCalledTimes(1); + expect(repository.readById).toHaveBeenCalledWith(input.id); + expect(repository.save).toHaveBeenCalledTimes(1); + + expect(chatService.updateAgentByTicketId).toHaveBeenCalledTimes(1); + expect(chatService.updateAgentByTicketId).toHaveBeenCalledWith(ticket.id, input.agentId); + + expect(output).not.toHaveProperty('toPrimitives'); + expect(output).not.toHaveProperty('escalate'); + expect(output).not.toHaveProperty('assignToAgent'); + }); +}); \ No newline at end of file diff --git a/backend/src/modules/chat/infra/chat.repository.mongodb.ts b/backend/src/modules/chat/infra/chat.repository.mongodb.ts index d980d36..3e12625 100644 --- a/backend/src/modules/chat/infra/chat.repository.mongodb.ts +++ b/backend/src/modules/chat/infra/chat.repository.mongodb.ts @@ -39,7 +39,7 @@ export class ChatRepositoryMongodb implements IChatRepository { async findById(id: string): Promise { const chatDoc = await this.chatModel.findById(id).exec(); - + if (!chatDoc) return null; return { id: chatDoc._id as string, @@ -53,7 +53,7 @@ export class ChatRepositoryMongodb implements IChatRepository { async findByTicketId(ticketId: string): Promise { const chatDoc = await this.chatModel.findOne({ ticketId }).exec(); - + if (!chatDoc) return null; return { id: chatDoc._id as string, @@ -93,4 +93,4 @@ export class ChatRepositoryMongodb implements IChatRepository { if (!doc) return null; return this.toDetails(doc); } -} +} \ No newline at end of file diff --git a/backend/src/modules/ticket/application/useCases/newAgent/newAgent.usecase.spec.ts b/backend/src/modules/ticket/application/useCases/newAgent/newAgent.usecase.spec.ts index ff346f5..d6eb2cf 100644 --- a/backend/src/modules/ticket/application/useCases/newAgent/newAgent.usecase.spec.ts +++ b/backend/src/modules/ticket/application/useCases/newAgent/newAgent.usecase.spec.ts @@ -1,10 +1,7 @@ /* eslint-disable @typescript-eslint/unbound-method */ import { randomUUID } from 'crypto'; import { ITicketRepository } from '../../../domain/repository/ticket.repository.interface'; -import { - Ticket, - TicketStatus, -} from '../../../domain/entities/ticket.entity'; +import { Ticket, TicketStatus } from '../../../domain/entities/ticket.entity'; import { NewAgentTicketUseCase } from './newAgent.usecase'; import { ChatService } from '../../../../chat/application/chat.service'; @@ -34,14 +31,13 @@ describe('NewAgentTicketUseCase', () => { useCase = new NewAgentTicketUseCase(repository, chatService); }); - it('should assing a new agent to a ticket successfully', async () => { + it('should assign a new agent to a ticket successfully', async () => { const input = { id: ticket.id, agentId: randomUUID(), }; repository.readById.mockResolvedValue(ticket); - ticket.assignToAgent(input.agentId); repository.save.mockResolvedValue(ticket); @@ -54,12 +50,13 @@ describe('NewAgentTicketUseCase', () => { expect(repository.readById).toHaveBeenCalledTimes(1); expect(repository.readById).toHaveBeenCalledWith(input.id); - expect(repository.save).toHaveBeenCalledTimes(1); expect(repository.save).toHaveBeenCalledWith(ticket); + expect(chatService.updateAgentByTicketId).toHaveBeenCalledTimes(1); + expect(chatService.updateAgentByTicketId).toHaveBeenCalledWith(ticket.id, input.agentId); expect(output).not.toHaveProperty('toPrimitives'); expect(output).not.toHaveProperty('escalate'); expect(output).not.toHaveProperty('assignToAgent'); }); -}); +}); \ No newline at end of file diff --git a/backend/src/modules/ticket/application/useCases/newAgent/newAgent.usecase.ts b/backend/src/modules/ticket/application/useCases/newAgent/newAgent.usecase.ts index 8f80b46..e5380a6 100644 --- a/backend/src/modules/ticket/application/useCases/newAgent/newAgent.usecase.ts +++ b/backend/src/modules/ticket/application/useCases/newAgent/newAgent.usecase.ts @@ -27,7 +27,7 @@ export class NewAgentTicketUseCase { if (!foundedTicket) { throw new Error('Ticket not found.'); } - + foundedTicket.assignToAgent(input.agentId); const updatedTicket = await this.repository.save(foundedTicket); @@ -39,7 +39,6 @@ export class NewAgentTicketUseCase { try { await this.chatService.updateAgentByTicketId(updatedTicket.id, updatedTicket.agentId); } catch (e) { - // Ignora se o chat não existir, pois os módulos estão fracamente acoplados console.warn('Chat não pôde ser atualizado ou não existe:', e); } @@ -49,4 +48,4 @@ export class NewAgentTicketUseCase { status: updatedTicket.status, }; } -} +} \ No newline at end of file