11import { SISStudyRight } from '@oodikone/shared/models'
2- import { FormattedStudent , CriteriaYear , Name , ProgressCriteria } from '@oodikone/shared/types'
2+ import { FormattedStudent , Name } from '@oodikone/shared/types'
33import { StudentData , StudentTags } from '@oodikone/shared/types/studentData'
4- import { dateYearsFromNow , dateDaysFromNow , dayInMilliseconds } from '@oodikone/shared/util/datetime'
5- import { CreditModel } from '../../models'
4+ import { dateDaysFromNow , dayInMilliseconds } from '@oodikone/shared/util/datetime'
65import { hasTransferredFromOrToProgramme } from '../studyProgramme/studyProgrammeHelpers'
7- import type { StudentStudyPlan , StudentStudyRight } from './getStudentData'
6+ import type { StudentStudyRight } from './getStudentData'
87import { getCurriculumVersion } from './shared'
9- import type { AnonymousCredit , AnonymousEnrollment } from './statisticsOf'
10-
11- const yearMap : [ string , keyof ProgressCriteria [ 'courses' ] ] [ ] = [
12- [ 'year1' , 'yearOne' ] ,
13- [ 'year2' , 'yearTwo' ] ,
14- [ 'year3' , 'yearThree' ] ,
15- [ 'year4' , 'yearFour' ] ,
16- [ 'year5' , 'yearFive' ] ,
17- [ 'year6' , 'yearSix' ] ,
18- ]
198
209const getTransferSource = ( code : string , studyRights : StudentStudyRight [ ] ) : [ boolean , string | undefined ] => {
2110 if ( code ) {
@@ -48,138 +37,18 @@ const getTransferSource = (code: string, studyRights: StudentStudyRight[]): [boo
4837 return [ false , undefined ]
4938}
5039
51- const getCriteriaBase = ( criteria : ProgressCriteria ) : [ boolean , Record < string , CriteriaYear > ] => {
52- const thereAreCriteriaCourses = ! ! Object . values ( criteria . courses ) . flatMap ( val => val ) . length
53- const thereAreCriteriaCredits = ! ! Object . values ( criteria . credits ) . reduce ( ( acc , cur ) => acc + cur , 0 )
54-
55- const createEmptyCriteriaYear = ( criteria : ProgressCriteria , year : keyof ProgressCriteria [ 'courses' ] ) => ( {
56- credits : false ,
57- totalSatisfied : 0 ,
58- coursesSatisfied : Object . fromEntries ( criteria . courses [ year ] . map ( course => [ course , null ] ) ) ,
59- } )
60-
61- const criteriaChecked : Record < string , CriteriaYear > = {
62- year1 : createEmptyCriteriaYear ( criteria , 'yearOne' ) ,
63- year2 : createEmptyCriteriaYear ( criteria , 'yearTwo' ) ,
64- year3 : createEmptyCriteriaYear ( criteria , 'yearThree' ) ,
65- year4 : createEmptyCriteriaYear ( criteria , 'yearFour' ) ,
66- year5 : createEmptyCriteriaYear ( criteria , 'yearFive' ) ,
67- year6 : createEmptyCriteriaYear ( criteria , 'yearSix' ) ,
68- }
69-
70- return [ thereAreCriteriaCourses || thereAreCriteriaCredits , criteriaChecked ]
71- }
72-
73- const getProgressCriteria = (
74- startDate : string ,
75- criteria : ProgressCriteria ,
76- credits : AnonymousCredit [ ] ,
77- hops : StudentStudyPlan | undefined
78- ) => {
79- const [ thereAreCriteria , criteriaChecked ] = getCriteriaBase ( criteria )
80- if ( ! thereAreCriteria ) return criteriaChecked
81-
82- const startDateFromISO = new Date ( startDate )
83-
84- const criteriaCoursesBySubstitutionMap = new Map < string , string > ( )
85- for ( const [ courseCode , substitutionCodes ] of Object . entries ( criteria . allCourses ) ) {
86- criteriaCoursesBySubstitutionMap . set ( courseCode , courseCode )
87-
88- for ( const substitutionCode of substitutionCodes ) {
89- criteriaCoursesBySubstitutionMap . set ( substitutionCode , courseCode )
90- }
91- }
92-
93- const academicYears = { year1 : 0 , year2 : 0 , year3 : 0 , year4 : 0 , year5 : 0 , year6 : 0 }
94-
95- const courses = credits . map ( ( { attainment_date, course_code, credits, credittypecode } ) => ( {
96- course_code,
97- credits,
98- credittypecode,
99- date :
100- attainment_date < startDateFromISO
101- ? dateDaysFromNow ( startDateFromISO , 1 ) . toISOString ( )
102- : attainment_date . toISOString ( ) ,
103- } ) )
104-
105- courses
106- . filter ( ( { course_code } ) => ! ! criteriaCoursesBySubstitutionMap . get ( course_code ) )
107- . filter ( ( { credittypecode } ) => CreditModel . passed ( { credittypecode } ) || CreditModel . improved ( { credittypecode } ) )
108- . forEach ( course => {
109- const courseDate = new Date ( course . date )
110- const correctCode = criteriaCoursesBySubstitutionMap . get ( course . course_code ) !
111-
112- yearMap . forEach ( ( [ yearToAdd , criteriaYear ] ) => {
113- if ( criteria . courses [ criteriaYear ] . includes ( correctCode ) ) {
114- const currentDate = criteriaChecked [ yearToAdd ] . coursesSatisfied [ correctCode ]
115- if ( ! currentDate || courseDate < new Date ( currentDate ) ) {
116- criteriaChecked [ yearToAdd ] . coursesSatisfied [ correctCode ] = course . date
117- }
118- }
119- } )
120- } )
121-
122- courses . forEach ( course => {
123- const courseDate = new Date ( course . date )
124- const correctCode = criteriaCoursesBySubstitutionMap . get ( course . course_code ) !
125-
126- if (
127- startDateFromISO < courseDate &&
128- ! ! hops &&
129- ( hops . included_courses . includes ( course . course_code ) || hops . included_courses . includes ( correctCode ) )
130- )
131- Object . keys ( academicYears )
132- . filter ( ( _ , index ) => courseDate < dateYearsFromNow ( startDateFromISO , index + 1 ) )
133- . forEach ( year => ( academicYears [ year ] += course . credits ) )
134- } )
135-
136- yearMap . forEach ( ( [ yearToAdd , criteriaYear ] ) => {
137- criteriaChecked [ yearToAdd ] . totalSatisfied +=
138- Object . values ( criteriaChecked [ yearToAdd ] . coursesSatisfied ) . filter ( course => ! ! course ) . length ?? 0
139- // UPDATE CREDIT CRITERIA
140- if ( ! ! criteria . credits [ criteriaYear ] && criteria . credits [ criteriaYear ] < academicYears [ yearToAdd ] ) {
141- criteriaChecked [ yearToAdd ] . credits = true
142- criteriaChecked [ yearToAdd ] . totalSatisfied += 1
143- }
144- } )
145-
146- return criteriaChecked
147- }
148-
14940export const formatStudentForAPI = (
15041 code : string ,
15142 startDate : string ,
15243 student : StudentData ,
15344 tags : StudentTags [ ] ,
154- credits : AnonymousCredit [ ] ,
155- enrollments : AnonymousEnrollment [ ] ,
156- optionData : Name | undefined ,
157- criteria : ProgressCriteria
158- ) : FormattedStudent => {
45+ optionData : Name | undefined
46+ ) : Omit < FormattedStudent , 'criteriaProgress' | 'courses' | 'enrollments' > => {
15947 const { studentnumber, studyRights, studyplans } = student
16048
16149 const hops = studyplans . find ( plan => plan . programme_code === code )
16250 const [ transferredStudyright , transferSource ] = getTransferSource ( code , studyRights )
16351
164- const courses = credits . map ( credit => {
165- const attainmentDateNormalized = credit . attainment_date . toISOString ( )
166- const passed =
167- CreditModel . passed ( { credittypecode : credit . credittypecode } ) ||
168- CreditModel . improved ( { credittypecode : credit . credittypecode } )
169-
170- return {
171- course_code : credit . course_code ,
172- date : attainmentDateNormalized ,
173- passed,
174- grade : passed ? credit . grade : 'Hyl.' ,
175- credits : credit . credits ,
176- isStudyModuleCredit : credit . isStudyModule ,
177- credittypecode : credit . credittypecode ,
178- language : credit . language ,
179- studyright_id : credit . studyright_id ,
180- }
181- } )
182-
18352 return {
18453 firstnames : student . firstnames ,
18554 lastname : student . lastname ,
@@ -199,15 +68,12 @@ export const formatStudentForAPI = (
19968 birthdate : student . birthdate ,
20069 sis_person_id : student . sis_person_id ,
20170 citizenships : student . citizenships ,
202- criteriaProgress : getProgressCriteria ( startDate , criteria , credits , hops ) ,
20371 curriculumVersion : getCurriculumVersion ( hops ?. curriculum_period_id ) ,
20472
20573 tags,
20674 transferredStudyright,
20775 transferSource,
20876 studyRights,
20977 studyplans,
210- courses,
211- enrollments,
21278 }
21379}
0 commit comments