Skip to content

Commit 7169693

Browse files
Merge pull request #115 from Daniel-Alvarenga/main
Fix empresa aluno messages sockets
2 parents e6690f1 + 5287fc7 commit 7169693

File tree

7 files changed

+103
-25
lines changed

7 files changed

+103
-25
lines changed

client/src/services/api/aluno.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export function enterSockets(token){
55
socket.emit(
66
'enter',
77
{
8+
type: "ALUNO",
89
authorization: `${token}`
910
}
1011
);
@@ -346,6 +347,7 @@ export const sendMessage = async (infoMesssage, token) => {
346347

347348
socket.emit('send-message', {
348349
message: response.data.message,
350+
sender: "ALUNO",
349351
authorization: `${token}`
350352
});
351353

client/src/services/api/empresa.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
import { socket } from '../../socket';
12
import api from '../api';
2-
import { enterSockets } from './aluno';
3+
4+
export function enterSockets(token){
5+
socket.emit(
6+
'enter',
7+
{
8+
type: "EMPRESA",
9+
authorization: `${token}`
10+
}
11+
);
12+
}
313

414
export const authEmpresa = async (token) => {
515
try {
@@ -148,10 +158,10 @@ export const sendMessage = async (infoMesssage, token) => {
148158
}
149159
});
150160

151-
// socket.emit('send-message', {
152-
// message: response.data.message,
153-
// authorization: `${token}`
154-
// });
161+
socket.emit('send-message', {
162+
message: response.data.message,
163+
authorization: `${token}`
164+
});
155165

156166
return response;
157167
} catch (error) {

server/src/connection/controllers/entryController.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Server, Socket } from 'socket.io';
2-
import { validateTokenAluno } from '../../middleware/auth/socket';
2+
import { validateTokenAluno, validateTokenEmpresa, validateTokenProfessor } from '../../middleware/auth/socket';
33

44
interface dataDTO{
55
email: string,
@@ -10,14 +10,23 @@ interface dataDTO{
1010
export const enter = async (io: Server, socket: Socket, data: dataDTO) => {
1111
try {
1212
const token = data.authorization;
13-
const decoded = await validateTokenAluno(token) as any;
13+
let decoded;
1414

15+
if(data.type == "EMPRESA"){
16+
decoded = await validateTokenEmpresa(token) as any;
17+
}
18+
if(data.type == "ALUNO"){
19+
decoded = await validateTokenAluno(token) as any;
20+
}
21+
if(data.type == "PROFESSOR"){
22+
decoded = await validateTokenProfessor(token) as any;
23+
}
24+
1525
if (!decoded) {
1626
console.log('Token inválido para enter-vinculo-aluno');
1727
// socket.emit('error', { message: 'Invalid token' });
1828
return;
1929
}
20-
2130
console.log(`Usuário de email ${decoded.email} registrado`);
2231
socket.join(decoded.email);
2332

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
11
import { Server, Socket } from 'socket.io';
2-
import { validateTokenAluno } from '../../middleware/auth/socket';
3-
4-
interface dataDTO{
5-
message:{
6-
conteudo: string,
7-
email: string,
8-
identifier: string,
9-
},
10-
authorization: string
2+
import {
3+
validateTokenAluno,
4+
validateTokenEmpresa,
5+
validateTokenProfessor,
6+
} from '../../middleware/auth/socket';
7+
8+
import { IdentificadorEnum } from '../../modules/interfaces/sharedDTOs';
9+
10+
interface dataDTO {
11+
message: {
12+
conteudo: string;
13+
email: string;
14+
identifier: string;
15+
};
16+
sender: string;
17+
authorization: string;
1118
}
1219

1320
export const sendMessage = async (io: Server, socket: Socket, data: dataDTO) => {
1421
try {
1522
const token = data.authorization;
16-
const decoded = await validateTokenAluno(token);
23+
24+
let decoded = await validateTokenAluno(token) as any;
25+
26+
if (!decoded) {
27+
let decoded = await validateTokenEmpresa(token) as any;
28+
29+
if (!decoded) {
30+
let decoded = await validateTokenProfessor(token) as any;
31+
32+
if (!decoded) {
33+
console.log('Token inválido para enter-vinculo-aluno');
34+
// socket.emit('error', { message: 'Invalid token' });
35+
return;
36+
}
37+
}
38+
}
39+
1740

1841
if (!decoded) {
19-
console.log('Token inválido para vinculo-update');
20-
// socket.emit('error', { message: 'Invalid token' });
42+
console.log('Token inválido para sendMessage');
2143
return;
2244
}
2345

2446
console.log('Nova mensagem para: ' + data.message.email);
2547
io.to(data.message.email).emit('new-message', data.message);
2648
} catch (error) {
27-
console.log('Erro em vinculo-update handler:', error);
49+
console.log('Erro em sendMessage handler:', error);
2850
}
29-
};
51+
};

server/src/middleware/auth/socket.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AppError } from '../../errors/error';
33
import { JwtPayload } from 'jsonwebtoken';
44
import {
55
verfifyAccessTokenAluno,
6+
verfifyAccessTokenEmpresa,
67
verfifyAccessTokenFuncinario,
78
verfifyAccessTokenProfessor
89
} from '../../jwt/jwtServices';
@@ -21,7 +22,7 @@ export async function validateTokenAluno(token: string){
2122
throw new AppError('Invalid token');
2223
}
2324

24-
const aluno = await prisma.aluno.findUnique({
25+
const aluno = await prisma.aluno.findFirst({
2526
where: { id: (decoded as JwtPayload).alunoId }
2627
});
2728

@@ -89,3 +90,31 @@ export async function validateTokenFuncionario(token: string){
8990
throw new Error('Token inválido: ' + error);
9091
}
9192
};
93+
94+
export async function validateTokenEmpresa(token: string){
95+
try {
96+
const decoded = verfifyAccessTokenEmpresa(token);
97+
98+
if (!decoded || typeof decoded === 'string') {
99+
throw new Error('Invalid token');
100+
}
101+
102+
103+
if (!decoded || typeof decoded === 'string') {
104+
throw new AppError('Invalid token');
105+
}
106+
107+
const empresa = await prisma.empresa.findUnique({
108+
where: { id: (decoded as JwtPayload).empresaId }
109+
});
110+
111+
if (!empresa) {
112+
throw new Error('Empresa not found');
113+
}
114+
115+
return empresa;
116+
} catch (error) {
117+
throw new Error('Token inválido: ' + error);
118+
}
119+
};
120+

server/src/modules/services/shared/CreateMessageUseCase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class CreateMessageUseCase {
3636
const senderId = senderData.id;
3737
const recipientId = recipientData.id;
3838

39-
console.log(senderId, senderIdentifier, recipientId, recipientIdentifier, message);
39+
console.log("###\n\n\n\nNova mensagem: " + senderId, senderIdentifier, recipientId, recipientIdentifier, message);
4040

4141
const data: any = {
4242
conteudo: message,

server/src/modules/services/shared/GetChatUseCase.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { minioClient } from "../../../minioService";
77
export class GetMessagesBetweenUseCase {
88
async execute({ email1, identifier1, email2, identifier2 }: GetMessageBetweenDTO) {
99

10-
console.log(email1, identifier1, email2, identifier2);
10+
console.log("############\n\n\n\n\nPegando chat de: " + email1, identifier1, email2, identifier2);
1111

1212
if (!email1 || !identifier1 || !email2 || !identifier2) {
1313
throw new AppError("Parâmetros insufientes ou inválidos.");
@@ -32,7 +32,11 @@ export class GetMessagesBetweenUseCase {
3232
OR: [
3333
{ alunoRemetenteId: entidade1Id, alunoDestinatarioId: entidade2Id },
3434
{ alunoRemetenteId: entidade1Id, empresaDestinatarioId: entidade2Id },
35+
{ alunoRemetenteId: entidade2Id, empresaDestinatarioId: entidade1Id },
3536
{ empresaRemetenteId: entidade1Id, alunoDestinatarioId: entidade2Id },
37+
{ empresaRemetenteId: entidade2Id, alunoDestinatarioId: entidade1Id },
38+
{ professorRemetenteId: entidade1Id, alunoDestinatarioId: entidade2Id },
39+
{ alunoRemetenteId: entidade1Id, professorDestinatarioId: entidade2Id },
3640
]
3741
},
3842
orderBy: {
@@ -44,6 +48,8 @@ export class GetMessagesBetweenUseCase {
4448
let sender = 'other';
4549
if (message.alunoRemetenteId === entidade1Id) {
4650
sender = 'me';
51+
} else if (message.empresaRemetenteId === entidade1Id) {
52+
sender = 'me';
4753
}
4854
return {
4955
...message,

0 commit comments

Comments
 (0)