Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/modules/chat/chat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
71 changes: 57 additions & 14 deletions backend/src/modules/chat/domain/chat.repository.ts
Original file line number Diff line number Diff line change
@@ -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<ChatDetails>;
describe('NewAgentTicketUseCase', () => {
let repository: jest.Mocked<ITicketRepository>;
let chatService: jest.Mocked<ChatService>;
let useCase: NewAgentTicketUseCase;
let ticket: Ticket;

findById(id: string): Promise<ChatDetails | null>;
beforeEach(() => {
ticket = Ticket.create({
title: 'titulo do ticket',
category: 'bi',
description: 'descricao do ticket',
clientId: randomUUID(),
});

findByParticipant(userId: string): Promise<ChatDetails[]>;
repository = {
readById: jest.fn(),
save: jest.fn(),
} as unknown as jest.Mocked<ITicketRepository>;

findByTicketId(ticketId: string): Promise<ChatDetails | null>;
// Usando o mock do ChatService conforme o padrão da develop
chatService = {
updateAgentByTicketId: jest.fn(),
} as unknown as jest.Mocked<ChatService>;

updateStatus(id: string, status: ChatStatus): Promise<ChatDetails | null>;
useCase = new NewAgentTicketUseCase(repository, chatService);
});

updateAgent(ticketId: string, agentId: string | null): Promise<ChatDetails | null>;
}
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');
});
});
6 changes: 3 additions & 3 deletions backend/src/modules/chat/infra/chat.repository.mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ChatRepositoryMongodb implements IChatRepository {

async findById(id: string): Promise<ChatDetails | null> {
const chatDoc = await this.chatModel.findById(id).exec();

if (!chatDoc) return null;
return {
id: chatDoc._id as string,
Expand All @@ -53,7 +53,7 @@ export class ChatRepositoryMongodb implements IChatRepository {

async findByTicketId(ticketId: string): Promise<ChatDetails | null> {
const chatDoc = await this.chatModel.findOne({ ticketId }).exec();

if (!chatDoc) return null;
return {
id: chatDoc._id as string,
Expand Down Expand Up @@ -93,4 +93,4 @@ export class ChatRepositoryMongodb implements IChatRepository {
if (!doc) return null;
return this.toDetails(doc);
}
}
}
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);

Expand All @@ -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');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand All @@ -49,4 +48,4 @@ export class NewAgentTicketUseCase {
status: updatedTicket.status,
};
}
}
}
Loading