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

feat: Merge LORIS integration to dev (M2-7488) #1580

Merged
merged 119 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
2790f4c
M2-3451: added module for integrations
ZenaY91 Nov 15, 2023
8174db7
some refactoring
ZenaY91 Dec 5, 2023
adc5376
some logic for loris
ZenaY91 Dec 12, 2023
1832e25
some changes in domain models
ZenaY91 Dec 14, 2023
f888386
integration creation
ZenaY91 Dec 14, 2023
da18db3
added method that return all answers that can be shared
ZenaY91 Dec 15, 2023
3c40029
added method for interation with data decryption for LORIS
ZenaY91 Dec 15, 2023
2d76772
fix recording cq
ZenaY91 Dec 15, 2023
de3d785
added integrations details for AppletSingleLanguageDetailMobilePublic
ZenaY91 Dec 15, 2023
eb9dde6
comment unused fields
ZenaY91 Dec 19, 2023
53ebab7
added improved logic for communication with LORIS
ZenaY91 Dec 20, 2023
3f8e93c
improve logic fro communication with report server
ZenaY91 Dec 20, 2023
45c07df
added logging. fix imports order. added answers.
ZenaY91 Dec 20, 2023
bab3e13
turn on logs
ZenaY91 Dec 20, 2023
4860318
small fix for Activitie model
ZenaY91 Dec 20, 2023
0ebcbbe
some improvements. new method
ZenaY91 Dec 27, 2023
c8c94ef
some refactoring and logic improvement
ZenaY91 Dec 27, 2023
ee681d7
remove unused field
ZenaY91 Dec 27, 2023
e8ce1f6
added new logic for candidate and answers
ZenaY91 Dec 29, 2023
5c1628d
fix url add cohort
ZenaY91 Dec 29, 2023
02cb2b3
one more item type processing
ZenaY91 Jan 5, 2024
01b096e
added temporary endpoint for schema
ZenaY91 Jan 5, 2024
9bddced
added temporary function that send schema to the LORIS server
ZenaY91 Jan 5, 2024
5cfefa3
added call for instrument creation
ZenaY91 Jan 8, 2024
7e8b052
added call for instrument creation
ZenaY91 Jan 8, 2024
0e4755e
added more items types for processing
ZenaY91 Jan 9, 2024
fb6af78
increment value bacause count start from 0
ZenaY91 Jan 10, 2024
23d16ed
removed unnecessary file
ZenaY91 Jan 10, 2024
61a8274
some refactoring
ZenaY91 Jan 10, 2024
f3898df
fixed some typo, typing
ZenaY91 Jan 10, 2024
f1761db
improve logis for singleSelect
ZenaY91 Jan 11, 2024
27510ab
added migration that actualize develop with loris integration
ZenaY91 Feb 9, 2024
c518d9a
added new table that describe relationship between ml and loris user
ZenaY91 Feb 9, 2024
0270deb
refactoring
ZenaY91 Feb 9, 2024
b869184
added new logic for center and candidate. provide refactoring
ZenaY91 Feb 9, 2024
4609539
merge heads, actualize loris with dev v1.3.14-rc
ZenaY91 Feb 21, 2024
e0a22c1
entites remaping. refactoring. code update.
ZenaY91 May 7, 2024
4dce697
added loris to config. refactoring. last changes from dev
ZenaY91 May 20, 2024
de297af
actualize from develop
ZenaY91 May 20, 2024
92481de
fix: merge migration heads to one
ZenaY91 May 21, 2024
5e1dfc6
Merge pull request #1319 from ChildMindInstitute/loris-integration-ph…
ZenaY91 May 21, 2024
57816d4
feat: merge with shell account
ZenaY91 May 30, 2024
ccd1179
fit: added migration with merged heads
ZenaY91 Jun 3, 2024
6c4ec3c
Merge pull request #1371 from ChildMindInstitute/feature/M2-6635-merg…
ZenaY91 Jun 5, 2024
23297af
Merge branch 'develop' into loris-integration-phase-2 for actualization
ZenaY91 Jun 10, 2024
41d101c
Merge pull request #1395 from ChildMindInstitute/feature/update-loris…
ZenaY91 Jun 10, 2024
8803d79
test: added tests for loris
ZenaY91 Jun 6, 2024
17a4e97
fix: added missing values
ZenaY91 Jun 6, 2024
b93fa3b
fit: added migration for arbitrary
ZenaY91 Jun 11, 2024
6bf1252
fix: fix test for loris integration
ZenaY91 Jun 11, 2024
edcfec8
refactor: rename field in answers shema. tests refactoring
ZenaY91 Jun 13, 2024
00255d2
Merge pull request #1386 from ChildMindInstitute/feature/M2-6637-test…
ZenaY91 Jun 13, 2024
80b08f7
feat:send to loris versions of applets & missing answers(M2-6646) (#1…
Damirkhon Jun 14, 2024
dadcced
feat:(Loris): Endpoints for enabling and disabling integration for wo…
Damirkhon Jun 21, 2024
7ee664c
Merge branch 'develop' into feature/update-loris-with-dev-branch
ZenaY91 Jun 25, 2024
6ac03f0
fit: added migration with merged heads
ZenaY91 Jun 25, 2024
807bb64
fix: added ability to not provide consent_to_share for applet answer
ZenaY91 Jun 25, 2024
bdc932a
Merge pull request #1463 from ChildMindInstitute/feature/update-loris…
ZenaY91 Jun 25, 2024
360b474
fit: added endpoint that return list of visits that exist in LORIS sy…
ZenaY91 Jun 13, 2024
0f591d0
fit: added ability to collent, group and provide infromation about us…
ZenaY91 Jun 25, 2024
955488d
fit: improvements requested by team
ZenaY91 Jun 27, 2024
c634f1e
fix: fix test
ZenaY91 Jun 27, 2024
5014f99
tests: update tests
ZenaY91 Jun 27, 2024
f4f88fa
Merge pull request #1465 from ChildMindInstitute/feature/M2-6649-mult…
ZenaY91 Jun 27, 2024
bf9afe9
M2-6641: loris integration status via alerts (#1468)
Damirkhon Jun 28, 2024
a6bfee3
fix: added detailed information into domain models
ZenaY91 Jul 1, 2024
900c387
Merge pull request #1470 from ChildMindInstitute/feature/M2-6649-mult…
ZenaY91 Jul 1, 2024
0fc2258
fix: added fields that helps fully describe data retrieved from frontend
ZenaY91 Jul 2, 2024
dcc809e
tests: fixed existing tests accordingly changes in domain model
ZenaY91 Jul 2, 2024
f2c2980
Merge pull request #1472 from ChildMindInstitute/feature/M2-6649-mult…
ZenaY91 Jul 2, 2024
5b62960
Merge branch 'develop' into feature/update-loris-with-dev-branch
ZenaY91 Jul 4, 2024
0b581f8
actualize from develop
ZenaY91 Jul 4, 2024
7eaed42
Merge pull request #1477 from ChildMindInstitute/feature/update-loris…
ZenaY91 Jul 4, 2024
1c5ba5a
fix: fixed problem when try to create visit that already exist for user
ZenaY91 Jul 9, 2024
224c719
fix: change message for ws
ZenaY91 Jul 9, 2024
fd1654c
Merge pull request #1480 from ChildMindInstitute/fix/M2-7238-upload-b…
ZenaY91 Jul 9, 2024
89a52c9
fix: Make answers.consent_to_share optional on answer create EP (M2-7…
vshvechko Jul 24, 2024
5320c09
fix: rework answer visits EP (M2-7316) (#1506)
vshvechko Jul 25, 2024
9d585a1
Merge branch 'develop' into feature/update-loris-with-dev-branch
ZenaY91 Jul 30, 2024
b3544c3
actualize from develop
ZenaY91 Jul 30, 2024
8c0b59c
Merge pull request #1515 from ChildMindInstitute/feature/update-loris…
ZenaY91 Jul 31, 2024
936db73
feat: changed dto. collected only selected answers
ZenaY91 Aug 6, 2024
1f5ef4a
fix: changed logic for answers collection
ZenaY91 Aug 6, 2024
4927812
Merge pull request #1527 from ChildMindInstitute/fix/M2-7403-data-is-…
ZenaY91 Aug 6, 2024
8ce530e
fix: added ability to send answer if it was skeped (M2-7437)
ZenaY91 Aug 7, 2024
9b88cf7
feat: set specific secret_id in alerts if applet relates to loris int…
ZenaY91 Aug 7, 2024
8dd6141
Merge pull request #1532 from ChildMindInstitute/fix/M2-7437-data-can…
ZenaY91 Aug 8, 2024
db2fc11
fix: added ability to send data only for chosen user (M2-7573)
ZenaY91 Aug 13, 2024
874d734
Merge pull request #1542 from ChildMindInstitute/fix/M2-7573-user-not…
ZenaY91 Aug 13, 2024
9348afd
feat: created new columns in both applet and applet_history tables to…
david-montano-metalab Aug 15, 2024
3774d89
fix: fixed problem with duplicated answers for one respondent (M2-77453)
ZenaY91 Aug 20, 2024
7eef4ae
Merge pull request #1552 from ChildMindInstitute/fix/M2-7453-answers-…
ZenaY91 Aug 20, 2024
f8d33e4
Merge branch 'develop' into feature/update-loris-with-dev-branch
ZenaY91 Aug 23, 2024
9aa41a6
actualize from develop
ZenaY91 Aug 23, 2024
f56a14e
fix: fixed import order
ZenaY91 Aug 23, 2024
e13cbc0
Merge pull request #1561 from ChildMindInstitute/feature/update-loris…
ZenaY91 Aug 23, 2024
765bd2f
Merge branch 'develop' into feature/update-loris-with-dev-branch
ZenaY91 Aug 29, 2024
628fa02
fix: some refactoring
ZenaY91 Aug 29, 2024
162e9b7
fix: fixed formating
ZenaY91 Aug 29, 2024
55b0bd9
Merge pull request #1578 from ChildMindInstitute/feature/update-loris…
ZenaY91 Aug 29, 2024
92e20f4
Merge branch 'develop' into feature/update-loris-with-dev-branch
ZenaY91 Aug 30, 2024
95f315c
Merge pull request #1586 from ChildMindInstitute/feature/update-loris…
ZenaY91 Aug 30, 2024
9896b6b
Create endpoint for checking LORIS integration - retrieve server conf…
david-montano-metalab Sep 20, 2024
a9a9316
Create endpoint for removing LORIS integration - Remove server connec…
david-montano-metalab Sep 25, 2024
423e912
Update LORIS integration to use stored config (M2-7455) (#1613)
david-montano-metalab Oct 8, 2024
5040f70
Update loris branch with develop 65ea0558ed170344c4316ae5b0780501d9d2…
david-montano-metalab Oct 8, 2024
a74bd23
Loris integration phase 2 pull develop da6e8cba73b4da8465224c85125b11…
david-montano-metalab Oct 18, 2024
b0db4e0
Merge branch 'develop' into loris-integration-phase-2
ChaconC Oct 30, 2024
d9c4b30
fix: remove slash from integrations router
ChaconC Nov 19, 2024
9779d7b
Merge remote-tracking branch 'origin/develop' into loris-integration-…
ChaconC Nov 25, 2024
1bae6b9
fix: update failing integrations tests
ChaconC Dec 7, 2024
b16d66d
remove workspace.integrations column
ChaconC Dec 7, 2024
9a4536a
remove enable/disable workspace integrations endpoint
ChaconC Dec 7, 2024
b9aeebb
chore: codequality
ChaconC Dec 7, 2024
e890872
Merge branch 'develop' into loris-integration-phase-2
ChaconC Dec 7, 2024
41a976f
Merge branch 'develop' into loris-integration-phase-2
ChaconC Dec 10, 2024
a2b5cac
Merge remote-tracking branch 'origin/develop' into loris-integration-…
ChaconC Jan 15, 2025
6d202cc
chore: run code quality
ChaconC Jan 15, 2025
380427f
Update src/apps/integrations/loris/service/loris.py
ChaconC Jan 15, 2025
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
18 changes: 16 additions & 2 deletions src/apps/alerts/crud/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from apps.alerts.db.schemas import AlertSchema
from apps.applets.db.schemas import AppletHistorySchema, AppletSchema
from apps.integrations.db.schemas import IntegrationsSchema
from apps.shared.ordering import Ordering
from apps.shared.paging import paging
from apps.shared.searching import Searching
Expand Down Expand Up @@ -40,11 +41,23 @@ async def get_all_for_user(
self, user_id: uuid.UUID, page: int, limit: int
) -> list[
tuple[
AlertSchema, AppletHistorySchema, UserAppletAccessSchema, AppletSchema, UserWorkspaceSchema, SubjectSchema
AlertSchema,
AppletHistorySchema,
UserAppletAccessSchema,
AppletSchema,
UserWorkspaceSchema,
SubjectSchema,
IntegrationsSchema,
]
]:
query: Query = select(
AlertSchema, AppletHistorySchema, UserAppletAccessSchema, AppletSchema, UserWorkspaceSchema, SubjectSchema
AlertSchema,
AppletHistorySchema,
UserAppletAccessSchema,
AppletSchema,
UserWorkspaceSchema,
SubjectSchema,
IntegrationsSchema.type,
)
query = query.join(
UserAppletAccessSchema,
Expand Down Expand Up @@ -72,6 +85,7 @@ async def get_all_for_user(
isouter=True,
)
query = query.outerjoin(SubjectSchema, SubjectSchema.id == AlertSchema.subject_id)
query = query.outerjoin(IntegrationsSchema, IntegrationsSchema.applet_id == UserAppletAccessSchema.applet_id)
query = query.where(AlertSchema.user_id == user_id, AppletSchema.is_deleted.is_(False))
query = query.order_by(AlertSchema.created_at.desc())
query = paging(query, page, limit)
Expand Down
1 change: 1 addition & 0 deletions src/apps/alerts/db/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ class AlertSchema(Base):
activity_item_id = Column(UUID(as_uuid=True))
alert_message = Column(StringEncryptedType(Unicode, get_key), nullable=False)
answer_id = Column(UUID(as_uuid=True))
type = Column(String())
26 changes: 18 additions & 8 deletions src/apps/alerts/domain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import datetime
import uuid
from enum import Enum

from pydantic import validator
from pydantic import Field, validator

from apps.shared.domain import InternalModel, PublicModel, ResponseMulti, dict_keys_to_camel_case

Expand All @@ -12,15 +13,20 @@
]


class AlertTypes(str, Enum):
ANSWER_ALERT = "answer"
INTEGRATION_ALERT = "integration"


class Alert(InternalModel):
id: uuid.UUID
is_watched: bool
applet_id: uuid.UUID
applet_name: str
version: str
secret_id: str
activity_id: uuid.UUID
activity_item_id: uuid.UUID
activity_id: uuid.UUID | None
activity_item_id: uuid.UUID | None
message: str
created_at: datetime.datetime
answer_id: uuid.UUID | None
Expand All @@ -29,6 +35,7 @@ class Alert(InternalModel):
workspace: str
respondent_id: uuid.UUID
subject_id: uuid.UUID | None
type: AlertTypes = Field(default=AlertTypes.ANSWER_ALERT)


class AlertPublic(PublicModel):
Expand All @@ -38,8 +45,8 @@ class AlertPublic(PublicModel):
applet_name: str
version: str
secret_id: str
activity_id: uuid.UUID
activity_item_id: uuid.UUID
activity_id: uuid.UUID | None
activity_item_id: uuid.UUID | None
message: str
created_at: datetime.datetime
answer_id: uuid.UUID | None
Expand All @@ -48,6 +55,7 @@ class AlertPublic(PublicModel):
workspace: str
respondent_id: uuid.UUID
subject_id: uuid.UUID | None
type: AlertTypes = Field(default=AlertTypes.ANSWER_ALERT)

@validator("encryption", pre=True)
def convert_response_values_keys(cls, response_values):
Expand All @@ -64,9 +72,10 @@ class AlertMessage(InternalModel):
version: str
message: str
created_at: datetime.datetime
activity_id: uuid.UUID
activity_item_id: uuid.UUID
answer_id: uuid.UUID
activity_id: uuid.UUID | None
activity_item_id: uuid.UUID | None
answer_id: uuid.UUID | None
type: AlertTypes = Field(default=AlertTypes.ANSWER_ALERT)


class AlertHandlerResult(InternalModel):
Expand All @@ -85,6 +94,7 @@ class AlertHandlerResult(InternalModel):
workspace: str
respondent_id: str
subject_id: str | None
type: AlertTypes = Field(default=AlertTypes.ANSWER_ALERT)


class AlertResponseMulti(ResponseMulti[AlertPublic]):
Expand Down
11 changes: 8 additions & 3 deletions src/apps/alerts/service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid

from apps.alerts.crud.alert import AlertCRUD
from apps.alerts.domain import Alert
from apps.alerts.domain import Alert, AlertTypes
from apps.shared.query_params import QueryParams


Expand All @@ -13,15 +13,19 @@ def __init__(self, session, user_id: uuid.UUID):
async def get_all_alerts(self, filters: QueryParams) -> list[Alert]:
alerts = []
schemas = await AlertCRUD(self.session).get_all_for_user(self.user_id, filters.page, filters.limit)
for alert, applet_history, access, applet, workspace, subject in schemas:
for alert, applet_history, access, applet, workspace, subject, integrations in schemas:
if integrations and "LORIS" in integrations:
_secret_id = "Loris Integration"
else:
_secret_id = subject.secret_user_id if subject else "Anonymous"
alerts.append(
Alert(
id=alert.id,
is_watched=alert.is_watched,
applet_id=alert.applet_id,
applet_name=applet_history.display_name,
version=alert.version,
secret_id=subject.secret_user_id if subject else "Anonymous",
secret_id=_secret_id,
activity_id=alert.activity_id,
activity_item_id=alert.activity_item_id,
message=alert.alert_message,
Expand All @@ -32,6 +36,7 @@ async def get_all_alerts(self, filters: QueryParams) -> list[Alert]:
workspace=workspace.workspace_name,
respondent_id=alert.respondent_id,
subject_id=alert.subject_id,
type=alert.type if alert.type else AlertTypes.ANSWER_ALERT,
)
)
return alerts
Expand Down
8 changes: 7 additions & 1 deletion src/apps/alerts/ws_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ async def _handle_websocket(websocket, user_id, session):
traceback.print_tb(e.__traceback__)
continue
try:
if applet.integrations and "loris" in applet.integrations:
_secret_id = "Loris Integration"
else:
_secret_id = subject.secret_user_id if subject else "Anonymous"

applet_alert = AlertHandlerResult(
id=str(alert_message.id),
applet_id=str(alert_message.applet_id),
applet_name=applet_history.display_name,
version=alert_message.version,
secret_id=subject.secret_user_id if subject else "Anonymous",
secret_id=_secret_id,
activity_id=str(alert_message.activity_id),
activity_item_id=str(alert_message.activity_item_id),
message=alert_message.message,
Expand All @@ -80,6 +85,7 @@ async def _handle_websocket(websocket, user_id, session):
workspace=workspace.workspace_name,
respondent_id=str(alert_message.respondent_id),
subject_id=str(alert_message.subject_id),
type=alert_message.type,
)
await websocket.send_json(applet_alert.dict())
except ConnectionClosed:
Expand Down
35 changes: 35 additions & 0 deletions src/apps/answers/crud/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,41 @@ async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: u
)
return res.mappings().all()

async def get_by_applet_id_and_readiness_to_share_data(
self, applet_id: uuid.UUID, respondent_id: uuid.UUID, answer_ids: list[uuid.UUID]
) -> list[AnswerSchema] | None:
query: Query = select(AnswerSchema)
query = query.where(AnswerSchema.applet_id == applet_id)
query = query.where(AnswerSchema.respondent_id == respondent_id)
query = query.where(AnswerSchema.consent_to_share.is_(True))
query = query.where(AnswerSchema.id.in_(answer_ids))
query = query.order_by(AnswerSchema.created_at.asc())
db_result = await self._execute(query)
return db_result.scalars().all()

async def get_respondents_by_applet_id_and_readiness_to_share_data(
self, applet_id: uuid.UUID
) -> list[AnswerSchema] | None:
query: Query = select(AnswerSchema.respondent_id)
query = query.where(AnswerSchema.applet_id == applet_id)
query = query.where(AnswerSchema.consent_to_share.is_(True))
query = query.order_by(AnswerSchema.created_at.asc())
db_result = await self._execute(query)
return db_result.scalars().all()

async def get_shareable_answers(self, applet_id: uuid.UUID):
query: Query = (
select(AnswerSchema)
.where(
AnswerSchema.applet_id == applet_id,
AnswerSchema.consent_to_share.is_(True),
)
.order_by(AnswerSchema.created_at)
)
result = await self._execute(query)

return result.scalars().all()

@staticmethod
def _query_submissions_metadata_by_subject(subject_column: InstrumentedAttribute, subject_id: uuid.UUID) -> Query:
query: Query = (
Expand Down
1 change: 1 addition & 0 deletions src/apps/answers/db/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class AnswerSchema(HistoryAware, Base):
source_subject_id = Column(UUID(as_uuid=True), nullable=True, index=True)
input_subject_id = Column(UUID(as_uuid=True), nullable=True, index=True)
relation = Column(String(length=20), nullable=True)
consent_to_share = Column(Boolean(), default=False)

answer_item = relationship(
"AnswerItemSchema",
Expand Down
1 change: 1 addition & 0 deletions src/apps/answers/domain/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class AppletAnswerCreate(InternalModel):
target_subject_id: uuid.UUID | None = None
source_subject_id: uuid.UUID | None = None
input_subject_id: uuid.UUID | None = None
consent_to_share: bool | None = False

_dates_from_ms = validator("created_at", pre=True, allow_reuse=True)(datetime_from_ms)

Expand Down
68 changes: 67 additions & 1 deletion src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from apps.activity_flows.crud import FlowsCRUD, FlowsHistoryCRUD
from apps.alerts.crud.alert import AlertCRUD
from apps.alerts.db.schemas import AlertSchema
from apps.alerts.domain import AlertMessage
from apps.alerts.domain import AlertMessage, AlertTypes
from apps.answers.crud import AnswerItemsCRUD
from apps.answers.crud.answers import AnswersCRUD
from apps.answers.crud.notes import AnswerNotesCRUD
Expand Down Expand Up @@ -381,6 +381,7 @@ async def _create_answer(self, applet_answer: AppletAnswerCreate) -> AnswerSchem
source_subject_id=source_subject.id,
input_subject_id=input_subject.id,
relation=relation,
consent_to_share=applet_answer.consent_to_share,
)
)
item_answer = applet_answer.answer
Expand Down Expand Up @@ -1620,6 +1621,7 @@ async def _create_alerts(
activity_item_id=raw_alert.activity_item_id,
alert_message=raw_alert.message,
answer_id=answer_id,
type=AlertTypes.ANSWER_ALERT.value,
)
)
alerts = await AlertCRUD(self.session).create_many(alert_schemas)
Expand All @@ -1640,6 +1642,7 @@ async def _create_alerts(
activity_id=alert.activity_id,
activity_item_id=alert.activity_item_id,
answer_id=answer_id,
type=alert.type,
).dict(),
)
except Exception as e:
Expand Down Expand Up @@ -2211,6 +2214,69 @@ async def _prepare_responses(self, answers: list[Answer]) -> list[dict]:
)
return responses

async def _prepare_loris_responses(self, answers_map: dict[uuid.UUID, AnswerSchema]) -> list[dict]:
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, userPublicKey=answer_item.user_public_key
)
)
return responses

async def decrypt_data_for_loris(
self, applet_id: uuid.UUID, respondent_id: uuid.UUID, answer_ids: list[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, answer_ids=answer_ids
)
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 = await self._prepare_loris_responses(answer_map)

data = dict(
responses=responses,
appletEncryption=dict(
accountId=applet.encryption["account_id"],
base=applet.encryption["base"],
prime=applet.encryption["prime"],
publicKey=applet.encryption["public_key"],
),
appletId=str(applet_id),
)

url: str = "{}/decrypt-user-responses".format(applet.report_server_ip.rstrip("/"))

async with aiohttp.ClientSession() as session:
logger.info(f"Sending request to the report server for LORIS {url}")
start = time.time()
async with session.post(
url,
json=data,
) as resp:
duration = time.time() - start
if resp.status == 200:
logger.info(f"Successful request (for LORIS) in {duration:.1f}" " seconds.")
response_data = await resp.json()
# return ReportServerResponse(**response_data)
return response_data, answer_versions
else:
logger.error(f"Failed request (for LORIS) in {duration:.1f}" " seconds.")
error_message = await resp.text()
raise ReportServerError(message=error_message)


class ReportServerEncryption:
_rate = 0.58
Expand Down
Loading
Loading