Skip to content

Commit df91ce3

Browse files
authored
Merge pull request #232 from Tanzania-AI-Community/hotfix/filter_subjects_with_no_active_class_in_flowa
hotfix: Filter Subjects With No Active Class in Flows
2 parents 28c42d9 + 798402b commit df91ce3

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎app/services/flows/handlers/subjects_classes_flow_handler.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,19 @@ def _build_class_option_title(self, subject_title: str, grade_label: str) -> str
412412

413413
async def _build_subject_selection_screen_data(self, user: User) -> dict[str, Any]:
414414
subjects = await db.read_subjects() or []
415+
selectable_subjects = [
416+
subject
417+
for subject in subjects
418+
if any(
419+
self._is_active_class(class_)
420+
for class_ in (subject.subject_classes or [])
421+
)
422+
]
415423
all_subject_options = [
416424
option
417-
for option in (self._build_subject_option(subject) for subject in subjects)
425+
for option in (
426+
self._build_subject_option(subject) for subject in selectable_subjects
427+
)
418428
if option is not None
419429
]
420430

‎tests/services/test_flow_service.py‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,18 @@ async def test_build_subject_selection_screen_data_respects_chips_limits() -> No
586586
service = FlowService()
587587
user = User(id=405, wa_id="255700006665", name="Teacher")
588588
subjects = [
589-
Subject(id=index + 1, name=subject_name)
589+
Subject(
590+
id=index + 1,
591+
name=subject_name,
592+
subject_classes=[
593+
Class(
594+
id=1000 + index,
595+
subject_id=index + 1,
596+
grade_level=enums.GradeLevel.os1,
597+
status=enums.SubjectClassStatus.active,
598+
)
599+
],
600+
)
590601
for index, subject_name in enumerate(list(enums.SubjectName)[:25])
591602
]
592603

@@ -602,6 +613,57 @@ async def test_build_subject_selection_screen_data_respects_chips_limits() -> No
602613
assert all(len(option["title"]) <= 30 for option in data["subject_options"])
603614

604615

616+
@pytest.mark.asyncio
617+
async def test_build_subject_selection_screen_data_excludes_subjects_without_active_classes() -> (
618+
None
619+
):
620+
service = FlowService()
621+
user = User(id=406, wa_id="255700006664", name="Teacher")
622+
subjects = [
623+
Subject(
624+
id=1,
625+
name=enums.SubjectName.geography,
626+
subject_classes=[
627+
Class(
628+
id=101,
629+
subject_id=1,
630+
grade_level=enums.GradeLevel.os1,
631+
status=enums.SubjectClassStatus.inactive,
632+
)
633+
],
634+
),
635+
Subject(
636+
id=2,
637+
name=enums.SubjectName.biology,
638+
subject_classes=[
639+
Class(
640+
id=201,
641+
subject_id=2,
642+
grade_level=enums.GradeLevel.os1,
643+
status=enums.SubjectClassStatus.active,
644+
)
645+
],
646+
),
647+
Subject(
648+
id=3,
649+
name=enums.SubjectName.history,
650+
subject_classes=[],
651+
),
652+
]
653+
654+
with patch(
655+
"app.services.flows.handlers.subjects_classes_flow_handler.db.read_subjects",
656+
AsyncMock(return_value=subjects),
657+
):
658+
data = await service._subjects_classes_flow_handler._build_subject_selection_screen_data(
659+
user=user
660+
)
661+
662+
assert data["subject_options"] == [{"id": "2", "title": "Biology 🧬"}]
663+
assert data["selected_subject_ids"] == []
664+
assert data["has_subject_options"] is True
665+
666+
605667
def test_build_class_option_title_keeps_form_prefix_when_truncating() -> None:
606668
service = FlowService()
607669

0 commit comments

Comments
 (0)