mirror of
https://github.com/plausible/analytics.git
synced 2024-11-23 03:04:43 +03:00
CRM improvements (#1903)
* lock/unlock sites, show user role, link to manage subscription * merged remove gp and unlock sites into one single action + code style tweaks
This commit is contained in:
parent
5b5c70b82d
commit
225c1138b6
68
lib/mix/tasks/pull_sandbox_subscription.ex
Normal file
68
lib/mix/tasks/pull_sandbox_subscription.ex
Normal file
@ -0,0 +1,68 @@
|
||||
defmodule Mix.Tasks.PullSandboxSubscription do
|
||||
use Mix.Task
|
||||
use Plausible.Repo
|
||||
alias Plausible.{Repo, Auth.User, Billing.Subscription}
|
||||
require Logger
|
||||
|
||||
# Steps to create a subscription in dev environment
|
||||
#
|
||||
# 1) Subscribe to a sandbox plan in the UI > User Settings. Instructions:
|
||||
# https://developer.paddle.com/getting-started/ZG9jOjIxODY4NjYx-sandbox
|
||||
#
|
||||
# 2) find the created subscription_ID here:
|
||||
# https://sandbox-vendors.paddle.com/subscriptions/customers
|
||||
#
|
||||
# 3) run from command line:
|
||||
# mix pull_sandbox_subscription <subscription_ID>
|
||||
|
||||
@headers [
|
||||
{"Content-type", "application/json"},
|
||||
{"Accept", "application/json"}
|
||||
]
|
||||
|
||||
def run([paddle_subscription_id]) do
|
||||
Mix.Task.run("app.start")
|
||||
|
||||
config = Application.get_env(:plausible, :paddle)
|
||||
|
||||
endpoint = Plausible.Billing.PaddleApi.vendors_domain() <> "/api/2.0/subscription/users"
|
||||
|
||||
params = %{
|
||||
vendor_id: config[:vendor_id],
|
||||
vendor_auth_code: config[:vendor_auth_code],
|
||||
subscription_id: paddle_subscription_id
|
||||
}
|
||||
|
||||
case HTTPoison.post(endpoint, Jason.encode!(params), @headers) do
|
||||
{:ok, response} ->
|
||||
body = Jason.decode!(response.body)
|
||||
|
||||
if body["success"] do
|
||||
res = body["response"] |> List.first()
|
||||
user = Repo.get_by!(User, email: res["user_email"])
|
||||
|
||||
subscription = %{
|
||||
paddle_subscription_id: res["subscription_id"] |> to_string(),
|
||||
paddle_plan_id: res["plan_id"] |> to_string(),
|
||||
cancel_url: res["cancel_url"],
|
||||
update_url: res["update_url"],
|
||||
user_id: user.id,
|
||||
status: res["state"],
|
||||
next_bill_date: res["next_payment"]["date"],
|
||||
next_bill_amount: res["next_payment"]["amount"] |> to_string(),
|
||||
currency_code: res["next_payment"]["currency"]
|
||||
}
|
||||
|
||||
Subscription.changeset(%Subscription{}, subscription)
|
||||
|> Repo.insert!()
|
||||
|
||||
Logger.info("Subscription created for user #{user.id} (#{user.email})")
|
||||
else
|
||||
Logger.error(body["error"])
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error(reason)
|
||||
end
|
||||
end
|
||||
end
|
@ -20,7 +20,7 @@ defmodule Plausible.Auth.UserAdmin do
|
||||
email: nil,
|
||||
inserted_at: %{name: "Created at", value: &format_date(&1.inserted_at)},
|
||||
trial_expiry_date: %{name: "Trial expiry", value: &format_date(&1.trial_expiry_date)},
|
||||
subscription_tier: %{value: &subscription_tier/1},
|
||||
subscription_plan: %{value: &subscription_plan/1},
|
||||
subscription_status: %{value: &subscription_status/1},
|
||||
grace_period: %{value: &grace_period_status/1}
|
||||
]
|
||||
@ -28,16 +28,18 @@ defmodule Plausible.Auth.UserAdmin do
|
||||
|
||||
def resource_actions(_) do
|
||||
[
|
||||
remove_grace_period: %{
|
||||
name: "Remove grace period",
|
||||
action: fn _, user -> remove_grace_period(user) end
|
||||
unlock: %{
|
||||
name: "Unlock",
|
||||
action: fn _, user -> unlock(user) end
|
||||
}
|
||||
]
|
||||
end
|
||||
|
||||
defp remove_grace_period(user) do
|
||||
defp unlock(user) do
|
||||
if user.grace_period do
|
||||
Plausible.Auth.User.remove_grace_period(user) |> Repo.update()
|
||||
Plausible.Billing.SiteLocker.set_lock_status_for(user, false)
|
||||
{:ok, user}
|
||||
else
|
||||
{:error, user, "No active grace period on this user"}
|
||||
end
|
||||
@ -54,11 +56,17 @@ defmodule Plausible.Auth.UserAdmin do
|
||||
end
|
||||
end
|
||||
|
||||
defp subscription_tier(user) do
|
||||
defp subscription_plan(user) do
|
||||
if user.subscription && user.subscription.status == "active" do
|
||||
quota = PlausibleWeb.AuthView.subscription_quota(user.subscription)
|
||||
interval = PlausibleWeb.AuthView.subscription_interval(user.subscription)
|
||||
"#{quota} (#{interval})"
|
||||
|
||||
manage_url =
|
||||
Plausible.Billing.PaddleApi.vendors_domain() <>
|
||||
"/subscriptions/customers/manage/" <>
|
||||
user.subscription.paddle_subscription_id
|
||||
|
||||
{:safe, ~s(<a href="#{manage_url}">#{quota} \(#{interval}\)</a>)}
|
||||
else
|
||||
"--"
|
||||
end
|
||||
|
@ -19,7 +19,7 @@ defmodule Plausible.Billing.SiteLocker do
|
||||
end
|
||||
end
|
||||
|
||||
defp set_lock_status_for(user, status) do
|
||||
def set_lock_status_for(user, status) do
|
||||
site_ids =
|
||||
Repo.all(
|
||||
from s in Plausible.Site.Membership,
|
||||
|
@ -28,7 +28,7 @@ defmodule Plausible.SiteAdmin do
|
||||
timezone: nil,
|
||||
public: nil,
|
||||
owner: %{value: &get_owner_email/1},
|
||||
other_members: %{value: &get_other_members_emails/1}
|
||||
other_members: %{value: &get_other_members/1}
|
||||
]
|
||||
end
|
||||
|
||||
@ -52,9 +52,10 @@ defmodule Plausible.SiteAdmin do
|
||||
Enum.find(site.memberships, fn m -> m.role == :owner end).user.email
|
||||
end
|
||||
|
||||
defp get_other_members_emails(site) do
|
||||
memberships = Enum.reject(site.memberships, fn m -> m.role == :owner end)
|
||||
Enum.map(memberships, fn m -> m.user.email end) |> Enum.join(", ")
|
||||
defp get_other_members(site) do
|
||||
Enum.filter(site.memberships, &(&1.role != :owner))
|
||||
|> Enum.map(fn m -> m.user.email <> "(#{to_string(m.role)})" end)
|
||||
|> Enum.join(", ")
|
||||
end
|
||||
|
||||
def transfer_data([from_site], params) do
|
||||
|
Loading…
Reference in New Issue
Block a user