Skip to content

Instantly share code, notes, and snippets.

@gcmatheusj
Last active May 19, 2020 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcmatheusj/f00658f4c02d7aa96fbf109d35ffdf35 to your computer and use it in GitHub Desktop.
Save gcmatheusj/f00658f4c02d7aa96fbf109d35ffdf35 to your computer and use it in GitHub Desktop.
async findOneByEnrollment({ accountId, userId, courseId, courseVersionId, lessonId }) {
const [enrolment] = await this.knex('enrollments')
.where({
user_id: userId
})
.andWhere({
account_id: accountId
})
.andWhere({
course_id: courseId
})
.andWhere({
course_version_id: courseVersionId
});
if (!enrolment) {
throw new Error('No enrollment found');
}
if ((new Date(enrolment.start_date) > new Date())) {
throw new Error('Enrollment not started');
}
if (enrolment.end_date && (new Date() > new Date(enrolment.end_date))) {
throw new Error('Enrollment expired');
}
const [lesson] = await this.knex('course_lessons')
.andWhere({
account_id: accountId
})
.andWhere({
course_id: courseId
})
.andWhere({
course_version_id: courseVersionId
})
.andWhere({
id: lessonId
});
if (lesson.status === 'draft') {
throw new Error('Lesson not available');
}
return lesson;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment