towerops/lib/towerops/on_call/schedule_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

35 lines
939 B
Elixir

defmodule Towerops.OnCall.ScheduleQuery do
@moduledoc """
Composable query fragments for `Towerops.OnCall.Schedule`.
Lets the OnCall context build queries by piping filters together instead of
repeating `where: s.organization_id == ^...` clauses inline.
## Example
ScheduleQuery.base()
|> ScheduleQuery.for_organization(org_id)
|> Repo.all()
"""
import Ecto.Query
alias Towerops.OnCall.Schedule
@doc "Starting point for a Schedule query."
def base, do: Schedule
@doc "Filter to a single organization."
def for_organization(query \\ base(), organization_id) do
where(query, [s], s.organization_id == ^organization_id)
end
@doc "Filter by id within an organization."
def with_id(query \\ base(), id) do
where(query, [s], s.id == ^id)
end
@doc "Default ordering: alphabetical by name."
def order_by_name(query \\ base()) do
order_by(query, [s], asc: s.name)
end
end