diff --git a/extra/lib/plausible_web/live/customer_support/team.ex b/extra/lib/plausible_web/live/customer_support/team.ex index bdcb8d505cb1..59731a4140b8 100644 --- a/extra/lib/plausible_web/live/customer_support/team.ex +++ b/extra/lib/plausible_web/live/customer_support/team.ex @@ -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) diff --git a/lib/mix/tasks/cancel_subscription.ex b/lib/mix/tasks/cancel_subscription.ex index 3646fac485b8..e7d2c669376a 100644 --- a/lib/mix/tasks/cancel_subscription.ex +++ b/lib/mix/tasks/cancel_subscription.ex @@ -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 diff --git a/lib/mix/tasks/pull_sandbox_subscription.ex b/lib/mix/tasks/pull_sandbox_subscription.ex index 24c268b14366..acc8097dd965 100644 --- a/lib/mix/tasks/pull_sandbox_subscription.ex +++ b/lib/mix/tasks/pull_sandbox_subscription.ex @@ -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 diff --git a/lib/plausible/audit/repo.ex b/lib/plausible/audit/repo.ex index dcffb5748d03..4defe14cc1df 100644 --- a/lib/plausible/audit/repo.ex +++ b/lib/plausible/audit/repo.ex @@ -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 diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index c064087ead23..6ad198654fd3 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/lib/plausible/billing/subscription.ex b/lib/plausible/billing/subscription.ex index 545066620e25..fefe1549d265 100644 --- a/lib/plausible/billing/subscription.ex +++ b/lib/plausible/billing/subscription.ex @@ -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 @@ -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 diff --git a/lib/plausible/teams/billing.ex b/lib/plausible/teams/billing.ex index b483d56c1323..81f31f676c20 100644 --- a/lib/plausible/teams/billing.ex +++ b/lib/plausible/teams/billing.ex @@ -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 diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs index da25ecf4c2dc..836cb1323d9c 100644 --- a/test/plausible/billing/billing_test.exs +++ b/test/plausible/billing/billing_test.exs @@ -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 @@ -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 @@ -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 @@ -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", @@ -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 @@ -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", @@ -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 @@ -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 diff --git a/test/plausible/sites/index_test.exs b/test/plausible/sites/index_test.exs index bb53a57f12d1..c74a3189aa27 100644 --- a/test/plausible/sites/index_test.exs +++ b/test/plausible/sites/index_test.exs @@ -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 diff --git a/test/plausible_web/live/customer_support/teams_test.exs b/test/plausible_web/live/customer_support/teams_test.exs index fb273ca570d9..45c35de0f7d6 100644 --- a/test/plausible_web/live/customer_support/teams_test.exs +++ b/test/plausible_web/live/customer_support/teams_test.exs @@ -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) diff --git a/test/support/teams/test.ex b/test/support/teams/test.ex index 7f3b71ad28ce..007742cd0f61 100644 --- a/test/support/teams/test.ex +++ b/test/support/teams/test.ex @@ -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