FIX: ensures we are updating the correct invitee

FIX: ensures we are updating the correct invitee

diff --git a/app/controllers/discourse_post_event/invitees_controller.rb b/app/controllers/discourse_post_event/invitees_controller.rb
index b7b557b..1335802 100644
--- a/app/controllers/discourse_post_event/invitees_controller.rb
+++ b/app/controllers/discourse_post_event/invitees_controller.rb
@@ -10,19 +10,32 @@ module DiscoursePostEvent
       if params[:filter]
         event_invitees = event_invitees
           .joins(:user)
-          .where("LOWER(users.username) LIKE :filter", filter: "%#{params[:filter].downcase}%")
+          .where('LOWER(users.username) LIKE :filter', filter: "%#{params[:filter].downcase}%")
       end
 
-      render json: ActiveModel::ArraySerializer.new(event_invitees.order([:status, :user_id]).limit(10), each_serializer: InviteeSerializer).as_json
+      render json: ActiveModel::ArraySerializer.new(
+        event_invitees
+          .order([:status, :user_id])
+          .limit(10),
+        each_serializer: InviteeSerializer
+      ).as_json
     end
 
     def update
-      invitee = Invitee.upsert_attendance!(params[:id], invitee_params, guardian)
+      invitee = Invitee.update_attendance!(
+        params[:id],
+        invitee_params.tap { |ip| ip.require(:status) },
+        guardian
+      )
       render json: InviteeSerializer.new(invitee)
     end
 
     def create
-      invitee = Invitee.upsert_attendance!(current_user.id, invitee_params, guardian)
+      invitee = Invitee.create_attendance!(
+        current_user.id,
+        invitee_params.tap { |ip| ip.require([:status, :post_id]) },
+        guardian
+      )
       render json: InviteeSerializer.new(invitee)
     end
 
diff --git a/app/models/discourse_post_event/invitee.rb b/app/models/discourse_post_event/invitee.rb
index 137db77..684c66f 100644
--- a/app/models/discourse_post_event/invitee.rb
+++ b/app/models/discourse_post_event/invitee.rb
@@ -21,29 +21,28 @@ module DiscoursePostEvent
       @statuses ||= Enum.new(going: 0, interested: 1, not_going: 2)
     end
 
-    def update_attendance(params)
-      self.update!(params)
+    def self.create_attendance!(user_id, params, guardian)
+      event = Event.find(params[:post_id])
+      guardian.ensure_can_see!(event.post)
+      invitee = Invitee.create!(
+        status: Invitee.statuses[params[:status].to_sym],
+        post_id: params[:post_id],
+        user_id: user_id,
+      )
+      invitee.event.publish_update!
+      invitee
     end
 
-    def self.upsert_attendance!(user_id, params, guardian)
-      invitee = Invitee.find_by(id: user_id)
-      status = Invitee.statuses[params[:status].to_sym]
-
-      if invitee
-        guardian.ensure_can_act_on_invitee!(invitee)
-        invitee.update_attendance(status: status)
-      else
-        event = Event.find(params[:post_id])
-        guardian.ensure_can_see!(event.post)
-        invitee = Invitee.create!(
-          status: status,
-          post_id: params[:post_id],
-          user_id: user_id,
-        )
-      end
+    def update_attendance!(params, guardian)
+      guardian.ensure_can_act_on_invitee!(self)
+      self.update(status: Invitee.statuses[params[:status].to_sym])
+      self.event.publish_update!
+      self
+    end
 
-      invitee.event.publish_update!
-      invitee
+    def self.update_attendance!(invitee_id, params, guardian)
+      invitee = Invitee.find(invitee_id)
+      invitee.update_attendance!(params, guardian)
     end
 
     def self.extract_uniq_usernames(groups)
diff --git a/assets/javascripts/discourse/widgets/discourse-post-event.js.es6 b/assets/javascripts/discourse/widgets/discourse-post-event.js.es6
index 0fec61c..ec19914 100644
--- a/assets/javascripts/discourse/widgets/discourse-post-event.js.es6
+++ b/assets/javascripts/discourse/widgets/discourse-post-event.js.es6
@@ -57,10 +57,7 @@ export default createWidget("discourse-post-event", {
       this.store.update(
         "discourse-post-event-invitee",
         this.state.eventModel.watching_invitee.id,
-        {
-          status,
-          post_id: this.state.eventModel.id
-        }
+        { status }
       );
     } else {
       this.store
diff --git a/spec/requests/invitees_controller_spec.rb b/spec/requests/invitees_controller_spec.rb
index e155a0c..4003a0c 100644
--- a/spec/requests/invitees_controller_spec.rb
+++ b/spec/requests/invitees_controller_spec.rb
@@ -13,14 +13,14 @@ module DiscoursePostEvent
     end
 
     let(:user) { Fabricate(:user, admin: true) }
-    let(:topic) { Fabricate(:topic, user: user) }
-    let(:post1) { Fabricate(:post, user: user, topic: topic) }
+    let(:topic_1) { Fabricate(:topic, user: user) }
+    let(:post_1) { Fabricate(:post, user: user, topic: topic_1) }
 
     context 'when a post event exists' do
       context 'when an invitee exists' do
         let(:invitee1) { Fabricate(:user) }
-        let(:post_event) {
-          pe = Fabricate(:event, post: post1)
+        let(:post_event_2) {
+          pe = Fabricate(:event, post: post_1)
           pe.create_invitees([{
             user_id: invitee1.id,
             status: Invitee.statuses[:going]
@@ -29,58 +29,60 @@ module DiscoursePostEvent
         }
 
         it 'updates its status' do
-          invitee = post_event.invitees.first
+          invitee = post_event_2.invitees.first
 
           expect(invitee.status).to eq(0)
 
           put "/discourse-post-event/invitees/#{invitee.id}.json", params: {
             invitee: {
-              status: "interested"
+              status: 'interested'
             }
           }
 
           invitee.reload
 
           expect(invitee.status).to eq(1)
+          expect(invitee.post_id).to eq(post_1.id)
         end
       end
 
       context 'when an invitee doesn’t exist' do
-        let(:post_event) { Fabricate(:event, post: post1) }
+        let(:post_event_2) { Fabricate(:event, post: post_1) }
 
         it 'creates an invitee' do
           post "/discourse-post-event/invitees.json", params: {
             invitee: {
               user_id: user.id,
-              post_id: post_event.id,
-              status: "not_going",
+              post_id: post_event_2.id,
+              status: 'not_going',
             }
           }
 
           expect(Invitee).to exist(
-            post_id: post_event.id,
+            post_id: post_event_2.id,
             user_id: user.id,
             status: 2,
           )
         end
 
         context 'when the invitee is the event owner' do
-          let(:post_event) { Fabricate(:event, post: post1) }
+          let(:post_event_2) { Fabricate(:event, post: post_1) }
 
           it 'creates an invitee' do
-            expect(post_event.invitees.length).to eq(0)
+            expect(post_event_2.invitees.length).to eq(0)
 
-            put "/discourse-post-event/invitees/#{post1.user.id}.json", params: {
+            post "/discourse-post-event/invitees.json", params: {
               invitee: {
-                post_id: post1.id,
-                status: "interested"
+                post_id: post_1.id,
+                status: 'interested'
               }
             }
 
-            post_event.reload
+            post_event_2.reload
 
-            invitee = post_event.invitees.first
+            invitee = post_event_2.invitees.first
             expect(invitee.status).to eq(1)
+            expect(invitee.post_id).to eq(post_1.id)
           end
         end
       end

GitHub sha: ea0dd410