Skip to content

Commit

Permalink
bugfix(summary-flows) Filter out not completed submissions and use fl…
Browse files Browse the repository at this point in the history
…ows order (M2-6921, M2-6238) (#1393)

* bugfix(summary-flows) Filter out not completed submissions and use flows order

* wip(summary-flows) Add additional filter
  • Loading branch information
iwankrshkin authored Jun 7, 2024
1 parent d5aeca7 commit 59384ef
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/apps/answers/crud/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
Expand Down
26 changes: 26 additions & 0 deletions src/apps/answers/tests/test_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 59384ef

Please sign in to comment.