Skip to content

Commit

Permalink
Filtering out deleted subject for submissions queries to avoid wroing…
Browse files Browse the repository at this point in the history
… activity listing
  • Loading branch information
rcmerlo committed Dec 5, 2024
1 parent f53837d commit 5857102
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
17 changes: 11 additions & 6 deletions src/apps/answers/crud/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,30 +973,35 @@ def __activity_and_flow_ids_by_subject_query(subject_column: InstrumentedAttribu
AnswerSchema.id_from_history_id(AnswerSchema.flow_history_id),
),
else_=AnswerSchema.id_from_history_id(AnswerSchema.activity_history_id),
).label("id")
).label("activity_id"),
(
AnswerSchema.source_subject_id
if subject_column == AnswerSchema.target_subject_id
else AnswerSchema.target_subject_id
).label("subject_id"),
)
.where(subject_column == subject_id)
.distinct()
.group_by("activity_id", "subject_id")
)
return query

async def get_activity_and_flow_ids_by_target_subject(self, target_subject_id: uuid.UUID) -> list[uuid.UUID]:
async def get_activity_and_flow_ids_by_target_subject(self, target_subject_id: uuid.UUID) -> list[dict]:
"""
Get a list of activity and flow IDs based on answers submitted for a target subject
"""
res = await self._execute(
self.__activity_and_flow_ids_by_subject_query(AnswerSchema.target_subject_id, target_subject_id)
)
return res.scalars().all()
return res.mappings().all()

async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: uuid.UUID) -> list[uuid.UUID]:
async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: uuid.UUID) -> list[dict]:
"""
Get a list of activity and flow IDs based on answers submitted for a source subject
"""
res = await self._execute(
self.__activity_and_flow_ids_by_subject_query(AnswerSchema.source_subject_id, source_subject_id)
)
return res.scalars().all()
return res.mappings().all()

@staticmethod
def _query_submissions_by_subject(subject_column: InstrumentedAttribute, subject_id: uuid.UUID) -> Query:
Expand Down
25 changes: 14 additions & 11 deletions src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,11 @@ async def get_activity_and_flow_ids_by_target_subject(self, target_subject_id: u
The data returned is just a combined list of activity and flow IDs, without any
distinction between the two
"""
return await AnswersCRUD(self.answer_session).get_activity_and_flow_ids_by_target_subject(target_subject_id)
results = await AnswersCRUD(self.answer_session).get_activity_and_flow_ids_by_target_subject(target_subject_id)
existing_subject_ids = await self._filter_out_soft_deleted_subjects(results)
activity_ids = [result["activity_id"] for result in results if result["subject_id"] in existing_subject_ids]

return activity_ids

async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: uuid.UUID) -> list[uuid.UUID]:
"""
Expand All @@ -1936,18 +1940,17 @@ async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: u
The data returned is just a combined list of activity and flow IDs, without any
distinction between the two
"""
return await AnswersCRUD(self.answer_session).get_activity_and_flow_ids_by_source_subject(source_subject_id)
results = await AnswersCRUD(self.answer_session).get_activity_and_flow_ids_by_source_subject(source_subject_id)
existing_subject_ids = await self._filter_out_soft_deleted_subjects(results)
activity_ids = [result["activity_id"] for result in results if result["subject_id"] in existing_subject_ids]

return activity_ids

async def _filter_out_soft_deleted_subjects(
self, submissions_target: list[dict], submissions_respondent: list[dict]
) -> set[uuid.UUID]:
async def _filter_out_soft_deleted_subjects(self, submissions: list[dict]) -> set[uuid.UUID]:
"""
Filter out soft-deleted source subjects from submissions_target
and soft-deleted target subjects from submissions_respondent
Return subject_ids contained in given submissions array that correspond to soft-deleted subjects
"""
all_submissions = submissions_target + submissions_respondent

subject_ids = set([activityOrFlow["subject_id"] for activityOrFlow in all_submissions])
subject_ids = set([activityOrFlow["subject_id"] for activityOrFlow in submissions])

assert self.user_id
existing_subjects = await SubjectsService(self.session, self.user_id).get_by_ids(list(subject_ids))
Expand All @@ -1963,7 +1966,7 @@ async def get_submissions_by_subject(self, subject_id: uuid.UUID) -> Submissions
submissions_target_coro, submissions_respondent_coro
)

existing_subject_ids = await self._filter_out_soft_deleted_subjects(submissions_target, submissions_respondent)
existing_subject_ids = await self._filter_out_soft_deleted_subjects(submissions_target + submissions_respondent)

submissions_activity_count = SubmissionsActivityCountBySubject(subject_id=subject_id)

Expand Down

0 comments on commit 5857102

Please sign in to comment.