Skip to content

Commit

Permalink
[Backend] Replace ElementDetails where they were still used
Browse files Browse the repository at this point in the history
  • Loading branch information
valtterikantanen committed Aug 26, 2024
1 parent 620bfcb commit d35e303
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import moment from 'moment'
import { col, literal, Op, QueryTypes } from 'sequelize'
import { col, literal, Op } from 'sequelize'

import { dbConnections } from '../../database/connection'
import {
Course,
Credit,
ElementDetail,
Enrollment,
Student,
Studyplan,
Expand All @@ -19,8 +17,6 @@ import {
import { Tag, TagStudent } from '../../models/kone'
import { EnrollmentState } from '../../types'

const { sequelize } = dbConnections

const getStudentTags = async (studyRights: string[], studentNumbers: string[]) => {
return await TagStudent.findAll({
attributes: ['tag_id', 'studentnumber'],
Expand Down Expand Up @@ -238,26 +234,12 @@ const getSemesters = async (studentNumbers: string[], startDate: string, endDate
})
}

const getElementDetails = async (studentNumbers: string[]) => {
const elementDetails: Array<Pick<ElementDetail, 'code' | 'name' | 'type'>> = await sequelize.query(
`SELECT DISTINCT ON (code) code, name, type FROM element_details WHERE
EXISTS (SELECT 1 FROM transfers WHERE studentnumber IN (:studentNumbers) AND (code = sourcecode OR code = targetcode)) OR
EXISTS (SELECT 1 FROM studyright_elements WHERE studentnumber IN (:studentNumbers) AND element_details.code = studyright_elements.code)`,
{
replacements: { studentNumbers },
type: QueryTypes.SELECT,
}
)
return elementDetails
}

type StudentsIncludeCoursesBetween = {
students: Awaited<ReturnType<typeof getStudents>>
enrollments: Awaited<ReturnType<typeof getEnrollments>>
credits: Awaited<ReturnType<typeof getCredits>>
extents: Awaited<ReturnType<typeof getExtents>>
semesters: Awaited<ReturnType<typeof getSemesters>>
elementdetails: Awaited<ReturnType<typeof getElementDetails>>
courses: Awaited<ReturnType<typeof getCourses>>
}

Expand Down Expand Up @@ -302,27 +284,18 @@ export const getStudentsIncludeCoursesBetween = async (

const creditsOfStudent = await getCreditsOfStudent(studentNumbers, studyRights, attainmentDateFrom, endDate)

const [courses, enrollments, students, credits, extents, semesters, elementdetails] = await Promise.all([
const [courses, enrollments, students, credits, extents, semesters] = await Promise.all([
getCourses(creditsOfStudent),
getEnrollments(studentNumbers, attainmentDateFrom, endDate),
getStudents(studentNumbers),
getCredits(creditsOfStudent),
getExtents(),
getSemesters(studentNumbers, startDate, endDate),
getElementDetails(studentNumbers),
])

students.forEach(student => {
student.tags = studentNumberToTags[student.studentnumber] || []
})

return {
students,
enrollments,
credits,
extents,
semesters,
elementdetails,
courses,
} as StudentsIncludeCoursesBetween
return { students, enrollments, credits, extents, semesters, courses } as StudentsIncludeCoursesBetween
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,16 @@ export const optimizedStatisticsOf = async (query: Query, studentNumberList?: st
if (code.includes('KH') || ['MH30_001', 'MH30_003'].includes(code)) {
criteria = await getCriteria(code)
}
const { students, enrollments, credits, extents, semesters, elementdetails, courses } =
await getStudentsIncludeCoursesBetween(
studentNumbers,
startDate,
dateMonthsFromNow(startDate, months),
studyRights,
tag
)
const { students, enrollments, credits, extents, semesters, courses } = await getStudentsIncludeCoursesBetween(
studentNumbers,
startDate,
dateMonthsFromNow(startDate, months),
studyRights,
tag
)

const formattedStudents = await formatStudentsForApi(
{ students, enrollments, credits, extents, semesters, elementdetails, courses },
{ students, enrollments, credits, extents, semesters, courses },
startDate,
endDate,
studyRights,
Expand Down
19 changes: 3 additions & 16 deletions services/backend/src/services/populations/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Op, QueryTypes } from 'sequelize'

import { dbConnections } from '../../database/connection'
import { Course, Credit, SISStudyRight, SISStudyRightElement } from '../../models'
import { ElementDetailType, Criteria, Name } from '../../types'
import { Criteria, Name } from '../../types'
import { SemesterStart } from '../../util/semester'
import { getCurrentSemester } from '../semesters'

Expand Down Expand Up @@ -332,7 +332,7 @@ export const getOptionsForStudents = async (studentNumbers: string[], code: stri
}

export const formatStudentsForApi = async (
{ students, enrollments, credits, extents, semesters, elementdetails, courses },
{ students, enrollments, credits, extents, semesters, courses },
startDate: string,
endDate: string,
studyRights: string[],
Expand All @@ -344,16 +344,6 @@ export const formatStudentsForApi = async (
const endDateMoment = moment(endDate)
const currentSemester = (await getCurrentSemester()).semestercode

elementdetails = elementdetails.reduce(
(acc, elementDetail) => {
acc.data[elementDetail.code] = elementDetail
if (elementDetail.type === ElementDetailType.PROGRAMME) {
acc.programmes.push(elementDetail.code)
}
return acc
},
{ programmes: [], data: {} }
)
credits = credits.reduce((acc, credit) => {
acc[credit.student_studentnumber] = acc[credit.student_studentnumber] || []
acc[credit.student_studentnumber].push(credit)
Expand Down Expand Up @@ -426,16 +416,13 @@ export const formatStudentsForApi = async (
return student
}

const returnvalue = {
return {
students: result.students.map(transferredStudyright),
transfers: result.transfers,
extents,
semesters,
courses,
elementdetails,
}

return returnvalue
}

export const formatQueryParamsToArrays = (query: Record<string, any>, params: string[]) => {
Expand Down
11 changes: 8 additions & 3 deletions services/backend/src/services/students.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Student,
Credit,
Course,
ElementDetail,
ProgrammeModule,
SemesterEnrollment,
Semester,
Studyplan,
Expand Down Expand Up @@ -83,16 +83,19 @@ const byStudentNumber = async (studentNumber: string) => {
],
}),
TagStudent.findAll({
attributes: ['tag_id'],
include: {
model: Tag,
attributes: ['personal_user_id', 'studytrack', 'tagname', 'year'],
},
where: {
studentnumber: studentNumber,
},
}),
])
if (!student) return null
const tagprogrammes = await ElementDetail.findAll({
const tagprogrammes = await ProgrammeModule.findAll({
attributes: ['code', 'name'],
where: {
code: {
[Op.in]: tags.map(tag => tag.tag.studytrack),
Expand Down Expand Up @@ -303,7 +306,9 @@ const formatSharedStudentData = ({
const formatStudent = (
studentData: Partial<Omit<Student, 'semester_enrollments'>> & {
semester_enrollments: SemesterEnrollmentWithNameAndYear[]
} & { tags: Array<InferAttributes<TagStudent> & { programme?: ElementDetail }> }
} & {
tags: Array<InferAttributes<TagStudent> & { programme?: Pick<InferAttributes<ProgrammeModule>, 'code' | 'name'> }>
}
) => {
const formattedData = formatSharedStudentData(studentData)
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useLanguage } from '@/components/LanguagePicker/useLanguage'
import { SortableTable } from '@/components/SortableTable'
import { DISPLAY_DATE_FORMAT, ISO_DATE_FORMAT, ISO_DATE_FORMAT_DEV } from '@/constants/date'
import { useGetAuthorizedUserQuery } from '@/redux/auth'
import { useGetProgrammesQuery } from '@/redux/populations'
import { useGetSemestersQuery } from '@/redux/semesters'
import { reformatDate } from '@/util/timeAndDate'
import { createMaps } from './columnHelpers/createMaps'
Expand All @@ -37,6 +38,7 @@ export const GeneralTab = ({
return obj
}, {})
const { isAdmin } = useGetAuthorizedUserQuery()
const { data: programmes = {} } = useGetProgrammesQuery()

const fromSemester = from
? Object.values(semesterData.semesters)
Expand All @@ -60,7 +62,7 @@ export const GeneralTab = ({

const currentSemester = useCurrentSemester()

if (!populationStatistics || !populationStatistics.elementdetails) return null
if (!populationStatistics) return null

const studyGuidanceGroupProgrammes =
group?.tags?.studyProgramme && group?.tags?.studyProgramme.includes('+')
Expand Down Expand Up @@ -120,7 +122,7 @@ export const GeneralTab = ({
currentSemester: currentSemester?.semestercode,
})

const transferFrom = student => getTextIn(populationStatistics.elementdetails.data[student.transferSource].name)
const transferFrom = student => getTextIn(programmes[student.transferSource]?.name) ?? student.transferSource

const getCorrectStudyRight = studyRights =>
studyRights?.find(studyRight =>
Expand Down

0 comments on commit d35e303

Please sign in to comment.