Skip to content

Commit

Permalink
[Course stats] Remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
valtterikantanen committed Jun 24, 2024
1 parent b1f3447 commit 7b6c7ae
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 110 deletions.
83 changes: 81 additions & 2 deletions services/backend/src/services/courses/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const crypto = require('crypto')
const { Op } = require('sequelize')

const { Course, Credit, Enrollment, Organization } = require('../../models')
Expand All @@ -9,8 +10,86 @@ const {
enrollmentsForCourses,
getStudentNumberToSrElementsMap,
} = require('./creditsAndEnrollmentsOfCourse')
const { parseCredit } = require('./parseCredits')
const { parseEnrollment } = require('./parseEnrollments')

const formatStudyrightElement = ({ code, element_detail, startdate, studyright: sr }) => {
const studyright = sr.get({ plain: true })
return {
code,
name: element_detail.name,
startdate,
faculty_code: studyright.faculty_code || null,
organization: studyright.organization
? {
name: studyright.organization.name,
code: studyright.organization.code,
}
: null,
}
}

const parseCredit = (credit, anonymizationSalt, studentNumberToSrElementsMap) => {
const { semester, grade, course_code, credits, attainment_date, student_studentnumber: studentnumber } = credit
const { yearcode, yearname, semestercode, name: semestername } = semester

const studyrightElements = studentNumberToSrElementsMap[studentnumber] || []

const formattedCredit = {
yearcode,
yearname,
semestercode,
semestername,
attainment_date,
coursecode: course_code,
grade,
passed: !Credit.failed(credit) || Credit.passed(credit) || Credit.improved(credit),
studentnumber,
programmes: studyrightElements.map(formatStudyrightElement),
credits,
}

if (anonymizationSalt) {
const anonymizedStudentNumber = crypto
.createHash('sha256')
.update(`${studentnumber}${anonymizationSalt}`)
.digest('hex')

formattedCredit.obfuscated = true
formattedCredit.studentnumber = anonymizedStudentNumber
}

return formattedCredit
}

const parseEnrollment = (enrollment, anonymizationSalt, studentNumberToSrElementsMap) => {
const { studentnumber, semester, state, enrollment_date_time, course_code } = enrollment
const { yearcode, yearname, semestercode, name: semestername } = semester

const studyrightElements = studentNumberToSrElementsMap[studentnumber] || []

const formattedEnrollment = {
yearcode,
yearname,
semestercode,
semestername,
coursecode: course_code,
state,
enrollment_date_time,
studentnumber,
programmes: studyrightElements.map(formatStudyrightElement),
}

if (anonymizationSalt) {
const anonymizedStudentNumber = crypto
.createHash('sha256')
.update(`${studentnumber}${anonymizationSalt}`)
.digest('hex')

formattedEnrollment.obfuscated = true
formattedEnrollment.studentnumber = anonymizedStudentNumber
}

return formattedEnrollment
}

const isOpenUniCourseCode = code => code.match(/^AY?(.+?)(?:en|fi|sv)?$/)

Expand Down
64 changes: 0 additions & 64 deletions services/backend/src/services/courses/parseCredits.js

This file was deleted.

44 changes: 0 additions & 44 deletions services/backend/src/services/courses/parseEnrollments.js

This file was deleted.

0 comments on commit 7b6c7ae

Please sign in to comment.