refactor: reduce nesting depth in process_batch function
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Moritz 2025-12-15 16:25:55 +01:00
parent c3ccecd138
commit 342ebb73b9
Signed by: moritz
GPG key ID: 1020A035E5DD0824

View file

@ -179,19 +179,7 @@ defmodule Mv.MembershipFees.CycleGenerator do
defp process_batch(batch, today) do defp process_batch(batch, today) do
batch batch
|> Task.async_stream(fn member -> |> Task.async_stream(fn member ->
case generate_cycles_for_member(member, today: today) do process_member_cycle_generation(member, today)
{: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
end) end)
|> Enum.map(fn |> Enum.map(fn
{:ok, result} -> {:ok, result} ->
@ -204,6 +192,27 @@ defmodule Mv.MembershipFees.CycleGenerator do
end) end)
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 defp build_results_summary(results) do
success_count = Enum.count(results, fn {_id, result} -> match?({:ok, _, _}, result) end) success_count = Enum.count(results, fn {_id, result} -> match?({:ok, _, _}, result) end)
failed_count = Enum.count(results, fn {_id, result} -> match?({:error, _}, result) end) failed_count = Enum.count(results, fn {_id, result} -> match?({:error, _}, result) end)