Skip to content

Commit

Permalink
Merge pull request #87 from ufabc-next/refac/queues
Browse files Browse the repository at this point in the history
Better sync
  • Loading branch information
Joabesv authored Aug 10, 2024
2 parents 048320a + c92651a commit 8469640
Show file tree
Hide file tree
Showing 38 changed files with 814 additions and 503 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ build
.gitsecret/keys/random_seed
tmp
.env.dev
debug.js
1 change: 1 addition & 0 deletions apps/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"fastify-type-provider-zod": "^1.1.9",
"jsonwebtoken": "^9.0.2",
"lodash-es": "^4.17.21",
"lru-cache": "^11.0.0",
"mongoose": "^8.4.3",
"mongoose-lean-virtuals": "^0.9.1",
"ms": "^2.1.3",
Expand Down
2 changes: 1 addition & 1 deletion apps/core/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const [watch, parallel] = process.argv.slice(2);
const defaultTimeout = isCI ? 30_000 : 60_000;

const files: string[] = [];
for await (const testFile of glob('tests/**/*.spec.ts')) {
for await (const testFile of glob('src/**/*.spec.ts')) {
files.push(testFile as string);
}

Expand Down
5 changes: 2 additions & 3 deletions apps/core/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ const envSchema = z.object({
REDIS_PASSWORD: z.string().min(8).default('localRedis'),
REDIS_HOST: z.string().default('localhost'),
REDIS_PORT: z.coerce.number().default(6379),
MONGODB_CONNECTION_URL: z
.string()
.default('mongodb://127.0.0.1:27017/next-db'),
MONGODB_CONNECTION_URL: z.string().default('mongodb://127.0.0.1:27017/local'),
REDIS_CONNECTION_URL: z.string().optional(),
UF_PROCESSOR_URL: z.string().url(),
});

const _env = envSchema.safeParse(process.env);
Expand Down
4 changes: 2 additions & 2 deletions apps/core/src/hooks/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { UserDocument } from '@/models/User.js';
import type { preHandlerHookHandler } from 'fastify';

// need to use this
export const admin: preHandlerHookHandler = function (request, reply, done) {
export const admin: preHandlerHookHandler = async function (request, reply) {
const token = request.headers.authorization?.replace('Bearer ', '');

if (!token) {
Expand All @@ -15,5 +15,5 @@ export const admin: preHandlerHookHandler = function (request, reply, done) {
return;
}

done(new Error('This route is for admins, only for now'));
return reply.unauthorized('This route is for admins, only for now');
};
4 changes: 2 additions & 2 deletions apps/core/src/models/Disciplina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { findQuarter } from '@next/common';
import { mongooseLeanVirtuals } from 'mongoose-lean-virtuals';

const CAMPUS = ['sao bernardo', 'santo andre'] as const;
const CAMPUS = ['sao bernardo', 'santo andre', 'sbc', 'sa'] as const;

const disciplinaSchema = new Schema(
{
Expand All @@ -18,7 +18,7 @@ const disciplinaSchema = new Schema(
vagas: { type: Number, required: true },
obrigatorias: { type: [Number], default: [] },
codigo: { type: String, required: true },
campus: { type: String, enum: CAMPUS },
campus: { type: String, enum: CAMPUS, required: true },
ideal_quad: Boolean,
identifier: {
type: String,
Expand Down
4 changes: 0 additions & 4 deletions apps/core/src/models/Teacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ const teacherSchema = new Schema(

teacherSchema.plugin(mongooseLeanVirtuals);

teacherSchema.pre('save', function () {
this.name = startCase(camelCase(this.name));
});

export type Teacher = InferSchemaType<typeof teacherSchema>;
export type TeacherDocument = ReturnType<(typeof TeacherModel)['hydrate']>;
export const TeacherModel = model('teachers', teacherSchema);
81 changes: 44 additions & 37 deletions apps/core/src/modules/Entities/disciplinas/disciplina.handlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { sortBy as LodashSortBy } from "lodash-es";
import { courseId, currentQuad } from "@next/common";
import { StudentModel } from "@/models/Student.js";
import { storage } from "@/services/unstorage.js";
import type { Disciplina } from "@/models/Disciplina.js";
import type { DisciplinaService } from "./disciplina.service.js";
import type { FastifyReply, FastifyRequest } from "fastify";
import { sortBy as LodashSortBy } from 'lodash-es';
import { courseId, currentQuad } from '@next/common';
import { StudentModel } from '@/models/Student.js';
import { storage } from '@/services/unstorage.js';
import type { Disciplina } from '@/models/Disciplina.js';
import type { DisciplinaService } from './disciplina.service.js';
import type { FastifyReply, FastifyRequest } from 'fastify';

export type DisciplinaKicksRequest = {
Params: {
Expand All @@ -21,18 +21,25 @@ export class DisciplinaHandler {

async listDisciplinas() {
const season = currentQuad();
const cacheKey = `allDisciplinas-${season}`;
const cacheKey = `list:components:${season}`;
const cachedResponse = await storage.getItem<Disciplina[]>(cacheKey);

if (cachedResponse) {
return cachedResponse;
}

const disciplinas = await this.disciplinaService.findDisciplinas(season);
await storage.setItem<Disciplina[]>(cacheKey, disciplinas, {
const components = await this.disciplinaService.findDisciplinas(season);
const toShow = components.map(({ id: _ignore, ...component }) => ({
...component,
teoria: component.teoria?.name,
pratica: component.pratica?.name,
subject: component.subject?.name,
}));
await storage.setItem<typeof toShow>(cacheKey, toShow, {
ttl: 60 * 60 * 24,
});

return disciplinas;
return toShow;
}

async listDisciplinasKicks(
Expand All @@ -43,7 +50,7 @@ export class DisciplinaHandler {
const { sort } = request.query;

if (!disciplinaId) {
await reply.badRequest("Missing DisciplinaId");
await reply.badRequest('Missing DisciplinaId');
}

const season = currentQuad();
Expand All @@ -53,19 +60,19 @@ export class DisciplinaHandler {
);

if (!disciplina) {
return reply.notFound("Disciplina not found");
return reply.notFound('Disciplina not found');
}

// create sort mechanism
const kicks = sort || kickRule(disciplina);
// @ts-expect-error for now
const order = [kicks.length || 0].fill("desc");
const order = [kicks.length || 0].fill('desc');

// turno must have a special treatment
const turnoIndex = kicks.indexOf("turno");
const turnoIndex = kicks.indexOf('turno');
if (turnoIndex !== -1) {
// @ts-expect-error for now
order[turnoIndex] = disciplina.turno === "diurno" ? "asc" : "desc";
order[turnoIndex] = disciplina.turno === 'diurno' ? 'asc' : 'desc';
}

const isAfterKick = [disciplina.after_kick].filter(Boolean).length;
Expand All @@ -81,13 +88,13 @@ export class DisciplinaHandler {

const interIds = [
await courseId(
"Bacharelado em Ciência e Tecnologia",
'Bacharelado em Ciência e Tecnologia',
season,
// TODO(Joabe): refac later
StudentModel,
),
await courseId(
"Bacharelado em Ciências e Humanidades",
'Bacharelado em Ciências e Humanidades',
season,
// TODO(Joabe): refac later
StudentModel,
Expand All @@ -104,7 +111,7 @@ export class DisciplinaHandler {
const graduationToStudent = Object.assign(
{
aluno_id: student.aluno_id,
cr: "-",
cr: '-',
cp: student.cursos.cp,
ik: reserva ? student.cursos.ind_afinidade : 0,
reserva,
Expand Down Expand Up @@ -137,28 +144,28 @@ function kickRule(disciplina: Disciplina) {
const season = currentQuad();
let coeffRule = null;
if (
season === "2020:2" ||
season === "2020:3" ||
season === "2021:1" ||
season === "2021:2" ||
season === "2021:3" ||
season === "2022:1" ||
season === "2022:2" ||
season === "2022:3" ||
season === "2023:1" ||
season === "2023:2" ||
season === "2023:3" ||
season === "2024:1" ||
season === "2024:2" ||
season === "2024:3" ||
season === "2025:1"
season === '2020:2' ||
season === '2020:3' ||
season === '2021:1' ||
season === '2021:2' ||
season === '2021:3' ||
season === '2022:1' ||
season === '2022:2' ||
season === '2022:3' ||
season === '2023:1' ||
season === '2023:2' ||
season === '2023:3' ||
season === '2024:1' ||
season === '2024:2' ||
season === '2024:3' ||
season === '2025:1'
) {
coeffRule = ["cp", "cr"];
coeffRule = ['cp', 'cr'];
} else {
coeffRule = disciplina.ideal_quad ? ["cr", "cp"] : ["cp", "cr"];
coeffRule = disciplina.ideal_quad ? ['cr', 'cp'] : ['cp', 'cr'];
}

return ["reserva", "turno", "ik"].concat(coeffRule);
return ['reserva', 'turno', 'ik'].concat(coeffRule);
}

function resolveMatricula(disciplina: Disciplina, isAfterKick: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ export class DisciplinaRepository implements EntitiesDisciplinaRepository {
) {
if (populateFields) {
const disciplinas = await this.disciplinaService

.find(filter, mapping)
.populate(populateFields)
.lean<Disciplina[]>({ virtuals: true });
return disciplinas;
}
const disciplinas = await this.disciplinaService

.find(filter, mapping)
.lean<Disciplina[]>({ virtuals: true });
return disciplinas;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import {
import { DisciplinaRepository } from './disciplina.repository.js';
import {
listDisciplinasKicksSchema,
listDisciplinasSchema
listDisciplinasSchema,
} from './disciplina.schema.js';
import { DisciplinaService } from './disciplina.service.js';

import type { FastifyInstance } from 'fastify';


export async function disciplinasRoute(app: FastifyInstance) {
const disciplinaRepository = new DisciplinaRepository(
DisciplinaModel,
Expand All @@ -24,7 +23,11 @@ export async function disciplinasRoute(app: FastifyInstance) {
app.decorate('disciplinaService', disciplinaService);
const disciplinaHandler = new DisciplinaHandler(disciplinaService);

app.get('/disciplina', {schema: listDisciplinasSchema}, disciplinaHandler.listDisciplinas);
app.get(
'/disciplina',
{ schema: listDisciplinasSchema },
disciplinaHandler.listDisciplinas,
);
app.get<DisciplinaKicksRequest>(
'/disciplina/:disciplinaId/kicks',
{ schema: listDisciplinasKicksSchema, onRequest: [setStudentId] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export class DisciplinaService {

async findDisciplinas(season: ReturnType<typeof currentQuad>) {
const disciplinaMapping = {
disciplina: 1,
disciplina_id: 1,
turno: 1,
turma: 1,
Expand All @@ -18,14 +17,15 @@ export class DisciplinaService {
requisicoes: 1,
teoria: 1,
pratica: 1,
_id: 0,
};

const discplinas = await this.disciplinaRepository.findMany(
{
season,
},
disciplinaMapping,
['pratica', 'teoria'],
['pratica', 'teoria', 'subject'],
);
return discplinas;
}
Expand Down
Loading

0 comments on commit 8469640

Please sign in to comment.