diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 56983a93c..fcc5d2785 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -92,16 +92,13 @@ defmodule Plausible.Billing do @spec check_needs_to_upgrade(User.t()) :: {:needs_to_upgrade, :no_trial | :no_active_subscription | :grace_period_ended} | :no_upgrade_needed - def check_needs_to_upgrade(%User{trial_expiry_date: nil}) do - {:needs_to_upgrade, :no_trial} - end - def check_needs_to_upgrade(user) do user = Plausible.Users.with_subscription(user) - trial_is_over = Timex.before?(user.trial_expiry_date, Timex.today()) + trial_is_over = user.trial_expiry_date && Timex.before?(user.trial_expiry_date, Timex.today()) subscription_active = subscription_is_active?(user.subscription) cond do + !user.trial_expiry_date && !subscription_active -> {:needs_to_upgrade, :no_trial} trial_is_over && !subscription_active -> {:needs_to_upgrade, :no_active_subscription} Plausible.Auth.GracePeriod.expired?(user) -> {:needs_to_upgrade, :grace_period_ended} true -> :no_upgrade_needed diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs index 47b0f7d10..da7330e40 100644 --- a/test/plausible/billing/billing_test.exs +++ b/test/plausible/billing/billing_test.exs @@ -58,6 +58,13 @@ defmodule Plausible.BillingTest do assert Billing.check_needs_to_upgrade(user) == {:needs_to_upgrade, :no_trial} end + test "is false for user with empty trial expiry date but with an active subscription" do + user = insert(:user, trial_expiry_date: nil) + insert(:subscription, user: user) + + assert Billing.check_needs_to_upgrade(user) == :no_upgrade_needed + end + test "is false for a user with an expired trial but an active subscription" do user = insert(:user, trial_expiry_date: Timex.shift(Timex.today(), days: -1)) insert(:subscription, user: user)