diff --git a/lib/mv/membership_fees/cycle_generator.ex b/lib/mv/membership_fees/cycle_generator.ex index 7ec8d81..feb7b53 100644 --- a/lib/mv/membership_fees/cycle_generator.ex +++ b/lib/mv/membership_fees/cycle_generator.ex @@ -179,19 +179,7 @@ defmodule Mv.MembershipFees.CycleGenerator do defp process_batch(batch, today) do batch |> Task.async_stream(fn member -> - case generate_cycles_for_member(member, today: today) do - {:ok, _cycles, notifications} = ok -> - # Send notifications for batch job - # This is a top-level job, so we need to send notifications explicitly - if Enum.any?(notifications) do - Ash.Notifier.notify(notifications) - end - - {member.id, ok} - - {:error, _reason} = err -> - {member.id, err} - end + process_member_cycle_generation(member, today) end) |> Enum.map(fn {:ok, result} -> @@ -204,6 +192,27 @@ defmodule Mv.MembershipFees.CycleGenerator do end) end + # Process cycle generation for a single member in batch job + # Returns {member_id, result} tuple where result is {:ok, cycles, notifications} or {:error, reason} + defp process_member_cycle_generation(member, today) do + case generate_cycles_for_member(member, today: today) do + {:ok, _cycles, notifications} = ok -> + send_notifications_for_batch_job(notifications) + {member.id, ok} + + {:error, _reason} = err -> + {member.id, err} + end + end + + # Send notifications for batch job + # This is a top-level job, so we need to send notifications explicitly + defp send_notifications_for_batch_job(notifications) do + if Enum.any?(notifications) do + Ash.Notifier.notify(notifications) + end + end + defp build_results_summary(results) do success_count = Enum.count(results, fn {_id, result} -> match?({:ok, _, _}, result) end) failed_count = Enum.count(results, fn {_id, result} -> match?({:error, _}, result) end)