Skip to content

Commit

Permalink
[Backend] Add null checks to students.ts and add a util function `key…
Browse files Browse the repository at this point in the history
…sOf`
  • Loading branch information
valtterikantanen committed Jul 18, 2024
1 parent 98a56ee commit cc692f3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions services/backend/src/services/students.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const byStudentNumber = async (studentNumber: string) => {
},
}),
])
if (!student) return null
const tagprogrammes = await ElementDetail.findAll({
where: {
code: {
Expand Down Expand Up @@ -324,6 +325,7 @@ const formatStudentWithoutTags = (
export const withStudentNumber = async (studentNumber: string) => {
try {
const student = await byStudentNumber(studentNumber)
if (!student) return null
return formatStudent(student)
} catch (error) {
logger.error(`Error when fetching single student ${error}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from 'moment'

import { Credit, SISStudyRight, SISStudyRightElement } from '../../models'
import { GenderCode, EnrollmentType, ExtentCode } from '../../types'
import { keysOf } from '../../util'
import { countTimeCategories } from '../graduationHelpers'
import { getSemestersAndYears } from '../semesters'
import { getDateOfFirstSemesterPresent } from './studyProgrammeBasics'
Expand Down Expand Up @@ -375,7 +376,7 @@ const getMainStatsByTrackAndYear = async (
yearlyStats.Total[track] = getEmptyYear()
}

for (const field of Object.keys(yearlyStats[year][track]) as Array<keyof (typeof yearlyStats)[string][string]>) {
for (const field of keysOf(yearlyStats[year][track])) {
if (field !== 'otherCountriesCounts') {
yearlyStats.Total[track][field] += yearlyStats[year][track][field]
continue
Expand Down
8 changes: 8 additions & 0 deletions services/backend/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ export const sortByProgrammeCode = (a: string, b: string) => {
}
return a.localeCompare(b)
}

/**
* Returns the keys of the given object as an array of strings, typed as the keys of the object.
* @param obj The object whose keys are to be returned.
*/
export const keysOf = <T extends object>(obj: T) => {
return Object.keys(obj) as Array<keyof T>
}

0 comments on commit cc692f3

Please sign in to comment.