towerops/lib/towerops/organizations/invitation_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

33 lines
983 B
Elixir

defmodule Towerops.Organizations.InvitationQuery do
@moduledoc """
Composable query fragments for `Towerops.Organizations.Invitation`.
Lets the Organizations context build queries by piping filters together
instead of repeating `where: i.organization_id == ^...` clauses inline.
## Example
InvitationQuery.base()
|> InvitationQuery.for_organization(org_id)
|> InvitationQuery.pending(now)
|> Repo.all()
"""
import Ecto.Query
alias Towerops.Organizations.Invitation
@doc "Starting point for an Invitation query."
def base, do: Invitation
@doc "Filter to a single organization."
def for_organization(query \\ base(), organization_id) do
where(query, [i], i.organization_id == ^organization_id)
end
@doc "Filter to invitations not yet accepted and not expired at the given datetime."
def pending(query \\ base(), now) do
query
|> where([i], is_nil(i.accepted_at))
|> where([i], i.expires_at > ^now)
end
end