From f4f4d70adb940b3d8e6f039a45866c1da58fa0c7 Mon Sep 17 00:00:00 2001 From: hq1 Date: Thu, 19 Dec 2024 11:58:20 +0100 Subject: [PATCH] Only check usage for EPs by _current_ subscription (#4929) --- lib/workers/check_usage.ex | 3 ++- test/workers/check_usage_test.exs | 32 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/workers/check_usage.ex b/lib/workers/check_usage.ex index 33568cacb299..b9fddcfab0be 100644 --- a/lib/workers/check_usage.ex +++ b/lib/workers/check_usage.ex @@ -43,7 +43,8 @@ defmodule Plausible.Workers.CheckUsage do inner_join: o in assoc(t, :owner), inner_lateral_join: s in subquery(Teams.last_subscription_join_query()), on: true, - left_join: ep in assoc(t, :enterprise_plan), + left_join: ep in Plausible.Billing.EnterprisePlan, + on: ep.team_id == t.id and ep.paddle_plan_id == s.paddle_plan_id, where: s.status in [ ^Subscription.Status.active(), diff --git a/test/workers/check_usage_test.exs b/test/workers/check_usage_test.exs index 33239554d976..744213639fab 100644 --- a/test/workers/check_usage_test.exs +++ b/test/workers/check_usage_test.exs @@ -372,6 +372,38 @@ defmodule Plausible.Workers.CheckUsageTest do ) end + test "will only check usage if enterprise plan matches subscription's paddle plan id", + %{ + user: user + } do + usage_stub = + Plausible.Teams.Billing + |> stub(:monthly_pageview_usage, fn _user -> + %{ + penultimate_cycle: %{date_range: @date_range, total: 1_100_000}, + last_cycle: %{date_range: @date_range, total: 1_100_000} + } + end) + + subscribe_to_enterprise_plan( + user, + monthly_pageview_limit: 1_000_000, + subscription: [ + last_bill_date: Timex.shift(Timex.today(), days: -1), + status: unquote(status), + # non-matching ID + paddle_plan_id: @paddle_id_10k + ] + ) + + CheckUsage.perform(nil, usage_stub) + + refute_email_delivered_with( + to: [{nil, "enterprise@plausible.io"}], + subject: "#{user.email} has outgrown their enterprise plan" + ) + end + test "checks site limit for enterprise customer, sends usage information to enterprise@plausible.io", %{ user: user