From f51943a289b05b21d593b1093f100c225e5319bf Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Tue, 2 Jul 2024 12:15:06 +0200 Subject: [PATCH] wip --- lib/workers/traffic_change_notifier.ex | 7 +++ test/workers/traffic_change_notifier_test.exs | 45 ++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/workers/traffic_change_notifier.ex b/lib/workers/traffic_change_notifier.ex index ec0851ed09b5..b133d890f031 100644 --- a/lib/workers/traffic_change_notifier.ex +++ b/lib/workers/traffic_change_notifier.ex @@ -10,6 +10,7 @@ defmodule Plausible.Workers.TrafficChangeNotifier do @impl Oban.Worker def perform(_job, clickhouse \\ Plausible.Stats.Clickhouse) do + today = Date.utc_today() notifications = Repo.all( from sn in TrafficChangeNotification, @@ -18,6 +19,12 @@ defmodule Plausible.Workers.TrafficChangeNotifier do join: s in Plausible.Site, on: sn.site_id == s.id, where: not s.locked, + join: sm in Plausible.Site.Membership, + on: sm.site_id == s.id, + where: sm.role == :owner, + join: u in Plausible.Auth.User, + on: u.id == sm.user_id, + where: is_nil(u.accept_traffic_until) or u.accept_traffic_until > ^today, preload: [site: s] ) diff --git a/test/workers/traffic_change_notifier_test.exs b/test/workers/traffic_change_notifier_test.exs index fb26780fd211..536926394920 100644 --- a/test/workers/traffic_change_notifier_test.exs +++ b/test/workers/traffic_change_notifier_test.exs @@ -5,6 +5,50 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do alias Plausible.Workers.TrafficChangeNotifier describe "drops" do + + test "does not notify anyone if we've stopped accepting traffic for the owner" do + site = insert(:site, memberships: [ + build(:site_membership, + user: build(:user, accept_traffic_until: Date.utc_today()), + role: :owner)]) + + insert(:drop_notification, + site: site, + threshold: 10, + recipients: ["jerod@example.com", "uku@example.com"] + ) + + clickhouse_stub = + stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 1 end) + + TrafficChangeNotifier.perform(nil, clickhouse_stub) + + assert_no_emails_delivered() + end + + test "does notify if threshold reached and we're accepting traffic" do + site = insert(:site, memberships: [ + build(:site_membership, + user: build(:user, accept_traffic_until: Date.utc_today() |> Date.add(+1)), + role: :owner)]) + + insert(:drop_notification, + site: site, + threshold: 10, + recipients: ["jerod@example.com"] + ) + + clickhouse_stub = + stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 1 end) + + TrafficChangeNotifier.perform(nil, clickhouse_stub) + + assert_email_delivered_with( + subject: "Traffic Drop on #{site.domain}", + to: [nil: "jerod@example.com"] + ) + end + test "does not notify anyone if current visitors does not drop below notification threshold" do site = insert(:site) @@ -24,7 +68,6 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do test "notifies all recipients when traffic drops under configured threshold" do site = insert(:site) - insert(:drop_notification, site: site, threshold: 10,