Skip to content

Instantly share code, notes, and snippets.

@edtsech

edtsech/xxx.ts

Last active Jul 5, 2019
Embed
What would you like to do?
@Field({
type: () => TopicEnrolmentModel,
description: `creates a new enrolment if retries are allowed`
})
async reEnrol(
@Context ctx: UserSessionModel
) {
const tv = await this.$relatedQuery('currentTopicVersion')
if (tv.allow_retry) {
const enrolment = await this.createEnrolment(ctx.user, tv)
return enrolment
} else {
return null
}
}
async selfEnrol(user: UserModel) {
const tv = await this.$relatedQuery('currentTopicVersion')
if (!tv) {
throw new InvalidOperation(`topic ${this.id} is not published`) // no user can self enrol to an unpublished topic
}
const enrolment = await this.createEnrolment(user, tv)
return enrolment
}
private async createEnrolment(user: UserModel, topicVersion: TopicVersionModel) {
const enrolment = await this.$relatedQuery<TopicEnrolmentModel>(
'enrolments'
).insertAndFetch({
topic_version_id: this.production_topic_version_id,
user_id: user.id,
topic_id: this.id,
start_date: new Date(),
shared_by: null,
available_points: topicVersion.number_quizzes,
pass_mark: topicVersion.pass_mark,
is_assigned: false
})
return enrolment
}
@edtsech

This comment has been minimized.

Copy link
Owner Author

@edtsech edtsech commented Jul 5, 2019

TopicModel or TopicModelAsStudent ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment