Skip to content

Commit

Permalink
Only check usage for EPs by _current_ subscription (plausible#4929)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol authored Dec 19, 2024
1 parent 6e7f3dd commit f4f4d70
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/workers/check_usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
32 changes: 32 additions & 0 deletions test/workers/check_usage_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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, "[email protected]"}],
subject: "#{user.email} has outgrown their enterprise plan"
)
end

test "checks site limit for enterprise customer, sends usage information to [email protected]",
%{
user: user
Expand Down

0 comments on commit f4f4d70

Please sign in to comment.