From 445813f39441deb7494170170e281f9d015706ab Mon Sep 17 00:00:00 2001 From: Francesco Baldino Date: Mon, 9 Oct 2023 11:26:13 +0200 Subject: [PATCH] Suddivisi gli esami del modello Proposal in anni --- api/controllers/ProposalsController.js | 108 +++++++++++++------------ api/migrate-mysql.js | 22 +++-- api/models/ProposalSchema.js | 5 +- frontend/src/pages/ProposalPage.js | 15 ++-- 4 files changed, 81 insertions(+), 69 deletions(-) diff --git a/api/controllers/ProposalsController.js b/api/controllers/ProposalsController.js index d3afdef9..e3d03beb 100644 --- a/api/controllers/ProposalsController.js +++ b/api/controllers/ProposalsController.js @@ -130,51 +130,53 @@ const ProposalsController = { body.attachments = [] const credits = 0 - for (const e of body.exams) { - switch (e.__t) { - case 'CompulsoryExam': - if (curriculum.years[e.year-1].exams.filter(e => e.__t == 'CompulsoryExam' && e.exam_id == e.exam_id).length === 0) { - throw new BadRequestError(`Exam ${e.exam_id} not found in curriculum compulsory exams of year ${e.year}`) - } - break - case 'CompulsoryGroup': - if (curriculum.years[e.year-1].exams.filter(e => e.__t == 'CompulsoryGroup' && e.group == e.group).length === 0) { - throw new BadRequestError(`Group ${e.group} not found in curriculum compulsory exams of year ${e.year}`) - } - break - case 'FreeChoiceGroup': - if (curriculum.years[e.year-1].exams.filter(e => e.__t == 'FreeChoiceGroup' && e.group == e.group).length === 0) { - throw new BadRequestError(`Group ${e.group} not found in curriculum free choice exams of year ${e.year}`) - } - break - case 'FreeChoiceExam': - if (curriculum.years[e.year-1].exams.filter(e => e.__t == 'FreeChoiceExam').length === 0) { - throw new BadRequestError(`No free choice exams for year ${e.year}`) - } - break - case 'ExternalExam': - break - default: - throw new BadRequestError(`Invalid exam type ${e.__t}`) - } - if (e.exam_id) { - e.exam_id = new ObjectId(e.exam_id) - const exam = await Exam.findOne({_id: e.exam_id}) - e.exam_name = exam.name - e.exam_code = exam.code - e.exam_credits = exam.credits - } - if (e.year) { - e.year = parseInt(e.year) - if (isNaN(e.year)) throw new BadRequestError("Invalid year") - if (e.year < 1 || e.year > degree.years) throw new BadRequestError("Invalid year") - } - if (e.group) { - const lst = degree.groups[e.group] - if (!lst) throw new BadRequestError(`Invalid group ${e.group}`) - if (!lst.includes(e.exam_id)) throw new BadRequestError(`Invalid group ${e.group} for exam ${e.exam_id}`) + for (const [year, year_exams] of body.exams.entries()) { + for (const e of year_exams) { + switch (e.__t) { + case 'CompulsoryExam': + if (curriculum.years[year].exams.filter(f => f.__t == 'CompulsoryExam' && e.exam_id == f.exam_id).length === 0) { + throw new BadRequestError(`Exam ${e.exam_id} not found in curriculum compulsory exams of year ${year}`) + } + break + case 'CompulsoryGroup': + if (curriculum.years[year].exams.filter(f => f.__t == 'CompulsoryGroup' && e.group == f.group).length === 0) { + throw new BadRequestError(`Group ${e.group} not found in curriculum compulsory exams of year ${year}`) + } + break + case 'FreeChoiceGroup': + if (curriculum.years[year].exams.filter(f => f.__t == 'FreeChoiceGroup' && e.group == f.group).length === 0) { + throw new BadRequestError(`Group ${e.group} not found in curriculum free choice exams of year ${year}`) + } + break + case 'FreeChoiceExam': + if (curriculum.years[year].exams.filter(f => f.__t == 'FreeChoiceExam').length === 0) { + throw new BadRequestError(`No free choice exams for year ${year}`) + } + break + case 'ExternalExam': + break + default: + throw new BadRequestError(`Invalid exam type ${e.__t}`) + } + if (e.exam_id) { + e.exam_id = new ObjectId(e.exam_id) + const exam = await Exam.findOne({_id: e.exam_id}) + e.exam_name = exam.name + e.exam_code = exam.code + e.exam_credits = exam.credits + } + // if (e.year) { + // e.year = parseInt(e.year) + // if (isNaN(e.year)) throw new BadRequestError("Invalid year") + // if (e.year < 1 || e.year > degree.years) throw new BadRequestError("Invalid year") + // } + if (e.group) { + const lst = degree.groups[e.group] + if (!lst) throw new BadRequestError(`Invalid group ${e.group}`) + if (!lst.includes(e.exam_id)) throw new BadRequestError(`Invalid group ${e.group} for exam ${e.exam_id}`) + } + if (e.exam_credits) credits += e.exam_credits } - if (e.exam_credits) credits += e.exam_credits } if (state === 'submitted') { @@ -188,14 +190,16 @@ const ProposalsController = { let exam_names = [] // gli esami inseriti sono tutti diversi? - for (const e of body.exams) { - if (e.exam_id) { - if (exam_ids.includes(e.exam_id)) throw new BadRequestError(`Exam ${e.exam_id} is present more than once`) - exam_ids.push(e.exam_id) - } - if (e.exam_name) { - if (exam_names.includes(e.exam_name)) throw new BadRequestError(`Exam ${e.exam_name} is present more than once`) - exam_names.push(e.exam_name) + for (const year_exams of body.exams) { + for (const e of year_exams) { + if (e.exam_id) { + if (exam_ids.includes(e.exam_id)) throw new BadRequestError(`Exam ${e.exam_id} is present more than once`) + exam_ids.push(e.exam_id) + } + if (e.exam_name) { + if (exam_names.includes(e.exam_name)) throw new BadRequestError(`Exam ${e.exam_name} is present more than once`) + exam_names.push(e.exam_name) + } } } diff --git a/api/migrate-mysql.js b/api/migrate-mysql.js index 0d2daf41..af6581b8 100644 --- a/api/migrate-mysql.js +++ b/api/migrate-mysql.js @@ -270,6 +270,7 @@ async function importData() { chosen_exams.filter(e => (e.proposal_id === element.old_id)) .forEach(e => { + const year = e.chosen_year - 1 exam = exams[e.exam_id] console.assert(e.exam_id === exam.old_id) data = { @@ -277,10 +278,13 @@ async function importData() { exam_name: exam.name, exam_code: exam.code, exam_credits: exam.credits, - year: e.chosen_year, + // year: e.chosen_year, + } + if (element.exams[year] === undefined) { + element.exams[year] = [] } if (e.compulsory_exam_id) { - element.exams.push(new ProposalCompulsoryExam({ + element.exams[year].push(new ProposalCompulsoryExam({ ...data })) } else if (e.compulsory_group_id) { @@ -298,16 +302,16 @@ async function importData() { console.log(`${e.curriculum_id} !== ${element.curriculum_id.old_id}`) process.abort() } - element.exams.push(new ProposalCompulsoryGroup({ + element.exams[year].push(new ProposalCompulsoryGroup({ ...data, group: group_by_id(group.group_id).name })) } else if (e.free_choice_exam_id) { - element.exams.push(new ProposalFreeChoiceExam({ + element.exams[year].push(new ProposalFreeChoiceExam({ ...data })) } else { - element.exams.push(new ProposalFreeChoiceExam({ + element.exams[year].push(new ProposalFreeChoiceExam({ ...data })) } @@ -315,10 +319,14 @@ async function importData() { chosen_free_choice_exams.filter(e => (e.proposal_id === element.old_id)) .forEach(e => { - element.exams.push(new ProposalExternalExam({ + const year = e.chosen_year - 1 + if (element.exams[year] === undefined) { + element.exams[year] = [] + } + element.exams[e.chosen_year - 1].push(new ProposalExternalExam({ exam_name: e.name, exam_credits: e.credits, - year: e.chosen_year, + // year: e.chosen_year, })) }) diff --git a/api/models/ProposalSchema.js b/api/models/ProposalSchema.js index bb749264..efef5055 100644 --- a/api/models/ProposalSchema.js +++ b/api/models/ProposalSchema.js @@ -60,7 +60,10 @@ ProposalSchema = new mongoose.Schema({ type: Date, }, exams: [ - ProposalExamSchema + // Il primo array itera sugli anni. Il secondo array itera sugli esami di quell'anno + [ + ProposalExamSchema + ] ], attachments: [ ProposalAttachmentSchema diff --git a/frontend/src/pages/ProposalPage.js b/frontend/src/pages/ProposalPage.js index 94451e0d..8b7492ea 100644 --- a/frontend/src/pages/ProposalPage.js +++ b/frontend/src/pages/ProposalPage.js @@ -376,7 +376,6 @@ export default function ProposalPage() { const query = useGet(Proposal, id || null) const proposal = id ? ( query.isSuccess ? new Proposal(query.data) : null ) : empty const curriculumQuery = useGet(Curriculum, (proposal && proposal.curriculum_id) ? proposal.curriculum_id : null) - console.log('ehi', proposal, curriculumQuery) const curriculum = curriculumQuery.isSuccess ? new Curriculum(curriculumQuery.data) : null const degreeQuery = useGet(Degree, curriculum ? curriculum.degree_id : null) const degree = degreeQuery.isSuccess ? new Degree(degreeQuery.data) : null @@ -466,8 +465,10 @@ export default function ProposalPage() { } - function Year({year}) { - return + function Year({number, exams}) { + const yearName = ["Primo", "Secondo", "Terzo"][number] || `#${number}` + + return @@ -479,10 +480,7 @@ export default function ProposalPage() { - { proposal.exams - .filter(e => e.year === year) - .map(e => ) - } + { exams .map(e => ) }
@@ -538,8 +536,7 @@ export default function ProposalPage() {

Piano di studi di {proposal.user_name}

- { degree && [...Array(degree.years).keys()].map(year => )} - { JSON.stringify(proposal)} + { proposal.exams.map((year_exams, index) => )} }