Skip to content

Commit

Permalink
fix groups migration
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Oct 20, 2023
1 parent 42ccea6 commit 9ef2b56
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
13 changes: 9 additions & 4 deletions api/migrate-mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,18 @@ async function importData() {
e.years = element.years;
e.enabled = element.enabled;
e.enable_sharing = element.enable_sharing;
e.groups = {}
const my_groups = {}
groups
.filter(g => g.degree_id===element.id)
.map(g => {
e.groups[g.name] = exams_groups
.forEach(g => {
const exam_ids = exams_groups
.filter(eg => eg.group_id===g.id)
.map(eg => exams[eg.exam_id]._id)});
.map(eg => new ObjectId(exams[eg.exam_id]._id))
my_groups[g.name] = exam_ids
})

e.groups = my_groups

if (element.default_group_id) {
e.default_group = group_by_id(element.default_group_id).name;
} else {
Expand Down
24 changes: 19 additions & 5 deletions frontend/src/modules/engine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,33 +341,47 @@ export type ProposalGet = {
attachments: ProposalAttachmentGet[],
}

export type ProposalExamGet = {
export type ProposalExamGet =
ProposalCompulsoryExamGet |
ProposalCompulsoryGroupGet |
ProposalFreeChoiceGet


export type ProposalCompulsoryExamGet = {
__t: "CompulsoryExam",
exam_id: string,
exam_name: string,
exam_code: string,
exam_credits: number,
}|{
}

export type PoposalCompulsoryGroupGet = {
__t: "CompulsoryGroup",
group: string,
exam_id: string,
exam_name: string,
exam_code: string,
exam_credits: number,
}|{
}

export type ProposalFreeChoiceGroupGet = {
__t: "FreeChoiceGroup",
group: string,
exam_id: string,
exam_name: string,
exam_code: string,
exam_credits: number,
}|{
}

export type ProposalFreeChoiceGet = {
__t: "FreeChoiceExam",
exam_id: string,
exam_name: string,
exam_code: string,
exam_credits: number,
}|{
}

export type ProposalExternalExamGet = {
__t: "ExternalExam",
exam_name: string,
exam_credits: number,
Expand Down
42 changes: 33 additions & 9 deletions frontend/src/pages/ProposalPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { Dispatch, SetStateAction, useState } from 'react'
import { useParams } from "react-router-dom"
import {Button} from 'react-bootstrap'
import assert from 'assert'
Expand All @@ -7,7 +7,7 @@ import { useEngine, useGet,
useIndexExam, useGetProposal,
useIndexDegree, useIndexCurriculum,
useGetDegree, useGetCurriculum, useGetExam,
ExamGet, ProposalGet, ProposalExamGet } from '../modules/engine'
ExamGet, ProposalGet, ProposalExamGet, CurriculumExamGet } from '../modules/engine'
import LoadingMessage from '../components/LoadingMessage'
import Card from '../components/Card'
import {formatDate,displayAcademicYears} from '../modules/utils'
Expand Down Expand Up @@ -138,8 +138,15 @@ function ProposalForm({ proposal }:{

console.log(chosenExams)


function ExamSelect({ exam, allExams, groups, yearNumber, examNumber, chosenExams, setChosenExams }) {
function ExamSelect({ exam, allExams, groups, yearNumber, examNumber, chosenExams, setChosenExams }:{
exam: CurriculumExamGet,
allExams: {[key: string]: ExamGet},
groups: {[key: string]: ExamGet[]},
yearNumber: number,
examNumber: number,
chosenExams: (ProposalExamGet|null)[][],
setChosenExams: Dispatch<SetStateAction<(ProposalExamGet|null)[][]>>,
}) {
if (exam.__t === "CompulsoryExam") {
const compulsoryExam = allExams[exam.exam_id]
return <li className='form-group exam-input'>
Expand All @@ -156,23 +163,40 @@ function ProposalForm({ proposal }:{
</li>
} else if (exam.__t === "CompulsoryGroup") {
const options = groups[exam.group]

function isCompulsoryGroupExam(exm: ProposalExamGet|null): asserts exm is ProposalExamGet {
assert(exm !== null && exm.__t === "CompulsoryGroup")
}

const exm = chosenExams[yearNumber][examNumber]

assert (!exm || exm.__t === "CompulsoryGroup")
console.log(exm?.exam_id)
return <li className='form-group exam-input'>
<div className='row'>
<div className='col-9'>
<select className='form-control'
value={chosenExams[yearNumber][examNumber] || -1}
value={exm ? exm.exam_name : -1}
onChange={e => {
let newChosenExams = chosenExams
newChosenExams[yearNumber][examNumber] = e.target.value
const newChosenExams = chosenExams
const newExam = allExams[e.target.value]
newChosenExams[yearNumber][examNumber] = {
__t: "CompulsoryGroup",
exam_id: newExam._id,
exam_name: newExam.name,
exam_code: newExam.code,
exam_credits: newExam.credits,
group: exam.group,
}
setChosenExams(newChosenExams)
}}
>
<option disabled value={-1}>Un esame a scelta nel gruppo {exam.group}</option>
{options.map(opt => <option key={opt._id} value={opt._id}>{opt.name}</option>)}
{options && options.map(opt => <option key={opt._id} value={opt._id}>{opt.name}</option>)}
</select>
</div>
<div className="col-3">
<input className='form-control col' readOnly value={chosenExams[yearNumber][examNumber] ? allExams[chosenExams[yearNumber][examNumber]].credits : ""}/>
<input className='form-control col' readOnly value={exm ? allExams[exm.exam_id].credits : ""}/>
</div>
</div>
</li>
Expand Down
1 change: 0 additions & 1 deletion frontend/src/pages/SinglePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ function SinglePageInternal () {
<Route path="/" element={<Splash/>}/>
<Route path="/index.html" element={<Splash/>}/>
<Route path="/proposals/edit/:id" element={<EditProposalPage/>}/>
<Route path="/proposals/edit" element={<NewProposalPage/>}/>
<Route path="/proposals/:id" element={<ProposalPage/>}/>
<Route path="/proposals" element={<ProposalsPage/>}/>
{/* <Route path="/proposals/new" element={<ProposalPage/>}/> */}
Expand Down

0 comments on commit 9ef2b56

Please sign in to comment.