diff --git a/services/backend/src/routes/courses.ts b/services/backend/src/routes/courses.ts index fd600ef85..594179ecb 100644 --- a/services/backend/src/routes/courses.ts +++ b/services/backend/src/routes/courses.ts @@ -12,7 +12,12 @@ import { import type { CourseWithSubsId } from '@oodikone/shared/types/course' import { getCourseYearlyStats } from '../services/courses' import { getCoursesByNameAndOrCode } from '../services/courses/courseFinders' -import { getFullStudyProgrammeRights, hasFullAccessToStudentData, validateParamLength } from '../util' +import { + getFullStudyProgrammeRights, + handleQueryArrays, + hasFullAccessToStudentData, + validateParamLength, +} from '../util' const router = Router() @@ -53,7 +58,9 @@ router.get, CourseYearlyStatsReqBody, async (req, res) => { const { codes, combineSubstitutions, separate } = req.query - if (!codes) { + const courseCodes = handleQueryArrays(codes) + + if (!courseCodes) { return res.status(422).send({ error: 'Missing required query parameters' }) } @@ -75,7 +82,7 @@ router.get, CourseYearlyStatsReqBody, const useCombineSubstitutions = combineSubstitutions !== 'false' const useSeparate = separate === 'true' - const results = await getCourseYearlyStats(codes, useSeparate, anonymizationSalt, useCombineSubstitutions) + const results = await getCourseYearlyStats(courseCodes, useSeparate, anonymizationSalt, useCombineSubstitutions) res.json(results) } ) diff --git a/services/backend/src/routes/population.ts b/services/backend/src/routes/population.ts index 54c76a3ad..186a67ad4 100644 --- a/services/backend/src/routes/population.ts +++ b/services/backend/src/routes/population.ts @@ -26,7 +26,7 @@ import { parseDateRangeFromParams } from '../services/populations/shared' import { statisticsOf } from '../services/populations/statisticsOf' import { getStudentNumbersWithStudyRights } from '../services/populations/studentNumbersWithStudyRights' import { findByCourseAndSemesters } from '../services/students' -import { getFullStudyProgrammeRights, hasFullAccessToStudentData } from '../util' +import { getFullStudyProgrammeRights, handleQueryArrays, hasFullAccessToStudentData } from '../util' const router = Router() @@ -103,10 +103,6 @@ router.get, PopulationstatisticsReq const { id: userId, roles: userRoles, programmeRights: userProgrammeRights, studentsUserCanAccess } = req.user const { programme, combinedProgramme } = req.query - // NOTE: Make sure we have only arrays, "/...&years=2021" is parsed by express as years: 2021, not [2021] - const handleQueryArrays = (requestField: T | T[]): T[] => { - return Array.isArray(requestField) ? requestField : [requestField] - } const years = handleQueryArrays(req.query.years) const semesters = handleQueryArrays(req.query.semesters) const studentStatuses = handleQueryArrays(req.query.studentStatuses) // NOTE: TRANSFERRED means transferred out of the program diff --git a/services/backend/src/util/index.ts b/services/backend/src/util/index.ts index 3789479ed..842035bb7 100644 --- a/services/backend/src/util/index.ts +++ b/services/backend/src/util/index.ts @@ -100,3 +100,8 @@ export const safeJSONParse = async (json: string): Promise => { return data } + +// NOTE: Make sure we have only arrays, "/...&years=2021" is parsed by express as years: 2021, not [2021] +export const handleQueryArrays = (requestField: T | T[]): T[] => { + return Array.isArray(requestField) ? requestField : [requestField] +}