Skip to content

Commit f895eb9

Browse files
authored
Merge pull request #1243 from code-corps/fix-issues
Upgrade deps, add missing specs, fix some dialyzer issues
2 parents 0ad379b + ed4b5db commit f895eb9

13 files changed

+90
-111
lines changed

lib/code_corps/emails/base_email.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule CodeCorps.Emails.BaseEmail do
2-
import Bamboo.Email
2+
import Bamboo.Email, only: [from: 2, new_email: 0]
33

4+
@spec create :: Bamboo.Email.t
45
def create do
56
new_email()
67
|> from("Code Corps<team@codecorps.org>")

lib/code_corps/emails/forgot_password_email.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
defmodule CodeCorps.Emails.ForgotPasswordEmail do
2-
import Bamboo.Email
2+
import Bamboo.Email, only: [to: 2]
33
import Bamboo.PostmarkHelper
44

5-
alias CodeCorps.{Emails.BaseEmail, WebClient}
5+
alias CodeCorps.{Emails.BaseEmail, User, WebClient}
66

7-
def create(user, token) do
7+
@spec create(User.t, String.t) :: Bamboo.Email.t
8+
def create(%User{} = user, token) do
89
BaseEmail.create
910
|> to(user.email)
1011
|> template(template_id(), %{link: link(token)})
1112
end
1213

14+
@spec template_id :: String.t
1315
defp template_id, do: Application.get_env(:code_corps, :postmark_forgot_password_template)
1416

17+
@spec link(String.t) :: String.t
1518
defp link(token) do
1619
WebClient.url()
1720
|> URI.merge("password/reset?token=#{token}")
Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
defmodule CodeCorps.Emails.OrganizationInviteEmail do
2-
import Bamboo.Email
3-
import Bamboo.PostmarkHelper
2+
import Bamboo.Email, only: [to: 2]
3+
import Bamboo.PostmarkHelper
44

5-
alias CodeCorps.{Emails.BaseEmail, OrganizationInvite, WebClient}
5+
alias CodeCorps.{Emails.BaseEmail, OrganizationInvite, WebClient}
66

7-
def create(%OrganizationInvite{} = invite) do
8-
BaseEmail.create
9-
|> to(invite.email)
10-
|> template(template_id(), build_model(invite))
11-
end
7+
@spec create(OrganizationInvite.t) :: Bamboo.Email.t
8+
def create(%OrganizationInvite{} = invite) do
9+
BaseEmail.create
10+
|> to(invite.email)
11+
|> template(template_id(), build_model(invite))
12+
end
1213

13-
defp build_model(%OrganizationInvite{} = invite) do
14-
%{
15-
organization_name: invite.organization_name,
16-
invite_url: invite_url(invite.code, invite.organization_name),
17-
subject: "Create your first project on Code Corps"
18-
}
19-
end
14+
@spec build_model(OrganizationInvite.t) :: map
15+
defp build_model(%OrganizationInvite{} = invite) do
16+
%{
17+
organization_name: invite.organization_name,
18+
invite_url: invite_url(invite.code, invite.organization_name),
19+
subject: "Create your first project on Code Corps"
20+
}
21+
end
2022

21-
defp invite_url(code, organization_name) do
22-
WebClient.url()
23-
|> URI.merge("/invites/organization" <> "?" <> set_params(code, organization_name))
24-
|> URI.to_string
25-
end
23+
@spec invite_url(String.t, String.t) :: String.t
24+
defp invite_url(code, organization_name) do
25+
query_params = set_params(code, organization_name)
26+
WebClient.url()
27+
|> URI.merge("/invites/organization" <> "?" <> query_params)
28+
|> URI.to_string
29+
end
2630

27-
defp set_params(code, organization_name) do
28-
%{code: code, organization_name: organization_name }
29-
|> URI.encode_query
30-
end
31+
@spec set_params(String.t, String.t) :: binary
32+
defp set_params(code, organization_name) do
33+
%{code: code, organization_name: organization_name}
34+
|> URI.encode_query
35+
end
3136

