Make sure the latest subscription is always used

This commit is contained in:
Uku Taht 2021-06-18 11:13:54 +03:00
parent 7c9ca12a3e
commit 883841f789
3 changed files with 24 additions and 3 deletions

View File

@ -2,7 +2,8 @@ defmodule Plausible.Auth.UserAdmin do
use Plausible.Repo
def custom_index_query(_conn, _schema, query) do
from(r in query, preload: [:subscription])
subscripton_q = from(s in Plausible.Billing.Subscription, order_by: [desc: s.inserted_at])
from(r in query, preload: [subscription: ^subscripton_q])
end
def form_fields(_) do

View File

@ -4,7 +4,8 @@ defmodule Plausible.Workers.LockSites do
@impl Oban.Worker
def perform(_job) do
users = Repo.all(from u in Plausible.Auth.User, preload: :subscription)
subscription_q = from(s in Plausible.Billing.Subscription, order_by: [desc: s.inserted_at])
users = Repo.all(from u in Plausible.Auth.User, preload: [subscription: ^subscription_q])
for user <- users do
Plausible.Billing.SiteLocker.check_sites_for(user)

View File

@ -51,7 +51,7 @@ defmodule Plausible.Workers.LockSitesTest do
end
test "locks user who cancelled subscription and the cancelled subscription has expired" do
user = insert(:user)
user = insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
insert(:subscription,
status: "deleted",
@ -63,6 +63,25 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
assert Repo.reload!(site).locked
end
test "does not lock if user has an old cancelled subscription and a new active subscription" do
user = insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
insert(:subscription,
status: "deleted",
next_bill_date: Timex.today() |> Timex.shift(days: -1),
user: user,
inserted_at: Timex.now() |> Timex.shift(days: -1)
)
insert(:subscription, status: "active", user: user)
site = insert(:site, members: [user])
LockSites.perform(nil)
refute Repo.reload!(site).locked
end