Skip to content

Commit

Permalink
Reapply "feat: Report answer export updates (M2-7255) (#1670)" (#1685)
Browse files Browse the repository at this point in the history
This reverts commit 2cd9967.
  • Loading branch information
ChaconC authored Dec 12, 2024
1 parent 1009137 commit f172f7c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/apps/answers/crud/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ async def get_applet_answers(
AnswerItemSchema.respondent_id,
self._exclude_assessment_val(AnswerSchema.target_subject_id).label("target_subject_id"),
self._exclude_assessment_val(AnswerSchema.source_subject_id).label("source_subject_id"),
self._exclude_assessment_val(AnswerSchema.input_subject_id).label("input_subject_id"),
self._exclude_assessment_val(AnswerSchema.relation).label("relation"),
AnswerItemSchema.answer,
AnswerItemSchema.events,
Expand Down
13 changes: 10 additions & 3 deletions src/apps/answers/domain/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,19 @@ class UserAnswerDataBase(BaseModel):
submit_id: uuid.UUID
version: str
respondent_id: uuid.UUID | str | None = None
target_subject_id: uuid.UUID | str | None = None
target_secret_id: uuid.UUID | str | None = None
respondent_secret_id: str | None = None
source_subject_id: uuid.UUID | str | None = None
source_secret_id: uuid.UUID | str | None = None
source_user_nickname: str | None = None
source_user_tag: str | None = None
target_subject_id: uuid.UUID | str | None = None
target_secret_id: uuid.UUID | str | None = None
target_user_nickname: str | None = None
target_user_tag: str | None = None
input_subject_id: uuid.UUID | str | None = None
input_secret_id: uuid.UUID | str | None = None
input_user_nickname: str | None = None
relation: str | None = None
respondent_secret_id: str | None = None
legacy_profile_id: str | None = None
user_public_key: str | None
answer: str | None = None
Expand Down
47 changes: 33 additions & 14 deletions src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,15 +1215,16 @@ async def get_export_data( # noqa: C901
applet_assessment_ids = set()
activity_hist_ids = set()
flow_hist_ids = set()

# collect ids to resolve data
for answer in answers:
# collect id to resolve data
if answer.reviewed_answer_id:
# collect reviewer ids to fetch the data
respondent_ids.add(answer.respondent_id) # type: ignore[arg-type] # noqa: E501
if answer.target_subject_id:
subject_ids.add(answer.target_subject_id) # type: ignore[arg-type] # noqa: E501
respondent_ids.add(answer.respondent_id) # type: ignore[arg-type] # noqa: E501
if answer.source_subject_id:
subject_ids.add(answer.source_subject_id) # type: ignore[arg-type] # noqa: E501
if answer.target_subject_id:
subject_ids.add(answer.target_subject_id) # type: ignore[arg-type] # noqa: E501
if answer.input_subject_id:
subject_ids.add(answer.input_subject_id) # type: ignore[arg-type] # noqa: E501
if answer.reviewed_answer_id:
applet_assessment_ids.add(answer.applet_history_id)
if answer.flow_history_id:
Expand All @@ -1234,35 +1235,53 @@ async def get_export_data( # noqa: C901
flows_coro = FlowsHistoryCRUD(self.session).get_by_id_versions(list(flow_hist_ids))
user_map_coro = AppletAccessCRUD(self.session).get_respondent_export_data(applet_id, list(respondent_ids))
subject_map_coro = AppletAccessCRUD(self.session).get_subject_export_data(applet_id, list(subject_ids))
current_user_subject_coro = SubjectsCrud(self.session).get_user_subject(self.user_id, applet_id) # type: ignore[arg-type]

coros_result = await asyncio.gather(
flows_coro,
user_map_coro,
subject_map_coro,
current_user_subject_coro,
return_exceptions=True,
)
for res in coros_result:
if isinstance(res, BaseException):
raise res

flows, user_map, subject_map = coros_result
flows, user_map, subject_map, current_user = coros_result
flow_map = {flow.id_version: flow for flow in flows} # type: ignore

for answer in answers:
# respondent data
if answer.reviewed_answer_id:
# assessment
respondent = user_map[answer.respondent_id] # type: ignore
else:
respondent = subject_map[answer.target_subject_id] # type: ignore
respondent = user_map[answer.respondent_id] # type: ignore
answer.respondent_secret_id = current_user.secret_user_id # type: ignore

answer.respondent_secret_id = respondent.secret_id
answer.source_secret_id = (
subject_map.get(answer.source_subject_id).secret_id if answer.source_subject_id else None # type: ignore
)
answer.source_user_nickname = (
subject_map.get(answer.source_subject_id).nickname if answer.source_subject_id else None # type: ignore
)
answer.source_user_tag = (
subject_map.get(answer.source_subject_id).tag if answer.source_subject_id else None # type: ignore
)

answer.target_secret_id = (
subject_map.get(answer.target_subject_id).secret_id if answer.target_subject_id else None # type: ignore
)
answer.target_user_nickname = (
subject_map.get(answer.target_subject_id).nickname if answer.target_subject_id else None # type: ignore
)
answer.target_user_tag = (
subject_map.get(answer.target_subject_id).tag if answer.target_subject_id else None # type: ignore
)

answer.input_secret_id = (
subject_map.get(answer.input_subject_id).secret_id if answer.input_subject_id else None # type: ignore
)
answer.input_user_nickname = (
subject_map.get(answer.input_subject_id).nickname if answer.input_subject_id else None # type: ignore
)

answer.respondent_email = respondent.email
answer.is_manager = respondent.is_manager
answer.legacy_profile_id = respondent.legacy_profile_id
Expand Down
11 changes: 7 additions & 4 deletions src/apps/answers/tests/test_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1840,9 +1840,11 @@ async def test_answers_export(
"flowName", "id", "itemIds", "migratedData", "respondentId",
"respondentSecretId", "reviewedAnswerId", "userPublicKey",
"version", "submitId", "scheduledDatetime", "startDatetime",
"endDatetime", "legacyProfileId", "migratedDate",
"relation", "sourceSubjectId", "sourceSecretId", "targetSubjectId",
"targetSecretId", "client", "tzOffset", "scheduledEventId", "reviewedFlowSubmitId"
"endDatetime", "legacyProfileId", "migratedDate", "relation",
"sourceSubjectId", "sourceSecretId", "sourceUserNickname", "sourceUserTag",
"targetSubjectId", "targetSecretId", "targetUserNickname", "targetUserTag",
"inputSubjectId", "inputSecretId", "inputUserNickname",
"client", "tzOffset", "scheduledEventId", "reviewedFlowSubmitId"
}
# Comment for now, wtf is it
# assert int(answer['startDatetime'] * 1000) == answer_item_create.start_time
Expand Down Expand Up @@ -1874,7 +1876,8 @@ async def test_get_applet_answers_without_assessment(
assert len(data["answers"]) == 1
assert resp_data["count"] == 1
assert data["answers"][0]["respondentId"] == str(tom.id)
assert data["answers"][0]["respondentSecretId"] == answer_shell_account_target["target_secret_user_id"]
respondent_secret_id = data["answers"][0]["respondentSecretId"].split(" ")[-1].strip("()")
assert respondent_secret_id == answer_shell_account_target["respondent_secret_user_id"]

@pytest.mark.parametrize(
"user_fixture, exp_cnt",
Expand Down
8 changes: 5 additions & 3 deletions src/apps/answers/tests/test_answers_arbitrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,11 @@ async def test_answers_export(
"flowName", "id", "itemIds", "migratedData", "respondentId",
"respondentSecretId", "reviewedAnswerId", "userPublicKey",
"version", "submitId", "scheduledDatetime", "startDatetime",
"endDatetime", "legacyProfileId", "migratedDate",
"relation", "sourceSubjectId", "sourceSecretId", "targetSubjectId",
"targetSecretId", "client", "tzOffset", "scheduledEventId", "reviewedFlowSubmitId"
"endDatetime", "legacyProfileId", "migratedDate", "relation",
"sourceSubjectId", "sourceSecretId", "sourceUserNickname", "sourceUserTag",
"targetSubjectId", "targetSecretId", "targetUserNickname", "targetUserTag",
"inputSubjectId", "inputSecretId", "inputUserNickname",
"client", "tzOffset", "scheduledEventId", "reviewedFlowSubmitId"
}

assert set(assessment.keys()) == expected_keys
Expand Down
2 changes: 2 additions & 0 deletions src/apps/workspaces/crud/applet_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ async def get_subject_export_data(
SubjectSchema.id,
SubjectSchema.user_id,
SubjectSchema.email,
SubjectSchema.nickname,
SubjectSchema.tag,
SubjectSchema.secret_user_id.label("secret_id"),
UserAppletAccessSchema.legacy_profile_id,
has_manager_role.label("is_manager"),
Expand Down
2 changes: 2 additions & 0 deletions src/apps/workspaces/domain/user_applet_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class RespondentExportData(InternalModel):

class SubjectExportData(RespondentExportData):
user_id: uuid.UUID | None
nickname: str | None
tag: str | None


class RespondentInfoPublic(PublicModel):
Expand Down

0 comments on commit f172f7c

Please sign in to comment.