Skip to content

Commit

Permalink
Suddivisi gli esami del modello Proposal in anni
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran314 committed Oct 9, 2023
1 parent 0c33117 commit 445813f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 69 deletions.
108 changes: 56 additions & 52 deletions api/controllers/ProposalsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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)
}
}
}

Expand Down
22 changes: 15 additions & 7 deletions api/migrate-mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,21 @@ 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 = {
exam_id: exam._id,
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) {
Expand All @@ -298,27 +302,31 @@ 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
}))
}
})

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,
}))
})

Expand Down
5 changes: 4 additions & 1 deletion api/models/ProposalSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 6 additions & 9 deletions frontend/src/pages/ProposalPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -466,8 +465,10 @@ export default function ProposalPage() {
</>
}

function Year({year}) {
return <Card title={`${Curriculum.ordinal(year)} anno`}>
function Year({number, exams}) {
const yearName = ["Primo", "Secondo", "Terzo"][number] || `#${number}`

return <Card title={`${yearName} anno`}>
<table className="table">
<thead>
<tr>
Expand All @@ -479,10 +480,7 @@ export default function ProposalPage() {
</tr>
</thead>
<tbody>
{ proposal.exams
.filter(e => e.year === year)
.map(e => <ExamRow key={e._id} exam={e}/>)
}
{ exams .map(e => <ExamRow key={e._id} exam={e}/>) }
</tbody>
</table>
</Card>
Expand Down Expand Up @@ -538,8 +536,7 @@ export default function ProposalPage() {
<h1>Piano di studi di {proposal.user_name}</h1>
<MessageCard />
<InfoCard />
{ degree && [...Array(degree.years).keys()].map(year => <Year key={year} year={year+1}/>)}
{ JSON.stringify(proposal)}
{ proposal.exams.map((year_exams, index) => <Year key={index} number={index} exams={year_exams}/>)}
</>
}

0 comments on commit 445813f

Please sign in to comment.