- Created Towerops.Schema base macro (95+ schemas updated to use it) - Created Towerops.Snmp.Reading macro (7 reading schemas consolidated) - Created Towerops.Gaiia.BaseSchema macro (4 Gaiia schemas consolidated) - Created Towerops.SyncLog shared module (UISP + Preseem merge) - Created Towerops.LogFilters (3 log filter modules merged into 1) - Created Towerops.OnCall.ChangesetHelpers (9 on-call schemas simplified) - Created API v1 ResourceController shared helpers (7 controllers) - Extracted ConnectionHelpers.format_connection_result (2 LiveViews) - Removed 5 dead config keys (scopes, mib_dirs, stripe_meter_id, etc.) - Fixed 2 pre-existing broken tests - All 13,219 tests pass, Credo clean (0 issues)
44 lines
1.3 KiB
Elixir
44 lines
1.3 KiB
Elixir
defmodule Towerops.Alerts.NotificationDigest do
|
|
@moduledoc """
|
|
Schema for notification rate limiting per user.
|
|
|
|
Tracks how many notifications a user has received within a time window.
|
|
When the limit is exceeded, additional alerts are queued for digest delivery.
|
|
"""
|
|
use Towerops.Schema
|
|
|
|
alias Towerops.Accounts.User
|
|
alias Towerops.Organizations.Organization
|
|
|
|
@type t :: %__MODULE__{}
|
|
|
|
schema "notification_digests" do
|
|
field :window_start, :utc_datetime
|
|
field :notification_count, :integer, default: 0
|
|
field :digest_sent, :boolean, default: false
|
|
field :digest_sent_at, :utc_datetime
|
|
field :suppressed_alert_ids, {:array, Ecto.UUID}, default: []
|
|
|
|
belongs_to :user, User
|
|
belongs_to :organization, Organization
|
|
|
|
timestamps(type: :utc_datetime)
|
|
end
|
|
|
|
def changeset(digest, attrs) do
|
|
digest
|
|
|> cast(attrs, [
|
|
:user_id,
|
|
:organization_id,
|
|
:window_start,
|
|
:notification_count,
|
|
:digest_sent,
|
|
:digest_sent_at,
|
|
:suppressed_alert_ids
|
|
])
|
|
|> validate_required([:user_id, :organization_id, :window_start])
|
|
|> foreign_key_constraint(:user_id)
|
|
|> foreign_key_constraint(:organization_id)
|
|
|> unique_constraint([:user_id, :window_start], name: :notification_digests_user_window_idx)
|
|
end
|
|
end
|