You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/lib/plausible/site/spike_notification.ex

36 lines
884 B
Elixir

defmodule Plausible.Site.SpikeNotification do
use Ecto.Schema
import Ecto.Changeset
schema "spike_notifications" do
field :recipients, {:array, :string}
field :threshold, :integer
field :last_sent, :naive_datetime
belongs_to :site, Plausible.Site
timestamps()
end
def changeset(schema, attrs) do
schema
|> cast(attrs, [:site_id, :recipients, :threshold])
|> validate_required([:site_id, :recipients, :threshold])
|> unique_constraint(:site_id)
end
def add_recipient(schema, recipient) do
schema
|> change(recipients: schema.recipients ++ [recipient])
end
def remove_recipient(schema, recipient) do
schema
|> change(recipients: List.delete(schema.recipients, recipient))
end
def was_sent(schema) do
schema
|> change(last_sent: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second))
end
end