@@ -67,48 +67,21 @@ defmodule AlgoraWeb.Admin.CampaignLive do
6767
6868  @ impl  true 
6969  def  handle_event ( "send_email" ,  % { "email"  =>  email } ,  socket )  do 
70-     recipient  =  Enum . find ( socket . assigns . csv_data ,  fn  row  ->  row [ "email" ]  ==  email  end ) 
71-     subject  =  get_change ( socket . assigns . form . source ,  :subject ) 
72- 
73-     case  deliver_email ( recipient ,  subject ,  markdown:  socket . assigns . preview ,  img:  "#{ recipient [ "repo_owner" ] }  )  do 
74-       { :ok ,  _ }  -> 
75-         { :noreply ,  put_flash ( socket ,  :info ,  "Email sent successfully" ) } 
76- 
77-       { :error ,  _ }  -> 
78-         { :noreply ,  put_flash ( socket ,  :error ,  "Failed to send email" ) } 
79-     end 
70+     socket . assigns . csv_data  |>  Enum . filter ( fn  row  ->  row [ "email" ]  ==  email  end )  |>  handle_send ( socket ) 
8071  end 
8172
8273  @ impl  true 
8374  def  handle_event ( "send_all" ,  _params ,  socket )  do 
75+     handle_send ( socket . assigns . csv_data ,  socket ) 
76+   end 
77+ 
78+   defp  handle_send ( recipients ,  socket )  do 
8479    subject  =  get_change ( socket . assigns . form . source ,  :subject ) 
8580    template  =  get_change ( socket . assigns . form . source ,  :template ) 
8681
87-     result  = 
88-       Repo . transact ( fn  _  -> 
89-         socket . assigns . csv_data 
90-         |>  Enum . map ( fn  recipient  -> 
91-           template_params  =  [ markdown:  render_preview ( template ,  recipient ) ,  img:  "#{ recipient [ "repo_owner" ] }  ] 
92- 
93-           % { 
94-             id:  "2025-04-oss" , 
95-             subject:  subject , 
96-             recipient_email:  recipient [ "email" ] , 
97-             recipient:  Algora.Util . term_to_base64 ( recipient ) , 
98-             template_params:  Algora.Util . term_to_base64 ( template_params ) 
99-           } 
100-         end ) 
101-         |>  Enum . reduce_while ( :ok ,  fn  args ,  acc  -> 
102-           case  args  |>  SendCampaignEmail . new ( )  |>  Oban . insert ( )  do 
103-             { :ok ,  _ }  ->  { :cont ,  acc } 
104-             { :error ,  _ }  ->  { :halt ,  :error } 
105-           end 
106-         end ) 
107-       end ) 
108- 
109-     case  result  do 
82+     case  enqueue_emails ( recipients ,  subject ,  template )  do 
11083      { :ok ,  _ }  -> 
111-         { :noreply ,  put_flash ( socket ,  :info ,  "Enqueued #{ length ( socket . assigns . csv_data ) }  ) } 
84+         { :noreply ,  put_flash ( socket ,  :info ,  "Enqueued #{ length ( recipients ) }  ) } 
11285
11386      { :error ,  _ }  -> 
11487        { :noreply ,  put_flash ( socket ,  :error ,  "Failed to enqueue emails" ) } 
@@ -239,6 +212,30 @@ defmodule AlgoraWeb.Admin.CampaignLive do
239212    end 
240213  end 
241214
215+   @ spec  enqueue_emails ( recipients  ::  list ( ) ,  subject  ::  String . t ( ) ,  template  ::  String . t ( ) )  ::  :ok 
216+   def  enqueue_emails ( recipients ,  subject ,  template )  do 
217+     Repo . transact ( fn  _  -> 
218+       recipients 
219+       |>  Enum . map ( fn  recipient  -> 
220+         template_params  =  [ markdown:  render_preview ( template ,  recipient ) ,  img:  "#{ recipient [ "repo_owner" ] }  ] 
221+ 
222+         % { 
223+           id:  "2025-04-oss" , 
224+           subject:  subject , 
225+           recipient_email:  recipient [ "email" ] , 
226+           recipient:  Algora.Util . term_to_base64 ( recipient ) , 
227+           template_params:  Algora.Util . term_to_base64 ( template_params ) 
228+         } 
229+       end ) 
230+       |>  Enum . reduce_while ( :ok ,  fn  args ,  acc  -> 
231+         case  args  |>  SendCampaignEmail . new ( )  |>  Oban . insert ( )  do 
232+           { :ok ,  _ }  ->  { :cont ,  acc } 
233+           { :error ,  _ }  ->  { :halt ,  :error } 
234+         end 
235+       end ) 
236+     end ) 
237+   end 
238+ 
242239  @ spec  deliver_email ( recipient  ::  map ( ) ,  subject  ::  String . t ( ) ,  template_params  ::  Keyword . t ( ) )  ::  :ok 
243240  def  deliver_email ( recipient ,  subject ,  template_params )  do 
244241    case  :get 
0 commit comments