Skip to content

Commit 9550eea

Browse files
refactor: remove code duplication in billing controller
1 parent b764c03 commit 9550eea

1 file changed

Lines changed: 12 additions & 85 deletions

File tree

lib/logflare_web/controllers/billing_controller.ex

Lines changed: 12 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ defmodule LogflareWeb.BillingController do
9898
) do
9999
plan = Billing.get_plan_by(stripe_id: stripe_id)
100100

101-
with false <- billing_accoount_has_subscription?(billing_account),
101+
with false <- billing_account_has_subscription?(billing_account),
102102
{:ok, session} <- Stripe.create_payment_session(user, plan) do
103103
conn
104104
|> put_session(:stripe_session, session)
@@ -119,7 +119,7 @@ defmodule LogflareWeb.BillingController do
119119
%{"stripe_id" => stripe_id, "type" => "metered"}
120120
) do
121121
with plan <- Billing.get_plan_by(stripe_id: stripe_id),
122-
false <- billing_accoount_has_subscription?(billing_account),
122+
false <- billing_account_has_subscription?(billing_account),
123123
false <- billing_account.lifetime_plan,
124124
{:ok, session} <- Stripe.create_metered_customer_session(user, plan) do
125125
conn
@@ -141,7 +141,7 @@ defmodule LogflareWeb.BillingController do
141141
%{"stripe_id" => stripe_id}
142142
) do
143143
with plan <- Billing.get_plan_by(stripe_id: stripe_id),
144-
false <- billing_accoount_has_subscription?(billing_account),
144+
false <- billing_account_has_subscription?(billing_account),
145145
{:ok, session} <- Stripe.create_customer_session(user, plan) do
146146
conn
147147
|> put_session(:stripe_session, session)
@@ -161,13 +161,14 @@ defmodule LogflareWeb.BillingController do
161161
%{
162162
assigns: %{
163163
user: %User{billing_account: billing_account, sources: sources} = _user,
164-
plan: %Billing.Plan{type: "standard"}
164+
plan: %Billing.Plan{type: billing_plan_type}
165165
}
166166
} = conn,
167-
%{"plan" => plan_id, "type" => "metered"}
168-
) do
167+
%{"plan" => plan_id, "type" => type}
168+
)
169+
when billing_plan_type in ["standard", "metered"] and type in ["standard", "metered"] do
169170
with plan <- Billing.get_plan!(plan_id),
170-
true <- billing_accoount_has_subscription?(billing_account),
171+
true <- billing_account_has_subscription?(billing_account),
171172
{:ok, _response} <- Stripe.change_to_metered_subscription(billing_account, sources, plan) do
172173
success_and_redirect(conn, "Plan successfully changed!")
173174
else
@@ -181,80 +182,6 @@ defmodule LogflareWeb.BillingController do
181182
end
182183
end
183184

184-
def change_subscription(
185-
%{
186-
assigns: %{
187-
user: %User{billing_account: billing_account, sources: sources} = _user,
188-
plan: %Billing.Plan{type: "metered"}
189-
}
190-
} = conn,
191-
%{"plan" => plan_id, "type" => "metered"}
192-
) do
193-
with plan <- Billing.get_plan!(plan_id),
194-
true <- billing_accoount_has_subscription?(billing_account),
195-
{:ok, _response} <-
196-
Stripe.change_from_metered_subscription(billing_account, sources, plan) do
197-
success_and_redirect(conn, "Plan successfully changed!")
198-
else
199-
false ->
200-
error_and_redirect(conn, "You need a subscription to change first!")
201-
202-
err ->
203-
Logger.error("Billing error: #{inspect(err)}", %{billing: %{error_string: inspect(err)}})
204-
205-
error_and_redirect(conn, @default_error_message)
206-
end
207-
end
208-
209-
def change_subscription(
210-
%{
211-
assigns: %{
212-
user: %User{billing_account: billing_account, sources: sources} = _user,
213-
plan: %Billing.Plan{type: "standard"}
214-
}
215-
} = conn,
216-
%{"plan" => plan_id, "type" => "standard"}
217-
) do
218-
with plan <- Billing.get_plan!(plan_id),
219-
true <- billing_accoount_has_subscription?(billing_account),
220-
{:ok, _response} <- Stripe.change_subscription(billing_account, sources, plan) do
221-
success_and_redirect(conn, "Plan successfully changed!")
222-
else
223-
false ->
224-
error_and_redirect(conn, "You need a subscription to change first!")
225-
226-
err ->
227-
Logger.error("Billing error: #{inspect(err)}", %{billing: %{error_string: inspect(err)}})
228-
229-
error_and_redirect(conn, @default_error_message)
230-
end
231-
end
232-
233-
def change_subscription(
234-
%{
235-
assigns: %{
236-
user: %User{billing_account: billing_account, sources: sources} = _user,
237-
plan: %Billing.Plan{type: "metered"}
238-
}
239-
} = conn,
240-
%{"plan" => plan_id, "type" => "standard"}
241-
) do
242-
with plan <- Billing.get_plan!(plan_id),
243-
true <- billing_accoount_has_subscription?(billing_account),
244-
{:ok, _response} <-
245-
Stripe.change_from_metered_subscription(billing_account, sources, plan) do
246-
success_and_redirect(conn, "Plan successfully changed!")
247-
else
248-
false ->
249-
error_and_redirect(conn, "You need a subscription to change first!")
250-
251-
err ->
252-
Logger.error("Billing error: #{inspect(err)}", %{billing: %{error_string: inspect(err)}})
253-
254-
error_and_redirect(conn, @default_error_message)
255-
end
256-
end
257-
258185
def portal(
259186
%{assigns: %{user: %User{billing_account: billing_account}} = _user} = conn,
260187
_params
@@ -275,7 +202,7 @@ defmodule LogflareWeb.BillingController do
275202
%{assigns: %{user: %User{billing_account: billing_account}} = _user} = conn,
276203
_params
277204
) do
278-
with true <- billing_accoount_has_subscription?(billing_account),
205+
with true <- billing_account_has_subscription?(billing_account),
279206
{:ok, session} <- Stripe.create_add_credit_card_session(billing_account) do
280207
conn
281208
|> put_session(:stripe_session, session)
@@ -406,9 +333,9 @@ defmodule LogflareWeb.BillingController do
406333
end
407334
end
408335

409-
defp billing_accoount_has_subscription?(billing_account) do
410-
if subcriptions = billing_account.stripe_subscriptions["data"] do
411-
Enum.count(subcriptions) > 0
336+
defp billing_account_has_subscription?(billing_account) do
337+
if subscriptions = billing_account.stripe_subscriptions["data"] do
338+
Enum.count(subscriptions) > 0
412339
else
413340
false
414341
end

0 commit comments

Comments
 (0)