towerops/lib/towerops/integrations.ex
Graham McIntire 8379664cb0 fix: 3 more bugs (M17, M23, L9)
- M17: default SNMPv3 auth protocol on the four SNMP executors
  (interface/storage/processor/sensor) is now SHA-256 instead of MD5.
  Devices that explicitly set MD5 still use it; only the fallback for
  unconfigured devices changes.
- M23: permissions check now logs a warning when an org's memberships
  aren't preloaded so a silent denial is visible at the call site
  instead of looking identical to a real authz failure.
- L9: extracted the duplicated should_sync?/1 logic from gaiia, sonar,
  splynx, visp, and netbox sync workers into
  Towerops.Integrations.due_for_sync?/2. Each worker now passes its
  own default interval (10 or 30 minutes) to a single shared helper.
  Tests updated to cover the helper directly.
2026-05-12 11:45:22 -05:00

117 lines
3.3 KiB
Elixir

defmodule Towerops.Integrations do
@moduledoc """
Context for managing third-party integrations per organization.
"""
import Ecto.Query
alias Towerops.Integrations.Integration
alias Towerops.Repo
@doc """
Returns true if the integration is due for another sync. Used by the
per-vendor cron dispatcher workers so the same interval check doesn't
get rewritten in each one.
`default_interval_minutes` is used when the integration has no explicit
`sync_interval_minutes` value.
"""
@spec due_for_sync?(map(), pos_integer()) :: boolean()
def due_for_sync?(integration, default_interval_minutes \\ 10) do
case integration.last_synced_at do
nil ->
true
last_synced_at ->
interval_seconds = (integration.sync_interval_minutes || default_interval_minutes) * 60
elapsed = DateTime.diff(DateTime.utc_now(), last_synced_at, :second)
elapsed >= interval_seconds
end
end
def list_integrations(organization_id) do
Integration
|> where(organization_id: ^organization_id)
|> order_by(:provider)
|> Repo.all()
end
def get_integration(organization_id, provider) do
case Repo.get_by(Integration, organization_id: organization_id, provider: provider) do
nil -> {:error, :not_found}
integration -> {:ok, integration}
end
end
def get_integration!(organization_id, provider) do
Repo.get_by!(Integration, organization_id: organization_id, provider: provider)
end
def create_integration(organization_id, attrs) do
%Integration{}
|> Integration.changeset(Map.put(attrs, :organization_id, organization_id))
|> Repo.insert()
end
def update_integration(%Integration{} = integration, attrs) do
integration
|> Integration.changeset(attrs)
|> Repo.update()
end
def update_sync_status(%Integration{} = integration, status, message \\ nil) do
integration
|> Integration.changeset(%{
last_sync_status: status,
last_synced_at: Towerops.Time.now(),
last_sync_message: message
})
|> Repo.update()
end
def delete_integration(%Integration{} = integration) do
Repo.delete(integration)
end
def change_integration(%Integration{} = integration, attrs \\ %{}) do
Integration.changeset(integration, attrs)
end
def get_integration_by_id(id) do
case Repo.get(Integration, id) do
nil -> {:error, :not_found}
integration -> {:ok, integration}
end
end
@billing_providers ~w(gaiia splynx visp sonar)
def update_billing_totals(%Integration{} = integration, subscriber_count, total_mrr) do
integration
|> Integration.changeset(%{subscriber_count: subscriber_count, total_mrr: total_mrr})
|> Repo.update()
end
def get_billing_summary(organization_id) do
result =
Integration
|> where(organization_id: ^organization_id)
|> where([i], i.provider in @billing_providers)
|> where(enabled: true)
|> select([i], %{
total_subscribers: coalesce(sum(i.subscriber_count), 0),
total_mrr: coalesce(sum(i.total_mrr), 0)
})
|> Repo.one()
%{
total_subscribers: result.total_subscribers,
total_mrr: result.total_mrr || Decimal.new(0)
}
end
def list_enabled_integrations(provider) do
Integration
|> where(provider: ^provider, enabled: true)
|> Repo.all()
end
end