Skip to content

Commit be9cf3d

Browse files
authored
Merge pull request #631 from code-corps/project-can-activate-donations
Project has can-activate-donations on its view
2 parents fea7d7b + 9724296 commit be9cf3d

4 files changed

Lines changed: 32 additions & 24 deletions

File tree

lib/code_corps/stripe_service/validators/project_can_enable_donations.ex

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ defmodule CodeCorps.StripeService.Validators.ProjectCanEnableDonations do
1313
1414
These are:
1515
16-
* At least one `CodeCorps.DonationGoal`
17-
18-
and only one of:
19-
20-
- `Organization` with a `StripeConnectAccount`, in production env, which has `charges_enabled: true`
21-
- `Organization` with a `StripeConnectAccount`, not in production env
16+
- At least one `CodeCorps.DonationGoal`
17+
- `Organization` with a `StripeConnectAccount` which
18+
has `charges_enabled: true` and `transfers_enabled: true`
2219
2320
If the project has these relationships set up, it returns `{:ok, project}`
2421
@@ -30,18 +27,7 @@ defmodule CodeCorps.StripeService.Validators.ProjectCanEnableDonations do
3027

3128
defp do_validate(%Project{
3229
donation_goals: [_h | _t],
33-
organization: %Organization{stripe_connect_account: %StripeConnectAccount{charges_enabled: true}}
30+
organization: %Organization{stripe_connect_account: %StripeConnectAccount{charges_enabled: true, transfers_enabled: true}}
3431
} = project), do: {:ok, project}
35-
36-
defp do_validate(%Project{
37-
donation_goals: [_h | _t],
38-
organization: %Organization{stripe_connect_account: %StripeConnectAccount{}}
39-
} = project) do
40-
case Application.get_env(:code_corps, :stripe_env) do
41-
:prod -> @invalid
42-
_ -> {:ok, project}
43-
end
44-
end
45-
4632
defp do_validate(_), do: @invalid
4733
end

test/controllers/stripe_connect_plan_controller_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule CodeCorps.StripeConnectPlanControllerTest do
3939
test "creates and renders resource when user is authenticated and authorized", %{conn: conn, current_user: current_user} do
4040
organization = insert(:organization)
4141
insert(:organization_membership, role: "owner", member: current_user, organization: organization)
42-
insert(:stripe_connect_account, organization: organization, charges_enabled: true)
42+
insert(:stripe_connect_account, organization: organization, charges_enabled: true, transfers_enabled: true)
4343
project = insert(:project, organization: organization)
4444
insert(:donation_goal, project: project)
4545

@@ -63,10 +63,10 @@ defmodule CodeCorps.StripeConnectPlanControllerTest do
6363
end
6464

6565
@tag :authenticated
66-
test "does not create resource and renders 422 when no donation goals exist", %{conn: conn, current_user: current_user} do
66+
test "does not create resource and renders 422 when no donation goals exist and transfers not enabled", %{conn: conn, current_user: current_user} do
6767
organization = insert(:organization)
6868
insert(:organization_membership, role: "owner", member: current_user, organization: organization)
69-
insert(:stripe_connect_account, organization: organization)
69+
insert(:stripe_connect_account, organization: organization, transfers_enabled: false)
7070
project = insert(:project, organization: organization)
7171

7272
assert conn |> request_create(%{project: project}) |> json_response(422)

test/views/project_view_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule CodeCorps.ProjectViewTest do
1919
expected_json = %{
2020
"data" => %{
2121
"attributes" => %{
22+
"can-activate-donations" => false,
2223
"description" => project.description,
2324
"donations-active" => true,
2425
"icon-large-url" => CodeCorps.ProjectIcon.url({project.icon, project}, :large),
@@ -94,6 +95,17 @@ defmodule CodeCorps.ProjectViewTest do
9495
assert rendered_json == expected_json
9596
end
9697

98+
test "renders can-activate-donations true when project has donations, no plan, transfers are enabled" do
99+
organization = insert(:organization)
100+
project = insert(:project, organization: organization)
101+
insert(:donation_goal, project: project)
102+
insert(:stripe_connect_account, organization: organization, charges_enabled: true, transfers_enabled: true)
103+
104+
conn = Phoenix.ConnTest.build_conn
105+
rendered_json = render(CodeCorps.ProjectView, "show.json-api", data: project, conn: conn)
106+
assert rendered_json["data"]["attributes"]["can-activate-donations"] == true
107+
end
108+
97109
test "renders donations-active true when project has donations and a plan" do
98110
project = insert(:project)
99111
insert(:donation_goal, project: project)

web/views/project_view.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
defmodule CodeCorps.ProjectView do
2+
alias CodeCorps.StripeService.Validators.ProjectCanEnableDonations
3+
24
use CodeCorps.PreloadHelpers,
35
default_preloads: [
4-
:donation_goals, :organization, :project_categories,
5-
:stripe_connect_plan, :project_skills, :task_lists, :tasks
6+
:donation_goals, [organization: :stripe_connect_account],
7+
:project_categories, :project_skills, :stripe_connect_plan, :task_lists, :tasks
68
]
79
use CodeCorps.Web, :view
810
use JaSerializer.PhoenixView
911

1012
attributes [
11-
:slug, :title, :description, :donations_active, :icon_thumb_url,
13+
:slug, :title, :can_activate_donations, :description,
14+
:donations_active, :icon_thumb_url,
1215
:icon_large_url, :long_description_body, :long_description_markdown,
1316
:inserted_at, :total_monthly_donated, :updated_at]
1417

@@ -21,6 +24,13 @@ defmodule CodeCorps.ProjectView do
2124
has_many :task_lists, serializer: CodeCorps.TaskListView, identifiers: :always
2225
has_many :tasks, serializer: CodeCorps.TaskView, identifiers: :always
2326

27+
def can_activate_donations(project, _conn) do
28+
case ProjectCanEnableDonations.validate(project) do
29+
{:ok, _} -> true
30+
{:error, _} -> false
31+
end
32+
end
33+
2434
def donations_active(project, _conn) do
2535
Enum.any?(project.donation_goals) && project.stripe_connect_plan != nil
2636
end

0 commit comments

Comments
 (0)