Skip to content
Merged
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ temp/
# Database local
# ======================
*.sqlite
*.db
*.db

# ======================
# Files local storage
# ======================
uploads/
32 changes: 27 additions & 5 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@nestjs/jwt": "^11.0.2",
"@nestjs/mongoose": "^11.0.4",
"@nestjs/passport": "^11.0.5",
"@nestjs/platform-express": "^11.0.1",
"@nestjs/platform-express": "^11.1.19",
"@nestjs/platform-socket.io": "^11.1.17",
"@nestjs/swagger": "^11.2.6",
"@nestjs/websockets": "^11.1.17",
Expand All @@ -36,6 +36,7 @@
"class-validator": "^0.15.1",
"mongoose": "^9.3.1",
"morgan": "^1.10.1",
"multer": "^2.1.1",
"node-nlp": "^5.0.0-alpha.5",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
Expand All @@ -52,6 +53,7 @@
"@types/express": "^5.0.0",
"@types/jest": "^30.0.0",
"@types/morgan": "^1.9.10",
"@types/multer": "^2.1.0",
"@types/node": "^22.10.7",
"@types/passport-jwt": "^4.0.1",
"@types/supertest": "^6.0.2",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ChatModule } from './modules/chat/chat.module';
import { TriageModule } from './modules/triage/triage.module';
import { CategoryModule } from './modules/category/category.module';
import { TicketModule } from './modules/ticket/ticket.module';
import { FileModule } from './modules/file/file.module';

@Module({
imports: [
Expand All @@ -28,6 +29,7 @@ import { TicketModule } from './modules/ticket/ticket.module';
TriageModule,
CategoryModule,
TicketModule,
FileModule,
],
controllers: [],
providers: [],
Expand Down
3 changes: 3 additions & 0 deletions backend/src/modules/Messages/infra/message.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class Message {

@Prop({ required: false })
readAt?: Date; // Controle pra saber se a mensagem foi lida

@Prop({ type: [String], default: [] })
fileIds?: string[]; // Arquivos anexados a mensagem
}

export const MessageSchema = SchemaFactory.createForClass(Message);
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class MessageGateway
senderId: data.senderId,
content: data.content,
isSystemMessage: data.isSystemMessage || false,
fileIds: data.fileIds || [],
});

console.log(`Mensagem salva no banco com ID: ${mensagemSalva.id}`);
Expand Down
192 changes: 192 additions & 0 deletions backend/src/modules/chat/application/chat.file-message.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ChatService } from './chat.service';
import { UserRole } from '../../user/user.schema';
import { NotFoundException } from '@nestjs/common';

import type { IChatRepository } from '../domain/chat.repository';
import type { IMessageRepository } from '../../Messages/domain/message.repository';

/* ===========================
MOCKS
=========================== */

const mockChatRepository: jest.Mocked<IChatRepository> = {
create: jest.fn(),
findById: jest.fn(),
findByParticipant: jest.fn(),
findByTicketId: jest.fn(),
updateStatus: jest.fn(),
};

const mockMessageRepository: jest.Mocked<IMessageRepository> = {
create: jest.fn(),
findByChatId: jest.fn(),
};

/* ===========================
CONSTANTES
=========================== */

const CHAT_ID = 'chat-001';
const CLIENT_ID = 'user-001';

const mockChat = {
id: CHAT_ID,
ticketId: 'ticket-001',
clientId: CLIENT_ID,
agentId: 'agent-001',
groupId: 'group-001',
status: 'open',
createdAt: new Date(),
};

/* ===========================
TESTES
=========================== */

describe('ChatService — envio de mensagem com arquivo', () => {
let service: ChatService;

beforeEach(async () => {
const module: TestingModule =
await Test.createTestingModule({
providers: [
ChatService,
{
provide: 'IChatRepository',
useValue: mockChatRepository,
},
{
provide: 'IMessageRepository',
useValue: mockMessageRepository,
},
],
}).compile();

service =
module.get<ChatService>(ChatService);

jest.clearAllMocks();
});

/* ===========================
TESTE PRINCIPAL
=========================== */

it('should send message with attached file', async () => {

const FILE_ID = 'file-001';

const mockSavedMessage = {
id: 'msg-001',
chatId: CHAT_ID,
senderId: CLIENT_ID,
content: 'Arquivo anexado',
fileIds: [FILE_ID],
isSystemMessage: false,
createdAt: new Date(),
};

/* Chat existe */
mockChatRepository
.findById
.mockResolvedValue(mockChat as any);

/* Mensagem salva */
mockMessageRepository
.create
.mockResolvedValue(mockSavedMessage as any);

const result =
await service.sendMessage(
CHAT_ID,
CLIENT_ID,
UserRole.CLIENT,
'Arquivo anexado',
[FILE_ID],
);

expect(result)
.toEqual(mockSavedMessage);

expect(
mockMessageRepository.create
).toHaveBeenCalledWith({
chatId: CHAT_ID,
senderId: CLIENT_ID,
content: 'Arquivo anexado',
isSystemMessage: false,
fileIds: [FILE_ID],
});
});

/* ===========================
TESTE: SEM ARQUIVO
=========================== */

it('should send message without fileIds', async () => {

const mockSavedMessage = {
id: 'msg-002',
chatId: CHAT_ID,
senderId: CLIENT_ID,
content: 'Mensagem normal',
fileIds: [],
isSystemMessage: false,
createdAt: new Date(),
};

mockChatRepository
.findById
.mockResolvedValue(mockChat as any);

mockMessageRepository
.create
.mockResolvedValue(mockSavedMessage as any);

const result =
await service.sendMessage(
CHAT_ID,
CLIENT_ID,
UserRole.CLIENT,
'Mensagem normal',
);

expect(result)
.toEqual(mockSavedMessage);

expect(
mockMessageRepository.create
).toHaveBeenCalledWith({
chatId: CHAT_ID,
senderId: CLIENT_ID,
content: 'Mensagem normal',
isSystemMessage: false,
fileIds: [],
});
});

/* ===========================
TESTE: CHAT NÃO EXISTE
=========================== */

it('should throw NotFoundException when chat does not exist', async () => {

mockChatRepository
.findById
.mockResolvedValue(null);

await expect(
service.sendMessage(
'invalid-chat',
CLIENT_ID,
UserRole.CLIENT,
'Teste',
['file-001'],
)
).rejects.toThrow(
NotFoundException,
);
});

});
1 change: 1 addition & 0 deletions backend/src/modules/chat/application/chat.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ describe('ChatService', () => {
senderId: CLIENT_ID,
content: 'Olá, preciso de ajuda!',
isSystemMessage: false,
fileIds: [],
});
});

Expand Down
2 changes: 2 additions & 0 deletions backend/src/modules/chat/application/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class ChatService {
senderId: string,
senderRole: UserRole,
content: string,
fileIds?: string[],
): Promise<any> {
const chat = await this.chatRepository.findById(chatId);
if (!chat) {
Expand All @@ -63,6 +64,7 @@ export class ChatService {
senderId,
content,
isSystemMessage: false,
fileIds: fileIds || [],
});
}

Expand Down
Loading
Loading