diff --git a/lms/djangoapps/mobile_api/users/serializers.py b/lms/djangoapps/mobile_api/users/serializers.py index 76453dc4a6b4..82389199ef72 100644 --- a/lms/djangoapps/mobile_api/users/serializers.py +++ b/lms/djangoapps/mobile_api/users/serializers.py @@ -257,7 +257,7 @@ def get_progress(self, model: CourseEnrollment) -> Dict[str, int]: """ return self.calculate_progress(model) - def get_course_assignments(self, model: CourseEnrollment) -> Optional[Dict[str, List[Dict[str, str]]]]: + def get_course_assignments(self, model: CourseEnrollment) -> Dict[str, Optional[List[Dict[str, str]]]]: """ Returns the future assignment data and past assignments data for the user in the course. """ @@ -267,20 +267,28 @@ def get_course_assignments(self, model: CourseEnrollment) -> Optional[Dict[str, self.context.get('request'), include_past_dates=True ) - next_assignment = None - past_assignment = [] + past_assignments = [] + future_assignments = [] timezone = get_user_timezone_or_last_seen_timezone_or_utc(model.user) for assignment in sorted(assignments, key=lambda x: x.date): if assignment.date < datetime.now(timezone): - past_assignment.append(assignment) + past_assignments.append(assignment) else: - next_assignment = DateSummarySerializer(assignment).data - break + if not assignment.complete: + future_assignments.append(assignment) + + if future_assignments: + future_assignment_date = future_assignments[0].date.date() + next_assignments = [ + assignment for assignment in future_assignments if assignment.date.date() == future_assignment_date + ] + else: + next_assignments = [] return { - 'future_assignment': next_assignment, - 'past_assignments': DateSummarySerializer(past_assignment, many=True).data, + 'future_assignments': DateSummarySerializer(next_assignments, many=True).data, + 'past_assignments': DateSummarySerializer(past_assignments, many=True).data, } class Meta: diff --git a/lms/djangoapps/mobile_api/users/tests.py b/lms/djangoapps/mobile_api/users/tests.py index 5b842851db0b..05a75f30f5c3 100644 --- a/lms/djangoapps/mobile_api/users/tests.py +++ b/lms/djangoapps/mobile_api/users/tests.py @@ -902,6 +902,21 @@ def test_user_enrollment_api_v4_status_all(self): self.assertEqual(enrollments['results'][2]['course']['id'], str(old_course.id)) self.assertNotIn('primary', response.data) + @patch('lms.djangoapps.mobile_api.users.serializers.cache.set', return_value=None) + def test_response_contains_primary_enrollment_assignments_info(self, cache_mock: MagicMock): + self.login() + course = CourseFactory.create(org='edx', mobile_available=True) + self.enroll(course.id) + + response = self.api_response(api_version=API_V4) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertIn('course_assignments', response.data['primary']) + self.assertIn('past_assignments', response.data['primary']['course_assignments']) + self.assertIn('future_assignments', response.data['primary']['course_assignments']) + self.assertListEqual(response.data['primary']['course_assignments']['past_assignments'], []) + self.assertListEqual(response.data['primary']['course_assignments']['future_assignments'], []) + @override_settings(MKTG_URLS={'ROOT': 'dummy-root'}) class TestUserEnrollmentCertificates(UrlResetMixin, MobileAPITestCase, MilestonesTestCaseMixin):