From 1ad618220d345b0ea71b535fef32e8f2b5062482 Mon Sep 17 00:00:00 2001 From: Sushil Tiwari Date: Fri, 20 Sep 2024 10:28:58 +0545 Subject: [PATCH] Implement graphql mutation node for delete lead in bulk (#1460) Add mutation for bulk delete lead * remove the count from login --- apps/lead/mutation.py | 1 + apps/lead/tests/test_mutations.py | 63 +++++++++++++++++++++++++++++++ schema.graphql | 2 +- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/apps/lead/mutation.py b/apps/lead/mutation.py index 122a7b0c3d..a9b3a5d605 100644 --- a/apps/lead/mutation.py +++ b/apps/lead/mutation.py @@ -92,6 +92,7 @@ class BulkLeadInputType(LeadInputType): class BulkLead(LeadMutationMixin, PsBulkGrapheneMutation): class Arguments: items = graphene.List(graphene.NonNull(BulkLeadInputType)) + delete_ids = graphene.List(graphene.NonNull(graphene.ID)) result = graphene.List(LeadType) deleted_result = graphene.List(graphene.NonNull(LeadType)) diff --git a/apps/lead/tests/test_mutations.py b/apps/lead/tests/test_mutations.py index 68dcbcd40f..5fc4f4d191 100644 --- a/apps/lead/tests/test_mutations.py +++ b/apps/lead/tests/test_mutations.py @@ -389,6 +389,69 @@ def _query_check(**kwargs): self.assertMatchSnapshot(response, 'success') self.assertEqual(lead_count + 2, Lead.objects.count()) + def test_lead_bulk_delete_validation(self): + query = ''' + mutation MyMutation ($projectId: ID! $input: [ID!]) { + project(id: $projectId) { + leadBulk(deleteIds: $input) { + errors + deletedResult { + id + title + } + } + } + } + ''' + + project = ProjectFactory.create() + non_member_user = UserFactory.create() + read_only_member_user = UserFactory.create() + member_user = UserFactory.create() + project.add_member(member_user, role=self.project_role_member) + project.add_member(read_only_member_user, role=self.project_role_reader_non_confidential) + + lead1, lead2, _ = LeadFactory.create_batch(3, project=project) + + lead_count = Lead.objects.count() + + minput = [ + str(lead1.pk), + str(lead2.pk), + ] + + def _query_check(**kwargs): + return self.query_check(query, minput=minput, variables={'projectId': project.pk}, **kwargs) + + # Error without login + _query_check(assert_for_error=True) + + # -- With login (non-member) + self.force_login(non_member_user) + _query_check(assert_for_error=True) + self.assertEqual(lead_count, Lead.objects.count()) + + # --- login as read-only member + self.force_login(read_only_member_user) + _query_check(assert_for_error=True) + self.assertEqual(lead_count, Lead.objects.count()) + + # --- login (member) + self.force_login(member_user) + # Success with normal lead (with project membership) + result = _query_check()['data']['project']['leadBulk'] + self.assertEqual( + result, + { + 'errors': [], + 'deletedResult': [ + dict(id=str(lead1.pk), title=lead1.title), + dict(id=str(lead2.pk), title=lead2.title), + ], + }, + ) + self.assertEqual(1, Lead.objects.count()) + class TestLeadGroupMutation(GraphQLTestCase): def test_lead_group_delete(self): diff --git a/schema.graphql b/schema.graphql index d9bcc791c0..27834267fe 100644 --- a/schema.graphql +++ b/schema.graphql @@ -5488,7 +5488,7 @@ type ProjectMutationType { leadCreate(data: LeadInputType!): CreateLead leadUpdate(data: LeadInputType!, id: ID!): UpdateLead leadDelete(id: ID!): DeleteLead - leadBulk(items: [BulkLeadInputType!]): BulkLead + leadBulk(deleteIds: [ID!], items: [BulkLeadInputType!]): BulkLead leadGroupDelete(id: ID!): DeleteLeadGroup leadCopy(data: LeadCopyInputType!): LeadCopy leadFilterSave(data: UserSavedLeadFilterInputType!): SaveUserSavedLeadFilter