Skip to content

Commit

Permalink
[Shared] Move formatQueryParamsToArrays to shared/util
Browse files Browse the repository at this point in the history
  • Loading branch information
rikurauhala committed Oct 24, 2024
1 parent 3d5ced8 commit 8a7426a
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 32 deletions.
2 changes: 1 addition & 1 deletion services/backend/src/routes/population.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { bottlenecksOf } from '../services/populations/bottlenecksOf'
import { optimizedStatisticsOf } from '../services/populations/optimizedStatisticsOf'
import { populationStudentsMerger, populationCourseStatsMerger } from '../services/statMerger'
import { findByTag, findByCourseAndSemesters } from '../services/students'
import { mapToProviders } from '../shared/util/mapToProviders'
import { mapToProviders } from '../shared/util'
import { GenderCode, ParsedCourse, Unarray, Unification, UnifyStatus } from '../types'
import { getFullStudyProgrammeRights, hasFullAccessToStudentData } from '../util'
import { ApplicationError } from '../util/customErrors'
Expand Down
2 changes: 1 addition & 1 deletion services/backend/src/routes/teachers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Request, Response, Router } from 'express'
import { getProvidersOfFaculty, isFaculty } from '../services/organizations'
import { getTeachersBySearchTerm, getTeacherStatistics, getYearlyStatistics } from '../services/teachers'
import { CategoryID, getTeacherStats, findAndSaveTeachers, getCategoriesAndYears } from '../services/teachers/top'
import { mapToProviders } from '../shared/util/mapToProviders'
import { mapToProviders } from '../shared/util'
import { getFullStudyProgrammeRights, splitByEmptySpace } from '../util'

const router = Router()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { col, Op } from 'sequelize'

import { Course, Credit, Organization, Student, Studyplan, SISStudyRight, SISStudyRightElement } from '../../models'
import { mapToProviders } from '../../shared/util/mapToProviders'
import { mapToProviders } from '../../shared/util'
import { CreditTypeCode, DegreeProgrammeType, ExtentCode, Name, SemesterEnrollment } from '../../types'
import { redisClient } from '../redis'
import { getCurriculumVersion } from './shared'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { formatQueryParamsToArrays } from '../../shared/util'
import { Criteria, DegreeProgrammeType, Name } from '../../types'
import { getDegreeProgrammeType } from '../../util'
import { getCriteria } from '../studyProgramme/studyProgrammeCriteria'
import { getStudentsIncludeCoursesBetween } from './getStudentsIncludeCoursesBetween'
import {
dateMonthsFromNow,
formatQueryParamsToArrays,
formatStudentsForApi,
getOptionsForStudents,
parseQueryParams,
Expand Down
11 changes: 0 additions & 11 deletions services/backend/src/services/populations/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,6 @@ export const formatStudentsForApi = async (
}
}

export const formatQueryParamsToArrays = (query: Record<string, any>, params: string[]) => {
const result = { ...query }
params.forEach(param => {
if (!result[param]) {
return
}
result[param] = Array.isArray(result[param]) ? result[param] : [result[param]]
})
return result
}

const getSubstitutions = async (codes: string[]) => {
const courses = await Course.findAll({
attributes: ['code', 'substitutions'],
Expand Down
2 changes: 1 addition & 1 deletion services/backend/src/services/providerCredits.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InferAttributes } from 'sequelize'

import { SISStudyRight } from '../models'
import { mapToProviders } from '../shared/util/mapToProviders'
import { mapToProviders } from '../shared/util'
import { EnrollmentType, ExtentCode } from '../types'
import { getCreditStats, setCreditStats } from './analyticsService'
import { getCourseCodesOfProvider } from './providers'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { orderBy, range } from 'lodash'
import { Op } from 'sequelize'

import { Credit, Enrollment } from '../../models'
import { mapToProviders } from '../../shared/util/mapToProviders'
import { mapToProviders } from '../../shared/util'
import { CreditTypeCode, EnrollmentState, Name } from '../../types'
import { isOpenUniCourseCode } from '../../util'
import { createArrayOfCourses } from '../languageCenterData'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { indexOf, orderBy } from 'lodash'
import moment from 'moment'

import { mapToProviders } from '../../shared/util/mapToProviders'
import { mapToProviders } from '../../shared/util'
import { DegreeProgrammeType, ExtentCode, Name, SemesterEnrollment, StudyTrack } from '../../types'
import { getDegreeProgrammeType, sortByProgrammeCode } from '../../util'
import { countTimeCategories } from '../graduationHelpers'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useGetAuthorizedUserQuery } from '@/redux/auth'
import { getPopulationStatistics, clearPopulations, useGetProgrammesQuery } from '@/redux/populations'
import { clearSelected } from '@/redux/populationSelectedStudentCourses'
import { useGetStudyProgrammePinsQuery } from '@/redux/studyProgrammePins'
import { formatQueryParamsToArrays } from '@/shared/util'
import { momentFromFormat, reformatDate } from '@/util/timeAndDate'
import './populationSearch.css'

Expand Down Expand Up @@ -94,19 +95,6 @@ const PopulationSearchForm = ({ queries, onProgress, clearSelected, getPopulatio
return sameStudyrights && sameMonths && sameYear && sameSemesters && sameStudentStatuses && sameYears && sameTag
}

// Identical logic can be found in the backend (src/services/populations/shared.js)
// If the logic changes, remember to update the backend as well
const formatQueryParamsToArrays = (query, params) => {
const res = { ...query }
params.forEach(param => {
if (!res[param]) {
return
}
res[param] = Array.isArray(res[param]) ? res[param] : [res[param]]
})
return res
}

const fetchPopulation = async query => {
const formattedQueryParams = formatQueryParamsToArrays(query, ['semesters', 'studentStatuses', 'years'])
const uuid = crypto.randomUUID()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useGetAuthorizedUserQuery } from '@/redux/auth'
import { useGetProvidersQuery } from '@/redux/providers'
import { useGetSemestersQuery } from '@/redux/semesters'
import { useLazyGetTeacherStatisticsQuery } from '@/redux/teachers'
import { mapToProviders } from '@/shared/util/mapToProviders'
import { mapToProviders } from '@/shared/util'

export const TeacherStatistics = () => {
const { getTextIn } = useLanguage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ const programmeCodeToProviderCode = (programmeCode: string) => {
export const mapToProviders = (programmeCodes: string[]) => {
return programmeCodes.map(programmeCodeToProviderCode)
}

export const formatQueryParamsToArrays = (query: Record<string, any>, params: string[]) => {
const result = { ...query }
params.forEach(param => {
if (!result[param]) {
return
}
result[param] = Array.isArray(result[param]) ? result[param] : [result[param]]
})
return result
}

0 comments on commit 8a7426a

Please sign in to comment.