diff --git a/src/apps/answers/crud/answers.py b/src/apps/answers/crud/answers.py index 4c88d3ba970..bb81efaeed1 100644 --- a/src/apps/answers/crud/answers.py +++ b/src/apps/answers/crud/answers.py @@ -490,10 +490,20 @@ async def get_submitted_activity_with_last_date( async def get_submitted_flows_with_last_date( self, applet_id: uuid.UUID, target_subject_id: uuid.UUID | None ) -> list[tuple[str, datetime.datetime]]: - query: Query = select(AnswerSchema.flow_history_id, func.max(AnswerSchema.created_at)) - query = query.where(AnswerSchema.applet_id == applet_id, AnswerSchema.flow_history_id.isnot(None)) + subquery: Query = select(AnswerSchema.submit_id) + subquery = subquery.where( + AnswerSchema.applet_id == applet_id, + AnswerSchema.flow_history_id.isnot(None), + AnswerSchema.is_flow_completed.is_(True), + ) if target_subject_id: - query = query.where(AnswerSchema.target_subject_id == target_subject_id) + subquery = subquery.where(AnswerSchema.target_subject_id == target_subject_id) + + query: Query = select(AnswerSchema.flow_history_id, func.max(AnswerSchema.created_at)) + query = query.where( + AnswerSchema.submit_id.in_(subquery), + AnswerSchema.is_flow_completed.is_(True), + ) query = query.group_by(AnswerSchema.flow_history_id) query = query.order_by(AnswerSchema.flow_history_id) db_result = await self._execute(query) diff --git a/src/apps/answers/service.py b/src/apps/answers/service.py index 429fe54ca7d..3c4576d11c8 100644 --- a/src/apps/answers/service.py +++ b/src/apps/answers/service.py @@ -1094,7 +1094,7 @@ async def get_summary_activity_flows( flow_histories = await flow_crud.retrieve_by_applet_version(f"{applet.id}_{applet.version}") flow_histories_curr = [flow_h.id for flow_h in flow_histories] results = [] - for flow_history in activity_flow_histories: + for flow_history in sorted(activity_flow_histories, key=lambda x: x.order): flow_history_answer_date = submitted_activity_flows.get( flow_history.id_version, submitted_activity_flows.get(str(flow_history.id)) ) diff --git a/src/apps/answers/tests/test_answers.py b/src/apps/answers/tests/test_answers.py index b10f76faa07..86c1c946f08 100644 --- a/src/apps/answers/tests/test_answers.py +++ b/src/apps/answers/tests/test_answers.py @@ -2006,3 +2006,29 @@ async def test_deleted_flow_not_included_in_submission_list( assert applet__deleted_flow_without_answers.activity_flows[0].id assert payload["count"] == 1 assert payload["result"][0]["id"] == str(applet__deleted_flow_without_answers.activity_flows[0].id) + + async def test_summary_flow_list_order_completed_submissions_only( + self, client, tom: User, applet_with_flow: AppletFull, tom_answer_activity_flow_not_completed + ): + client.login(tom) + url = self.summary_activity_flows_url.format(applet_id=applet_with_flow.id) + response = await client.get(url) + assert response.status_code == 200 + payload = response.json() + assert "result" in payload + for flow in payload["result"]: + assert flow["hasAnswer"] is False + + async def test_summary_flow_list_order( + self, client, tom: User, applet_with_flow: AppletFull, tom_answer_activity_flow_not_completed + ): + client.login(tom) + url = self.summary_activity_flows_url.format(applet_id=applet_with_flow.id) + response = await client.get(url) + assert response.status_code == 200 + payload = response.json() + + assert "result" in payload + flows_order_expected = [str(flow.id) for flow in sorted(applet_with_flow.activity_flows, key=lambda x: x.order)] + flows_order_actual = [flow["id"] for flow in payload["result"]] + assert flows_order_actual == flows_order_expected