11defmodule 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 )
8696end
0 commit comments