32-
defp template_id, do: Application.get_env(:code_corps, :organization_invite_email_template)
37+
@spec template_id :: String.t
38+
defp template_id, do: Application.get_env(:code_corps, :organization_invite_email_template)
3339
end

lib/code_corps/emails/project_user_acceptance_email.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
defmodule CodeCorps.Emails.ProjectUserAcceptanceEmail do
2-
import Bamboo.Email
2+
import Bamboo.Email, only: [to: 2]
33
import Bamboo.PostmarkHelper
44

55
alias CodeCorps.{Project, ProjectUser, Repo, User, WebClient}
66
alias CodeCorps.Emails.BaseEmail
77
alias CodeCorps.Presenters.ImagePresenter
88

9+
@spec create(ProjectUser.t) :: Bamboo.Email.t
910
def create(%ProjectUser{project: project, user: user}) do
1011
BaseEmail.create
1112
|> to(user.email)
1213
|> template(template_id(), build_model(project, user))
1314
end
1415

16+
@spec build_model(Project.t, User.t) :: map
1517
defp build_model(%Project{} = project, %User{} = user) do
1618
%{
1719
project_logo_url: ImagePresenter.large(project),
@@ -23,13 +25,16 @@ defmodule CodeCorps.Emails.ProjectUserAcceptanceEmail do
2325
}
2426
end
2527

28+
@spec preload(Project.t) :: Project.t
2629
defp preload(%Project{} = project), do: project |> Repo.preload(:organization)
2730

31+
@spec url(Project.t) :: String.t
2832
defp url(project) do
2933
WebClient.url()
3034
|> URI.merge(project.organization.slug <> "/" <> project.slug)
3135
|> URI.to_string
3236
end
3337

38+
@spec template_id :: String.t
3439
defp template_id, do: Application.get_env(:code_corps, :postmark_project_acceptance_template)
3540
end

lib/code_corps/emails/receipt_email.ex

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
defmodule CodeCorps.Emails.ReceiptEmail do
2-
import Bamboo.Email
2+
import Bamboo.Email, only: [to: 2]
33
import Bamboo.PostmarkHelper
44

55
alias CodeCorps.Emails.BaseEmail
66
alias CodeCorps.{DonationGoal, Project, Repo, StripeConnectCharge, StripeConnectSubscription, WebClient}
77

8+
@spec create(StripeConnectCharge.t, Stripe.Invoice.t) :: Bamboo.Email.t
89
def create(%StripeConnectCharge{} = charge, %Stripe.Invoice{} = invoice) do
910
with %StripeConnectCharge{} = charge <- preload(charge),
1011
%Project{} = project <- get_project(invoice.subscription),
@@ -20,10 +21,10 @@ defmodule CodeCorps.Emails.ReceiptEmail do
2021
end
2122
end
2223

23-
defp preload(%StripeConnectCharge{} = charge) do
24-
Repo.preload(charge, :user)
25-
end
24+
@spec preload(Project.t) :: Project.t
25+
defp preload(%StripeConnectCharge{} = charge), do: Repo.preload(charge, :user)
2626

27+
@spec get_project(String.t) :: Project.t | {:error, :subscription_not_found}
2728
defp get_project(subscription_id_from_stripe) do
2829
with %StripeConnectSubscription{} = subscription <- get_subscription(subscription_id_from_stripe) do
2930
subscription.stripe_connect_plan.project
@@ -32,19 +33,22 @@ defmodule CodeCorps.Emails.ReceiptEmail do
3233
end
3334
end
3435

36+
@spec get_subscription(String.t) :: Subscription.t | nil
3537
defp get_subscription(subscription_id_from_stripe) do
3638
StripeConnectSubscription
3739
|> Repo.get_by(id_from_stripe: subscription_id_from_stripe)
3840
|> Repo.preload(stripe_connect_plan: [project: :organization])
3941
end
4042

