Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(flow-submission): Fix flow submission endpoints filtering out incomplete submissions(M2-6616) #1378

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/apps/answers/crud/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import parse_obj_as
from sqlalchemy import Text, and_, case, column, delete, func, null, or_, select, text, update
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Query, contains_eager
from sqlalchemy.orm import Query, aliased, contains_eager
from sqlalchemy.sql import Values
from sqlalchemy.sql.elements import BooleanClauseList

Expand Down Expand Up @@ -134,6 +134,7 @@ async def get_flow_submission_data(
AnswerSchema.submit_id, AnswerSchema.flow_history_id, AnswerSchema.applet_id, AnswerSchema.version
)
.order_by(created_at)
.having(func.bool_or(AnswerSchema.is_flow_completed.is_(True))) # completed submissions only
)

_filters = _AnswerListFilter().get_clauses(**filters)
Expand Down Expand Up @@ -764,6 +765,15 @@ async def delete_by_subject(self, subject_id: uuid.UUID):
await self._execute(query)

async def get_flow_identifiers(self, flow_id: uuid.UUID, target_subject_id: uuid.UUID) -> list[IdentifierData]:
completed_submission = aliased(AnswerSchema, name="completed_submission")
is_submission_completed = (
select(completed_submission.submit_id)
.where(
completed_submission.submit_id == AnswerSchema.submit_id,
completed_submission.is_flow_completed.is_(True),
)
.exists()
)
query = (
select(
AnswerItemSchema.identifier,
Expand All @@ -777,6 +787,7 @@ async def get_flow_identifiers(self, flow_id: uuid.UUID, target_subject_id: uuid
AnswerSchema.id_from_history_id(AnswerSchema.flow_history_id) == str(flow_id),
AnswerSchema.target_subject_id == target_subject_id,
AnswerItemSchema.identifier.isnot(None),
is_submission_completed,
)
.group_by(
AnswerItemSchema.identifier,
Expand Down
8 changes: 8 additions & 0 deletions src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ async def get_flow_submission(
applet_id: uuid.UUID,
flow_id: uuid.UUID,
submit_id: uuid.UUID,
is_completed: bool | None = None,
) -> FlowSubmissionDetails:
allowed_subjects = await self._get_allowed_subjects(applet_id)

Expand All @@ -521,6 +522,7 @@ async def get_flow_submission(

answer_result: list[ActivityAnswer] = []

is_flow_completed = False
for answer in answers:
answer_result.append(
ActivityAnswer(
Expand All @@ -539,6 +541,11 @@ async def get_flow_submission(
)
)
activity_hist_ids.add(answer.activity_history_id)
if answer.is_flow_completed:
is_flow_completed = True

if is_completed and is_completed != is_flow_completed:
raise AnswerNotFoundError()

flow_history_id = answers[0].flow_history_id
assert flow_history_id
Expand All @@ -555,6 +562,7 @@ async def get_flow_submission(
created_at=max([a.created_at for a in answer_result]),
end_datetime=max([a.end_datetime for a in answer_result]),
answers=answer_result,
is_completed=is_flow_completed,
),
flow=flows[0],
)
Expand Down
134 changes: 132 additions & 2 deletions src/apps/answers/tests/test_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,31 @@ async def tom_answer_activity_flow(session: AsyncSession, tom: User, applet_with
)


@pytest.fixture
async def tom_answer_activity_flow_incomplete(
session: AsyncSession, tom: User, applet_with_flow: AppletFull
) -> AnswerSchema:
answer_service = AnswerService(session, tom.id)
return await answer_service.create_answer(
AppletAnswerCreate(
applet_id=applet_with_flow.id,
version=applet_with_flow.version,
submit_id=uuid.uuid4(),
flow_id=applet_with_flow.activity_flows[0].id,
is_flow_completed=False,
activity_id=applet_with_flow.activities[0].id,
answer=ItemAnswerCreate(
item_ids=[applet_with_flow.activities[0].items[0].id],
start_time=datetime.datetime.utcnow(),
end_time=datetime.datetime.utcnow(),
user_public_key=str(tom.id),
identifier="encrypted_identifier",
),
client=ClientMeta(app_id=f"{uuid.uuid4()}", app_version="1.1", width=984, height=623),
)
)


@pytest.fixture
def applet_with_flow_answer_create(applet_with_flow: AppletFull) -> list[AppletAnswerCreate]:
submit_id = uuid.uuid4()
Expand Down Expand Up @@ -426,7 +451,7 @@ class TestAnswerActivityItems(BaseTest):
applet_submit_dates_url = "/answers/applet/{applet_id}/dates"

activity_answer_url = "/answers/applet/{applet_id}/activities/{activity_id}/answers/{answer_id}"
flow_submission_url = "/answers/applet/{applet_id}/flows/{flow_id}/submissions/{submit_id}"
flow_submission_url = f"{flow_submissions_url}/{{submit_id}}"
assessment_answers_url = "/answers/applet/{applet_id}/answers/{answer_id}/assessment"

assessment_submissions_url = "/answers/applet/{applet_id}/submissions/{submission_id}/assessments"
Expand Down Expand Up @@ -1050,6 +1075,32 @@ async def test_get_flow_identifiers(
assert data[i]["identifier"] == _answer.answer.identifier
assert data[i]["userPublicKey"] == _answer.answer.user_public_key

async def test_get_flow_identifiers_incomplete_submission(
self,
mock_kiq_report,
client,
tom: User,
applet_with_flow: AppletFull,
applet_with_flow_answer_create: list[AppletAnswerCreate],
tom_answer_activity_flow_incomplete,
session,
):
applet = applet_with_flow
client.login(tom)

tom_subject = await SubjectsService(session, tom.id).get_by_user_and_applet(tom.id, applet.id)
assert tom_subject

identifier_url = self.flow_identifiers_url.format(
applet_id=applet.id, flow_id=applet_with_flow.activity_flows[0].id
)
response = await client.get(identifier_url, dict(targetSubjectId=tom_subject.id))

assert response.status_code == 200
data = response.json()
assert data["count"] == 0
assert data["result"] == []

# TODO: Move to another place, not needed any answer for test
async def test_get_all_activity_versions_for_applet(self, client: TestClient, tom: User, applet: AppletFull):
client.login(tom)
Expand Down Expand Up @@ -1612,6 +1663,40 @@ async def test_review_flows_one_answer(
assert set(data[0]["answerDates"][0].keys()) == {"submitId", "createdAt", "endDatetime"}
assert len(data[1]["answerDates"]) == 0

async def test_review_flows_one_answer_incomplete_submission(
self,
mock_kiq_report,
client,
tom: User,
applet_with_flow: AppletFull,
tom_answer_activity_flow_incomplete,
session,
):
client.login(tom)
url = self.review_flows_url.format(applet_id=applet_with_flow.id)

tom_subject = await SubjectsService(session, tom.id).get_by_user_and_applet(tom.id, applet_with_flow.id)
assert tom_subject

response = await client.get(
url,
dict(
targetSubjectId=tom_subject.id,
createdDate=datetime.datetime.utcnow().date(),
),
)
assert response.status_code == 200
data = response.json()
assert "result" in data
data = data["result"]
assert len(data) == len(applet_with_flow.activity_flows)
assert set(data[0].keys()) == {"id", "name", "answerDates", "lastAnswerDate"}
for i, row in enumerate(data):
assert row["id"] == str(applet_with_flow.activity_flows[i].id)
assert row["name"] == applet_with_flow.activity_flows[i].name
assert len(data[0]["answerDates"]) == 0
assert len(data[1]["answerDates"]) == 0

async def test_review_flows_multiple_answers(
self,
mock_kiq_report,
Expand Down Expand Up @@ -1653,7 +1738,7 @@ async def test_flow_submission(self, client, tom: User, applet_with_flow: Applet
assert "result" in data
data = data["result"]
assert set(data.keys()) == {"flow", "submission", "summary"}

assert data["submission"]["isCompleted"] is True
assert len(data["submission"]["answers"]) == len(applet_with_flow.activity_flows[0].items)
answer_data = data["submission"]["answers"][0]
# fmt: off
Expand Down Expand Up @@ -1689,6 +1774,24 @@ async def test_flow_submission(self, client, tom: User, applet_with_flow: Applet
assert data["summary"]["identifier"]["identifier"] == "encrypted_identifier"
# fmt: on

async def test_flow_submission_incomplete(
self, client, tom: User, applet_with_flow: AppletFull, tom_answer_activity_flow_incomplete
):
client.login(tom)
url = self.flow_submission_url.format(
applet_id=applet_with_flow.id,
flow_id=applet_with_flow.activity_flows[0].id,
submit_id=tom_answer_activity_flow_incomplete.submit_id,
)
response = await client.get(url)
assert response.status_code == 200
data = response.json()
assert "result" in data
data = data["result"]
assert set(data.keys()) == {"flow", "submission", "summary"}
assert data["submission"]["isCompleted"] is False
assert len(data["submission"]["answers"]) == len(applet_with_flow.activity_flows[0].items)

async def test_flow_submission_no_flow(
self, client, tom: User, applet_with_flow: AppletFull, tom_answer_activity_no_flow
):
Expand Down Expand Up @@ -1801,6 +1904,33 @@ async def test_get_flow_submissions(
# fmt: on
assert flow_data["idVersion"] == tom_answer_activity_flow.flow_history_id

async def test_get_flow_submissions_incomplete(
self,
mock_kiq_report,
client,
tom: User,
applet_with_flow: AppletFull,
tom_answer_activity_flow_incomplete,
session,
):
client.login(tom)
url = self.flow_submissions_url.format(
applet_id=applet_with_flow.id,
flow_id=applet_with_flow.activity_flows[0].id,
)

tom_subject = await SubjectsService(session, tom.id).get_by_user_and_applet(tom.id, applet_with_flow.id)
assert tom_subject
response = await client.get(url, dict(targetSubjectId=tom_subject.id))
assert response.status_code == 200
data = response.json()
assert set(data.keys()) == {"result", "count"}
assert data["count"] == 0
data = data["result"]
assert set(data.keys()) == {"flows", "submissions"}

assert len(data["submissions"]) == 0

@pytest.mark.parametrize(
"query_params",
(
Expand Down
Loading