Refactor cycle generator and update translations
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Extract error handling into separate functions to reduce nesting depth.
This commit is contained in:
parent
1bb03b52c9
commit
a8ea121800
5 changed files with 82 additions and 42 deletions
|
|
@ -379,25 +379,10 @@ defmodule Mv.MembershipFees.CycleGenerator do
|
|||
status: :unpaid
|
||||
}
|
||||
|
||||
case Ash.create(MembershipFeeCycle, attrs, return_notifications?: true) do
|
||||
{:ok, cycle, notifications} when is_list(notifications) ->
|
||||
{:ok, cycle, notifications}
|
||||
|
||||
{:ok, cycle} ->
|
||||
{:ok, cycle, []}
|
||||
|
||||
{:error, %Ash.Error.Invalid{errors: [%Ash.Error.Changes.InvalidAttribute{private_vars: %{constraint: constraint, constraint_type: :unique}}]}} = error ->
|
||||
# Cycle already exists (unique constraint violation) - skip it silently
|
||||
# This makes the function idempotent and prevents errors on server restart
|
||||
if constraint == "membership_fee_cycles_unique_cycle_per_member_index" do
|
||||
{:skip, cycle_start}
|
||||
else
|
||||
{:error, {cycle_start, error}}
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, {cycle_start, reason}}
|
||||
end
|
||||
handle_cycle_creation_result(
|
||||
Ash.create(MembershipFeeCycle, attrs, return_notifications?: true),
|
||||
cycle_start
|
||||
)
|
||||
end)
|
||||
|
||||
{successes, skips, errors} =
|
||||
|
|
@ -419,9 +404,7 @@ defmodule Mv.MembershipFees.CycleGenerator do
|
|||
successful_cycles = Enum.map(successes, fn {:ok, cycle, _notifications} -> cycle end)
|
||||
|
||||
if Enum.any?(skips) do
|
||||
Logger.debug(
|
||||
"Skipped #{length(skips)} cycles that already exist for member #{member_id}"
|
||||
)
|
||||
Logger.debug("Skipped #{length(skips)} cycles that already exist for member #{member_id}")
|
||||
end
|
||||
|
||||
{:ok, successful_cycles, all_notifications}
|
||||
|
|
@ -433,4 +416,45 @@ defmodule Mv.MembershipFees.CycleGenerator do
|
|||
{:error, {:partial_failure, errors}}
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_cycle_creation_result({:ok, cycle, notifications}, _cycle_start)
|
||||
when is_list(notifications) do
|
||||
{:ok, cycle, notifications}
|
||||
end
|
||||
|
||||
defp handle_cycle_creation_result({:ok, cycle}, _cycle_start) do
|
||||
{:ok, cycle, []}
|
||||
end
|
||||
|
||||
defp handle_cycle_creation_result(
|
||||
{:error,
|
||||
%Ash.Error.Invalid{
|
||||
errors: [
|
||||
%Ash.Error.Changes.InvalidAttribute{
|
||||
private_vars: %{constraint: constraint, constraint_type: :unique}
|
||||
}
|
||||
]
|
||||
}} = error,
|
||||
cycle_start
|
||||
) do
|
||||
# Cycle already exists (unique constraint violation) - skip it silently
|
||||
# This makes the function idempotent and prevents errors on server restart
|
||||
handle_unique_constraint_violation(constraint, cycle_start, error)
|
||||
end
|
||||
|
||||
defp handle_cycle_creation_result({:error, reason}, cycle_start) do
|
||||
{:error, {cycle_start, reason}}
|
||||
end
|
||||
|
||||
defp handle_unique_constraint_violation(
|
||||
"membership_fee_cycles_unique_cycle_per_member_index",
|
||||
cycle_start,
|
||||
_error
|
||||
) do
|
||||
{:skip, cycle_start}
|
||||
end
|
||||
|
||||
defp handle_unique_constraint_violation(_constraint, cycle_start, error) do
|
||||
{:error, {cycle_start, error}}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue