diff --git a/apps/lenra/lib/lenra/errors/business_error.ex b/apps/lenra/lib/lenra/errors/business_error.ex index 4dc97045..963c9fdf 100644 --- a/apps/lenra/lib/lenra/errors/business_error.ex +++ b/apps/lenra/lib/lenra/errors/business_error.ex @@ -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 diff --git a/apps/lenra/lib/lenra/subscriptions.ex b/apps/lenra/lib/lenra/subscriptions.ex index c95d1587..f0f84183 100644 --- a/apps/lenra/lib/lenra/subscriptions.ex +++ b/apps/lenra/lib/lenra/subscriptions.ex @@ -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 @@ -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 diff --git a/apps/lenra_web/lib/lenra_web/controllers/environment_controller.ex b/apps/lenra_web/lib/lenra_web/controllers/environment_controller.ex index 1ec39fc4..7e6b15d0 100644 --- a/apps/lenra_web/lib/lenra_web/controllers/environment_controller.ex +++ b/apps/lenra_web/lib/lenra_web/controllers/environment_controller.ex @@ -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 @@ -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