towerops/lib/towerops/agents/agent_token_query.ex
Graham McIntire 2528e78ff9 refactor: extract per-schema Query modules for organization_id filters
Adds 7 new composable Query modules following the existing
DeviceQuery/AlertQuery pattern. Each exposes base/0 and for_organization/1,2
plus filters used by 2+ callers.

New modules:
- Towerops.OnCall.ScheduleQuery
- Towerops.OnCall.EscalationPolicyQuery
- Towerops.Maintenance.MaintenanceWindowQuery
- Towerops.Organizations.MembershipQuery
- Towerops.Organizations.InvitationQuery
- Towerops.Agents.AgentTokenQuery
- Towerops.Gaiia.DeviceSubscriberLinkQuery

Refactors callsites in alerts, agents, devices, gaiia, maintenance, on_call,
organizations, search, and subscription_limits to use existing or new Query
modules instead of inline 'where: x.organization_id == ^...' clauses.
2026-04-30 14:26:30 -05:00

36 lines
1 KiB
Elixir

defmodule Towerops.Agents.AgentTokenQuery do
@moduledoc """
Composable query fragments for `Towerops.Agents.AgentToken`.
Lets context modules build queries by piping filters together instead of
repeating `where: t.organization_id == ^...` clauses inline.
## Example
AgentTokenQuery.base()
|> AgentTokenQuery.for_organization(org_id)
|> AgentTokenQuery.enabled()
|> Repo.all()
"""
import Ecto.Query
alias Towerops.Agents.AgentToken
@doc "Starting point for an AgentToken query."
def base, do: AgentToken
@doc "Filter to a single organization."
def for_organization(query \\ base(), organization_id) do
where(query, [t], t.organization_id == ^organization_id)
end
@doc "Filter to organization-scoped tokens (excludes cloud pollers)."
def excluding_cloud_pollers(query \\ base()) do
where(query, [t], t.is_cloud_poller == false)
end
@doc "Filter to enabled tokens only."
def enabled(query \\ base()) do
where(query, [t], t.enabled == true)
end
end