Skip to content

Commit

Permalink
update enrollments on sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Joabesv committed Nov 8, 2024
1 parent 8119f2c commit 4f55f9d
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion apps/core/src/modules/user/history/handlers/createHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type History,
HistoryModel,
} from '@/models/History.js';
import { logger } from '@next/common';
import { generateIdentifier, logger } from '@next/common';
import { transformCourseName } from '../utils/transformCourseName.js';
import type { FastifyReply, FastifyRequest } from 'fastify';
import {
Expand All @@ -14,6 +14,7 @@ import {
} from '@/services/ufprocessor.js';

import { type Subject, SubjectModel } from '@/models/Subject.js';
import { EnrollmentModel } from '@/models/Enrollment.js';

const CACHE_TTL = 1000 * 60 * 60;
const historyCache = new LRUCache<string, History>({ max: 3, ttl: CACHE_TTL });
Expand Down Expand Up @@ -138,13 +139,22 @@ export async function createHistory(
);
}

if (history) {
await updateEnrollments(
studentHistory.ra,
hydratedComponents,
history.coefficients,
);
}

logger.info({
student: studentHistory.ra,
dbGrade: history?.grade,
rawGrade: studentGrade,
studentHistory,
msg: 'Synced Successfully',
});

historyCache.set(cacheKey, history!);

return {
Expand Down Expand Up @@ -219,3 +229,66 @@ const resolveCategory = (
return 'Livre Escolha';
}
};

async function updateEnrollments(
ra: number,
components: HydratedComponent[],
coefficients: any,
) {
for (const component of components) {
const identifier = generateIdentifier({
ra,
year: component.ano,
quad: component.periodo,
disciplina: component.disciplina,
});

const periodCoeff = coefficients?.[component.ano]?.[component.periodo];
const enrollmentData = {
identifier,
ra,
year: component.ano,
quad: component.periodo,
disciplina: component.disciplina,
codigo: component.codigo,
conceito: component.conceito,
creditos: component.creditos,
categoria: component.categoria,
situacao: component.situacao,
cr_acumulado: periodCoeff?.cr_acumulado,
ca_acumulado: periodCoeff?.ca_acumulado,
cp_acumulado: periodCoeff?.cp_acumulado,
};

try {
await EnrollmentModel.findOneAndUpdate(
{
ra,
year: component.ano,
quad: component.periodo,
disciplina: {
$regex: new RegExp(`^${component.disciplina.toLowerCase()}`, 'i'),
},
},
enrollmentData,
{
new: true,
},
);
logger.info({
msg: 'Updated enrollment',
identifier,
ra,
disciplina: component.disciplina,
});
} catch (error) {
logger.error({
msg: 'Failed to update enrollment',
error,
identifier,
ra,
disciplina: component.disciplina,
});
}
}
}

0 comments on commit 4f55f9d

Please sign in to comment.