UX: Only show assigned tab for groups where all users can assign (#103)

UX: Only show assigned tab for groups where all users can assign (#103)

diff --git a/assets/javascripts/discourse-assign/connectors/group-reports-nav-item/assigned-topic-list.js.es6 b/assets/javascripts/discourse-assign/connectors/group-reports-nav-item/assigned-topic-list.js.es6
index 4d9d836..ace6464 100644
--- a/assets/javascripts/discourse-assign/connectors/group-reports-nav-item/assigned-topic-list.js.es6
+++ b/assets/javascripts/discourse-assign/connectors/group-reports-nav-item/assigned-topic-list.js.es6
@@ -3,6 +3,7 @@ export default {
     return (
       component.currentUser &&
       component.currentUser.can_assign &&
+      args.group.can_show_assigned_tab &&
       args.group.assignment_count > 0
     );
   }
diff --git a/plugin.rb b/plugin.rb
index 95c0910..69dba46 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -51,6 +51,10 @@ after_initialize do
     scope.can_assign?
   end
 
+  add_to_serializer(:group_show, :can_show_assigned_tab?) do
+    object.can_show_assigned_tab?
+  end
+
   add_model_callback(UserCustomField, :before_save) do
     self.value = self.value.to_i if self.name == frequency_field
   end
@@ -71,6 +75,25 @@ after_initialize do
     @can_assign == :true
   end
 
+  add_to_class(:group, :can_show_assigned_tab?) do
+    allowed_group_ids = SiteSetting.assign_allowed_on_groups.split("|")
+
+    group_has_disallowed_users = DB.query_single(<<~SQL, allowed_group_ids: allowed_group_ids, current_group_id: self.id)[0]
+      SELECT EXISTS(
+        SELECT 1 FROM users
+        JOIN group_users current_group_users
+          ON current_group_users.user_id=users.id
+          AND current_group_users.group_id = :current_group_id
+        LEFT JOIN group_users allowed_group_users
+          ON allowed_group_users.user_id=users.id
+          AND allowed_group_users.group_id IN (:allowed_group_ids)
+        WHERE allowed_group_users.user_id IS NULL
+      )
+    SQL
+
+    !group_has_disallowed_users
+  end
+
   add_to_class(:guardian, :can_assign?) { user && user.can_assign? }
 
   add_class_method(:user, :assign_allowed) do
@@ -301,6 +324,7 @@ after_initialize do
 
     raise Discourse::NotFound unless group
     raise Discourse::InvalidAccess unless current_user.can_assign?
+    raise Discourse::InvalidAccess unless group.can_show_assigned_tab?
 
     list_opts = build_topic_list_options
     list_opts[:page] = page
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb
index 72b8a9b..059b35e 100644
--- a/spec/models/group_spec.rb
+++ b/spec/models/group_spec.rb
@@ -1,14 +1,16 @@
 # frozen_string_literal: true
 
 require 'rails_helper'
+require_relative '../support/assign_allowed_group'
 
 RSpec.describe Group do
-  describe 'Tracking changes that could affect the allow assign on groups site setting' do
-    let(:group) { Fabricate(:group) }
+  let(:group) { Fabricate(:group) }
 
-    before do
-      SiteSetting.assign_enabled = true
-    end
+  before do
+    SiteSetting.assign_enabled = true
+  end
+
+  context 'Tracking changes that could affect the allow assign on groups site setting' do
 
     let(:removed_group_setting) { '3|4' }
     let(:group_attribute) { group.id }
@@ -38,4 +40,34 @@ RSpec.describe Group do
       expect(SiteSetting.assign_allowed_on_groups).to eq removed_group_setting
     end
   end
+
+  context 'includes can_show_assigned_tab? method' do
+    let(:admin) { Fabricate(:admin) }
+    let(:user) { Fabricate(:user) }
+    let(:user1) { Fabricate(:user) }
+    let(:user2) { Fabricate(:user) }
+
+    include_context 'A group that is allowed to assign'
+
+    before do
+      add_to_assign_allowed_group(user)
+      add_to_assign_allowed_group(user1)
+      add_to_assign_allowed_group(admin)
+    end
+
+    it 'gives false in can_show_assigned_tab? when all users are not in assigned_allowed_group' do
+      group.add(user)
+      group.add(user1)
+      group.add(user2)
+
+      expect(group.can_show_assigned_tab?).to eq(false)
+    end
+
+    it 'gives true in can_show_assigned_tab? when all users are in assigned_allowed_group' do
+      group.add(user)
+      group.add(user1)
+
+      expect(group.can_show_assigned_tab?).to eq(true)
+    end
+  end
 end

GitHub sha: cd31a821

This commit appears in #103 which was merged by davidtaylorhq.