Created
July 12, 2020 11:01
-
-
Save atakangah/1705f45a4a2d83c496bd239688af4e8d to your computer and use it in GitHub Desktop.
<<Continuation>>An express routing js file to take a student's Id and a course Id from a request body to the api server and remove the associated course with the specified course Id from the student's courses list<<Continuation>>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
let student = await Students.find({ 'studentId': studentId }).exec(); | |
/* | |
* After finding the student with the provided studentId, | |
* Use O(n) algorithm to find the course to complete for student | |
* It is unlikely for any student to have more than 5 courses in its course list at aytime | |
* Therefore, O(n) algorithm is ok | |
*/ | |
if ( student.length > 0 ) { | |
let COCOMPLETEFLAGGED = false; | |
student[0].courses = student[0].courses.filter(course => { | |
if (course._id == courseId) { | |
COCOMPLETEFLAGGED = course.balanceUnpaid > 0 ? true : false; | |
console.log(COCOMPLETEFLAGGED); | |
} | |
return course._id != courseId | |
}); | |
if (COCOMPLETEFLAGGED == true) { | |
return res.status(Errors.STUDENT.COCOMPLETE_DISALLOWED.CODE).send({ | |
detail: `Course complete disallowed. ${student[0].firstName} still has pending balances unpaid` | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment