Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: double subscription #465

Merged
merged 4 commits into from
Sep 29, 2023
Merged
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
3 changes: 2 additions & 1 deletion apps/lenra/lib/lenra/errors/business_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule Lenra.Errors.BusinessError do
{:pipeline_runner_unkown_service,
"Currently not capable to handle this type of pipeline. (`pipeline_runner` can be: [GitLab, Kubernetes])"},
{:subscription_required, "You need a subscirption", 402},
{:stripe_error, "Stripe error"}
{:stripe_error, "Stripe error"},
{:subscription_already_exist, "You already have a subscription for this app"}
]
end
57 changes: 26 additions & 31 deletions apps/lenra/lib/lenra/subscriptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,13 @@ defmodule Lenra.Subscriptions do
require Logger

def get_subscription_by_app_id(application_id) do
subscription =
Repo.one(
from(s in Subscription,
where:
s.application_id == ^application_id and s.end_date >= ^Date.utc_today() and
s.start_date <= ^Date.utc_today()
)
Repo.one(
from(s in Subscription,
where:
s.application_id == ^application_id and s.end_date >= ^Date.utc_today() and
s.start_date <= ^Date.utc_today()
)

case subscription do
nil ->
BusinessError.subscription_required_tuple()

subscription ->
subscription
end
)
end

def get_customer_portal_url(user) do
Expand Down Expand Up @@ -86,22 +77,26 @@ defmodule Lenra.Subscriptions do
},
app
) do
"#{app.id}"
|> Stripe.Product.retrieve()
|> case do
{:ok, %Stripe.Product{} = product} ->
handle_create_session(plan, success_url, cancel_url, product.id, customer, app.id)

{:error, _} ->
product_id = create_product(app.id, app.name)
handle_create_session(plan, success_url, cancel_url, product_id, customer, app.id)
end
|> case do
{:ok, session} ->
session.url

{:error, error} ->
BusinessError.stripe_error(error)
if get_subscription_by_app_id(app.id) != nil do
BusinessError.subscription_already_exist_tuple()
else
"#{app.id}"
|> Stripe.Product.retrieve()
|> case do
{:ok, %Stripe.Product{} = product} ->
handle_create_session(plan, success_url, cancel_url, product.id, customer, app.id)

{:error, _} ->
product_id = create_product(app.id, app.name)
handle_create_session(plan, success_url, cancel_url, product_id, customer, app.id)
end
|> case do
{:ok, session} ->
session.url

{:error, error} ->
BusinessError.stripe_error(error)
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule LenraWeb.EnvsController do
module: LenraWeb.EnvsController.Policy

alias Lenra.Apps
alias Lenra.Errors.BusinessError
alias alias Lenra.Subscriptions
alias Lenra.Subscriptions.Subscription

Expand Down Expand Up @@ -39,6 +40,9 @@ defmodule LenraWeb.EnvsController do
{:ok, %{updated_env: env}} <- Apps.update_env(env, params) do
conn
|> reply(env)
else
nil -> BusinessError.subscription_required_tuple()
error -> error
end
end

Expand Down
Loading