43+
@spec get_current_donation_goal(Project.t) :: DonationGoal.t | {:error, :donation_goal_not_found}
4144
defp get_current_donation_goal(project) do
4245
case Repo.get_by(DonationGoal, current: true, project_id: project.id) do
4346
nil -> {:error, :donation_goal_not_found}
4447
donation_goal -> {:ok, donation_goal}
4548
end
4649
end
4750

51+
@spec build_model(StripeConnectCharge.t, Project.t, DonationGoal.t) :: map
4852
defp build_model(charge, project, current_donation_goal) do
4953
%{
5054
charge_amount: charge.amount |> format_amount(),
@@ -58,12 +62,15 @@ defmodule CodeCorps.Emails.ReceiptEmail do
5862
}
5963
end
6064

65+
@spec build_subject_line(Project.t) :: String.t
6166
defp build_subject_line(project) do
6267
"Your monthly donation to " <> project.title
6368
end
6469

70+
@spec high_five_image_url :: String.t
6571
defp high_five_image_url, do: Enum.random(high_five_image_urls())
6672

73+
@spec high_five_image_urls :: list(String.t)
6774
defp high_five_image_urls, do: [
6875
"https://d3pgew4wbk2vb1.cloudfront.net/emails/images/emoji-1f64c-1f3fb@2x.png",
6976
"https://d3pgew4wbk2vb1.cloudfront.net/emails/images/emoji-1f64c-1f3fc@2x.png",
@@ -72,15 +79,18 @@ defmodule CodeCorps.Emails.ReceiptEmail do
7279
"https://d3pgew4wbk2vb1.cloudfront.net/emails/images/emoji-1f64c-1f3ff@2x.png"
7380
]
7481

82+
@spec format_amount(float) :: String.t
7583
defp format_amount(amount) do
76-
Money.to_string(Money.new(amount, :USD))
84+
amount |> Money.new(:USD) |> Money.to_string()
7785
end
7886

87+
@spec url(Project.t) :: String.t
7988
defp url(project) do
8089
WebClient.url()
8190
|> URI.merge(project.organization.slug <> "/" <> project.slug)
8291
|> URI.to_string
8392
end
8493

94+
@spec template_id :: String.t
8595
defp template_id, do: Application.get_env(:code_corps, :postmark_receipt_template)
8696
end

lib/code_corps/services/donation_goals_service.ex

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ defmodule CodeCorps.Services.DonationGoalsService do
1515
alias CodeCorps.{DonationGoal, Project, Repo}
1616
alias Ecto.Multi
1717

18-
# Prevents warning for calling `Repo.transaction(multi)`.
19-
# The warning was caused with how the function is internally
20-
# implemented, so there's no way around it
21-
# As we update Ecto, we should check if this is still necessary.
22-
# Last check was Ecto 2.1.3
23-
@dialyzer :no_opaque
24-
2518
@doc """
2619
Creates the `CodeCorps.DonationGoal` by wrapping the following in a
2720
transaction:

lib/code_corps/services/user_service.ex

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,25 @@ defmodule CodeCorps.Services.UserService do
55
When operations happen on `CodeCorps.User`, we need to make sure changes
66
are propagated to related records, ex., `CodeCorps.StripePlatformCustomer` and
77
`CodeCorps.StripeConnectCustomer`
8-
98
"""
109

1110
alias CodeCorps.{Repo, StripeConnectCustomer, StripePlatformCustomer, User}
1211
alias CodeCorps.StripeService.{StripeConnectCustomerService, StripePlatformCustomerService}
1312
alias Ecto.{Changeset, Multi}
1413

