From 52a8cd2e7517e4a7ee0a7ebe31fd64b51ed322cb Mon Sep 17 00:00:00 2001 From: Jan Rundt Date: Tue, 8 Oct 2024 12:23:31 +0300 Subject: [PATCH] OOD-59: try-catch-clauses for updates and if-clauses for using HY-specific programmeCode-map --- .../backend/src/services/faculty/faculty.ts | 5 +- .../studyProgramme/studyProgrammeHelpers.ts | 6 ++- .../src/updater/updateMeta.js | 28 ++++++----- .../updateProgrammeModules/resolver.js | 4 +- .../updateProgrammeModules.js | 10 +++- .../updater/updateStudents/SISStudyRights.js | 46 +++++++++++-------- 6 files changed, 63 insertions(+), 36 deletions(-) diff --git a/services/backend/src/services/faculty/faculty.ts b/services/backend/src/services/faculty/faculty.ts index 11001d6b6a..628bf672a8 100644 --- a/services/backend/src/services/faculty/faculty.ts +++ b/services/backend/src/services/faculty/faculty.ts @@ -2,6 +2,7 @@ import { groupBy, orderBy } from 'lodash' import moment from 'moment' import { InferAttributes, QueryTypes } from 'sequelize' +import { serviceProvider } from '../../config' import { programmeCodes } from '../../config/programmeCodes' import { dbConnections } from '../../database/connection' import { Organization, ProgrammeModule } from '../../models' @@ -54,7 +55,9 @@ export const getDegreeProgrammesOfOrganization = async (organizationId: string, const programmesWithProgIds = programmesOfOrganization.map(programme => ({ ...programme, progId: - programme.code in programmeCodes ? programmeCodes[programme.code as keyof typeof programmeCodes] : programme.code, + programme.code in programmeCodes && serviceProvider !== 'fd' + ? programmeCodes[programme.code as keyof typeof programmeCodes] + : programme.code, })) const programmesGroupedByCode = groupBy(orderBy(programmesWithProgIds, ['valid_from'], ['desc']), prog => prog.code) const curriculumPeriods = await getCurriculumPeriods() diff --git a/services/backend/src/services/studyProgramme/studyProgrammeHelpers.ts b/services/backend/src/services/studyProgramme/studyProgrammeHelpers.ts index 41f3f2c91a..87234330aa 100644 --- a/services/backend/src/services/studyProgramme/studyProgrammeHelpers.ts +++ b/services/backend/src/services/studyProgramme/studyProgrammeHelpers.ts @@ -165,8 +165,10 @@ export const tableTitles = { studytracksEnd: ['Men', 'Women', 'Other/\nUnknown', 'Finland', 'Other'], } as const -export const getId = (code: string) => - code in programmeCodes ? programmeCodes[code as keyof typeof programmeCodes] : '' +export const getId = (code: string) => { + if (serviceProvider !== 'fd') return code in programmeCodes ? programmeCodes[code as keyof typeof programmeCodes] : '' + return code +} export const getGoal = (programme?: string) => { if (!programme) return 0 diff --git a/updater/sis-updater-worker/src/updater/updateMeta.js b/updater/sis-updater-worker/src/updater/updateMeta.js index 14cfbee568..2adc413945 100644 --- a/updater/sis-updater-worker/src/updater/updateMeta.js +++ b/updater/sis-updater-worker/src/updater/updateMeta.js @@ -11,6 +11,7 @@ const { Organization, StudyrightExtent, } = require('../db/models') +const { logger } = require('../utils/logger') const { courseMapper, courseProviderMapper, @@ -70,18 +71,23 @@ const updateCourses = async (courseIdToAttainments, groupIdToCourse) => { if (!courseUnitOrganisations) { continue } - for (const { share, organisationId, roleUrn, validityPeriod = {} } of courseUnitOrganisations) { - const effectiveValidityPeriod = Object.keys(validityPeriod).length ? validityPeriod : courseUnitValidityPeriod - const shareObj = { - share, - ...(effectiveValidityPeriod.startDate && { startDate: effectiveValidityPeriod.startDate }), - ...(effectiveValidityPeriod.endDate && { endDate: effectiveValidityPeriod.endDate }), - } - if (!organisationsById[organisationId]) { - organisationsById[organisationId] = { organisationId, roleUrn, shares: [shareObj] } - } else { - organisationsById[organisationId].shares.push(shareObj) + for (const { share, organisationId, roleUrn, validityPeriod = {} } of courseUnitOrganisations) { + try { + const effectiveValidityPeriod = Object.keys(validityPeriod).length ? validityPeriod : courseUnitValidityPeriod + const shareObj = { + share, + ...(effectiveValidityPeriod.startDate && { startDate: effectiveValidityPeriod.startDate }), + ...(effectiveValidityPeriod.endDate && { endDate: effectiveValidityPeriod.endDate }), + } + + if (!organisationsById[organisationId]) { + organisationsById[organisationId] = { organisationId, roleUrn, shares: [shareObj] } + } else { + organisationsById[organisationId].shares.push(shareObj) + } + } catch (error) { + logger.error(`Error in course unit organisation handling for orgId ${organisationId}`) } } } diff --git a/updater/sis-updater-worker/src/updater/updateProgrammeModules/resolver.js b/updater/sis-updater-worker/src/updater/updateProgrammeModules/resolver.js index 4235a8e03a..6f4f408aff 100644 --- a/updater/sis-updater-worker/src/updater/updateProgrammeModules/resolver.js +++ b/updater/sis-updater-worker/src/updater/updateProgrammeModules/resolver.js @@ -1,3 +1,5 @@ +const { serviceProvider } = require('../../config') + const anyModule = rule => ({ id: rule.localId, name: 'Any module' }) const anyCourse = rule => ({ id: rule.localId, name: 'Any course' }) const unknownRule = rule => ({ type: rule.type, fact: 'Unhandled rule' }) @@ -73,7 +75,7 @@ class ModuleResolver { const children = [] for (const mod of modules) { - if (mod.code?.startsWith('KK-')) continue + if (serviceProvider !== 'fd' && mod.code?.startsWith('KK-')) continue let result = this.moduleCache[mod.id] if (!result) { result = await this.resolveSingleModule(mod) diff --git a/updater/sis-updater-worker/src/updater/updateProgrammeModules/updateProgrammeModules.js b/updater/sis-updater-worker/src/updater/updateProgrammeModules/updateProgrammeModules.js index 4508e1aa7c..9fc03f0499 100644 --- a/updater/sis-updater-worker/src/updater/updateProgrammeModules/updateProgrammeModules.js +++ b/updater/sis-updater-worker/src/updater/updateProgrammeModules/updateProgrammeModules.js @@ -3,6 +3,7 @@ const { chunk } = require('lodash') const { bulkCreate, selectFromByIds } = require('../../db') const { dbConnections } = require('../../db/connection') const { ProgrammeModule, ProgrammeModuleChild } = require('../../db/models') +const { logger } = require('../../utils/logger') const ModuleResolver = require('./resolver') const resolveProgramme = async programme => { @@ -56,7 +57,14 @@ const recursiveWrite = (modArg, parentId, programmeMap, joinMap) => { if (parentId) joinMap[join.composite] = join if (!children) return - children.forEach(child => recursiveWrite(child, mod.id, programmeMap, joinMap)) + try { + children.forEach(child => recursiveWrite(child, mod.id, programmeMap, joinMap)) + } catch (error) { + logger.error( + `Could not update programme module with id ${mod.id} and parentId ${parentId} due to faulty children/rule handling`, + error + ) + } } const updateProgrammeModulesChunk = async programmeIds => { diff --git a/updater/sis-updater-worker/src/updater/updateStudents/SISStudyRights.js b/updater/sis-updater-worker/src/updater/updateStudents/SISStudyRights.js index f098c62a89..41edc9fe7c 100644 --- a/updater/sis-updater-worker/src/updater/updateStudents/SISStudyRights.js +++ b/updater/sis-updater-worker/src/updater/updateStudents/SISStudyRights.js @@ -1,5 +1,6 @@ const { selectAllFrom, bulkCreate, selectOneById } = require('../../db') const { SISStudyRight, SISStudyRightElement } = require('../../db/models') +const { logger } = require('../../utils') const { termRegistrationTypeToEnrollmenttype } = require('../mapper') const { getEducation, @@ -38,28 +39,33 @@ const getStudyRightSemesterEnrollments = semesterEnrollments => { } const studyRightMapper = (personIdToStudentNumber, admissionNamesById, semesterEnrollments) => studyRight => { - const semesterEnrollmentsForStudyRight = semesterEnrollments.find( - ({ study_right_id }) => study_right_id === studyRight.id - )?.term_registrations - const studyRightEducation = getEducation(studyRight.education_id) - if (!studyRightEducation) return null + try { + const semesterEnrollmentsForStudyRight = semesterEnrollments.find( + ({ study_right_id }) => study_right_id === studyRight.id + )?.term_registrations + const studyRightEducation = getEducation(studyRight.education_id) + if (!studyRightEducation) return null - const educationType = getEducationType(studyRightEducation.education_type) - const extentCode = educationTypeToExtentcode[educationType.id] || educationTypeToExtentcode[educationType.parent_id] + const educationType = getEducationType(studyRightEducation.education_type) + const extentCode = educationTypeToExtentcode[educationType.id] || educationTypeToExtentcode[educationType.parent_id] - return { - id: studyRight.id, - // validityPeriod of a study right always has a start date but not always an end date. The interval is end exclusive so we need to subtract one day from the end date to get the "real" end date. - startDate: new Date(studyRight.valid.startDate), - endDate: addDaysToDate(studyRight.valid.endDate, -1), - studyStartDate: studyRight.study_start_date ? new Date(studyRight.study_start_date) : null, - // cancellationType is always 'RESCINDED' or 'CANCELLED_BY_ADMINISTRATION' - cancelled: studyRight.study_right_cancellation != null, - studentNumber: personIdToStudentNumber[studyRight.person_id], - extentCode, - admissionType: admissionNamesById[studyRight.admission_type_urn], - semesterEnrollments: getStudyRightSemesterEnrollments(semesterEnrollmentsForStudyRight), - facultyCode: getOrganisationCode(studyRight.organisation_id), + return { + id: studyRight.id, + // validityPeriod of a study right always has a start date but not always an end date. The interval is end exclusive so we need to subtract one day from the end date to get the "real" end date. + startDate: new Date(studyRight.valid.startDate), + endDate: addDaysToDate(studyRight.valid.endDate, -1), + studyStartDate: studyRight.study_start_date ? new Date(studyRight.study_start_date) : null, + // cancellationType is always 'RESCINDED' or 'CANCELLED_BY_ADMINISTRATION' + cancelled: studyRight.study_right_cancellation != null, + studentNumber: personIdToStudentNumber[studyRight.person_id], + extentCode, + admissionType: admissionNamesById[studyRight.admission_type_urn], + semesterEnrollments: getStudyRightSemesterEnrollments(semesterEnrollmentsForStudyRight), + facultyCode: getOrganisationCode(studyRight.organisation_id), + } + } catch (error) { + logger.error(`Study right mapping failed for studyRightId ${studyRight.id}`, error) + return null } }