Skip to content

Commit

Permalink
Merge pull request VitorCarvalho67#115 from Daniel-Alvarenga/main
Browse files Browse the repository at this point in the history
Fix empresa aluno messages sockets
  • Loading branch information
Daniel-Alvarenga authored Nov 25, 2024
2 parents e6690f1 + 5287fc7 commit 7169693
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 25 deletions.
2 changes: 2 additions & 0 deletions client/src/services/api/aluno.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export function enterSockets(token){
socket.emit(
'enter',
{
type: "ALUNO",
authorization: `${token}`
}
);
Expand Down Expand Up @@ -346,6 +347,7 @@ export const sendMessage = async (infoMesssage, token) => {

socket.emit('send-message', {
message: response.data.message,
sender: "ALUNO",
authorization: `${token}`
});

Expand Down
20 changes: 15 additions & 5 deletions client/src/services/api/empresa.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { socket } from '../../socket';
import api from '../api';
import { enterSockets } from './aluno';

export function enterSockets(token){
socket.emit(
'enter',
{
type: "EMPRESA",
authorization: `${token}`
}
);
}

export const authEmpresa = async (token) => {
try {
Expand Down Expand Up @@ -148,10 +158,10 @@ export const sendMessage = async (infoMesssage, token) => {
}
});

// socket.emit('send-message', {
// message: response.data.message,
// authorization: `${token}`
// });
socket.emit('send-message', {
message: response.data.message,
authorization: `${token}`
});

return response;
} catch (error) {
Expand Down
15 changes: 12 additions & 3 deletions server/src/connection/controllers/entryController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Server, Socket } from 'socket.io';
import { validateTokenAluno } from '../../middleware/auth/socket';
import { validateTokenAluno, validateTokenEmpresa, validateTokenProfessor } from '../../middleware/auth/socket';

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

if(data.type == "EMPRESA"){
decoded = await validateTokenEmpresa(token) as any;
}
if(data.type == "ALUNO"){
decoded = await validateTokenAluno(token) as any;
}
if(data.type == "PROFESSOR"){
decoded = await validateTokenProfessor(token) as any;
}

if (!decoded) {
console.log('Token inválido para enter-vinculo-aluno');
// socket.emit('error', { message: 'Invalid token' });
return;
}

console.log(`Usuário de email ${decoded.email} registrado`);
socket.join(decoded.email);

Expand Down
50 changes: 36 additions & 14 deletions server/src/connection/controllers/messageControllers.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
import { Server, Socket } from 'socket.io';
import { validateTokenAluno } from '../../middleware/auth/socket';

interface dataDTO{
message:{
conteudo: string,
email: string,
identifier: string,
},
authorization: string
import {
validateTokenAluno,
validateTokenEmpresa,
validateTokenProfessor,
} from '../../middleware/auth/socket';

import { IdentificadorEnum } from '../../modules/interfaces/sharedDTOs';

interface dataDTO {
message: {
conteudo: string;
email: string;
identifier: string;
};
sender: string;
authorization: string;
}

export const sendMessage = async (io: Server, socket: Socket, data: dataDTO) => {
try {
const token = data.authorization;
const decoded = await validateTokenAluno(token);

let decoded = await validateTokenAluno(token) as any;

if (!decoded) {
let decoded = await validateTokenEmpresa(token) as any;

if (!decoded) {
let decoded = await validateTokenProfessor(token) as any;

if (!decoded) {
console.log('Token inválido para enter-vinculo-aluno');
// socket.emit('error', { message: 'Invalid token' });
return;
}
}
}


if (!decoded) {
console.log('Token inválido para vinculo-update');
// socket.emit('error', { message: 'Invalid token' });
console.log('Token inválido para sendMessage');
return;
}

console.log('Nova mensagem para: ' + data.message.email);
io.to(data.message.email).emit('new-message', data.message);
} catch (error) {
console.log('Erro em vinculo-update handler:', error);
console.log('Erro em sendMessage handler:', error);
}
};
};
31 changes: 30 additions & 1 deletion server/src/middleware/auth/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppError } from '../../errors/error';
import { JwtPayload } from 'jsonwebtoken';
import {
verfifyAccessTokenAluno,
verfifyAccessTokenEmpresa,
verfifyAccessTokenFuncinario,
verfifyAccessTokenProfessor
} from '../../jwt/jwtServices';
Expand All @@ -21,7 +22,7 @@ export async function validateTokenAluno(token: string){
throw new AppError('Invalid token');
}

const aluno = await prisma.aluno.findUnique({
const aluno = await prisma.aluno.findFirst({
where: { id: (decoded as JwtPayload).alunoId }
});

Expand Down Expand Up @@ -89,3 +90,31 @@ export async function validateTokenFuncionario(token: string){
throw new Error('Token inválido: ' + error);
}
};

export async function validateTokenEmpresa(token: string){
try {
const decoded = verfifyAccessTokenEmpresa(token);

if (!decoded || typeof decoded === 'string') {
throw new Error('Invalid token');
}


if (!decoded || typeof decoded === 'string') {
throw new AppError('Invalid token');
}

const empresa = await prisma.empresa.findUnique({
where: { id: (decoded as JwtPayload).empresaId }
});

if (!empresa) {
throw new Error('Empresa not found');
}

return empresa;
} catch (error) {
throw new Error('Token inválido: ' + error);
}
};

2 changes: 1 addition & 1 deletion server/src/modules/services/shared/CreateMessageUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class CreateMessageUseCase {
const senderId = senderData.id;
const recipientId = recipientData.id;

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

const data: any = {
conteudo: message,
Expand Down
8 changes: 7 additions & 1 deletion server/src/modules/services/shared/GetChatUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { minioClient } from "../../../minioService";
export class GetMessagesBetweenUseCase {
async execute({ email1, identifier1, email2, identifier2 }: GetMessageBetweenDTO) {

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

if (!email1 || !identifier1 || !email2 || !identifier2) {
throw new AppError("Parâmetros insufientes ou inválidos.");
Expand All @@ -32,7 +32,11 @@ export class GetMessagesBetweenUseCase {
OR: [
{ alunoRemetenteId: entidade1Id, alunoDestinatarioId: entidade2Id },
{ alunoRemetenteId: entidade1Id, empresaDestinatarioId: entidade2Id },
{ alunoRemetenteId: entidade2Id, empresaDestinatarioId: entidade1Id },
{ empresaRemetenteId: entidade1Id, alunoDestinatarioId: entidade2Id },
{ empresaRemetenteId: entidade2Id, alunoDestinatarioId: entidade1Id },
{ professorRemetenteId: entidade1Id, alunoDestinatarioId: entidade2Id },
{ alunoRemetenteId: entidade1Id, professorDestinatarioId: entidade2Id },
]
},
orderBy: {
Expand All @@ -44,6 +48,8 @@ export class GetMessagesBetweenUseCase {
let sender = 'other';
if (message.alunoRemetenteId === entidade1Id) {
sender = 'me';
} else if (message.empresaRemetenteId === entidade1Id) {
sender = 'me';
}
return {
...message,
Expand Down

0 comments on commit 7169693

Please sign in to comment.