Skip to content

Commit

Permalink
feat:send to loris versions of applets & missing answers(M2-6646) (#1412
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Damirkhon authored Jun 14, 2024
1 parent 00255d2 commit 80b08f7
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 105 deletions.
22 changes: 19 additions & 3 deletions src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,17 +1530,33 @@ async def _prepare_responses(self, answers_map: dict[uuid.UUID, AnswerSchema]) -
responses.append(dict(activityId=activity_id, answer=answer_item.answer))
return responses, [ai.user_public_key for ai in answer_items]

async def decrypt_data_for_loris(self, applet_id: uuid.UUID, respondent_id: uuid.UUID) -> dict | None:
async def _prepare_loris_responses(
self, answers_map: dict[uuid.UUID, AnswerSchema]
) -> tuple[list[dict], list[str]]:
answer_items = await AnswerItemsCRUD(self.answers_session).get_respondent_submits_by_answer_ids(
list(answers_map.keys())
)

responses = list()
for answer_item in answer_items:
answer = answers_map[answer_item.answer_id]
activity_id_version = str(answer.activity_history_id).replace("_", "__")
activity_answer_id = f"{activity_id_version}__{answer_item.answer_id}"
responses.append(dict(activityId=activity_answer_id, answer=answer_item.answer))
return responses, [ai.user_public_key for ai in answer_items]

async def decrypt_data_for_loris(self, applet_id: uuid.UUID, respondent_id: uuid.UUID) -> tuple[dict, list] | None:
answers = await AnswersCRUD(self.answers_session).get_by_applet_id_and_readiness_to_share_data(
applet_id=applet_id, respondent_id=respondent_id
)
if not answers:
return None

answer_map = dict((answer.id, answer) for answer in answers)
answer_versions = [a.version for a in answers]

applet = await AppletsCRUD(self.session).get_by_id(applet_id)
responses, user_public_keys = await self._prepare_responses(answer_map)
responses, user_public_keys = await self._prepare_loris_responses(answer_map)

data = dict(
responses=responses,
Expand Down Expand Up @@ -1568,7 +1584,7 @@ async def decrypt_data_for_loris(self, applet_id: uuid.UUID, respondent_id: uuid
logger.info(f"Successful request (for LORIS) in {duration:.1f}" " seconds.")
response_data = await resp.json()
# return ReportServerResponse(**response_data)
return response_data
return response_data, answer_versions
else:
logger.error(f"Failed request (for LORIS) in {duration:.1f}" " seconds.")
error_message = await resp.text()
Expand Down
8 changes: 7 additions & 1 deletion src/apps/integrations/loris/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from apps.activities.domain.conditional_logic import ConditionalLogic

__all__ = [
"UnencryptedAppletVersion",
"UnencryptedApplet",
"Consent",
"ConsentUpdate",
Expand All @@ -29,7 +30,7 @@ class Item(BaseModel):


class Activitie(BaseModel):
id: uuid.UUID
id: str
name: str
description: str
splash_screen: str = ""
Expand All @@ -46,6 +47,11 @@ class UnencryptedApplet(BaseModel):
activities: list[Activitie]


class UnencryptedAppletVersion(BaseModel):
version: str
applet: UnencryptedApplet


class LorisServerResponse(BaseModel):
pass

Expand Down
Loading

0 comments on commit 80b08f7

Please sign in to comment.