Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
291 changes: 281 additions & 10 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@kth/kth-reactstrap": "^0.5.0",
"@kth/log": "^4.0.7",
"@kth/monitor": "^4.3.1",
"@kth/om-kursen-ladok-client": "2.5.0",
"@kth/om-kursen-ladok-client": "2.5.4",
"@kth/server": "^4.1.0",
"@kth/session": "^3.0.9",
"@kth/style": "^1.13.0",
Expand Down
7 changes: 4 additions & 3 deletions public/js/app/utils-shared/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { parseSemesterIntoYearSemesterNumber } from '../../../../shared/semesterUtils'

const i18n = require('../../../../i18n')

const convertLangToIndex = langShortName => (langShortName === 'en' ? 0 : 1)
Expand All @@ -7,9 +9,8 @@ const seasonStr = (language, semesterRaw) => {
const isLangANumber = typeof language === 'number'
const langIndex = isLangANumber ? language : convertLangToIndex(language)
const { extraInfo } = i18n.messages[langIndex]
const termStringAsSeason = `${extraInfo.season[semesterRaw.toString()[4]]}${semesterRaw.toString().slice(0, 4)}`

return termStringAsSeason
const { year, semesterNumber } = parseSemesterIntoYearSemesterNumber(semesterRaw)
return `${extraInfo.season[semesterNumber]}${year}`
}

