Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Created May 21, 2021 12:43
Show Gist options
  • Select an option

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

Select an option

Save blacksheep557/885c63539c99ee66474d2bc5fc78a33e 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 (isStudentReEnrolling(existingEnrolments)) {
const enrolment = existingEnrolments[0];
enrolment.dates.push(moment())
await enrolmentsDao.update(enrolment._id, enrolment, {});
} else {
if (existingPurchasesForStudent.length === 0)
await enrolmentsDao.create({
creationTime: Date.now(),
courseId: `${course._id}`,
userId: stId,
dates: [new Date()]
}, {});
}
}));
}
function isStudentReEnrolling(existingEnrolments) {
if (!existingEnrolments.length) return false;
const dates = existingEnrolments[0].dates;
const lastDate = dates[dates.length - 1];
return (dates.length % 2 === 0) && (moment(lastDate) < moment())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment