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.
35 lines
939 B
Elixir
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
|