fix device count

This commit is contained in:
Graham McIntire 2026-02-05 15:04:34 -06:00
parent 156d9a47bf
commit 5342be99db
No known key found for this signature in database
2 changed files with 46 additions and 3 deletions

View file

@ -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
)

View file

@ -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