11defmodule CodeCorps.StripeService.StripeConnectPlanService do
2+ @ moduledoc """
3+ Used to perform actions on `StripeConnectPlan` records
4+ while at the same time propagating to and from associated `Stripe.Plan`
5+ records.
6+ """
7+
28 alias CodeCorps . { Project , Repo , StripeConnectPlan }
39 alias CodeCorps.StripeService.Adapters.StripeConnectPlanAdapter
410 alias CodeCorps.StripeService.Validators.ProjectCanEnableDonations
@@ -8,17 +14,15 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do
814 @ doc """
915 Creates a new `Stripe.Plan` record on Stripe API, as well as an associated local
1016 `StripeConnectPlan` record
11-
12- # Possible return values
13-
14- - `{:ok, %StripeConnectPlan{}}` - the created record.
15- - `{:error, %Ecto.Changeset{}}` - the record was not created due to validation issues.
16- - `{:error, :project_not_ready}` - the associated project does not meed the prerequisites for creating a plan.
17- - `{:error, %Stripe.APIErrorResponse{}}` - there was a problem with the stripe request
18- - `{:error, :not_found}` - one of the associated records was not found
1917 """
18+ @ spec create ( map ) :: { :ok , StripeConnectPlan . t } |
19+ { :error , Ecto.Changeset . t } |
20+ { :error , Stripe.APIErrorResponse . t } |
21+ { :error , :project_not_ready } |
22+ { :error , :not_found }
2023 def create ( % { "project_id" => project_id } = attributes ) do
21- with { :ok , % Project { } = project } <- get_project ( project_id ) |> ProjectCanEnableDonations . validate ,
24+ with { :ok , % Project { } = project } <- get_project ( project_id ) ,
25+ { :ok , % Project { } } <- ProjectCanEnableDonations . validate ( project ) ,
2226 % { } = create_attributes <- get_create_attributes ( project_id ) ,
2327 connect_account_id <- project . organization . stripe_connect_account . id_from_stripe ,
2428 { :ok , plan } <- @ api . Plan . create ( create_attributes , connect_account: connect_account_id ) ,
@@ -28,12 +32,10 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do
2832 |> StripeConnectPlan . create_changeset ( params )
2933 |> Repo . insert
3034 else
31- nil -> { :error , :not_found }
3235 failure -> failure
3336 end
3437 end
3538
36- @ spec get_create_attributes ( binary ) :: map
3739 defp get_create_attributes ( project_id ) do
3840 % {
3941 amount: 1 , # in cents
@@ -46,8 +48,11 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do
4648 end
4749
4850 defp get_project ( project_id ) do
49- Project
50- |> Repo . get ( project_id )
51- |> Repo . preload ( [ :donation_goals , { :organization , :stripe_connect_account } , :stripe_connect_plan ] )
51+ preloads = [ :donation_goals , { :organization , :stripe_connect_account } , :stripe_connect_plan ]
52+
53+ case Project |> Repo . get ( project_id ) |> Repo . preload ( preloads ) do
54+ nil -> { :error , :not_found }
55+ record -> { :ok , record }
56+ end
5257 end
5358end
0 commit comments