Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions extra/lib/plausible_web/live/customer_support/team.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ defmodule PlausibleWeb.Live.CustomerSupport.Team do
yesterday = Date.shift(Date.utc_today(), day: -1)
Plausible.Billing.SiteLocker.set_lock_status_for(team, true)

Plausible.Repo.update!(
Plausible.Billing.Subscription.changeset(team.subscription, %{next_bill_date: yesterday})
Plausible.Repo.update_with_audit!(
Plausible.Billing.Subscription.changeset(team.subscription, %{next_bill_date: yesterday}),
"subscription_refund_locked",
%{team_id: team.id}
)

Resource.Team.get(team.id)
Expand Down
6 changes: 4 additions & 2 deletions lib/mix/tasks/cancel_subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ defmodule Mix.Tasks.CancelSubscription do
def run([paddle_subscription_id]) do
Mix.Task.run("app.start")

Repo.get_by!(Subscription, paddle_subscription_id: paddle_subscription_id)
subscription = Repo.get_by!(Subscription, paddle_subscription_id: paddle_subscription_id)

subscription
|> Subscription.changeset(%{status: Subscription.Status.deleted()})
|> Repo.update!()
|> Repo.update_with_audit!("subscription_cancelled", %{team_id: subscription.team_id})

Logger.notice("Successfully set the subscription status to #{Subscription.Status.deleted()}")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/pull_sandbox_subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Mix.Tasks.PullSandboxSubscription do
}

Subscription.changeset(%Subscription{}, subscription)
|> Repo.insert!()
|> Repo.insert_with_audit!("subscription_created", %{team_id: team.id})

Logger.notice("Subscription created for user #{user.id} (#{user.email})")
else
Expand Down
10 changes: 5 additions & 5 deletions lib/plausible/audit/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,23 @@ defmodule Plausible.Audit.Repo do
quote do
@behaviour Plausible.Audit.Repo

def update_with_audit(changeset, _, _) do
def update_with_audit(changeset, _, _ \\ %{}) do
update(changeset)
end

def update_with_audit!(changeset, _, _) do
def update_with_audit!(changeset, _, _ \\ %{}) do
update!(changeset)
end

def insert_with_audit(changeset, _, _) do
def insert_with_audit(changeset, _, _ \\ %{}) do
insert(changeset)
end

def insert_with_audit!(changeset, _, _, _) do
def insert_with_audit!(changeset, _, _ \\ %{}, _ \\ []) do
insert!(changeset)
end

def delete_with_audit!(resource, _, _) do
def delete_with_audit!(resource, _, _ \\ %{}) do
delete!(resource)
end
end
Expand Down
13 changes: 9 additions & 4 deletions lib/plausible/billing/billing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Plausible.Billing do

team
|> Subscription.create_changeset(subscription_params)
|> Repo.insert!()
|> Repo.insert_with_audit!("subscription_created", %{team_id: team.id})
|> after_subscription_update()
end

Expand All @@ -80,7 +80,7 @@ defmodule Plausible.Billing do

subscription
|> Subscription.changeset(params)
|> Repo.update!()
|> Repo.update_with_audit!("subscription_updated", %{team_id: subscription.team_id})
|> after_subscription_update()
end
end
Expand All @@ -97,7 +97,10 @@ defmodule Plausible.Billing do
status: params["status"]
})

updated = Repo.update!(changeset)
updated =
Repo.update_with_audit!(changeset, "subscription_cancelled", %{
team_id: subscription.team_id
})

for recipient <- subscription.team.owners ++ subscription.team.billing_members do
recipient
Expand Down Expand Up @@ -125,7 +128,9 @@ defmodule Plausible.Billing do
next_bill_date: api_subscription["next_payment"]["date"],
last_bill_date: api_subscription["last_payment"]["date"]
})
|> Repo.update!()
|> Repo.update_with_audit!("subscription_payment_succeeded", %{
team_id: subscription.team_id
})
|> Repo.preload(:team)

Plausible.Teams.update_accept_traffic_until(subscription.team)
Expand Down
17 changes: 17 additions & 0 deletions lib/plausible/billing/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Plausible.Billing.Subscription do
@moduledoc false

use Ecto.Schema
use Plausible
import Ecto.Changeset
require Plausible.Billing.Subscription.Status
alias Plausible.Billing.Subscription
Expand All @@ -21,6 +22,22 @@ defmodule Plausible.Billing.Subscription do

@optional_fields [:last_bill_date]

on_ee do
@derive {Plausible.Audit.Encoder,
only: [
:id,
:paddle_subscription_id,
:paddle_plan_id,
:update_url,
:cancel_url,
:status,
:next_bill_amount,
:next_bill_date,
:last_bill_date,
:currency_code
]}
end

schema "subscriptions" do
field :paddle_subscription_id, :string
field :paddle_plan_id, :string
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/teams/billing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule Plausible.Teams.Billing do
next_bill_amount: amount,
next_bill_date: response["next_payment"]["date"]
})
|> Repo.update()
|> Repo.update_with_audit("subscription_plan_changed", %{team_id: subscription.team_id})

