private async Task HandleLessonAchievements(UserLesson userLesson) { int userId = userLesson.UserId; List <Achievement> achievementsForUser = await achievementService.GetAchievementsForUser(userId); List <Lesson> lessonsForUser = await courseRepo.GetCompletedLessonsForUserDistinct(userId); // Handling LESSON achievement Achievement achievementForLessonCount = await achievementService.GetAchievementForLessonCount(lessonsForUser.Count); if (achievementForLessonCount != null && !achievementsForUser.Contains(achievementForLessonCount)) { await achievementService.InsertUserAchievement(userId, achievementForLessonCount.AchievementId); } // Handling CHAPTER achievement Lesson lesson = await courseRepo.GetLesson(userLesson.LessonId); Chapter chapter = await courseRepo.GetChapter(lesson.ChapterId); bool didUserCompleteTheChapter = await DidUserCompleteTheChapter(userId, chapter); if (didUserCompleteTheChapter) { await HandleChapterAchievement(userId, achievementsForUser, chapter); } // Handling COURSE achievement Course course = await courseRepo.GetCourse(chapter.CourseId); bool didUserCompleteTheCourse = await DidUserCompleteTheCourse(userId, course); if (didUserCompleteTheChapter && didUserCompleteTheCourse) { await HandleCourseAchievement(userId, achievementsForUser, course); } }