Created
May 21, 2021 10:53
-
-
Save blacksheep557/e76fc2a2bfc27148f519504aebe040c8 to your computer and use it in GitHub Desktop.
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
| async function enrollStudents(course, courseId, studentIds) { | |
| if (!isPublicGroup(course)) { // private courses should work fine with default enrolment by omission (since student will never join its own course half-way through) | |
| return; | |
| } | |
| await Promise.all(studentIds.map(async stId => { | |
| const existingPurchasesForStudent = await dao.find({query: {courseId: courseId, studentIds: stId}}); | |
| const existingEnrolments = await enrolmentsDao.find({query: {userId: stId, courseId: `${course._id}`}}); | |
| if (existingPurchasesForStudent.length === 0 || ensureTheStudentIsEnrolled(existingEnrolments)) { | |
| await enrolmentsDao.create({ | |
| creationTime: Date.now(), | |
| courseId: `${course._id}`, | |
| userId: stId, | |
| dates: [new Date()] | |
| }, {}); | |
| } | |
| })); | |
| } | |
| function ensureTheStudentIsEnrolled(existingEnrolments) { | |
| return (existingEnrolments.length && existingEnrolments.length % 2 === 0) && (moment(existingEnrolments.pop().creationTime) < moment()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment