From 5342be99dbc7f4fd08f52b9c276781bb1240bad7 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Thu, 5 Feb 2026 15:04:34 -0600 Subject: [PATCH] fix device count --- .../organizations/subscription_limits.ex | 5 +-- .../subscription_limits_test.exs | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/towerops/organizations/subscription_limits.ex b/lib/towerops/organizations/subscription_limits.ex index 2b84c255..950bc75f 100644 --- a/lib/towerops/organizations/subscription_limits.ex +++ b/lib/towerops/organizations/subscription_limits.ex @@ -129,13 +129,12 @@ defmodule Towerops.Organizations.SubscriptionLimits do @doc """ Counts total devices for an organization. - Counts devices across all sites in the organization. + Counts all devices belonging to the organization, whether assigned to a site or not. """ def count_organization_devices(organization_id) do Repo.aggregate( from(d in Device, - join: s in assoc(d, :site), - where: s.organization_id == ^organization_id + where: d.organization_id == ^organization_id ), :count ) diff --git a/test/towerops/organizations/subscription_limits_test.exs b/test/towerops/organizations/subscription_limits_test.exs index 5904782f..ef99cd08 100644 --- a/test/towerops/organizations/subscription_limits_test.exs +++ b/test/towerops/organizations/subscription_limits_test.exs @@ -191,6 +191,34 @@ defmodule Towerops.Organizations.SubscriptionLimitsTest do assert SubscriptionLimits.count_organization_devices(organization.id) == 8 end + + test "counts devices without site assignment when sites are disabled", %{ + organization: organization + } do + # Create devices directly assigned to organization (no site) + for i <- 1..5 do + create_device_without_site(organization.id, "Device #{i}", "192.168.1.#{i}") + end + + assert SubscriptionLimits.count_organization_devices(organization.id) == 5 + end + + test "counts both site-assigned and unassigned devices", %{ + organization: organization, + site: site + } do + # Create 3 devices assigned to site + for i <- 1..3 do + create_device(site.id, "Site Device #{i}", "192.168.1.#{i}") + end + + # Create 2 devices without site assignment + for i <- 4..5 do + create_device_without_site(organization.id, "Org Device #{i}", "192.168.1.#{i}") + end + + assert SubscriptionLimits.count_organization_devices(organization.id) == 5 + end end # Helper functions @@ -213,4 +241,20 @@ defmodule Towerops.Organizations.SubscriptionLimitsTest do device end + + defp create_device_without_site(organization_id, name, ip_address) do + {:ok, device} = + Devices.create_device( + %{ + name: name, + ip_address: ip_address, + organization_id: organization_id, + monitoring_enabled: false, + snmp_enabled: false + }, + bypass_limits: true + ) + + device + end end