diff --git a/app/models/identity.rb b/app/models/identity.rb index 7495e37c3..18f4e3163 100644 --- a/app/models/identity.rb +++ b/app/models/identity.rb @@ -28,6 +28,10 @@ class Identity < ApplicationRecord end end + def users_with_active_accounts + users.joins(:account).merge(Account.active).includes(:account) + end + private def deactivate_users users.find_each(&:deactivate) diff --git a/app/views/my/identities/show.json.jbuilder b/app/views/my/identities/show.json.jbuilder index 090e97dfe..8db250846 100644 --- a/app/views/my/identities/show.json.jbuilder +++ b/app/views/my/identities/show.json.jbuilder @@ -1,6 +1,6 @@ json.id @identity.id -json.accounts @identity.users do |user| +json.accounts @identity.users_with_active_accounts do |user| json.partial! "my/identities/account", account: user.account json.user user, partial: "users/user", as: :user end diff --git a/test/controllers/my/identities_controller_test.rb b/test/controllers/my/identities_controller_test.rb index 392e9b547..3771d5396 100644 --- a/test/controllers/my/identities_controller_test.rb +++ b/test/controllers/my/identities_controller_test.rb @@ -7,12 +7,35 @@ class My::IdentitiesControllerTest < ActionDispatch::IntegrationTest test "show as JSON" do identity = identities(:kevin) + expected_count = identity.users_with_active_accounts.count untenanted do get my_identity_path, as: :json assert_response :success assert_equal identity.id, @response.parsed_body["id"] - assert_equal identity.accounts.count, @response.parsed_body["accounts"].count + assert_equal expected_count, @response.parsed_body["accounts"].count + end + end + + test "show as JSON includes users from active accounts only" do + identity = identities(:kevin) + + active_account = Account.create!(external_account_id: 9999981, name: "Active Account") + cancelled_account = Account.create!(external_account_id: 9999982, name: "Cancelled Account") + + identity.users.create!(account: active_account, name: "Kevin", role: :owner) + + cancelling_user = identity.users.create!(account: cancelled_account, name: "Kevin", role: :owner) + cancelled_account.cancel(initiated_by: cancelling_user) + + untenanted do + get my_identity_path, as: :json + assert_response :success + + account_ids = @response.parsed_body["accounts"].map { |account| account["id"] } + + assert_includes account_ids, active_account.id + assert_not_includes account_ids, cancelled_account.id end end end