Skip to content

Commit

Permalink
Check usage for users with active/cancelled/past due subscriptions (p…
Browse files Browse the repository at this point in the history
…lausible#4607)

* Check usage for users with active/cancelled/past due subscriptions

So that over limit e-mails and grace period is handled
regardless - they're still subscribers.

* Ensure current subscription is fetched, for an ongoing plan

* Add extra test

* Revert "Ensure current subscription is fetched, for an ongoing plan"

This reverts commit 9ddc7bd.

* Fixup test

* Reapply "Ensure current subscription is fetched, for an ongoing plan"

This reverts commit 7ab5379.

* Do a subscription preload and assert ID match

* Use inner lateral join to fetch subscriptions
  • Loading branch information
aerosol authored Sep 25, 2024
1 parent 00dffb5 commit 3251b5b
Show file tree
Hide file tree
Showing 3 changed files with 403 additions and 270 deletions.
8 changes: 7 additions & 1 deletion lib/plausible/billing/billing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,13 @@ defmodule Plausible.Billing do
user =
User
|> Repo.get!(subscription.user_id)
|> Map.put(:subscription, subscription)
|> Plausible.Users.with_subscription()

if subscription.id != user.subscription.id do
Sentry.capture_message("Susbscription ID mismatch",
extra: %{subscription: inspect(subscription), user_id: user.id}
)
end

user
|> Plausible.Users.update_accept_traffic_until()
Expand Down
31 changes: 23 additions & 8 deletions lib/workers/check_usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,34 @@ defmodule Plausible.Workers.CheckUsage do
def perform(_job, usage_mod \\ Quota.Usage, today \\ Date.utc_today()) do
yesterday = today |> Date.shift(day: -1)

last_subscription_query =
from(s in Subscription,
order_by: [desc: s.inserted_at],
where: s.user_id == parent_as(:user).id,
where:
s.status in [
^Subscription.Status.active(),
^Subscription.Status.past_due(),
^Subscription.Status.deleted()
],
where: not is_nil(s.last_bill_date),
# Accounts for situations like last_bill_date==2021-01-31 AND today==2021-03-01. Since February never reaches the 31st day, the account is checked on 2021-03-01.
where: s.next_bill_date >= ^today,
where:
least(day_of_month(s.last_bill_date), day_of_month(last_day_of_month(^yesterday))) ==
day_of_month(^yesterday),
limit: 1
)

active_subscribers =
Repo.all(
from(u in User,
join: s in Plausible.Billing.Subscription,
on: s.user_id == u.id,
as: :user,
inner_lateral_join: s in subquery(last_subscription_query),
on: true,
left_join: ep in Plausible.Billing.EnterprisePlan,
on: ep.user_id == u.id,
where: s.status == ^Subscription.Status.active(),
where: not is_nil(s.last_bill_date),
# Accounts for situations like last_bill_date==2021-01-31 AND today==2021-03-01. Since February never reaches the 31st day, the account is checked on 2021-03-01.
where:
least(day_of_month(s.last_bill_date), day_of_month(last_day_of_month(^yesterday))) ==
day_of_month(^yesterday),
order_by: u.id,
preload: [subscription: s, enterprise_plan: ep]
)
)
Expand Down
Loading

0 comments on commit 3251b5b

Please sign in to comment.