Add ops script to sync all subscription plans

This is helpful when we update a plan limits and want to pull them to all the accounts
This commit is contained in:
Andrew Dryga
2024-04-02 12:30:44 -06:00
parent 1e4ed7bad6
commit 2fc18fa538
3 changed files with 41 additions and 0 deletions

View File

@@ -190,6 +190,11 @@ defmodule Domain.Billing do
end
end
def list_all_subscriptions do
secret_key = fetch_config!(:secret_key)
APIClient.list_all_subscriptions(secret_key)
end
def create_subscription(%Accounts.Account{} = account) do
secret_key = fetch_config!(:secret_key)
default_price_id = fetch_config!(:default_price_id)

View File

@@ -58,6 +58,27 @@ defmodule Domain.Billing.Stripe.APIClient do
request(api_token, :get, "customers/#{customer_id}", "")
end
def list_all_subscriptions(api_token, page_after \\ nil, acc \\ []) do
query_params =
if page_after do
"?starting_after=#{page_after}"
else
""
end
case request(api_token, :get, "subscriptions#{query_params}", "") do
{:ok, %{"has_more" => true, "data" => data}} ->
page_after = List.last(data)["id"]
list_all_subscriptions(api_token, page_after, acc ++ data)
{:ok, %{"has_more" => false, "data" => data}} ->
{:ok, acc ++ data}
{:error, reason} ->
{:error, reason}
end
end
def fetch_product(api_token, product_id) do
request(api_token, :get, "products/#{product_id}", "")
end

View File

@@ -68,4 +68,19 @@ defmodule Domain.Ops do
{actor, identity}
end)
end
def sync_pricing_plans do
{:ok, subscriptions} = Domain.Billing.list_all_subscriptions()
Enum.each(subscriptions, fn subscription ->
%{
"object" => "event",
"data" => %{
"object" => subscription
},
"type" => "customer.subscription.updated"
}
|> Domain.Billing.EventHandler.handle_event()
end)
end
end