Skip to content

Commit

Permalink
Refactor mapToProviders
Browse files Browse the repository at this point in the history
  • Loading branch information
rikurauhala committed Jun 25, 2024
1 parent e86b327 commit 0e2c546
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 56 deletions.
37 changes: 21 additions & 16 deletions services/backend/src/util/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,32 @@ const mapObject = (obj, schema) => {
}, {})
}

const handleUnderscoreProgrammeCode = programmeCode => {
const [left, right] = programmeCode.split('_')
const prefix = [...left].filter(char => !Number.isNaN(Number(char))).join('')
const suffix = `${left[0]}${right}`
return `${prefix}0-${suffix}`
}

const handleDoctoralProgrammeCode = programmeCode => {
const numbers = programmeCode.substring(1)
const courseProvider = Number(`7${numbers}`)
if (courseProvider < 7920111 && courseProvider > 7920102) {
return `${courseProvider + 1}`
}
if (courseProvider === 7920111) {
return '7920103'
}
return `${courseProvider}`
}

const mapToProviders = programmeCodes => {
return programmeCodes.map(programmeCode => {
const isNumber = str => !Number.isNaN(Number(str))
if (programmeCode.includes('_')) {
const [left, right] = programmeCode.split('_')
const prefix = [...left].filter(isNumber).join('')
const suffix = `${left[0]}${right}`
const providerCode = `${prefix}0-${suffix}`
return providerCode
return handleUnderscoreProgrammeCode(programmeCode)
}
if (/^(T)[0-9]{6}$/.test(programmeCode)) {
const numbers = programmeCode.substring(1)
const courseProvider = Number(`7${numbers}`)
// Fix a bunch of doctoral degrees that got the wrong provider
if (courseProvider < 7920111 && courseProvider > 7920102) {
return `${courseProvider + 1}`
}
if (courseProvider === 7920111) {
return '7920103'
}
return `${courseProvider}`
return handleDoctoralProgrammeCode(programmeCode)
}
return programmeCode
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { useState } from 'react'
import { Form, Segment, Dropdown, Button, Message } from 'semantic-ui-react'
import { Button, Dropdown, Form, Message, Segment } from 'semantic-ui-react'

import { createLocaleComparator, getCurrentSemester, getFullStudyProgrammeRights } from '@/common'
import { useLanguage } from '@/components/LanguagePicker/useLanguage'
import { TeacherStatisticsTable } from '@/components/Teachers/TeacherStatisticsTable'
import { useGetAuthorizedUserQuery } from '@/redux/auth'
import { useGetProvidersQuery } from '@/redux/providers'
import { useGetSemestersQuery } from '@/redux/semesters'
import { useLazyGetTeacherStatisticsQuery } from '@/redux/teachers'
import { TeacherStatisticsTable } from '../TeacherStatisticsTable'
import { mapToProviders } from './mapToProviders'

export const TeacherStatistics = () => {
const { getTextIn } = useLanguage()
const [semesterStart, setSemesterStart] = useState(null)
const [semesterEnd, setSemesterEnd] = useState(null)
// awful variable name but for some reason we need providers for props and state :kuolemakiitos:
const [provs, setProviders] = useState([])
const [providers, setProviders] = useState([])
const { programmeRights, isAdmin } = useGetAuthorizedUserQuery()
const fullStudyProgrammeRights = getFullStudyProgrammeRights(programmeRights)
const [getTeacherStatistics, { data: teacherData, isFetching, isLoading }] = useLazyGetTeacherStatisticsQuery()
const { data: providers = [] } = useGetProvidersQuery()

const { data: providersData = [] } = useGetProvidersQuery()
const { data: semesterData } = useGetSemestersQuery()

const semesters = !semesterData?.semesters
Expand All @@ -39,34 +38,6 @@ export const TeacherStatistics = () => {
}
}

/** Maps study programme codes to provider codes, for example `KH50_005` -> `500-K005`. The same logic as in backend's mapToProviders function. */
const mapToProviders = programmeCodes =>
programmeCodes.map(r => {
const isNumber = str => !Number.isNaN(Number(str))
if (r.includes('_')) {
const [left, right] = r.split('_')
const prefix = [...left].filter(isNumber).join('')
const suffix = `${left[0]}${right}`
const providercode = `${prefix}0-${suffix}`
return providercode
}
if (/^(T)[0-9]{6}$/.test(r)) {
const numbers = r.substring(1)
const courseProvider = `7${numbers}`
const asNum = Number(courseProvider)
// God-awful hack to fix a bunch of doctoral degrees
// that got the wrong provider
if (asNum > 7920102 && asNum < 7920111) {
return `${asNum + 1}`
}
if (asNum === 7920111) {
return '7920103'
}
return `${asNum}`
}
return r
})

const setEndSemester = (_, { value }) => {
setSemesterEnd(value)
}
Expand All @@ -89,8 +60,10 @@ export const TeacherStatistics = () => {
const currentSemesterCode = getCurrentSemester(semesterData?.semesters)?.semestercode

const userProviders = mapToProviders(fullStudyProgrammeRights)
const invalidQueryParams = provs.length === 0 || !semesterStart
const providerOptions = isAdmin ? providers : providers.filter(provider => userProviders.includes(provider.code))
const invalidQueryParams = providers.length === 0 || !semesterStart
const providerOptions = isAdmin
? providersData
: providersData.filter(provider => userProviders.includes(provider.code))
const localizedProviderOptions = providerOptions
.map(({ name, code }) => ({
key: code,
Expand All @@ -103,8 +76,10 @@ export const TeacherStatistics = () => {
return (
<div>
<Message
content="Statistics for teachers that admitted credits during
and between the given semesters for one of the given course providers."
content={`
Statistics for teachers that admitted credits during and between
the given semesters for one of the given course providers.
`}
header="Teacher statistics by course providers"
/>
<Segment>
Expand Down Expand Up @@ -151,14 +126,14 @@ export const TeacherStatistics = () => {
selectOnBlur={false}
selectOnNavigation={false}
selection
value={provs}
value={providers}
/>
</Form.Field>
<Button
content="Search"
disabled={invalidQueryParams}
fluid
onClick={() => getTeacherStatistics({ semesterStart, semesterEnd, providers: provs })}
onClick={() => getTeacherStatistics({ semesterStart, semesterEnd, providers })}
/>
</Form>
</Segment>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Identical logic can be found in the backend (src/util/map.js)
// If the logic changes, remember to update the backend as well

const handleUnderscoreProgrammeCode = programmeCode => {
const [left, right] = programmeCode.split('_')
const prefix = [...left].filter(char => !Number.isNaN(Number(char))).join('')
const suffix = `${left[0]}${right}`
return `${prefix}0-${suffix}`
}

const handleDoctoralProgrammeCode = programmeCode => {
const numbers = programmeCode.substring(1)
const courseProvider = Number(`7${numbers}`)
if (courseProvider < 7920111 && courseProvider > 7920102) {
return `${courseProvider + 1}`
}
if (courseProvider === 7920111) {
return '7920103'
}
return `${courseProvider}`
}

export const mapToProviders = programmeCodes => {
return programmeCodes.map(programmeCode => {
if (programmeCode.includes('_')) {
return handleUnderscoreProgrammeCode(programmeCode)
}
if (/^(T)[0-9]{6}$/.test(programmeCode)) {
return handleDoctoralProgrammeCode(programmeCode)
}
return programmeCode
})
}

0 comments on commit 0e2c546

Please sign in to comment.