FIX: private events stats should include possible invitees

FIX: private events stats should include possible invitees

diff --git a/app/serializers/discourse_post_event/event_serializer.rb b/app/serializers/discourse_post_event/event_serializer.rb
index e173bf2..db987b2 100644
--- a/app/serializers/discourse_post_event/event_serializer.rb
+++ b/app/serializers/discourse_post_event/event_serializer.rb
@@ -88,6 +88,16 @@ module DiscoursePostEvent
       not_going = counts[Invitee.statuses[:not_going]] || 0
       unanswered = counts[nil] || 0
 
+      # when a group is private we know the list of possible users
+      # even if an invitee has not been created yet
+      if object.private?
+        unanswered += GroupUser
+          .includes(:group, :user)
+          .where('groups.name' => object.raw_invitees)
+          .where.not('users.id' => object.invitees.select(:user_id))
+          .count
+      end
+
       {
         going: going,
         interested: interested,
diff --git a/spec/serializers/discourse_post_event/event_serializer_spec.rb b/spec/serializers/discourse_post_event/event_serializer_spec.rb
new file mode 100644
index 0000000..8c30084
--- /dev/null
+++ b/spec/serializers/discourse_post_event/event_serializer_spec.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+require_relative '../../fabricators/event_fabricator'
+
+describe DiscoursePostEvent::EventSerializer do
+  Event ||= DiscoursePostEvent::Event
+  Invitee ||= DiscoursePostEvent::Invitee
+  EventSerializer ||= DiscoursePostEvent::EventSerializer
+
+  before do
+    Jobs.run_immediately!
+    SiteSetting.calendar_enabled = true
+    SiteSetting.discourse_post_event_enabled = true
+  end
+
+  let(:post_1) { Fabricate(:post) }
+  let(:event_1) { Fabricate(:event, post: post_1, status: Event.statuses[:private]) }
+  let(:invitee_1) { Fabricate(:user) }
+  let(:invitee_2) { Fabricate(:user) }
+  let(:group_1) {
+    Fabricate(:group).tap do |g|
+      g.add(invitee_1)
+      g.add(invitee_2)
+      g.save!
+    end
+  }
+
+  context 'private event' do
+    context 'some invited users didnt rsvped yet' do
+      before do
+        event_1.update_with_params!(raw_invitees: [group_1.name])
+        Invitee.create_attendance!(invitee_1.id, event_1.id, :going)
+        event_1.reload
+      end
+
+      it 'returns the correct stats' do
+        json = EventSerializer.new(event_1, scope: Guardian.new).as_json
+        expect(json[:event][:stats]).to eq(going: 1, interested: 0, invited: 2, not_going: 0)
+      end
+    end
+  end
+end

GitHub sha: 830689d2