Skip to content

Commit 5273536

Browse files
Add get bulletins ranking route
1 parent 25b59a3 commit 5273536

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

server/src/modules/controllers/sharedControllers.ts

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { GetEmpresasUseCase } from "../services/shared/GetEmpresasUseCase";
2222
import { GetVagasUseCase } from "../services/shared/GetVagasUseCase";
2323
import { GetVagaUseCase } from "../services/shared/GetVagaUseCase";
2424
import { GetExtracurricularsByRMUseCase } from "../services/shared/GetExtraUseCase";
25+
import { GetRankingUseCase } from "../services/shared/GetRanking";
2526

2627
export class CreateVinculoController {
2728
async handle(req: Request, res: Response) {
@@ -231,6 +232,16 @@ export class GetExtracurricularsByRMController {
231232

232233
const result = await getExtracurricularsByRMUseCase.execute(rm);
233234

235+
return res.status(201).json(result);
236+
}
237+
}
238+
239+
export class GetRankingController {
240+
async handle(req: Request, res: Response) {
241+
const getRankingUseCase = new GetRankingUseCase();
242+
243+
const result = await getRankingUseCase.execute();
244+
234245
return res.status(201).json(result);
235246
}
236247
}

server/src/modules/services/funcionario/CompareBoletinsUseCase.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AppError } from "../../../errors/error";
66
import { CompareBoletimDTO } from '../../interfaces/funcionarioDTOs';
77
import { clearUploads } from '../shared/helpers/helpers';
88
import { minioClient } from '../../../minioService';
9+
import { Readable } from 'stream';
910

1011
export class CompareBoletimUseCase {
1112
async execute({ boletimId, file }: CompareBoletimDTO) {
@@ -69,11 +70,11 @@ export class CompareBoletimUseCase {
6970

7071
async getFileFromMinio(bucketName: string, objectName: string): Promise<Buffer> {
7172
return new Promise((resolve, reject) => {
72-
minioClient.getObject(bucketName, objectName, (error: Error | null, dataStream: any) => {
73+
minioClient.getObject(bucketName, objectName, (error: Error | null, dataStream: Readable) => {
7374
if (error) {
7475
return reject(error);
7576
}
76-
77+
7778
const chunks: Buffer[] = [];
7879
dataStream.on('data', (chunk: Buffer) => {
7980
chunks.push(chunk);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { prisma } from "../../../prisma/client";
2+
import { AppError } from "../../../errors/error";
3+
4+
const notaValores: { [key: string]: number } = {
5+
MB: 10,
6+
B: 7.5,
7+
R: 5,
8+
I: 2.5
9+
};
10+
11+
export class GetRankingUseCase {
12+
async execute() {
13+
const notas = await prisma.nota.findMany({
14+
include: {
15+
aluno: true
16+
}
17+
});
18+
19+
if (!notas.length) {
20+
throw new AppError("Nenhuma nota encontrada.");
21+
}
22+
23+
const alunoNotas: { [alunoId: string]: { totalNotas: number; numeroNotas: number } } = {};
24+
25+
notas.forEach(nota => {
26+
const alunoId = nota.alunoId;
27+
const valorNota = notaValores[nota.mencao];
28+
29+
if (!alunoNotas[alunoId]) {
30+
alunoNotas[alunoId] = {
31+
totalNotas: 0,
32+
numeroNotas: 0
33+
};
34+
}
35+
36+
alunoNotas[alunoId].totalNotas += valorNota;
37+
alunoNotas[alunoId].numeroNotas += 1;
38+
});
39+
40+
const ranking = Object.keys(alunoNotas).map(alunoId => {
41+
const { totalNotas, numeroNotas } = alunoNotas[alunoId];
42+
const notaMaximaPossivel = 10 * numeroNotas;
43+
const rankingNota = totalNotas / notaMaximaPossivel;
44+
45+
return {
46+
alunoId,
47+
rankingNota
48+
};
49+
});
50+
51+
ranking.sort((a, b) => b.rankingNota - a.rankingNota);
52+
53+
const alunos = await prisma.aluno.findMany({
54+
where: {
55+
id: {
56+
in: ranking.map(r => r.alunoId)
57+
}
58+
}
59+
});
60+
61+
const rankingDetalhado = ranking.map(rank => {
62+
const aluno = alunos.find(a => a.id === rank.alunoId);
63+
return {
64+
aluno,
65+
rankingNota: rank.rankingNota
66+
};
67+
});
68+
69+
return rankingDetalhado;
70+
}
71+
}

server/src/router/routes/imports/shared.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
SearchUserController,
66
GetVagasController,
77
GetVagaController,
8-
GetExtracurricularsByRMController
8+
GetExtracurricularsByRMController,
9+
GetRankingController
910
} from "../../../modules/controllers/sharedControllers";
1011

1112
export const createControllers = () => ({
@@ -15,5 +16,6 @@ export const createControllers = () => ({
1516
searchUserController: new SearchUserController(),
1617
getVagasController: new GetVagasController(),
1718
getVagaController: new GetVagaController(),
18-
getExtracurricularsByRMController: new GetExtracurricularsByRMController()
19+
getExtracurricularsByRMController: new GetExtracurricularsByRMController(),
20+
getRankingController: new GetRankingController()
1921
});

server/src/router/routes/shared.routes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ sharedRoutes.get("/users", controllers.searchUserController.handle);
1111
sharedRoutes.get("/vagas", controllers.getVagasController.handle);
1212
sharedRoutes.get("/vaga", controllers.getVagaController.handle);
1313
sharedRoutes.get("/extracurriculares", controllers.getExtracurricularsByRMController.handle);
14+
sharedRoutes.get("/ranking/boletim", controllers.getRankingController.handle);
1415

1516
export { sharedRoutes };

0 commit comments

Comments
 (0)