@@ -17,13 +17,13 @@ import {
1717} from "./helpers" ;
1818import constants from "../../config/constants" ;
1919import GQLContext from "../../models/GQLContext" ;
20- import { Course } from "../../models/Course" ;
2120import { deleteMedia } from "../../services/medialit" ;
2221import { recordProgress } from "../users/logic" ;
23- import { Progress , Quiz } from "@courselit/common-models" ;
22+ import { Constants , Progress , Quiz } from "@courselit/common-models" ;
2423import LessonEvaluation from "../../models/LessonEvaluation" ;
2524import { checkPermission } from "@courselit/utils" ;
2625import { recordActivity } from "../../lib/record-activity" ;
26+ import { InternalCourse } from "@courselit/common-logic" ;
2727
2828const { permissions, quiz } = constants ;
2929
@@ -130,7 +130,7 @@ export const createLesson = async (
130130 lessonValidator ( lessonData ) ;
131131
132132 try {
133- const course : Course | null = await CourseModel . findOne ( {
133+ const course : InternalCourse | null = await CourseModel . findOne ( {
134134 courseId : lessonData . courseId ,
135135 domain : ctx . subdomain . _id ,
136136 } ) ;
@@ -199,7 +199,7 @@ export const deleteLesson = async (id: string, ctx: GQLContext) => {
199199
200200 try {
201201 // remove from the parent Course's lessons array
202- let course : Course | null = await CourseModel . findOne ( {
202+ let course : InternalCourse | null = await CourseModel . findOne ( {
203203 domain : ctx . subdomain . _id ,
204204 } ) . elemMatch ( "lessons" , { $eq : lesson . lessonId } ) ;
205205 if ( ! course ) {
@@ -223,7 +223,10 @@ export const deleteLesson = async (id: string, ctx: GQLContext) => {
223223 }
224224} ;
225225
226- export const getAllLessons = async ( course : Course , ctx : GQLContext ) => {
226+ export const getAllLessons = async (
227+ course : InternalCourse ,
228+ ctx : GQLContext ,
229+ ) => {
227230 const lessons = await LessonModel . find (
228231 {
229232 lessonId : {
@@ -316,16 +319,46 @@ export const markLessonCompleted = async (
316319 await recordActivity ( {
317320 domain : ctx . subdomain . _id ,
318321 userId : ctx . user . userId ,
319- type : "lesson_completed" ,
322+ type : Constants . ActivityType . LESSON_COMPLETED ,
320323 entityId : lesson . lessonId ,
321324 metadata : {
322325 courseId : lesson . courseId ,
323326 } ,
324327 } ) ;
325328
329+ await recordCourseCompleted ( lesson . courseId , ctx ) ;
330+
326331 return true ;
327332} ;
328333
334+ const recordCourseCompleted = async ( courseId : string , ctx : GQLContext ) => {
335+ const course = await CourseModel . findOne ( { courseId } ) ;
336+ if ( ! course ) {
337+ throw new Error ( responses . item_not_found ) ;
338+ }
339+
340+ const isCourseCompleted = course . lessons . every ( ( lessonId ) => {
341+ const progress = ctx . user . purchases . find (
342+ ( progress : Progress ) => progress . courseId === courseId ,
343+ ) ;
344+ if ( ! progress ) {
345+ return false ;
346+ }
347+ return progress . completedLessons . includes ( lessonId ) ;
348+ } ) ;
349+
350+ if ( ! isCourseCompleted ) {
351+ return ;
352+ }
353+
354+ await recordActivity ( {
355+ domain : ctx . subdomain . _id ,
356+ userId : ctx . user . userId ,
357+ type : Constants . ActivityType . COURSE_COMPLETED ,
358+ entityId : courseId ,
359+ } ) ;
360+ } ;
361+
329362export const evaluateLesson = async (
330363 lessonId : string ,
331364 answers : { answers : number [ ] [ ] } ,
0 commit comments