Skip to content

Commit

Permalink
feat: scientifically correct CU name madness
Browse files Browse the repository at this point in the history
  • Loading branch information
HRemonen committed Apr 19, 2024
1 parent 10cadf3 commit 6ac42d7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"pg": "^8.5.1",
"redis": "^4.6.12",
"sequelize": "^6.35.1",
"string-similarity-js": "^2.1.4",
"umzug": "^2.3.0",
"winston": "^3.8.1",
"winston-gelf-transporter": "^1.0.2"
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const start = async () => {
await initializeFunctions()
await redis.connect()
await updater.checkStatusOnStartup()
await updater.run()
await updater.start()
await startEnrolmentsCron()

Expand Down
4 changes: 2 additions & 2 deletions src/updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const JOB_TYPE = 'NIGHTLY'

const runUpdater = async () => {
// Dependencies between updating, may result in failure if order not kept
await updateUsers()
await updateOrganisations()
// await updateUsers()
// await updateOrganisations()
await updateCoursesAndTeacherFeedbackTargets()
await updateStudentFeedbackTargets()
await updateFeedbackTargetCounts()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const dateFns = require('date-fns')
const { stringSimilarity } = require("string-similarity-js")
const { parseFromTimeZone } = require('date-fns-timezone')
const { Op } = require('sequelize')
const _ = require('lodash')
Expand Down Expand Up @@ -112,27 +113,35 @@ const getIncludeCurs = async () => {
}

// Find the newest course unit that has started before the course realisation
const getCourseUnit = ({ activityPeriod, courseUnits }) => {
let courseUnit = courseUnits[0] // old default

const getCourseUnit = ({ activityPeriod, courseUnits, name }) => {
const { startDate: realisationStartDate } = activityPeriod

courseUnits.sort((a, b) => {
const { startDate: aStartDate } = a.validityPeriod
const { startDate: bStartDate } = b.validityPeriod
const scientificallyAccurateCUs = courseUnits.map((courseUnit) => {
const getSimilarityRanking = (language) => stringSimilarity(name[language] ?? '', courseUnit.name[language] ?? '')

const fiSimilarity = getSimilarityRanking('fi')
const enSimilarity = getSimilarityRanking('en')
const svSimilarity = getSimilarityRanking('sv')

if (!aStartDate || !bStartDate) return 0
const similarityRanking = Math.max(fiSimilarity, enSimilarity, svSimilarity)

return dateFns.isAfter(new Date(aStartDate), new Date(bStartDate)) ? -1 : 1
return ({ ...courseUnit, similarityRanking })
})

courseUnit = courseUnits.find(({ validityPeriod }) => {
const sortedCourseUnits = _.orderBy(scientificallyAccurateCUs, ['similarityRanking', (cu) => {
const { startDate } = cu.validityPeriod.startDate

return Date.parse(startDate)
}], ['desc', 'desc'])


const courseUnit = sortedCourseUnits.find(({ validityPeriod }) => {
const { startDate } = validityPeriod

if (!startDate) return false

return dateFns.isAfter(new Date(realisationStartDate), new Date(startDate))
}) ?? courseUnit
}) ?? sortedCourseUnits[0]

return courseUnit
}
Expand Down

0 comments on commit 6ac42d7

Please sign in to comment.