FIX: ensures a destroyed user is correctly removed from invitees

FIX: ensures a destroyed user is correctly removed from invitees

diff --git a/plugin.rb b/plugin.rb
index bde348d..0e61dda 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -470,5 +470,9 @@ after_initialize do
         end
       end
     end
+
+    on(:user_destroyed) do |user|
+      DiscoursePostEvent::Invitee.where(user_id: user.id).destroy_all
+    end
   end
 end
diff --git a/spec/acceptance/invitee_spec.rb b/spec/acceptance/invitee_spec.rb
new file mode 100644
index 0000000..93f0426
--- /dev/null
+++ b/spec/acceptance/invitee_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+require_relative '../fabricators/event_fabricator'
+
+describe DiscoursePostEvent::Invitee do
+  before do
+    freeze_time
+    SiteSetting.queue_jobs = false
+    SiteSetting.calendar_enabled = true
+    SiteSetting.discourse_post_event_enabled = true
+  end
+
+  let(:user) { Fabricate(:user, admin: true) }
+  let(:user_1) { Fabricate(:user) }
+  let(:topic) { Fabricate(:topic, user: user) }
+  let(:post1) { Fabricate(:post, topic: topic) }
+  let(:post_event) { Fabricate(:event, post: post1) }
+
+  context 'A user is destroyed' do
+    context 'The user is an invitee to an event' do
+      before do
+        post_event.create_invitees([
+          { user_id: user_1.id, status: nil }
+        ])
+      end
+
+      it 'destroys the invitee' do
+        expect(post_event.invitees.first.user.id).to eq(user_1.id)
+
+        UserDestroyer.new(user_1).destroy(user_1)
+
+        expect(post_event.invitees).to be_empty
+      end
+    end
+  end
+end

GitHub sha: b95380a1