export { convertLangToIndex, seasonStr }
2 changes: 0 additions & 2 deletions server/controllers/choiceOptionsCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ async function getCourseOptionsPage(req, res, next) {

const ladokCourseRounds = await getCourseRoundsData(courseCode, lang, user)
const ladokCourseData = await getLadokCourseData(courseCode, lang)

const ladokCourseRoundTerms = formatLadokData(ladokCourseRounds, ladokCourseData)

applicationStore.miniLadokObj = ladokCourseRoundTerms
const memoParams = getMemosParams(courseCode, applicationStore.miniLadokObj)

Expand Down
33 changes: 15 additions & 18 deletions server/controllers/memoCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const apis = require('../api')
const { getServerSideFunctions } = require('../utils/serverSideRendering')

const { getCourseInfo } = require('../kursinfoApi')
const { getLadokCourseData, getLadokCourseSyllabus } = require('../ladokApi')
const { getLadokCourseData, getLadokCourseSyllabus, getLadokCourseSyllabuses } = require('../ladokApi')
const { getMemoApiData, changeMemoApiData } = require('../kursPmDataApi')
const { getCourseEmployees } = require('../ugRestApi')
const { getValidUntilTerm } = require('../utils/getValidUntilTerm')
const serverPaths = require('../server').getPaths()
const { browser, server } = require('../configuration')
const i18n = require('../../i18n')
Expand All @@ -29,15 +30,14 @@ const mergeAllData = async (
courseInfoApiData,
ladokCourseData,
ladokCourseSyllabusData,
syllabusValidUntilTerm,
ugEmployeesData,
staticData
) => {
const memoDataWithDefaults = addDefaultValues(memoApiData)

// Source: kursinfo-api
const courseInfoApiValues = {
prerequisites: courseInfoApiData.recommendedPrerequisites,
}
const courseInfoApiValues = { prerequisites: courseInfoApiData.recommendedPrerequisites }

// Source: Ladok
const ladokCourseValues = {
Expand All @@ -60,6 +60,10 @@ const mergeAllData = async (
otherRequirementsForFinalGrade: ladokCourseSyllabusData.kursplan.ovrigakravforslutbetyg,
ethicalApproach: ladokCourseSyllabusData.kursplan.etisktforhallningssatt,
additionalRegulations: ladokCourseSyllabusData.kursplan.faststallande,
syllabusValid: {
validFromTerm: ladokCourseSyllabusData.kursplan.giltigfrom,
validUntilTerm: syllabusValidUntilTerm ?? '',
},
}

// Source: UG Admin
Expand All @@ -70,9 +74,7 @@ const mergeAllData = async (
}

// Source: Static data
const staticValues = {
permanentDisability: staticData.permanentDisability,
}
const staticValues = { permanentDisability: staticData.permanentDisability }

return {
...memoDataWithDefaults,
Expand Down Expand Up @@ -105,16 +107,14 @@ async function renderMemoEditorPage(req, res, next) {

applicationStore.doSetLanguageIndex(userLang)

applicationStore.setMemoBasicInfo({
courseCode,
memoEndPoint,
semester,
memoLangAbbr,
})
applicationStore.setMemoBasicInfo({ courseCode, memoEndPoint, semester, memoLangAbbr })

const courseInfoApiData = await getCourseInfo(courseCode, memoLangAbbr)
const ladokCourseData = await getLadokCourseData(courseCode, memoLangAbbr)
const ladokCourseSyllabusData = await getLadokCourseSyllabus(courseCode, semester, memoLangAbbr)

const ladokCourseSyllabusesData = await getLadokCourseSyllabuses(courseCode, semester, memoLangAbbr)
const syllabusValidUntilTerm = getValidUntilTerm(ladokCourseSyllabusesData, ladokCourseSyllabusData)
const ugEmployeesData = await getCourseEmployees(memoApiData)
const staticData = i18n.messages[memoLangAbbr === 'en' ? 0 : 1].staticMemoBodyByUserLang

Expand All @@ -123,6 +123,7 @@ async function renderMemoEditorPage(req, res, next) {
courseInfoApiData,
ladokCourseData,
ladokCourseSyllabusData,
syllabusValidUntilTerm,
ugEmployeesData,
staticData
)
Expand Down Expand Up @@ -177,8 +178,4 @@ async function updateContentByEndpoint(req, res, next) {
}
}

module.exports = {
mergeAllData,
renderMemoEditorPage,
updateContentByEndpoint,
}
module.exports = { mergeAllData, renderMemoEditorPage, updateContentByEndpoint }
11 changes: 11 additions & 0 deletions server/ladokApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,20 @@ async function getLadokCourseSyllabus(courseCode, semester, lang) {
}
}

async function getLadokCourseSyllabuses(courseCode, semester, lang) {
try {
const courseSyllabuses = await client.getCourseSyllabuses(courseCode, semester, lang)

return courseSyllabuses
} catch (error) {
throw new Error(error.message)
}
}

module.exports = {
getLadokCourseData,
getCourseRoundsData,
getCourseSchoolCode,
getLadokCourseSyllabus,
getLadokCourseSyllabuses,
}
2 changes: 1 addition & 1 deletion server/utils/formatLadokData.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const formatLadokData = (ladokCourseRounds, ladokCourseData) => {

const ladokCourseRoundTerms = {
course: {
title: ladokCourseData.benamning.name,
title: ladokCourseData.benamning?.name,
credits: ladokCourseData.omfattning,
creditUnitLabel: ladokCourseData.utbildningstyp?.creditsUnit,
creditUnitAbbr: ladokCourseData.utbildningstyp?.creditsUnit.code.toLowerCase(),
Expand Down
52 changes: 52 additions & 0 deletions server/utils/getValidUntilTerm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const {
calcPreviousSemester,
parseSemesterIntoYearSemesterNumber,
SemesterNumber,
LadokSemesterPrefix,
} = require('../../shared/semesterUtils')

const sortSyllabusesByGiltigFrom = syllabuses =>
syllabuses.sort((a, b) => {
const { year: yearA, semesterNumber: semesterNumberA } = parseSemesterIntoYearSemesterNumber(a.kursplan?.giltigfrom)

const { year: yearB, semesterNumber: semesterNumberB } = parseSemesterIntoYearSemesterNumber(b.kursplan?.giltigfrom)

// First sort by year, then by termOrder
if (yearA !== yearB) return yearA - yearB
return semesterNumberA - semesterNumberB
})

const getValidUntilTerm = (syllabuses, currentSyllabus) => {
// Sort syllabuses by semester in ascending order
if (!Array.isArray(syllabuses) || syllabuses.length === 0 || !currentSyllabus) {
return undefined
}
const syllabusesSortedByGiltigFrom = sortSyllabusesByGiltigFrom(syllabuses)
// Prevent duplicates
const seen = new Set()
const syllabusesUniqueGiltigFrom = syllabusesSortedByGiltigFrom.filter(item => {
const key = item.kursplan.giltigfrom
if (seen.has(key)) return false
seen.add(key)
return true
})

const index = syllabusesUniqueGiltigFrom.indexOf(
syllabusesUniqueGiltigFrom.find(syllabus => syllabus.kursplan.giltigfrom === currentSyllabus.kursplan.giltigfrom)
)
// validUntilTerm will be the term/semester before the next syllabus validFromTerm semester
if (index !== -1 && index < syllabusesUniqueGiltigFrom.length - 1) {
const nextSyllabusValidFrom = syllabusesUniqueGiltigFrom[index + 1].kursplan.giltigfrom
const semester = parseSemesterIntoYearSemesterNumber(nextSyllabusValidFrom)
return semester.semesterNumber === SemesterNumber.Autumn
? LadokSemesterPrefix.Spring + semester.year
: LadokSemesterPrefix.Autumn + calcPreviousSemester(semester).year
} else {
return undefined
}
}

module.exports = {
getValidUntilTerm,
sortSyllabusesByGiltigFrom,
}
40 changes: 40 additions & 0 deletions server/utils/getValidUntilTerm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { getValidUntilTerm, sortSyllabusesByGiltigFrom } = require('../utils/getValidUntilTerm')

describe('getValidUntilTerm', () => {
const make = term => ({ kursplan: { giltigfrom: term } })

it('returns the term before the next syllabus validFrom date', () => {
const syllabuses = [make('HT2018'), make('VT2020'), make('HT2022'), make('VT2023')]
const result = getValidUntilTerm(syllabuses, make('HT2018'))
expect(result).toBe('HT2019')
})

it('deduplicates by giltigfrom', () => {
const syllabuses = [make('HT2018'), make('HT2018'), make('VT2019'), make('VT2019'), make('HT2019')]

const result = getValidUntilTerm(syllabuses, make('HT2018'))
expect(result).toBe('HT2018') // Next unique is VT2019
})

it('should return undefined if no other syllabuses are present', () => {
const syllabuses = [make('HT2018')]
const result = getValidUntilTerm(syllabuses, make('HT2018'))
expect(result).toBe(undefined)
})
it('should return undefined if syllabuses array is empty', () => {
const syllabuses = []
const result = getValidUntilTerm(syllabuses, make('HT2018'))
expect(result).toBe(undefined)
})
it('should sort syllabuses correctly', () => {
const syllabuses = [make('HT2023'), make('VT2021'), make('VT2019'), make('VT2019'), make('HT2020')]
const result = sortSyllabusesByGiltigFrom(syllabuses)
expect(result).toStrictEqual([
{ kursplan: { giltigfrom: 'VT2019' } },
{ kursplan: { giltigfrom: 'VT2019' } },
{ kursplan: { giltigfrom: 'HT2020' } },
{ kursplan: { giltigfrom: 'VT2021' } },
{ kursplan: { giltigfrom: 'HT2023' } },
])
})
})
Loading