refactor: reduce nesting depth in process_batch function
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
c3ccecd138
commit
342ebb73b9
1 changed files with 22 additions and 13 deletions
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue