From a69e35e43b40ca177bf77a6b8fb5e76f29df5dce Mon Sep 17 00:00:00 2001 From: Ranindya Paramitha <23520019@std.stei.itb.ac.id> Date: Sun, 18 Apr 2021 22:35:55 +0000 Subject: [PATCH] #123 Fix get credits --- backend/docs/Student.md | 5 ++++- backend/src/controllers/student.js | 4 ++-- backend/src/test/student.controller.test.js | 2 +- backend/src/util/db/student.js | 22 ++++++++++++++++----- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/backend/docs/Student.md b/backend/docs/Student.md index cf8fd0c0..6ea4fb8b 100644 --- a/backend/docs/Student.md +++ b/backend/docs/Student.md @@ -151,7 +151,10 @@ "ip": 3.5, "ipk": 3.5 }, - "creditsTotal": 7 + "credits": { + "passed": 3, + "scoreT": 2 + } } ``` diff --git a/backend/src/controllers/student.js b/backend/src/controllers/student.js index d1aef7b2..91ee295e 100644 --- a/backend/src/controllers/student.js +++ b/backend/src/controllers/student.js @@ -11,7 +11,7 @@ const { const { getStudent, - getCreditsTotal, + getCredits, getHistoricalTranscript, getStudentIP, getStudentIPK, @@ -40,7 +40,7 @@ exports.getStudentData = async (req, res) => { } if (req.includeCredits.toLowerCase() === 'true') { - student.dataValues.creditsTotal = await getCreditsTotal(student); + student.dataValues.credits = await getCredits(student); } res.json(student); diff --git a/backend/src/test/student.controller.test.js b/backend/src/test/student.controller.test.js index 7cf5d920..f2de7973 100644 --- a/backend/src/test/student.controller.test.js +++ b/backend/src/test/student.controller.test.js @@ -234,7 +234,7 @@ describe('Student Test', () => { res.body.should.have.property('nim'); res.body.should.have.property('nik'); res.body.should.have.property('phone'); - res.body.should.have.property('creditsTotal').to.not.eql(null); + res.body.should.have.property('credits').to.not.eql(null); res.body.should.have.property('advisorId'); res.body.should.have.property('createdAt'); res.body.should.have.property('updatedAt'); diff --git a/backend/src/util/db/student.js b/backend/src/util/db/student.js index c6ed6158..55f8a973 100644 --- a/backend/src/util/db/student.js +++ b/backend/src/util/db/student.js @@ -3,6 +3,7 @@ const { StudyPlanCourse, Student, CourseClass, + Course, } = require('../../models/index'); const { ScoreEnum, ScoreStatusEnum } = require('../../enums/index'); @@ -106,8 +107,9 @@ const getStudentNR = async (student) => { return nr; }; -const getCreditsTotal = async (student) => { - let total = 0; +const getCredits = async (student) => { + let passed = 0; + let scoreT = 0; let studyPlans = student.studyPlans; if (!studyPlans) { @@ -115,10 +117,17 @@ const getCreditsTotal = async (student) => { } for (const studyPlan of studyPlans) { - total += studyPlan.creditsTotal; + for (const studyPlanCourse of studyPlan.studyPlanCourses){ + if (ScoreEnum[studyPlanCourse.score].value >= ScoreEnum.C.value && studyPlanCourse.scoreStatus === ScoreStatusEnum.FINAL){ + passed += studyPlanCourse.CourseClass.Course.credits; + } + if (ScoreEnum[studyPlanCourse.score].name === ScoreEnum.T.name || studyPlanCourse.scoreStatus !== ScoreStatusEnum.FINAL){ + scoreT += studyPlanCourse.CourseClass.Course.credits; + } + } } - return total; + return {passed, scoreT}; }; const getStudent = async (opts) => { @@ -142,6 +151,9 @@ const getHistoricalTranscript = async (studentId) => { { model: CourseClass, attributes: ['courseId'], + include: [{ + model: Course + }] }, ], }, @@ -157,7 +169,7 @@ module.exports = { getStudentIPK, getStudentIP, getStudentNR, - getCreditsTotal, + getCredits, getStudent, getHistoricalTranscript, }; -- GitLab