Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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}`

Expand All @@ -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
6 changes: 3 additions & 3 deletions test/controllers/stripe_connect_plan_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/views/project_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions web/views/project_view.ex
Original file line number Diff line number Diff line change
@@ -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]

Expand All @@ -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
Expand Down