Browse Source

Fixes #4749 - Secondary organization members are not updated properly while deleting an organization.

Mantas Masalskis 1 year ago
parent
commit
7e48178487
2 changed files with 36 additions and 0 deletions
  1. 5 0
      app/models/organization.rb
  2. 31 0
      spec/jobs/data_privacy_task_job_spec.rb

+ 5 - 0
app/models/organization.rb

@@ -51,7 +51,12 @@ class Organization < ApplicationModel
     else
       unset_associations
     end
+
+    secondary_members_to_touch = secondary_members.records
+
     super()
+
+    secondary_members_to_touch.each(&:touch)
   end
 
   def attributes_with_association_ids

+ 31 - 0
spec/jobs/data_privacy_task_job_spec.rb

@@ -51,6 +51,37 @@ RSpec.describe DataPrivacyTaskJob, type: :job do
       expect { organization.reload }.to raise_error(ActiveRecord::RecordNotFound)
     end
 
+    it 'checks if the organization is not deleted (delete_organization=true) if another user was added while task is queued' do
+      create(:data_privacy_task, deletable: user, preferences: { delete_organization: 'true' })
+
+      another_user = create(:user)
+      another_user.update! organization: organization
+
+      described_class.perform_now
+      expect(organization.reload).to be_present
+    end
+
+    it 'checks if the organization is removed from secondary organizations for users when deleted (delete_organization=true)', aggregate_failures: true do
+      create(:data_privacy_task, deletable: user, preferences: { delete_organization: 'true' })
+
+      another_user = create(:user)
+      another_user.organizations << organization
+
+      another_user.assets({}) # make sure cache exists
+
+      described_class.perform_now
+      expect { organization.reload }.to raise_error(ActiveRecord::RecordNotFound)
+
+      expect(another_user.assets({}))
+        .not_to include(
+          User: include(
+            another_user.id => include(
+              'organization_ids' => include(organization.id)
+            )
+          )
+        )
+    end
+
     it 'checks creation of activity stream log' do
       create(:data_privacy_task, deletable: user, created_by: admin)
       travel 15.minutes