15-
# Prevents warning for calling `Repo.transaction(multi)`.
16-
# The warning was caused with how the function is internally
17-
# implemented, so there's no way around it
18-
# As we update Ecto, we should check if this is still necessary.
19-
# Last check was Ecto 2.1.3
20-
@dialyzer :no_opaque
21-
2214
@doc """
2315
Updates a `CodeCorps.User` record and, if necessary, associated
2416
`CodeCorps.StripePlatformCustomer` and `CodeCorps.StripeConnectCustomer` records.
2517
2618
These related records inherit the email field from the user,
2719
so they need to be kept in sync, both locally, and on the Stripe platform.
2820
29-
Returns one of
30-
* `{:ok, %CodeCorps.User{}, nil, nil}`
31-
* `{:ok, %CodeCorps.User{}, %CodeCorps.StripePlatformCustomer{}, nil}`
32-
* `{:ok, %CodeCorps.User{}, %CodeCorps.StripePlatformCustomer{}, %CodeCorps.StripeConnectCustomer{}}`
33-
* `{:error, %Ecto.Changeset{}}`
34-
* `{:error, :unhandled}`
35-
21+
Returns one of:
22+
- `{:ok, %CodeCorps.User{}, nil, nil}`
23+
- `{:ok, %CodeCorps.User{}, %CodeCorps.StripePlatformCustomer{}, nil}`
24+
- `{:ok, %CodeCorps.User{}, %CodeCorps.StripePlatformCustomer{}, %CodeCorps.StripeConnectCustomer{}}`
25+
- `{:error, %Ecto.Changeset{}}`
26+
- `{:error, :unhandled}`
3627
"""
3728
def update(%User{} = user, attributes) do
3829
changeset = user |> User.update_changeset(attributes)

lib/code_corps/stripe_service/adapters/stripe_platform_card.ex

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule CodeCorps.StripeService.Adapters.StripePlatformCardAdapter do
33

44
@stripe_attributes [:brand, :customer, :cvc_check, :exp_month, :exp_year, :id, :last4, :name, :user_id]
55

6+
@spec to_params(Stripe.Card.t, map) :: {:ok, map}
67
def to_params(%Stripe.Card{} = stripe_card, %{} = attributes) do
78
result =
89
stripe_card
@@ -18,19 +19,10 @@ defmodule CodeCorps.StripeService.Adapters.StripePlatformCardAdapter do
1819

1920
@non_stripe_attributes ["user_id"]
2021

22+
@spec add_non_stripe_attributes(map, map) :: map
2123
defp add_non_stripe_attributes(%{} = params, %{} = attributes) do
22-
attributes
23-
|> get_non_stripe_attributes
24-
|> add_to(params)
25-
end
26-
27-
defp get_non_stripe_attributes(%{} = attributes) do
2824
attributes
2925
|> Map.take(@non_stripe_attributes)
30-
end
31-
32-
defp add_to(%{} = attributes, %{} = params) do
33-
params
34-
|> Map.merge(attributes)
26+
|> Map.merge(params)
3527
end
3628
end

lib/code_corps/stripe_service/stripe_connect_account_service.ex

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do
88

99
@api Application.get_env(:code_corps, :stripe)
1010

11-
# Prevents warning for calling `Repo.transaction(multi)`.
12-
# The warning was caused with how the function is internally
13-
# implemented, so there's no way around it
14-
# As we update Ecto, we should check if this is still necessary.
15-
# Last check was Ecto 2.1.3
16-
@dialyzer :no_opaque
17-
1811
@doc """
1912
Used to create a remote `Stripe.Account` record as well as an associated local
2013
`StripeConnectAccount` record.

lib/code_corps/stripe_service/stripe_platform_card.ex

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ defmodule CodeCorps.StripeService.StripePlatformCardService do
1212

1313
@api Application.get_env(:code_corps, :stripe)
1414

15-
# Prevents warning for calling `Repo.transaction(multi)`.
16-
# The warning was caused with how the function is internally
17-
# implemented, so there's no way around it
18-
# As we update Ecto, we should check if this is still necessary.
19-
# Last check was Ecto 2.1.3
20-
@dialyzer :no_opaque
21-
2215
def create(%{"stripe_token" => stripe_token, "user_id" => user_id} = attributes) do
2316
with %StripePlatformCustomer{} = customer <- StripePlatformCustomer |> CodeCorps.Repo.get_by(user_id: user_id),
2417
{:ok, %Stripe.Card{} = card} <- @api.Card.create(%{customer: customer.id_from_stripe, source: stripe_token}),

0 commit comments

Comments
 (0)