Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Created May 21, 2021 10:53
Show Gist options
  • Select an option

  • Save blacksheep557/e76fc2a2bfc27148f519504aebe040c8 to your computer and use it in GitHub Desktop.

Select an option

Save blacksheep557/e76fc2a2bfc27148f519504aebe040c8 to your computer and use it in GitHub Desktop.
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