From 7a6039806b61a0d50ab7f3a4bc7593fa615d1520 Mon Sep 17 00:00:00 2001 From: sniedzielski Date: Thu, 21 Nov 2024 14:15:22 +0100 Subject: [PATCH] OP-2163: added possibility to configure check for delete operation --- individual/apps.py | 2 ++ individual/gql_mutations.py | 6 +++++- individual/services.py | 7 ++++++- individual/tests/group_service_test.py | 28 ++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/individual/apps.py b/individual/apps.py index 2ef346e..a63b4ff 100644 --- a/individual/apps.py +++ b/individual/apps.py @@ -21,6 +21,7 @@ "check_individual_delete": True, "check_group_individual_update": True, "check_group_create": True, + "check_group_delete": True, "individual_schema": "{}", "individual_accept_enrolment": "individual_service.create_accept_enrolment_task", "validation_import_valid_items_workflow": "individual-import-valid-items", @@ -61,6 +62,7 @@ class IndividualConfig(AppConfig): check_individual_delete = None check_group_individual_update = None check_group_create = None + check_group_delete = None python_individual_import_workflow_group = None python_individual_import_workflow_name = None individual_schema = None diff --git a/individual/gql_mutations.py b/individual/gql_mutations.py index 5a3a219..eb35fb8 100644 --- a/individual/gql_mutations.py +++ b/individual/gql_mutations.py @@ -337,7 +337,11 @@ def _mutate(cls, user, **data): if ids: with transaction.atomic(): for identifier in ids: - service.delete({'id': identifier}) + obj_data = {'id': identifier} + if IndividualConfig.check_group_delete: + service.create_delete_task(obj_data) + else: + service.delete(obj_data) class Input(OpenIMISMutation.Input): ids = graphene.List(graphene.UUID) diff --git a/individual/services.py b/individual/services.py index 573328d..9354f4f 100644 --- a/individual/services.py +++ b/individual/services.py @@ -149,7 +149,12 @@ def __init__(self, user, validation_class=IndividualDataSourceValidation): super().__init__(user, validation_class) -class GroupService(BaseService, CreateCheckerLogicServiceMixin, UpdateCheckerLogicServiceMixin): +class GroupService( + BaseService, + CreateCheckerLogicServiceMixin, + UpdateCheckerLogicServiceMixin, + DeleteCheckerLogicServiceMixin +): OBJECT_TYPE = Group def __init__(self, user, validation_class=GroupValidation): diff --git a/individual/tests/group_service_test.py b/individual/tests/group_service_test.py index 8e2da76..a9ebfa5 100644 --- a/individual/tests/group_service_test.py +++ b/individual/tests/group_service_test.py @@ -118,6 +118,34 @@ def test_update_group_individuals(self): # self.assertFalse(individual2.id in individual_ids) self.assertTrue(individual3.id in individual_ids) + def test_delete_group_with_individual(self): + individual1 = self.__create_individual() + individual2 = self.__create_individual() + individual3 = self.__create_individual() + payload_individuals = { + 'code': str(datetime.now()), + 'individuals_data': [ + {'individual_id': str(individual1.id)}, + {'individual_id': str(individual2.id)}, + ] + } + result = self.service.create(payload_individuals) + self.assertTrue(result.get('success', False), result.get('detail', "No details provided")) + uuid = result.get('data', {}).get('uuid', None) + query = self.query_all.filter(uuid=uuid) + group = query.first() + self.assertEqual(query.count(), 1) + self.assertEqual(str(group.id), uuid) + group_individual_query = self.group_individual_query_all.filter(group=group) + self.assertEqual(group_individual_query.count(), 2) + delete_payload = {'id': uuid} + result = self.service.delete(delete_payload) + self.assertTrue(result.get('success', False), result.get('detail', "No details provided")) + query = self.query_all.filter(uuid=uuid) + self.assertEqual(query.count(), 0) + group_individual_query = self.group_individual_query_all.filter(group=group) + self.assertEqual(group_individual_query.count(), 0) + @classmethod def __create_individual(cls): object_data = {