From 9724296ffd5b81a0efe58970711dc0f4dceb6f43 Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Sat, 14 Jan 2017 15:22:55 -0800 Subject: [PATCH] Project has can-activate-donations on its view --- .../project_can_enable_donations.ex | 22 ++++--------------- .../stripe_connect_plan_controller_test.exs | 6 ++--- test/views/project_view_test.exs | 12 ++++++++++ web/views/project_view.ex | 16 +++++++++++--- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/lib/code_corps/stripe_service/validators/project_can_enable_donations.ex b/lib/code_corps/stripe_service/validators/project_can_enable_donations.ex index 7e6235fae..86aab2709 100644 --- a/lib/code_corps/stripe_service/validators/project_can_enable_donations.ex +++ b/lib/code_corps/stripe_service/validators/project_can_enable_donations.ex @@ -13,12 +13,9 @@ defmodule CodeCorps.StripeService.Validators.ProjectCanEnableDonations do These are: - * At least one `CodeCorps.DonationGoal` - - and only one of: - - - `Organization` with a `StripeConnectAccount`, in production env, which has `charges_enabled: true` - - `Organization` with a `StripeConnectAccount`, not in production env + - At least one `CodeCorps.DonationGoal` + - `Organization` with a `StripeConnectAccount` which + has `charges_enabled: true` and `transfers_enabled: true` If the project has these relationships set up, it returns `{:ok, project}` @@ -30,18 +27,7 @@ defmodule CodeCorps.StripeService.Validators.ProjectCanEnableDonations do defp do_validate(%Project{ donation_goals: [_h | _t], - organization: %Organization{stripe_connect_account: %StripeConnectAccount{charges_enabled: true}} + organization: %Organization{stripe_connect_account: %StripeConnectAccount{charges_enabled: true, transfers_enabled: true}} } = project), do: {:ok, project} - - defp do_validate(%Project{ - donation_goals: [_h | _t], - organization: %Organization{stripe_connect_account: %StripeConnectAccount{}} - } = project) do - case Application.get_env(:code_corps, :stripe_env) do - :prod -> @invalid - _ -> {:ok, project} - end - end - defp do_validate(_), do: @invalid end diff --git a/test/controllers/stripe_connect_plan_controller_test.exs b/test/controllers/stripe_connect_plan_controller_test.exs index e54856bd1..cd33ec4eb 100644 --- a/test/controllers/stripe_connect_plan_controller_test.exs +++ b/test/controllers/stripe_connect_plan_controller_test.exs @@ -39,7 +39,7 @@ defmodule CodeCorps.StripeConnectPlanControllerTest do test "creates and renders resource when user is authenticated and authorized", %{conn: conn, current_user: current_user} do organization = insert(:organization) insert(:organization_membership, role: "owner", member: current_user, organization: organization) - insert(:stripe_connect_account, organization: organization, charges_enabled: true) + insert(:stripe_connect_account, organization: organization, charges_enabled: true, transfers_enabled: true) project = insert(:project, organization: organization) insert(:donation_goal, project: project) @@ -63,10 +63,10 @@ defmodule CodeCorps.StripeConnectPlanControllerTest do end @tag :authenticated - test "does not create resource and renders 422 when no donation goals exist", %{conn: conn, current_user: current_user} do + test "does not create resource and renders 422 when no donation goals exist and transfers not enabled", %{conn: conn, current_user: current_user} do organization = insert(:organization) insert(:organization_membership, role: "owner", member: current_user, organization: organization) - insert(:stripe_connect_account, organization: organization) + insert(:stripe_connect_account, organization: organization, transfers_enabled: false) project = insert(:project, organization: organization) assert conn |> request_create(%{project: project}) |> json_response(422) diff --git a/test/views/project_view_test.exs b/test/views/project_view_test.exs index bf8fdd28b..ac8f1b034 100644 --- a/test/views/project_view_test.exs +++ b/test/views/project_view_test.exs @@ -19,6 +19,7 @@ defmodule CodeCorps.ProjectViewTest do expected_json = %{ "data" => %{ "attributes" => %{ + "can-activate-donations" => false, "description" => project.description, "donations-active" => true, "icon-large-url" => CodeCorps.ProjectIcon.url({project.icon, project}, :large), @@ -94,6 +95,17 @@ defmodule CodeCorps.ProjectViewTest do assert rendered_json == expected_json end + test "renders can-activate-donations true when project has donations, no plan, transfers are enabled" do + organization = insert(:organization) + project = insert(:project, organization: organization) + insert(:donation_goal, project: project) + insert(:stripe_connect_account, organization: organization, charges_enabled: true, transfers_enabled: true) + + conn = Phoenix.ConnTest.build_conn + rendered_json = render(CodeCorps.ProjectView, "show.json-api", data: project, conn: conn) + assert rendered_json["data"]["attributes"]["can-activate-donations"] == true + end + test "renders donations-active true when project has donations and a plan" do project = insert(:project) insert(:donation_goal, project: project) diff --git a/web/views/project_view.ex b/web/views/project_view.ex index 7b19fa3ee..5eff63456 100644 --- a/web/views/project_view.ex +++ b/web/views/project_view.ex @@ -1,14 +1,17 @@ defmodule CodeCorps.ProjectView do + alias CodeCorps.StripeService.Validators.ProjectCanEnableDonations + use CodeCorps.PreloadHelpers, default_preloads: [ - :donation_goals, :organization, :project_categories, - :stripe_connect_plan, :project_skills, :task_lists, :tasks + :donation_goals, [organization: :stripe_connect_account], + :project_categories, :project_skills, :stripe_connect_plan, :task_lists, :tasks ] use CodeCorps.Web, :view use JaSerializer.PhoenixView attributes [ - :slug, :title, :description, :donations_active, :icon_thumb_url, + :slug, :title, :can_activate_donations, :description, + :donations_active, :icon_thumb_url, :icon_large_url, :long_description_body, :long_description_markdown, :inserted_at, :total_monthly_donated, :updated_at] @@ -21,6 +24,13 @@ defmodule CodeCorps.ProjectView do has_many :task_lists, serializer: CodeCorps.TaskListView, identifiers: :always has_many :tasks, serializer: CodeCorps.TaskView, identifiers: :always + def can_activate_donations(project, _conn) do + case ProjectCanEnableDonations.validate(project) do + {:ok, _} -> true + {:error, _} -> false + end + end + def donations_active(project, _conn) do Enum.any?(project.donation_goals) && project.stripe_connect_plan != nil end