diff --git a/client/src/services/api/aluno.js b/client/src/services/api/aluno.js index b266655..01f7916 100644 --- a/client/src/services/api/aluno.js +++ b/client/src/services/api/aluno.js @@ -321,4 +321,23 @@ export const changePassword = async (info, token) => { } catch (error) { return error.response.data; } +} + +export const sendMessage = async (infoMesssage, token) => { + try { + const response = await api.post('aluno/message/send', infoMesssage, { + headers: { + authorization: `${token}` + } + }); + + socket.emit('send-message', { + message: infoMesssage, + authorization: `${token}` + }); + + return response; + } catch (error) { + return error.data; + } } \ No newline at end of file diff --git a/client/src/services/api/professor.js b/client/src/services/api/professor.js index a7dfcf8..9f386b3 100644 --- a/client/src/services/api/professor.js +++ b/client/src/services/api/professor.js @@ -114,4 +114,23 @@ export const refreshTokenProfessor = async (token) => { } catch (error) { return error.response.data; } +} + +export const sendMessage = async (infoMesssage, token) => { + try { + const response = await api.post('professor/message/send', infoMesssage, { + headers: { + authorization: `${token}` + } + }); + + socket.emit('send-message', { + message: infoMesssage, + authorization: `${token}` + }); + + return response; + } catch (error) { + return error.data; + } } \ No newline at end of file diff --git a/server/prisma/migrations/20240616234412_add_message_table/migration.sql b/server/prisma/migrations/20240616234412_add_message_table/migration.sql new file mode 100644 index 0000000..83ab235 --- /dev/null +++ b/server/prisma/migrations/20240616234412_add_message_table/migration.sql @@ -0,0 +1,41 @@ +-- CreateTable +CREATE TABLE `mensagens` ( + `id` VARCHAR(191) NOT NULL, + `conteudo` VARCHAR(191) NOT NULL, + `alunoRemetenteId` VARCHAR(191) NULL, + `alunoDestinatarioId` VARCHAR(191) NULL, + `professorRemetenteId` VARCHAR(191) NULL, + `professorDestinatarioId` VARCHAR(191) NULL, + `funcionarioRemetenteId` VARCHAR(191) NULL, + `funcionarioDestinatarioId` VARCHAR(191) NULL, + `empresaRemetenteId` VARCHAR(191) NULL, + `empresaDestinatarioId` VARCHAR(191) NULL, + `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + `updatedAt` DATETIME(3) NOT NULL, + + PRIMARY KEY (`id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- AddForeignKey +ALTER TABLE `mensagens` ADD CONSTRAINT `alunoRemetente_FK` FOREIGN KEY (`alunoRemetenteId`) REFERENCES `alunos`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `mensagens` ADD CONSTRAINT `alunoDestinatario_FK` FOREIGN KEY (`alunoDestinatarioId`) REFERENCES `alunos`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `mensagens` ADD CONSTRAINT `professorRemetente_FK` FOREIGN KEY (`professorRemetenteId`) REFERENCES `professores`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `mensagens` ADD CONSTRAINT `professorDestinatario_FK` FOREIGN KEY (`professorDestinatarioId`) REFERENCES `professores`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `mensagens` ADD CONSTRAINT `funcionarioRemetente_FK` FOREIGN KEY (`funcionarioRemetenteId`) REFERENCES `funcionarios`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `mensagens` ADD CONSTRAINT `funcionarioDestinatario_FK` FOREIGN KEY (`funcionarioDestinatarioId`) REFERENCES `funcionarios`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `mensagens` ADD CONSTRAINT `empresaRemetente_FK` FOREIGN KEY (`empresaRemetenteId`) REFERENCES `empresas`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `mensagens` ADD CONSTRAINT `empresaDestinatario_FK` FOREIGN KEY (`empresaDestinatarioId`) REFERENCES `empresas`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma index 903b0f1..78a88d5 100644 --- a/server/prisma/schema.prisma +++ b/server/prisma/schema.prisma @@ -8,27 +8,29 @@ datasource db { } model Aluno { - id String @id @default(uuid()) - name String - email String @unique - password String - imagem String? - recoveryPass String? - token String? - banner String? - dataNascimento DateTime? - endereco String? - curriculo String? @default("") @db.VarChar(5000) - sobre String? - rm String? @unique - tentativasRestantes Int @default(5) - turmas AlunoTurma[] - cursosExtracurriculares Extracurricular[] - vinculosAluno Vinculo[] @relation("AlunoVinculo") - vinculoComoAluno Vinculo[] @relation("VinculoComAluno") - validated Boolean @default(false) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + id String @id @default(uuid()) + name String + email String @unique + password String + imagem String? + recoveryPass String? + token String? + banner String? + dataNascimento DateTime? + endereco String? + curriculo String? @default("") @db.VarChar(5000) + sobre String? + rm String? @unique + tentativasRestantes Int @default(5) + validated Boolean @default(false) + turmas AlunoTurma[] + cursosExtracurriculares Extracurricular[] + vinculosAluno Vinculo[] @relation("AlunoVinculo") + vinculoComoAluno Vinculo[] @relation("VinculoComAluno") + mensagensEnviadas Mensagem[] @relation("MensagensEnviadasAluno") + mensagensRecebidas Mensagem[] @relation("MensagensRecebidasAluno") + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt @@map("alunos") } @@ -43,11 +45,13 @@ model Professor { imagem String? banner String? validated Boolean @default(false) - coordenador Coordenador[] - atividade Atividade[] tentativasRestantes Int @default(5) + atividade Atividade[] + coordenador Coordenador[] vinculosProfessor Vinculo[] @relation("ProfessorVinculo") vinculoComoProfessor Vinculo[] @relation("VinculoComProfessor") + mensagensEnviadas Mensagem[] @relation("MensagensEnviadasProfessor") + mensagensRecebidas Mensagem[] @relation("MensagensRecebidasProfessor") createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@ -73,13 +77,13 @@ model Vinculo { } model Admin { - id String @id @default(uuid()) - name String - email String @unique - password String - imagem String? - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + id String @id @default(uuid()) + name String + email String @unique + password String + imagem String? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt @@map("admins") } @@ -107,58 +111,60 @@ enum Turno { } model Turma { - id String @id @default(uuid()) + id String @id @default(uuid()) inicio String fim String - curso Curso @relation(fields: [cursoId], references: [id]) + curso Curso @relation(fields: [cursoId], references: [id]) cursoId String alunos AlunoTurma[] - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt @@map("turmas") } model AlunoTurma { - alunoId String @default(uuid()) - turmaId String @default(uuid()) - aluno Aluno @relation(fields: [alunoId], references: [id]) - turma Turma @relation(fields: [turmaId], references: [id]) + alunoId String @default(uuid()) + turmaId String @default(uuid()) + aluno Aluno @relation(fields: [alunoId], references: [id]) + turma Turma @relation(fields: [turmaId], references: [id]) @@id([alunoId, turmaId]) @@map("alunos_turmas") } model Extracurricular { - extracurricularId String @id @default(uuid()) - alunoId String - aluno Aluno @relation(fields: [alunoId], references: [id]) - instituicao String - descricao String - inicio DateTime - fim DateTime - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - + extracurricularId String @id @default(uuid()) + alunoId String + aluno Aluno @relation(fields: [alunoId], references: [id]) + instituicao String + descricao String + inicio DateTime + fim DateTime + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + @@map("extracurriculares") } model Empresa { - id String @id @default(uuid()) + id String @id @default(uuid()) name String - email String @unique + email String @unique password String - imagem String? - cnpj String @unique + imagem String? + cnpj String @unique endereco String? telefone String? token String? - patrocinador Boolean @default(false) - validated Boolean @default(false) + patrocinador Boolean @default(false) + validated Boolean @default(false) + tentativasRestantes Int @default(5) vaga Vaga[] - tentativasRestantes Int @default(5) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + mensagensEnviadas Mensagem[] @relation("MensagensEnviadasEmpresa") + mensagensRecebidas Mensagem[] @relation("MensagensRecebidasEmpresa") + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt @@map("empresas") } @@ -197,18 +203,20 @@ model Coordenador { } model Funcionario { - id String @id @default(uuid()) + id String @id @default(uuid()) name String - email String @unique + email String @unique password String recoveryPass String? - imagem String? + imagem String? cargo Cargo + validated Boolean @default(false) + tentativasRestantes Int @default(5) vaga Vaga[] - validated Boolean @default(false) - tentativasRestantes Int @default(5) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + mensagensEnviadas Mensagem[] @relation("MensagensEnviadasFuncionario") + mensagensRecebidas Mensagem[] @relation("MensagensRecebidasFuncionario") + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt @@map("funcionarios") } @@ -221,14 +229,39 @@ enum Cargo { } model Atividade { - id String @id @default(uuid()) - title String - descricao String @db.VarChar(1000) - professor Professor @relation(fields: [professorId], references: [id]) - professorId String - imagem String? - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + id String @id @default(uuid()) + title String + descricao String @db.VarChar(1000) + professor Professor @relation(fields: [professorId], references: [id]) + professorId String + imagem String? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt @@map("atividades") } + +model Mensagem { + id String @id @default(uuid()) + conteudo String + alunoRemetenteId String? + alunoDestinatarioId String? + professorRemetenteId String? + professorDestinatarioId String? + funcionarioRemetenteId String? + funcionarioDestinatarioId String? + empresaRemetenteId String? + empresaDestinatarioId String? + alunoRemetente Aluno? @relation("MensagensEnviadasAluno", fields: [alunoRemetenteId], references: [id], map: "alunoRemetente_FK") + alunoDestinatario Aluno? @relation("MensagensRecebidasAluno", fields: [alunoDestinatarioId], references: [id], map: "alunoDestinatario_FK") + professorRemetente Professor? @relation("MensagensEnviadasProfessor", fields: [professorRemetenteId], references: [id], map: "professorRemetente_FK") + professorDestinatario Professor? @relation("MensagensRecebidasProfessor", fields: [professorDestinatarioId], references: [id], map: "professorDestinatario_FK") + funcionarioRemetente Funcionario? @relation("MensagensEnviadasFuncionario", fields: [funcionarioRemetenteId], references: [id], map: "funcionarioRemetente_FK") + funcionarioDestinatario Funcionario? @relation("MensagensRecebidasFuncionario", fields: [funcionarioDestinatarioId], references: [id], map: "funcionarioDestinatario_FK") + empresaRemetente Empresa? @relation("MensagensEnviadasEmpresa", fields: [empresaRemetenteId], references: [id], map: "empresaRemetente_FK") + empresaDestinatario Empresa? @relation("MensagensRecebidasEmpresa", fields: [empresaDestinatarioId], references: [id], map: "empresaDestinatario_FK") + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + @@map("mensagens") +} \ No newline at end of file diff --git a/server/src/modules/controllers/sharedControllers.ts b/server/src/modules/controllers/sharedControllers.ts index 4a82f4e..09e1e40 100644 --- a/server/src/modules/controllers/sharedControllers.ts +++ b/server/src/modules/controllers/sharedControllers.ts @@ -8,9 +8,15 @@ import { IgnoreVinculoUseCase } from "../services/shared/IgnoreVinculoUseCase"; import { GetUnlinkedsUseCase } from "../services/shared/GetUnlinkedsUseCase"; import { GetCursosUseCase } from "../services/shared/GetCursosUseCase"; import { GetCurriculoUseCase } from "../services/shared/GetCurriculoUseCase"; -import { GetEntidadeDTO, IdentificadorEnum } from "../interfaces/sharedDTOs"; import { GetProfileImageUseCase } from '../services/shared/GetProfileImageUseCase'; import { GetBannerUseCase } from '../services/shared/GetBannerUseCase'; +import { CreateMessageUseCase } from "../services/shared/CreateMessageUseCase"; + +import { + CreateMessageDTO, + GetEntidadeDTO, + IdentificadorEnum +} from "../interfaces/sharedDTOs"; export class CreateVinculoController { async handle(req: Request, res: Response) { @@ -152,6 +158,21 @@ export class GetBannerController { const result = await getBannerUseCase.execute({ email, identifier }); + return res.status(201).json(result); + } +} + +export class CreateMessageController { + async handle(req: Request, res: Response) { + const email = req.body.entidade.email; + const { message, sender, recipient, senderIdentifier, recipientIdentifier } = req.body; + + console.log + + const createMessageUseCase = new CreateMessageUseCase(); + + const result = await createMessageUseCase.execute({ email, message, sender, recipient, senderIdentifier, recipientIdentifier }); + return res.status(201).json(result); } } \ No newline at end of file diff --git a/server/src/modules/interfaces/sharedDTOs.ts b/server/src/modules/interfaces/sharedDTOs.ts index 64c8949..281c7d0 100644 --- a/server/src/modules/interfaces/sharedDTOs.ts +++ b/server/src/modules/interfaces/sharedDTOs.ts @@ -22,4 +22,20 @@ export interface ReagirVinculoDTO { export interface GetEntidadeDTO { email: string, identifier: IdentificadorEnum, +} + +export interface CreateMessageDTO { + email: string, + message: string, + sender: string, + recipient: string, + senderIdentifier: EntidadeEnum, + recipientIdentifier: EntidadeEnum +} + +export enum EntidadeEnum { + ALUNO = 'ALUNO', + PROFESSOR = 'PROFESSOR', + FUNCIONARIO = 'FUNCIONARIO', + EMPRESA = 'EMPRESA', } \ No newline at end of file diff --git a/server/src/modules/services/shared/CreateMessageUseCase.ts b/server/src/modules/services/shared/CreateMessageUseCase.ts new file mode 100644 index 0000000..d7f0437 --- /dev/null +++ b/server/src/modules/services/shared/CreateMessageUseCase.ts @@ -0,0 +1,73 @@ +import { prisma } from "../../../prisma/client"; +import { CreateMessageDTO, EntidadeEnum } from '../../interfaces/sharedDTOs'; +import { AppError } from "../../../errors/error"; +import { FindEntidade } from "./helpers/helpers"; + +export class CreateMessageUseCase { + async execute({ email, message, sender, recipient, senderIdentifier, recipientIdentifier }: CreateMessageDTO) { + + if(!email || !message || !sender || !recipient || !senderIdentifier || !recipientIdentifier){ + throw new AppError("Parâmetros insuficientes ou inválidos!"); + } + + const senderData = await FindEntidade(sender, senderIdentifier); + const recipientData = await FindEntidade(recipient, recipientIdentifier); + + if (!senderData || senderData.email != email){ + throw new AppError("Remetente inválido!"); + } + + if (!recipientData || (recipient == sender)){ + throw new AppError("Destinatário inválido!"); + } + + const senderId = senderData.id; + const recipientId = recipientData.id; + + console.log(senderId, senderIdentifier, recipientId, recipientIdentifier, message); + + const data: any = { + conteudo: message, + }; + + switch (senderIdentifier) { + case 'ALUNO': + data.alunoRemetenteId = senderId; + break; + case 'PROFESSOR': + data.professorRemetenteId = senderId; + break; + case 'FUNCIONARIO': + data.funcionarioRemetenteId = senderId; + break; + case 'EMPRESA': + data.empresaRemetenteId = senderId; + break; + default: + throw new AppError("Tipo de remetente inválido!"); + } + + switch (recipientIdentifier) { + case 'ALUNO': + data.alunoDestinatarioId = recipientId; + break; + case 'PROFESSOR': + data.professorDestinatarioId = recipientId; + break; + case 'FUNCIONARIO': + data.funcionarioDestinatarioId = recipientId; + break; + case 'EMPRESA': + data.empresaDestinatarioId = recipientId; + break; + default: + throw new AppError("Tipo de destinatário inválido!"); + } + + await prisma.mensagem.create({ + data, + }); + + return ("Mensagem enviada com sucesso!"); + } +} diff --git a/server/src/modules/services/shared/CreateVinculoUseCase.ts b/server/src/modules/services/shared/CreateVinculoUseCase.ts index 4cada45..ffe327e 100644 --- a/server/src/modules/services/shared/CreateVinculoUseCase.ts +++ b/server/src/modules/services/shared/CreateVinculoUseCase.ts @@ -1,6 +1,6 @@ import { prisma } from "../../../prisma/client"; import { AppError } from "../../../errors/error"; -import { VinculoDTO } from "../../interfaces/sharedDTOs"; +import { VinculoDTO, EntidadeEnum } from "../../interfaces/sharedDTOs"; import { FindEntidade } from "./helpers/helpers"; export class CreateVinculoUseCase { @@ -13,8 +13,11 @@ export class CreateVinculoUseCase { throw new AppError("Remetente inválido."); } - const senderData = await FindEntidade(sender, senderIdentifier); - const recipientData = await FindEntidade(recipient, recipientIdentifier); + const senderIdentifierValue = EntidadeEnum[senderIdentifier]; + const recipientIdentifierValue = EntidadeEnum[recipientIdentifier]; + + const senderData = await FindEntidade(sender, senderIdentifierValue); + const recipientData = await FindEntidade(recipient, recipientIdentifierValue); if (!senderData) { throw new AppError(`${senderIdentifier.charAt(0).toUpperCase()}${senderIdentifier.slice(1).toLowerCase()}(a) remetente não encontrado(a).`); diff --git a/server/src/modules/services/shared/DeleteVinculoUseCase.ts b/server/src/modules/services/shared/DeleteVinculoUseCase.ts index 93362f1..80a6887 100644 --- a/server/src/modules/services/shared/DeleteVinculoUseCase.ts +++ b/server/src/modules/services/shared/DeleteVinculoUseCase.ts @@ -1,6 +1,6 @@ import { prisma } from "../../../prisma/client"; import { AppError } from "../../../errors/error"; -import { VinculoDTO } from "../../interfaces/sharedDTOs"; +import { VinculoDTO, EntidadeEnum } from "../../interfaces/sharedDTOs"; import { FindEntidade } from "./helpers/helpers"; export class DeleteVinculoUseCase { @@ -13,9 +13,12 @@ export class DeleteVinculoUseCase { throw new AppError("Usuário não pertence a nenhum dos elos da conexão."); } - const senderData = await FindEntidade(sender, senderIdentifier); - const recipientData = await FindEntidade(recipient, recipientIdentifier); + const senderIdentifierValue = EntidadeEnum[senderIdentifier]; + const recipientIdentifierValue = EntidadeEnum[recipientIdentifier]; + const senderData = await FindEntidade(sender, senderIdentifierValue); + const recipientData = await FindEntidade(recipient, recipientIdentifierValue); + if (!senderData) { throw new AppError(`${senderIdentifier.charAt(0).toUpperCase()}${senderIdentifier.slice(1).toLowerCase()}(a) remetente não encontrado(a).`); } diff --git a/server/src/modules/services/shared/GetProfileVincUseCase.ts b/server/src/modules/services/shared/GetProfileVincUseCase.ts index 558c7cf..3b1cca9 100644 --- a/server/src/modules/services/shared/GetProfileVincUseCase.ts +++ b/server/src/modules/services/shared/GetProfileVincUseCase.ts @@ -1,6 +1,6 @@ import { prisma } from "../../../prisma/client"; import { AppError } from "../../../errors/error"; -import { GetEntidadeDTO } from "../../interfaces/sharedDTOs"; +import { EntidadeEnum, GetEntidadeDTO } from "../../interfaces/sharedDTOs"; import { FindEntidade } from "./helpers/helpers"; export class GetVinculosProfileUseCase { @@ -13,7 +13,7 @@ export class GetVinculosProfileUseCase { throw new AppError("Parâmetros insuficientes ou inválidos."); } - const entidade = await FindEntidade(email, identifier); + const entidade = await FindEntidade(email, EntidadeEnum[identifier]); if (!entidade) { throw new AppError(`${identifier.charAt(0).toUpperCase()}${identifier.slice(1).toLowerCase()} não encontrado.`); diff --git a/server/src/modules/services/shared/GetUnlinkedsUseCase.ts b/server/src/modules/services/shared/GetUnlinkedsUseCase.ts index fed00d2..f29eab5 100644 --- a/server/src/modules/services/shared/GetUnlinkedsUseCase.ts +++ b/server/src/modules/services/shared/GetUnlinkedsUseCase.ts @@ -1,6 +1,6 @@ import { prisma } from "../../../prisma/client"; import { AppError } from "../../../errors/error"; -import { IdentificadorEnum, GetEntidadeDTO } from "../../interfaces/sharedDTOs"; +import { IdentificadorEnum, GetEntidadeDTO, EntidadeEnum } from "../../interfaces/sharedDTOs"; import { FindEntidade } from "./helpers/helpers"; export class GetUnlinkedsUseCase { @@ -13,7 +13,7 @@ export class GetUnlinkedsUseCase { throw new AppError("Parâmetros insuficientes ou inválidos."); } - const entidade = await FindEntidade(email, identifier); + const entidade = await FindEntidade(email, EntidadeEnum[identifier]); if (!entidade) { throw new AppError(`${identifier.charAt(0).toUpperCase()}${identifier.slice(1).toLowerCase()} não encontrado.`); diff --git a/server/src/modules/services/shared/GetVinculosUseCase.ts b/server/src/modules/services/shared/GetVinculosUseCase.ts index 5a0fb6c..8a43275 100644 --- a/server/src/modules/services/shared/GetVinculosUseCase.ts +++ b/server/src/modules/services/shared/GetVinculosUseCase.ts @@ -1,6 +1,6 @@ import { prisma } from "../../../prisma/client"; import { AppError } from "../../../errors/error"; -import { GetEntidadeDTO } from "../../interfaces/sharedDTOs"; +import { EntidadeEnum, GetEntidadeDTO } from "../../interfaces/sharedDTOs"; import { FindEntidade } from "./helpers/helpers"; export class GetVinculosUseCase { @@ -13,7 +13,7 @@ export class GetVinculosUseCase { throw new AppError("Parâmetros insuficientes ou inválidos."); } - const entidade = await FindEntidade(email, identifier); + const entidade = await FindEntidade(email, EntidadeEnum[identifier]); if (!entidade) { throw new AppError(`${identifier.charAt(0).toUpperCase()}${identifier.slice(1).toLowerCase()} não encontrado.`); diff --git a/server/src/modules/services/shared/IgnoreVinculoUseCase.ts b/server/src/modules/services/shared/IgnoreVinculoUseCase.ts index 90cb520..353c65f 100644 --- a/server/src/modules/services/shared/IgnoreVinculoUseCase.ts +++ b/server/src/modules/services/shared/IgnoreVinculoUseCase.ts @@ -1,6 +1,6 @@ import { prisma } from "../../../prisma/client"; import { AppError } from "../../../errors/error"; -import { ReagirVinculoDTO } from "../../interfaces/sharedDTOs"; +import { EntidadeEnum, ReagirVinculoDTO } from "../../interfaces/sharedDTOs"; import { FindEntidade } from "./helpers/helpers"; export class IgnoreVinculoUseCase { @@ -13,8 +13,11 @@ export class IgnoreVinculoUseCase { throw new AppError("Rementente inválido"); } - const senderData = await FindEntidade(sender, senderIdentifier); - const recipientData = await FindEntidade(recipient, recipientIdentifier); + const senderIdentifierValue = EntidadeEnum[senderIdentifier]; + const recipientIdentifierValue = EntidadeEnum[recipientIdentifier]; + + const senderData = await FindEntidade(sender, senderIdentifierValue); + const recipientData = await FindEntidade(recipient, recipientIdentifierValue); if (!senderData) { throw new AppError(`${senderIdentifier.charAt(0).toUpperCase()}${senderIdentifier.slice(1).toLowerCase()} não encontrado.`); diff --git a/server/src/modules/services/shared/helpers/helpers.ts b/server/src/modules/services/shared/helpers/helpers.ts index 922462f..4b5bd3e 100644 --- a/server/src/modules/services/shared/helpers/helpers.ts +++ b/server/src/modules/services/shared/helpers/helpers.ts @@ -1,18 +1,26 @@ -import { IdentificadorEnum } from "../../../interfaces/sharedDTOs"; +import { EntidadeEnum } from "../../../interfaces/sharedDTOs"; import { prisma } from "../../../../prisma/client"; import path from 'path'; import fs from 'fs/promises'; -export const FindEntidade = async (email: string, identifier: IdentificadorEnum) => { +export const FindEntidade = async (email: string, identifier: EntidadeEnum) => { switch (identifier) { - case IdentificadorEnum.ALUNO: + case EntidadeEnum.ALUNO: return prisma.aluno.findUnique({ where: { email } }); - case IdentificadorEnum.PROFESSOR: + case EntidadeEnum.PROFESSOR: return prisma.professor.findUnique({ where: { email } }); + case EntidadeEnum.FUNCIONARIO: + return prisma.funcionario.findUnique({ + where: { email } + }); + case EntidadeEnum.EMPRESA: + return prisma.empresa.findUnique({ + where: { email } + }); default: return null; } diff --git a/server/src/router/routes/aluno.routes.ts b/server/src/router/routes/aluno.routes.ts index a5fb325..9dee48a 100644 --- a/server/src/router/routes/aluno.routes.ts +++ b/server/src/router/routes/aluno.routes.ts @@ -9,7 +9,8 @@ import { IgnoreVinculoController, GetUnlinkedsController, GetCursosController, - DeleteVinculoController + DeleteVinculoController, + CreateMessageController } from "../../modules/controllers/sharedControllers"; import { CreateAlunoController, @@ -51,6 +52,7 @@ const createControllers = () => ({ uploadImgProfileController: new UploadImgProfileController(), uploadCapaController: new UploadCapaController(), changePassController: new ChangePassController(), + createMessageController: new CreateMessageController(), }); const controllers = createControllers(); @@ -82,6 +84,7 @@ alunoRoutes.post("/link/accept", alunoAuthMiddleware, controllers.acceptVinculoC alunoRoutes.post("/link/reject", alunoAuthMiddleware, controllers.ignoreVinculoController.handle); alunoRoutes.post("/link/delete", alunoAuthMiddleware, controllers.deleteVinculoController.handle); alunoRoutes.post("/password/change", alunoAuthMiddleware, controllers.changePassController.handle); +alunoRoutes.post("/message/send", alunoAuthMiddleware, controllers.createMessageController.handle); alunoRoutes.get("/auth", alunoAuthMiddleware, (req, res) => { res.status(200).send("Aluno autenticado com sucesso."); diff --git a/server/src/router/routes/professor.routes.ts b/server/src/router/routes/professor.routes.ts index bbc7e10..af8c42b 100644 --- a/server/src/router/routes/professor.routes.ts +++ b/server/src/router/routes/professor.routes.ts @@ -6,7 +6,8 @@ import { GetVinculosController, IgnoreVinculoController, GetUnlinkedsController, - DeleteVinculoController + DeleteVinculoController, + CreateMessageController } from "../../modules/controllers/sharedControllers"; import { ValidateProfessorController, @@ -29,7 +30,8 @@ const createControllers = () => ({ deleteVinculoController: new DeleteVinculoController(), getVinculosController: new GetVinculosController(), getUnlinkedsController: new GetUnlinkedsController(), - refreshTokenController: new RefreshTokenController() + refreshTokenController: new RefreshTokenController(), + createMessageController: new CreateMessageController() }); const controllers = createControllers(); @@ -43,6 +45,7 @@ professorRoutes.post("/link/send", professorAuthMiddleware, controllers.createVi professorRoutes.post("/link/accept", professorAuthMiddleware, controllers.acceptVinculoController.handle); professorRoutes.post("/link/reject", professorAuthMiddleware, controllers.ignoreVinculoController.handle); professorRoutes.post("/link/delete", professorAuthMiddleware, controllers.deleteVinculoController.handle); +professorRoutes.post("/message/send", professorAuthMiddleware, controllers.createMessageController.handle); professorRoutes.get("/auth", professorAuthMiddleware, (req, res) => { res.status(200).send("Professor autenticado com sucesso.");