FIX: prevents updates to private events to reset invitees

FIX: prevents updates to private events to reset invitees

diff --git a/app/models/discourse_post_event/event.rb b/app/models/discourse_post_event/event.rb
index 8f2fff7..573334d 100644
--- a/app/models/discourse_post_event/event.rb
+++ b/app/models/discourse_post_event/event.rb
@@ -2,6 +2,8 @@
 
 module DiscoursePostEvent
   class Event < ActiveRecord::Base
+    PUBLIC_GROUP = 'trust_level_0'
+
     self.table_name = 'discourse_post_event_events'
 
     def self.attributes_protected_by_default
@@ -255,11 +257,11 @@ module DiscoursePostEvent
 
       case params[:status] ? params[:status].to_i : self.status
       when Event.statuses[:private]
-        raw_invitees = Array(params[:raw_invitees]) - ['trust_level_0']
+        raw_invitees = Set.new(Array(self.raw_invitees) + Array(params[:raw_invitees]) - [PUBLIC_GROUP]).to_a
         self.update!(params.merge(raw_invitees: raw_invitees))
         self.enforce_private_invitees!
       when Event.statuses[:public]
-        self.update!(params.merge(raw_invitees: ['trust_level_0']))
+        self.update!(params.merge(raw_invitees: [PUBLIC_GROUP]))
       when Event.statuses[:standalone]
         self.update!(params.merge(raw_invitees: []))
         self.invitees.destroy_all
diff --git a/spec/models/discourse_post_event/event_spec.rb b/spec/models/discourse_post_event/event_spec.rb
index eb3590e..c6c99d3 100644
--- a/spec/models/discourse_post_event/event_spec.rb
+++ b/spec/models/discourse_post_event/event_spec.rb
@@ -304,6 +304,47 @@ describe DiscoursePostEvent::Event do
     end
   end
 
+  context '#update_with_params!' do
+    let!(:post_1) { Fabricate(:post) }
+    let!(:user_1) { Fabricate(:user) }
+    let(:group_1) {
+      Fabricate(:group).tap do |g|
+        g.add(user_1)
+        g.save!
+      end
+    }
+
+    before do
+      freeze_time
+    end
+
+    context 'private event' do
+      let!(:event_1) {
+        Fabricate(:event, post: post_1, status: Event.statuses[:private], raw_invitees: [group_1.name])
+      }
+
+      before do
+        freeze_time
+
+        event_1.create_invitees([
+          { user_id: user_1.id, status: 0 }
+        ])
+      end
+
+      context 'updating the name' do
+        it 'doesn’t clear existing invitees' do
+          expect(event_1.invitees.count).to eq(1)
+
+          expect {
+            event_1.update_with_params!(name: 'The event')
+          }.to change {
+            event_1.invitees.count
+          }.by(0)
+        end
+      end
+    end
+  end
+
   context 'reminders callbacks' do
     let!(:post_1) { Fabricate(:post) }
     let!(:event_1) { Fabricate(:event, post: post_1) }

GitHub sha: e8c06493