e ->
e
Expand Down
42 changes: 39 additions & 3 deletions test/plausible/billing/billing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ defmodule Plausible.BillingTest do
assert subscription.last_bill_date == ~D[2019-05-01]
assert subscription.next_bill_amount == "6.00"
assert subscription.currency_code == "EUR"

assert audited_entry("subscription_created",
team_id: team.id,
entity_id: "#{subscription.id}"
)
end

test "supports user without a team case" do
Expand All @@ -233,14 +238,21 @@ defmodule Plausible.BillingTest do
%{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id}"}
|> Billing.subscription_created()

team = team_of(user)

subscription =
user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
team |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)

assert subscription.paddle_subscription_id == @subscription_id
assert subscription.next_bill_date == ~D[2019-06-01]
assert subscription.last_bill_date == ~D[2019-05-01]
assert subscription.next_bill_amount == "6.00"
assert subscription.currency_code == "EUR"

assert audited_entry("subscription_created",
team_id: team.id,
entity_id: "#{subscription.id}"
)
end

test "unlocks sites if user has any locked sites" do
Expand Down Expand Up @@ -317,6 +329,11 @@ defmodule Plausible.BillingTest do
subscription = subscription_of(user)
assert subscription.paddle_plan_id == @plan_id_10k
assert subscription.next_bill_amount == "12.00"

assert audited_entry("subscription_updated",
team_id: team.id,
entity_id: "#{subscription.id}"
)
end

test "updates subscription with user/team passthrough" do
Expand Down Expand Up @@ -485,7 +502,10 @@ defmodule Plausible.BillingTest do
user = new_user()
subscribe_to_growth_plan(user, status: Subscription.Status.active())

subscription_id = subscription_of(user).paddle_subscription_id
subscription = subscription_of(user)
team = team_of(user)

subscription_id = subscription.paddle_subscription_id

Billing.subscription_cancelled(%{
"alert_name" => "subscription_cancelled",
Expand All @@ -499,6 +519,11 @@ defmodule Plausible.BillingTest do
|> Plausible.Teams.with_subscription()
|> Map.fetch!(:subscription)
|> Subscription.Status.deleted?()

assert audited_entry("subscription_cancelled",
team_id: team.id,
entity_id: "#{subscription.id}"
)
end

test "ignores if the subscription cannot be found" do
Expand Down Expand Up @@ -543,7 +568,8 @@ defmodule Plausible.BillingTest do
test "updates accept_traffic_until" do
user = new_user() |> subscribe_to_growth_plan()

subscription_id = subscription_of(user).paddle_subscription_id
subscription = subscription_of(user)
subscription_id = subscription.paddle_subscription_id

Billing.subscription_payment_succeeded(%{
"alert_name" => "subscription_payment_succeeded",
Expand All @@ -552,6 +578,11 @@ defmodule Plausible.BillingTest do

team = user |> team_of() |> Repo.reload!() |> Plausible.Teams.with_subscription()
assert team.accept_traffic_until == Date.add(team.subscription.next_bill_date, 30)

assert audited_entry("subscription_payment_succeeded",
team_id: team.id,
entity_id: "#{subscription.id}"
)
end

test "sets the next bill amount and date, last bill date" do
Expand Down Expand Up @@ -591,6 +622,11 @@ defmodule Plausible.BillingTest do
assert subscription.paddle_plan_id == "123123"
assert subscription.next_bill_date == ~D[2019-07-10]
assert subscription.next_bill_amount == "6.00"

assert audited_entry("subscription_plan_changed",
team_id: team.id,
entity_id: "#{subscription.id}"
)
end
end

Expand Down
6 changes: 5 additions & 1 deletion test/plausible/sites/index_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ defmodule Plausible.Sites.IndexTest do

team = Plausible.Teams.complete_setup(team)

assert Index.fetch_site_ids(user, team: team) == [regular1.id, regular2.id]
assert ids = Index.fetch_site_ids(user, team: team)

assert length(ids) == 2
assert regular1.id in ids
assert regular2.id in ids
end
end

Expand Down
9 changes: 8 additions & 1 deletion test/plausible_web/live/customer_support/teams_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,18 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
assert team.locked
refute team.grace_period

subscription = Plausible.Teams.with_subscription(team).subscription

assert Date.diff(
Plausible.Teams.with_subscription(team).subscription.next_bill_date,
subscription.next_bill_date,
Date.utc_today()
) == -1

assert audited_entry("subscription_refund_locked",
team_id: team.id,
entity_id: "#{subscription.id}"
)

# make sure this team doesn't unlock automatically
Plausible.Workers.LockSites.perform(nil)
team = Plausible.Repo.reload!(team)
Expand Down
4 changes: 4 additions & 0 deletions test/support/teams/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -462,5 +462,9 @@ defmodule Plausible.Teams.Test do
raise "Expected audited entry #{inspect(attrs)} but only found #{inspect(Plausible.Audit.list_entries([]), pretty: true)}."
end
end
else
def audited_entry(_, _ \\ []), do: true

def audited_entries(_, _, _ \\ []), do: true
end
end
Loading