Skip to content

Commit badf0bc

Browse files
committed
Fix validator
1 parent be9cf3d commit badf0bc

4 files changed

Lines changed: 47 additions & 6 deletions

File tree

lib/code_corps/stripe_service/stripe_connect_plan.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do
4848
defp get_project(project_id) do
4949
Project
5050
|> Repo.get(project_id)
51-
|> Repo.preload([:donation_goals, {:organization, :stripe_connect_account}])
51+
|> Repo.preload([:donation_goals, {:organization, :stripe_connect_account}, :stripe_connect_plan])
5252
end
5353
end

lib/code_corps/stripe_service/validators/project_can_enable_donations.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule CodeCorps.StripeService.Validators.ProjectCanEnableDonations do
33
Ensures a `CodeCorps.Project` is able to receive subscriptions.
44
"""
55

6-
alias CodeCorps.{Organization, Project, StripeConnectAccount}
6+
alias CodeCorps.{Organization, Project, StripeConnectAccount, StripeConnectPlan}
77

88
@doc """
99
Determines if the provided `CodeCorps.Project` can enable donations.
@@ -23,11 +23,10 @@ defmodule CodeCorps.StripeService.Validators.ProjectCanEnableDonations do
2323
"""
2424
def validate(%Project{} = project), do: do_validate(project)
2525

26-
@invalid {:error, :project_not_ready}
27-
26+
defp do_validate(%Project{stripe_connect_plan: %StripeConnectPlan{}}), do: {:error, :project_has_plan}
2827
defp do_validate(%Project{
2928
donation_goals: [_h | _t],
3029
organization: %Organization{stripe_connect_account: %StripeConnectAccount{charges_enabled: true, transfers_enabled: true}}
3130
} = project), do: {:ok, project}
32-
defp do_validate(_), do: @invalid
31+
defp do_validate(_), do: {:error, :project_not_ready}
3332
end

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"segment": {:hex, :segment, "0.1.1", "47bf9191590e7a533c105d1e21518e0d6da47c91e8d98ebb649c624db5dfc359", [:mix], [{:httpoison, "~> 0.8", [hex: :httpoison, optional: false]}, {:poison, "~> 1.3 or ~> 2.0", [hex: :poison, optional: false]}]},
5757
"sentry": {:hex, :sentry, "2.1.0", "51e7ca261b519294ac73b30763893c4a7ad2005205514aefa5bf37ccb83e44ea", [:mix], [{:hackney, "~> 1.6.1", [hex: :hackney, optional: false]}, {:plug, "~> 1.0", [hex: :plug, optional: true]}, {:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}, {:uuid, "~> 1.0", [hex: :uuid, optional: false]}]},
5858
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.0", "edee20847c42e379bf91261db474ffbe373f8acb56e9079acb6038d4e0bf414f", [:make, :rebar], []},
59-
"stripity_stripe": {:git, "https://github.com/code-corps/stripity_stripe.git", "f29a3308136594f976d8e165100385628e2eece2", [branch: "2.0"]},
59+
"stripity_stripe": {:git, "https://github.com/code-corps/stripity_stripe.git", "9e74bd330e3013b5fd8e13874177d1bbf27d4bbf", [branch: "2.0"]},
6060
"sweet_xml": {:hex, :sweet_xml, "0.6.3", "814265792baeb163421811c546581c522dfdcb9d1767b1e59959c52906414e80", [:mix], []},
6161
"timber": {:hex, :timber, "0.4.7", "df3fcd79bcb4eb4b53874d906ef5f3a212937b4bc7b7c5b244745202cc389443", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: true]}, {:phoenix, "~> 1.2", [hex: :phoenix, optional: true]}, {:plug, "~> 1.2", [hex: :plug, optional: true]}, {:poison, "~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}]},
6262
"timex": {:hex, :timex, "3.1.5", "413d6d8d6f0162a5d47080cb8ca520d790184ac43e097c95191c7563bf25b428", [:mix], [{:combine, "~> 0.7", [hex: :combine, optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, optional: false]}]},
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule CodeCorps.StripeService.Validators.ProjectCanEnableDonationsTest do
2+
use ExUnit.Case, async: true
3+
4+
use CodeCorps.ModelCase
5+
6+
alias CodeCorps.{Project}
7+
alias CodeCorps.StripeService.Validators.ProjectCanEnableDonations
8+
9+
describe "validate" do
10+
test "succeeds when project has donation_goals and organization where charges and transfers are enabled" do
11+
organization = insert(:organization)
12+
project = insert(:project, organization: organization)
13+
insert(:donation_goal, project: project)
14+
insert(:stripe_connect_account, organization: organization, charges_enabled: true, transfers_enabled: true)
15+
16+
project =
17+
Project
18+
|> Repo.get(project.id)
19+
|> Repo.preload([:donation_goals, [organization: :stripe_connect_account], :stripe_connect_plan])
20+
21+
assert {:ok, _project} = ProjectCanEnableDonations.validate(project)
22+
end
23+
24+
test "fails when project has a StripeConnectPlan" do
25+
project = insert(:project)
26+
insert(:stripe_connect_plan, project: project)
27+
28+
project =
29+
Project
30+
|> Repo.get(project.id)
31+
|> Repo.preload([:stripe_connect_plan])
32+
33+
assert {:error, :project_has_plan} = ProjectCanEnableDonations.validate(project)
34+
end
35+
36+
test "fails when project is not ready" do
37+
project = insert(:project)
38+
39+
assert {:error, :project_not_ready} = ProjectCanEnableDonations.validate(project)
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)