Last active
May 19, 2020 22:25
-
-
Save gcmatheusj/f00658f4c02d7aa96fbf109d35ffdf35 to your computer and use it in GitHub Desktop.
This file contains 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 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