From 71024fe2f40f59dc8ff8a61cd31303c9201950d0 Mon Sep 17 00:00:00 2001 From: Pavlo Mokiichuk Date: Wed, 11 Sep 2024 11:49:28 +0200 Subject: [PATCH] Update RDI summary after creation Needs Adjudication tickets (#4185) * fix statistics after HardDocumentDeduplication * black * upd tests * more fixes * refactor * refactor * review * black * black --- .../services/needs_adjudication_ticket_services.py | 2 +- .../tasks/deduplicate_and_check_sanctions.py | 8 ++++++-- .../hct_mis_api/apps/registration_data/models.py | 14 ++++++++++++++ .../apps/registration_datahub/celery_tasks.py | 8 +------- .../apps/registration_datahub/tasks/rdi_merge.py | 1 + .../tests/test_handling_documents_duplicates.py | 4 +++- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/backend/hct_mis_api/apps/grievance/services/needs_adjudication_ticket_services.py b/backend/hct_mis_api/apps/grievance/services/needs_adjudication_ticket_services.py index 0cb717a0f5..89e13a57c0 100644 --- a/backend/hct_mis_api/apps/grievance/services/needs_adjudication_ticket_services.py +++ b/backend/hct_mis_api/apps/grievance/services/needs_adjudication_ticket_services.py @@ -49,7 +49,7 @@ def _clear_deduplication_individuals_fields(individuals: Sequence[Individual]) - individual.deduplication_batch_status = UNIQUE_IN_BATCH individual.deduplication_golden_record_results = {} individual.deduplication_batch_results = {} - HardDocumentDeduplication().deduplicate(individual.documents.all()) + HardDocumentDeduplication().deduplicate(individual.documents.all(), individual.registration_data_import) Individual.objects.bulk_update( individuals, [ diff --git a/backend/hct_mis_api/apps/grievance/tasks/deduplicate_and_check_sanctions.py b/backend/hct_mis_api/apps/grievance/tasks/deduplicate_and_check_sanctions.py index a5619d68c6..7aa7b9183c 100644 --- a/backend/hct_mis_api/apps/grievance/tasks/deduplicate_and_check_sanctions.py +++ b/backend/hct_mis_api/apps/grievance/tasks/deduplicate_and_check_sanctions.py @@ -37,7 +37,9 @@ def execute(self, should_populate_index: bool, individuals_ids: List[str]) -> No if business_area.postpone_deduplication: logger.info("Postponing deduplication for business area %s", business_area) - HardDocumentDeduplication().deduplicate(Document.objects.filter(individual_id__in=individuals_ids)) + HardDocumentDeduplication().deduplicate( + Document.objects.filter(individual_id__in=individuals_ids), + ) return DeduplicateTask(business_area.slug, individuals.first().program_id).deduplicate_individuals_from_other_source( @@ -65,4 +67,6 @@ def execute(self, should_populate_index: bool, individuals_ids: List[str]) -> No if business_area.screen_beneficiary: CheckAgainstSanctionListPreMergeTask.execute() - HardDocumentDeduplication().deduplicate(Document.objects.filter(individual_id__in=individuals_ids)) + HardDocumentDeduplication().deduplicate( + Document.objects.filter(individual_id__in=individuals_ids), + ) diff --git a/backend/hct_mis_api/apps/registration_data/models.py b/backend/hct_mis_api/apps/registration_data/models.py index 74199ed5eb..b6c9fa7bc5 100644 --- a/backend/hct_mis_api/apps/registration_data/models.py +++ b/backend/hct_mis_api/apps/registration_data/models.py @@ -239,6 +239,20 @@ def refresh_population_statistics(self) -> None: def biometric_deduplication_enabled(self) -> bool: return self.program.biometric_deduplication_enabled + def update_needs_adjudication_tickets_statistic(self) -> None: + from hct_mis_api.apps.grievance.models import GrievanceTicket + + # AB#201950 + self.golden_record_possible_duplicates = ( + self.grievanceticket_set.filter( + category=GrievanceTicket.CATEGORY_NEEDS_ADJUDICATION, + registration_data_import=self, + ) + .exclude(status=GrievanceTicket.STATUS_CLOSED) + .count() + ) + self.save(update_fields=["golden_record_possible_duplicates"]) + def bulk_update_household_size(self) -> None: # AB#208387 if self.program and self.program.data_collecting_type.recalculate_composition: diff --git a/backend/hct_mis_api/apps/registration_datahub/celery_tasks.py b/backend/hct_mis_api/apps/registration_datahub/celery_tasks.py index 7ec1d9c18b..b79d90a9f7 100644 --- a/backend/hct_mis_api/apps/registration_datahub/celery_tasks.py +++ b/backend/hct_mis_api/apps/registration_datahub/celery_tasks.py @@ -389,14 +389,8 @@ def deduplicate_documents() -> bool: documents_query, registration_data_import=rdi, ) + rdi.update_needs_adjudication_tickets_statistic() - with transaction.atomic(): - documents_query = Document.objects.filter( - status=Document.STATUS_PENDING, individual__registration_data_import__isnull=True - ) - HardDocumentDeduplication().deduplicate( - documents_query, - ) return True diff --git a/backend/hct_mis_api/apps/registration_datahub/tasks/rdi_merge.py b/backend/hct_mis_api/apps/registration_datahub/tasks/rdi_merge.py index c1a6494c23..3815bec08f 100644 --- a/backend/hct_mis_api/apps/registration_datahub/tasks/rdi_merge.py +++ b/backend/hct_mis_api/apps/registration_datahub/tasks/rdi_merge.py @@ -323,6 +323,7 @@ def execute(self, registration_data_import_id: str) -> None: CheckAgainstSanctionListPreMergeTask.execute(registration_data_import=obj_hct) logger.info(f"RDI:{registration_data_import_id} Checked against sanction list") + obj_hct.update_needs_adjudication_tickets_statistic() obj_hct.status = RegistrationDataImport.MERGED obj_hct.save() diff --git a/backend/hct_mis_api/apps/registration_datahub/tests/test_handling_documents_duplicates.py b/backend/hct_mis_api/apps/registration_datahub/tests/test_handling_documents_duplicates.py index 93181e42fc..75f029788c 100644 --- a/backend/hct_mis_api/apps/registration_datahub/tests/test_handling_documents_duplicates.py +++ b/backend/hct_mis_api/apps/registration_datahub/tests/test_handling_documents_duplicates.py @@ -426,7 +426,9 @@ def test_hard_documents_deduplication_for_the_diff_program(self) -> None: new_document_from_other_program.refresh_from_db() self.assertEqual(new_document_from_other_program.status, Document.STATUS_PENDING) - HardDocumentDeduplication().deduplicate(self.get_documents_query([new_document_from_other_program])) + HardDocumentDeduplication().deduplicate( + self.get_documents_query([new_document_from_other_program]), self.registration_data_import + ) new_document_from_other_program.refresh_from_db() self.assertEqual(new_document_from_other_program.status, Document.STATUS_VALID)