Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Do not allow duplicate spike notification to be created
Uku Tahta4bf74a84c3e
diff --git a/lib/plausible/site/spike_notification.ex b/lib/plausible/site/spike_notification.exindex b1de93e23e..c37692da23 100644--- a/lib/plausible/site/spike_notification.ex+++ b/lib/plausible/site/spike_notification.ex@@ -15,7 +15,7 @@ defmodule Plausible.Site.SpikeNotification doschema|> cast(attrs, [:site_id, :recipients, :threshold])|> validate_required([:site_id, :recipients, :threshold])- |> unique_constraint(:site)+ |> unique_constraint(:site_id)enddef add_recipient(schema, recipient) dodiff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.exindex 1ada071e37..505e1fe972 100644--- a/lib/plausible_web/controllers/site_controller.ex+++ b/lib/plausible_web/controllers/site_controller.ex@@ -440,16 +440,25 @@ defmodule PlausibleWeb.SiteController dodef enable_spike_notification(conn, %{"website" => website}) dosite = Sites.get_for_user!(conn.assigns[:current_user].id, website)- Plausible.Site.SpikeNotification.changeset(%Plausible.Site.SpikeNotification{}, %{- site_id: site.id,- threshold: 10,- recipients: [conn.assigns[:current_user].email]- })- |> Repo.insert!()+ res =+ Plausible.Site.SpikeNotification.changeset(%Plausible.Site.SpikeNotification{}, %{+ site_id: site.id,+ threshold: 10,+ recipients: [conn.assigns[:current_user].email]+ })+ |> Repo.insert()- conn- |> put_flash(:success, "You will a notification with traffic spikes going forward")- |> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")+ case res do+ {:ok, _} ->+ conn+ |> put_flash(:success, "You will a notification with traffic spikes going forward")+ |> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")++ {:error, _} ->+ conn+ |> put_flash(:error, "Unable to create a spike notification")+ |> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")+ endenddef disable_spike_notification(conn, %{"website" => website}) dodiff --git a/lib/plausible_web/templates/layout/app.html.eex b/lib/plausible_web/templates/layout/app.html.eexindex 982368c579..e89bfa3cb4 100644--- a/lib/plausible_web/templates/layout/app.html.eex+++ b/lib/plausible_web/templates/layout/app.html.eex@@ -97,12 +97,6 @@<%= render("_flash.html", assigns) %>- <%= if get_flash(@conn, :error) do %>- <div id="flash" class="text-center bg-red-dark-500 text-red-100 text-sm font-bold px-4 absolute w-full transition overflow-hidden" style="top: 91px" role="alert">- <p class="py-3 "><%= get_flash(@conn, :error) %></p>- </div>- <% end %>-<%= if @conn.assigns[:current_user] && @conn.assigns[:current_user].subscription && @conn.assigns[:current_user].subscription.status == "past_due" do %><div class="container"><div class="p-2 rounded-lg bg-yellow-100 sm:p-3">diff --git a/priv/repo/migrations/20210119093337_add_unique_index_to_spike_notification.exs b/priv/repo/migrations/20210119093337_add_unique_index_to_spike_notification.exsnew file mode 100644index 0000000000..264092eb59--- /dev/null+++ b/priv/repo/migrations/20210119093337_add_unique_index_to_spike_notification.exs@@ -0,0 +1,7 @@+defmodule Plausible.Repo.Migrations.AddUniqueIndexToSpikeNotification do+ use Ecto.Migration++ def change do+ create unique_index(:spike_notifications, :site_id)+ end+enddiff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exsindex f8ead78eba..88bbf82ac3 100644--- a/test/plausible_web/controllers/site_controller_test.exs+++ b/test/plausible_web/controllers/site_controller_test.exs@@ -409,6 +409,19 @@ defmodule PlausibleWeb.SiteControllerTest donotification = Repo.get_by(Plausible.Site.SpikeNotification, site_id: site.id)assert notification.recipients == [user.email]end++ test "does not allow duplicate spike notification to be created", %{+ conn: conn,+ site: site+ } do+ post(conn, "/sites/#{site.domain}/spike-notification/enable")+ post(conn, "/sites/#{site.domain}/spike-notification/enable")++ assert Repo.aggregate(+ from(s in Plausible.Site.SpikeNotification, where: s.site_id == ^site.id),+ :count+ ) == 1+ endenddescribe "POST /sites/:website/spike-notification/disable" do